-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibo.asm
71 lines (56 loc) · 1.66 KB
/
fibo.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
; for osx
; brew install yasm
; yasm -f macho64 fibo.asm && clang fibo.o -o fibo && ./fibo 5
; a nice tutorial for people http://cs.lmu.edu/~ray/notes/nasmtutorial/
global _main
extern _atoi
extern _printf
default rel
section .text
_main:
push rbx ; we don't ever use this, but it is necesary
; to align the stack so we can call stuff
dec rdi ; argc-1, since we don't count program name
cmp rdi, 1
jne incorrectArgs
mov rdi, [rsi+rdi*8] ; argv[rdi]
call _atoi ; now rax has the int value of arg
mov rdi, rax
call fib
lea rdi, [format]
mov rsi, rax
mov rax, 0
call _printf
jmp done
fib:
cmp rdi, 0
jbe return0
cmp rdi, 1
je return1
dec rdi
push rdi
call fib
pop rdi
push rax
dec rdi
call fib
pop rdi
add rax, rdi
ret
return1:
mov rax, 1
ret
return0:
mov rax, 0
ret
incorrectArgs:
lea rdi, [error]
xor rax, rax
call _printf
done:
pop rbx ; undoes the stupid push at the beginning
mov rax, 0
ret
section .data
format: db "%llu", 10, 0
error: db "Need exactly one arg: the number of the fibonacci number to be computed", 10, 0