dComArk15/Aflevering5-x86-64/fib.s
Christoffer Müller Madsen e2e36f9f0d initial commit
2016-01-03 13:29:05 +01:00

34 lines
1.6 KiB
ArmAsm

.global fib
.section .text
fib: push %rbp # Save old rbp to stack and align
# the stack pointer to 16-bytes.
movq %rsp, %rbp # Write new base pointer.
cmpq $1, %rdi # Compare the argument to 1.
je one # If equal to one go to 'one' and
# return 1.
cmpq $0, %rdi # Compare the argument to 0.
je zero # If equal to zero go to 'zero'
# and return zero.
decq %rdi # Decrement n by 1.
push %rdi # Save the value n - 1 on the stack.
call fib # Call the function.
pop %rdi # Restore rdi = n-1
decq %rdi # Decrement rdi by 1.
push %rax # Save the result of fib(n-1)
call fib # Call the function.
addq (%rsp),%rax # Add the top element (fib(n-1)) to
# rax which contains fib(n-2).
jmp end # Jump to 'end' to leave and return
# from this call to the caller.
zero: movq $0, %rax # Set return value to 0.
jmp end
one: movq $1, %rax # Set return value to 1.
end: leave # Return the state of the base and
# stack pointers to their
# original state.
ret # Return from the current function
# with the contents of rax as the
# return value.