Despite I searched everywhere I couldn't find any solution to my problem.The problem is that I I defined a function "hello_world() " in a C file "hello.c" and I want to call this function in an assembly file . "hello_assembly.asm" .Can anyone help me ? Thank you.
-
4With which assembler, compiler, operating system and linker are you trying to do this? – Elliott Frisch Jan 09 '14 at 21:14
-
Where and how exactly did you search? – Andreas Fester Jan 09 '14 at 21:15
-
Look in your compiler's documentation for "assembly language binding conventions" or something resembling that. The exact details vary from compiler to compiler and architecture to architecture. – keshlam Jan 09 '14 at 22:26
-
1If your C compiler permits inline assembly (some do), you may find it easier to call assembly from C. Or you may not, depending on exactly what you're doing. – keshlam Jan 09 '14 at 22:27
-
I use gcc assembler ,ubuntu OS,gcc linker . @ElliottFrisch – Ümit Bilgin Jan 10 '14 at 05:04
-
I searched almost every pages in Google ,I might be doing smth wrong or misunderstanding the problem.Sorry for that @Andreas – Ümit Bilgin Jan 10 '14 at 05:04
-
@ÜmitBilgin You can't easily call C functions from assembly because you (usually!) want a standard library. Using inline assembler in "C" is much easier. – Elliott Frisch Jan 10 '14 at 05:07
-
Then I might be misunderstanding the problem . Thank you for your helps @ElliottFrisch – Ümit Bilgin Jan 10 '14 at 05:09
-
1Elliott, you can indeed call C from assembler -- you just have to link your assembler program not only with the C code but with the C libraries needed to support it. – keshlam Jan 10 '14 at 05:11
-
You might want to look at [musl](http://www.musl-libc.org/) and [dietlibc](http://www.fefe.de/dietlibc/). – Elliott Frisch Jan 10 '14 at 05:15
-
Read about calling convention, also you should tag the architecture to the question, otherwise you may not have the correct answer – phuclv Jan 10 '14 at 05:26
-
Are you trying to call the C function from a line of code which is physically located in your assembly language file ? i.e., are you trying to do something like this ? `call hello_world` where that line is in your file `hello_assembly.asm` ? – User.1 Jan 10 '14 at 14:54
2 Answers
You could check the below example which might give some idea.
\#include <stdio.h>
int main(void)
{
signed int a, b;
a=5,b=25;
mymul(&a,&b);
printf("\nresult=%d",b);
return 0;
}
mymul is a function which is being written in assembly language in file called mymul.S
Below is the code for mymul.S
.globl mymul
mymul:
pushl %ebp # save the old base pointer register
movl %esp, %ebp #copy the stack pointer to base pointer register
movl 8(%ebp), %eax # get the address of a
movl 12(%ebp), %ebx # get the address of b
xchg (%eax), %ecx # we get the value of a and store it in ecx
xchg (%ebx), %edx # we get the value of b and stored it in edx
imul %ecx,%edx # do the multiplication
xchg %ecx, (%eax) #save the value back in a
xchg %edx, (%ebx) # save the value back in b
movl %ebp, %esp # get the stack pointer back to ebp
popl %ebp #restore old ebp
ret #back to the main function
We use the command "cc" to compile our above programs
$ cc mymul.S mul.c -o mulprogram
In the mul.c when we call mymul, we are passing address of a and b , and these address are getting pushed to the stack. When the execution of program enters the mymul function, the stack looks like this: addressofb,addressofa, returnaddress, oldebp
we get the value stored in the address of a and address of b using xchg(we could use movl here) , do the multiplication and save the result in b.
I hope the above program helps you.

- 343
- 1
- 6
- 23
-
Thank you for your help .My actual problem is calling a function defined in a C file from assembly. It is smth like : (Hello.c hello_world(){printf("Hello World"); ) (Hello.asm extern hello_world ... call hello_world .. ) – Ümit Bilgin Jan 10 '14 at 04:48
The gcc documentation should spell this out in more detail.
If you couldn't find documentation for your compiler and environment, I'd suggest you compile your C function to an assembler listing and look at how it expects arguments to be passed in and what it leaves on the stack when exiting.

- 7,931
- 2
- 19
- 33