# Ivan Novick # this program demonstrates how to call printf from assembly using GNU assembler # on a x86 intel compatible CPU # # build command lines # as -o callprintf.o callprintf.s # ld -dynamic-linker /lib/ld-linux.so.2 -o callprintf -lc callprintf.o # # # data section we will declare 2 variables an integer and a null-terminated string .section .data x: .int 123456789 mystring: .asciz "The number is: %d\n" # we right the instructions int the text section .section .text # this is how we declare the main function .globl _start _start: # push the 2 arguments to the printf function onto the stack # arguments are pushed in reverse order, so first the integer and then # the string, as reverse for how you would type printf in C # when done pop the parameters off the stack by adding the immediate value 8 to the stack pointer movl x, %eax pushl %eax pushl $mystring call printf addl $8, %esp # exit system call movl $1, %eax movl $0, %ebx int $0x80