Pass an argument from C to assembly? -


how can pass argument c main function assembly function? know custom function has like:

void function(char *somedata) __attribute__((cdecl)); 

now how use somedata in assembly file. operation system linux ubuntu , processor x86.

i'm bit of noob @ example on way. i've tested , works, issue might have software not being available. i'm using nasm assembly.

main.c

extern void myfunc(char * somedata);  void main(){     myfunc("hello world"); } 

myfunc.asm

section .text     global myfunc     extern printf      myfunc:         push ebp         mov  ebp, esp          push dword [ebp+8]         call printf           mov esp, ebp         pop ebp         ret 

compile

nasm -f elf myfunc.asm gcc main.c myfunc.o -o main 

notes:

you need install nasm (assembler) (ubuntu is: sudo apt-get install nasm)

what happens in c code calls myfunc message. in myfunc.asm address of first character of string (which in [ebp+8] see here information (http://www.nasm.us/xdoc/2.09.04/html/nasmdoc9.html see 9.1.2 describes c calling conventions somewhat.) , pass printf function (by pushing onto stack). printf in c standard library gcc automatically link our code default unless not to.

we have export myfunc in assembly file , declare myfunc extrnal function in main.c file. in myfunc.asm importing printf function stdlib can output message possible.

hope helps somewhat.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -