forked from embedded2014/arm-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib.s
55 lines (43 loc) · 873 Bytes
/
fib.s
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
.syntax unified
.arch armv7-a
.text
.align 2
.thumb
.thumb_func
.global fibonacci
.type fibonacci, function
fibonacci:
@ ADD/MODIFY CODE BELOW
@ PROLOG
push {r3, r4, r5, lr}
@ R4 = R0 - 0 (update flags)
sub r4, r0, #0
@ if(R0 <= 0) goto .L3 (which returns 0)
cmp r4, #0
ble .L3
@ Compare R4 wtih 1
@ If R4 == 1 goto .L4 (which returns 1)
cmp r4, #1
beq .L4
@ R0 = R4 - 1
sub r0, r4, #1
@ Recursive call to fibonacci with R4 - 1 as parameter
bl fibonacci
@ R5 = R0
@ R0 = R4 - 2
mov r5, r0
sub r0, r4, #2
@ Recursive call to fibonacci with R4 - 2 as parameter
bl fibonacci
@ R0 = R5 + R0 (update flags)
add r0, r5, r0
pop {r3, r4, r5, pc} @EPILOG
@ END CODE MODIFICATION
.L3:
mov r0, #0 @ R0 = 0
pop {r3, r4, r5, pc} @ EPILOG
.L4:
mov r0, #1 @ R0 = 1
pop {r3, r4, r5, pc} @ EPILOG
.size fibonacci, .-fibonacci
.end