Find the answer to your Linux question:
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 ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    [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
    Code:
    .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
    testit.c - version one
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef void (*pfunc)(void);
    
    extern pfunc printit;
    
    int main(int argc, char**argv)
    {
    	printit();
    	exit(EXIT_SUCCESS);
    }
    Now when I compile version one with
    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
    Code:
    .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
    testit.c - version two
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef void (*pfunc)(void);
    
    extern pfunc myprintit;
    
    int main(int argc, char**argv)
    {
    	myprintit();
    	exit(EXIT_SUCCESS);
    }
    I compile with the same lines:

    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

  2. #2
    Linux User
    Join Date
    Jan 2006
    Posts
    414
    You're declaring the function (prototype) as a pointer, it shouldn't be.

    Code:
    typedef void (*pfunc)(void);
    try this:
    Code:
    typedef void pfunc(void);

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by darkrose0510 View Post
    You're declaring the function (prototype) as a pointer, it shouldn't be.

    Code:
    typedef void (*pfunc)(void);
    try this:
    Code:
    typedef void pfunc(void);
    Thanks darkrose0510 that was it...
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...