/* hand-coded fibonacci function - 2014 */ .file "handfib.s" .section .rodata STR1: .string "answer is %d\n" .text .globl fib fib: /* fib function */ push %ebp /* will know it should be pushl since ebp is 32 bits */ mov %esp, %ebp /* no extra stack space needed */ mov $1, %ebx /* initial two fibs */ mov $1, %eax /* will hold answer */ /* begin loop */ Loop: cmpl $2, 8(%ebp) /* if compare n with 2 - 2nd arg can't be imm*/ jl LpExit /* jump if less than (n<2) - note args switched */ /* body of loop*/ mov %eax, %ecx add %ebx, %eax /* eax += ebx */ mov %ecx, %ebx subl $1, 8(%ebp) /* n-- */ jmp Loop LpExit: /* eax already holds answer, else move anser to eax */ mov %ebp,%esp pop %ebp ret .globl main main: push %ebp mov %esp, %ebp and $-16, %esp /* don't appear to be necessary */ pushl $10 /* calculate fib(10) */ call fib add $4, %esp /* deallocate arg (10) */ /* pushal save all registers before calling printf */ push %eax /* 2nd arg to printf */ pushl $STR1 /* 1st arg to printf - why pushl? */ call printf add $8, %esp /* popal */ mov $0, %eax /* return 0 */ mov %ebp, %esp pop %ebp ret