/* To build and run this: as fun_hello_world.s -o fun_hello_world.o --32 ld -m elf_i386 fun_hello_world.o ./a.out */ .section .data hwmessage: .asciz "Hello World\n" .section .text .globl _start _start: push $5 call print_number call print_newline call print_newline call print_newline call print_newline movl $27, %ebx movl $1, %eax int $0x80 /* Print "hello world" a given number of times We'll follow 32-bit C calling convention and put the paramter on the stack */ print_newline: push $0x0a movl $4, %eax movl $1, %ebx movl %esp, %ecx movl $1, %edx int $0x80 add $4, %esp ret print_number: push %ebp mov %esp, %ebp // We'll have one local variable, and it'll be the address of the last non-0 character sub $16, %esp movl 8(%ebp), %eax # Puts our parameter into eax movl $10, %ecx loop_top: dec %ecx movl $0, %edx movl $10, %ebx div %ebx add $48, %edx mov %esp, %esi add %ecx, %esi cmp $48, %edx je skip_address_update # Save this as the last non-0 digit's address mov %esi, 12(%esp) skip_address_update: movb %dl, (%esi) cmp $0, %ecx jne loop_top movl $4, %eax movl $1, %ebx movl 12(%esp), %ecx mov %ecx, %edx sub %esp, %edx mov $10, %esi sub %edx, %esi mov %esi, %edx int $0x80 mov %ebp, %esp pop %ebp ret