Created
April 14, 2014 20:06
-
-
Save davidad/10678877 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%include "os_dependent_stuff.asm" | |
global fib | |
global _fib | |
fib: | |
_fib: | |
cmp rdi, 1 | |
jle return_one; if the relationship is "less than or equal", goto return_one | |
push rdi ; save n | |
mov rsi, rdi | |
inc rsi ; rsi now contains n+1 | |
shl rsi, 3 ; multiply by 8 to get the number of bytes, since integers take up 8 bytes each | |
mov rdi, 0 | |
mov r8, -1 | |
mov r9, 0 | |
mov r10, MAP_PRIVATE | MAP_ANON | |
mov rdx, PROT_READ | PROT_WRITE | |
mov rax, SYSCALL_MMAP | |
syscall | |
; assume allocation was sucessful, and buffer is now in rax | |
mov rsi, rax ; rsi will be our working buffer | |
pop rcx ; rcx contains n | |
mov qword [rsi+rcx*8], 1 | |
dec rcx ; rcx contains n-1 | |
mov qword [rsi+rcx*8], 1 | |
.loop: | |
dec rcx | |
; at this point, rcx is the index to compute | |
; computation consists of adding the rcx+1th element and rcx+2th element | |
mov rax, [rsi+(rcx+1)*8] | |
mov rdx, [rsi+(rcx+2)*8] | |
add rax, rdx | |
mov [rsi+rcx*8], rax | |
test rcx, rcx | |
jg .loop | |
; done | |
push rax | |
; TODO: free the memory | |
pop rax | |
ret | |
return_one: | |
mov rax, 1 | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment