This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How can I do calling for external address in my "C" programm by inline GAS?


Presuming you're building this for Linux, I highly recommend you read the 64-bit ABI.

http://www.x86-64.org/documentation/abi.pdf

Chapter 3 is your friend.  There's a lot of details there, hand-rolling 64-bit assembly is more complicated than 32-bit.  There are things like red-zone, which you're writing into with the pushq.  And since you probably won't read the ABI, here's fixed source:

#include <stdio.h>

int main(){

void *p_printf = (void *)&printf;
const char *str = "Hello\n";

printf("addr of printf=%p\n", &printf); // 0x400490

asm ("movq %0, %%rdi;" /* str into RDI */
    "leaq (, %1), %%rcx;" /* address of PRINTF into RCX */
    "call * %%rcx;" /* call PRINTF */
 :  /* output */
 :"r"(str),"r"(p_printf)         /* input */
 : "rdi", "rcx"
);

return 0;
}

Nota Bene: The Windows 64-bit ABI is completely different, so while this source may compile and work on Linux... it will not work for Windows.

Cheers,

Joe


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]