c - How to compile this program with inline asm? -


i cannot compile program taken tutorial. should print "hello world".

void main()  {     __asm__("jmp forward\n\t" "backward:\n\t"              "popl   %esi\n\t"              "movl   $4, %eax\n\t"              "movl   $2, %ebx\n\t"              "movl   %esi, %ecx\n\t"              "movl   $12, %edx\n\t"              "int    $0x80\n\t"              "int3\n\t" "forward:\n\t"              "call   backward\n\t"              ".string \"hello world\\n\""            ); } 

gcc 4.7 under linux gives me following error:

gcc hello.c -o hello hello.c: assembler messages: hello.c:5: error: invalid instruction suffix `pop' 

is there way avoid specify double quotes each line?

also, i'd know how modify program use libc call printf instead of kernel service.

q:

hello.c: assembler messages: hello.c:5: error: invalid instruction suffix `pop' 

a: popl available on x86-32 not on x86-64 (it has popq instead). need either adapt assembly code work on x86-64, or need invoke gcc generate x86-32 binary output.

assuming want generate x86-32, use command-line option -m32.

q:

is there way avoid specify double quotes each line?

a: nope. because __asm__() pseudo-function takes string arguments, string follows c syntax. contents of string passed assembler little or no processing.

note in c, when strings juxtaposed, concatenated. example, "a" "b" same "ab".

note in assembly language syntax (gas), can separate statements newline or semicolon, this: "movl xxx; call yyy" or "movl xxx \n call yyy".

q:

how modify program use libc call printf

a: follow calling convention c on x86. push arguments right left, call function, clean stack. example:

pushl $5678  /* second number */ pushl $1234  /* first number */ pushl $fmtstr call printf addl $12, %esp  /* pop 3 arguments of 4 bytes each */  /* put away code */ fmtstr: .string "hello %d %d\n"  /* \n needs double-backslashed in c */ 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -