473,770 Members | 6,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using assembly in C

hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?

Thanks,
Alice

Nov 14 '05 #1
7 2107
On Tue, 11 Jan 2005 12:05:43 -0800, alice wrote:
hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?

Thanks,
Alice


Although acknowleged in the standard as a common extension, such
functionality is not standardized and is implementation specific. You
should check your compiler's documentation for details (look for an asm
or __asm__ keyword).

Rob Gamble
Nov 14 '05 #2
alice wrote:
hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?

Thanks,
Alice


Look in your compiler documentation for the protocol for
calling a function and returning, especially how parameters
are handled.

A fast method is to write a simple function and have your
compiler spit out the assembly language for the function.
Use this as a stencil for writing your own assembly language
function.

If your assembly language obeys the function calling
conventions, then calling your assembly language from C
should be no different than calling a C language function.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 14 '05 #3
alice wrote:
Can anybody please tell me that how
an assembly language code can be called from a program written in C?
I mean what is the syntax for doing this thing?
Once compiled, a C program can't tell whether it is calling
a function written in assembler, C or any other programming language.
You can consult the C Application Binary Interface (ABI)
for your platform but it would probably be much easier
to inspect the assembler output from your C compiler.
For example
cat f.c double f(double x) {
return x*x;
}
gcc -Wall -std=c99 -pedantic -S f.c
cat f.s

.file "f.c"
.text
.globl f
.type f, @function
f:
pushl %ebp // save frame pointer
movl %esp, %ebp
fldl 8(%ebp) // st0 <-- x
fmull 8(%ebp) // st0 <-- st0*x
popl %ebp // restore frame pointer
ret
.size f, .-f
.section .note.GNU-stack,"",@progb its
.ident "GCC: (GNU) 3.4.1"

The calling program pushes argument x onto the stack and calls f.
The call instruction pushes the return address on the stack
and function f pushes the old frame pointer on the stack
then copies the stack pointer into the frame pointer
so that argument x is now at 8(%ebp).
fldl loads argument x into the floating-point register stack and
fmull pushes argument x onto the floating-point register stack again
so that both st0 and st1 contain the value of x then
fmull multiplies st1 and st0 together and leaves the product in st0.
Function f then pops the old frame pointer off the stack
and returns with the return value in floating-point register st0.
Nov 14 '05 #4
alice wrote:
hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?


There are at least two ways to interpret the question.

The C syntax for calling a function written in assembly
language is exactly the same as for calling a function written
in ordinary C: you write the name of the function followed by
an open parenthesis, followed a (possibly empty) comma-separated
list of arguments, followed by a close parenthesis. The details
of how the assembly-implemented function obtains its arguments
(if any) and returns its value (if any) and jumps through whatever
linkage hoops the platform requires (if any) are specific to the
platform in question, and usually don't work exactly the same
way on other platforms. You'll need to consult your platform's
documentation for the details; they're really not part of C.

Some C implementations allow assembly code to be intermixed
with C code, so a "C and then some" function can contain both
C and assembly source statements. If the implementation permits
such an extension, the details are implementation-specific and
not portable, and you'll need to consult your documentation --
once again, this really isn't C. It isn't really assembly,
either, but a mixed-language patois, a sort of trade argot
with little structure and less grammar but occasionally useful
nonetheless.

--
Er*********@sun .com

Nov 14 '05 #5
<posted & mailed>

alice wrote:
hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?
C doesn't provide a syntax for it. If you have a compiler that does support
it, consult the documentation about how it may be done.


Thanks,
Alice


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 14 '05 #6

"alice" <al***********@ yahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?


It's the same as for calling a C function.

However the details of how the assembly routine must
be written depend upon your compiler. Check your documentation.

-Mike
Nov 14 '05 #7
> hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?


It the assembly code is in its own function it's pretty easy. See the
book Programming from the Ground Up, which includes a lot of information
on C/assembly interaction (specifically in Linux):

http://www.cafeshops.com/bartlettpublish.8640017

Basically, if the assembly function is following the C calling
convention, you just need to add a header for it, call it by the right
name, and link them together. If you need help writing assembly code
that follows the C calling convention, let me know.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9413
by: Mountain Bikn' Guy | last post by:
This code (adapted from the examples in the docs) doesn't make complete sense to me. I have it working, but I'm wondering why I need to declare an assembly reference in 2 places. TIA. Dave CodeCompileUnit compileUnit = new CodeCompileUnit(); CodeNamespace myNamespace = new CodeNamespace("MyNamespace"); compileUnit.Namespaces.Add( myNamespace ); compileUnit.ReferencedAssemblies.Add(@"MyDLL.dll");//I declare an assembly reference here...
43
2676
by: nospam | last post by:
I got three (3) files (1) Untitled.aspx (2) Untitled.aspx.1.cs (3) Untitled.aspx.2.cs These three files must be used together to make file #1, Untitled.aspx, page work via J.I.T. when the User first hits Internet Explorer 6.0 on your browser.
3
1081
by: TT (Tom Tempelaere) | last post by:
Hi there I am making a service project in C#, and I'm in the process of writing the installer. I made an installer class by using the "Add Installer" menu-item in the design window of the service, then I configured the properties of the installers When I run InstallUtil on the executable, it fails with the following message "An exception occurred while trying to find the installers in the c:\...\obj\debug\MyService.exe assembly...
1
1793
by: Marc Falesse | last post by:
Hello, My application needs to serialize a class, which was generated with xsd.exe from a XSD. It was working .NET 1.1 and it is working with .NET 2.0 very well. Now I need my assembly to be a plugin, and to be called by reflexion, but I get this Exception: Exception.Message: "There was an error generating the XML document."
3
5084
by: Michael Hoehne | last post by:
Hi, I'm currently facing a problem with a mixed environment using .NET 1.1 and ..NET 2.0 web services. We have a client application (the "client", system 1) running on .NET 2.0/WinXP, calling our web service (The "Web Service Server") running on a separate machine (also .NET 2.0, Win2003 Server, system 2). The web service server (system 2) itself makes calls to methods in the CRM
3
2857
by: Siv | last post by:
Hi, A little while ago I wrote a small program that allowed the user to view products from a database. The database holds the details of the products which can be viewed via a form and additionally pictures of the product are stored in an images subfolder and the database holds the file name of the relevant picture. The user can then click a button to display the picture in a pop-up window and also another button to email the potential...
0
1635
by: Bill Nguyen | last post by:
I ran into this error using ClickOne to deploy a .NET 2.0 app. CrystalReports for .NET 2.0 is included in the delployment. The error indicates that an incorrect version of CrystalReports.Engine DLL is added to the client GAC. My question is how to obtain a correct redistributable package of R10 so that I can automate the delpoyment. Thanks Bill
17
6259
by: Janiv Ratson | last post by:
Hello, I have written a C# dll in VS2005. One interface and class are exported using Interop Services. I want to use this c# dll in my MFC application, using VS2003. It compiles after I imported the dll to my MFC app. On runtime I get class not registered HR error in my MFC project. I use the code as follow: _NewActivationPtr myActivation = NULL;
0
1351
by: per9000 | last post by:
Hi all, we use CVS to manage versions of our code. Since the CVS keeps track of everything we need I want to synchronize the assembly info with CVS. The problem is that if I update the assembly info the file is no longer up-to-date with our CVS-repository. Also if I check the file in to the CVS-repository the assembly info is no longer up-to-date. I've seen the 1.0.* -trick and I can accept it on the AssemblyVersion but the...
3
4295
by: shobhitguptait | last post by:
How to Run C#.NET Windows App on N/W with centralized DB using SQL SERVER 2000 Hello All...i m really grate full to c such a website where developers try to help people like us who face problems in s/w develepment...I am shobhit Gupta...23 years of age prusuing B.Tech in I.T. Engg. Final Year from Kurukshetra University, Kurukshetra. Recently, i have been working on a project which is to be implemented in a hospital for real life use...Its a...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.