Results 1 to 3 of 3
Hi,
I have a question about calling an asm function from C....It doesn't work unless I create an asm variable to hold the value of the function in....Why?
Here's the ...
- 06-04-2010 #1
[SOLVED] Calling an asm function from C
Hi,
I have a question about calling an asm function from C....It doesn't work unless I create an asm variable to hold the value of the function in....Why?
Here's the code that doesn't work...
asmfile.s - version one
testit.c - version oneCode:.section .data mydata: .ascii "this is the message!\n" .equ mylen, . - mydata .section .bss .section .text .global printit printit: pushq %rbp movq %rsp, %rbp movq $1, %rax movq $1, %rdi movq $mydata, %rsi movq $mylen, %rdx syscall movq %rbp, %rsp popq %rbp ret
Now when I compile version one withCode:#include <stdio.h> #include <stdlib.h> typedef void (*pfunc)(void); extern pfunc printit; int main(int argc, char**argv) { printit(); exit(EXIT_SUCCESS); }
as asmfile.s -o asmfile.o
gcc testit.c asmfile.o -Wall -ansi -pedantic -o testit
and run ./testit I get a Segmentation fault
And here's the code that works....Why is it behaving this way?
asmfile.s - version two
testit.c - version twoCode:.section .data mydata: .ascii "this is the message!\n" .equ mylen, . - mydata myprintit: .quad printit .section .bss .section .text .global myprintit printit: pushq %rbp movq %rsp, %rbp movq $1, %rax movq $1, %rdi movq $mydata, %rsi movq $mylen, %rdx syscall movq %rbp, %rsp popq %rbp ret
I compile with the same lines:Code:#include <stdio.h> #include <stdlib.h> typedef void (*pfunc)(void); extern pfunc myprintit; int main(int argc, char**argv) { myprintit(); exit(EXIT_SUCCESS); }
as asmfile.s -o asmfile.o
gcc testit.c asmfile.o -Wall -ansi -pedantic -o testit
and execute...and "this is the message!"
Does anyone know why the first one doesn't work but the second does?
Also my compiler version - gcc version 4.5.0 20100520 (prerelease) (GCC)Make mine Arch Linux
- 06-04-2010 #2Linux User
- Join Date
- Jan 2006
- Posts
- 414
You're declaring the function (prototype) as a pointer, it shouldn't be.
try this:Code:typedef void (*pfunc)(void);
Code:typedef void pfunc(void);
- 06-04-2010 #3



