Created
July 17, 2015 06:18
-
-
Save hikilaka/faf19b0c4d975578d60e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
; input01.s.arm | |
.data | |
.balign 4 ; scanf format | |
in_format: .asciz "%d" | |
.balign 4 ; printf format | |
out_format: .asciz "%d\n" | |
.balign 4 ; read number from stdin | |
input_num: .word 0 | |
.text | |
; Calculates the Nth position in | |
; Fibonacci's sequence. | |
; | |
; INPUT: | |
; r0 - N in f(N) | |
; OUTPUT: | |
; r0 - result | |
; MODIFIED REGISTERS: | |
; r1, r2, r3 | |
fibonacci: | |
stmdb sp!, {r4} ; push r4 onto the stack | |
mov r2, #0 ; first = 0 | |
mov r3, #1 ; second = 1 | |
mov r4, #0 ; step = 0 | |
b fibonacci_check ; unconditionally branch | |
fibonacci_loop: | |
cmp r4, #1 ; if step < 1 | |
blt fibonacci_ltone ; branch to ltone | |
add r1, r2, r3 ; accumulator = first + second | |
mov r2, r3 ; first = second | |
mov r3, r1 ; second = accumulator | |
b fibonacci_inc ; unconditionally branch | |
fibonacci_ltone: | |
mov r1, r4 ; accumulator = step | |
fibonacci_inc: | |
add r4, #1 ; step += 1 | |
fibonacci_check: | |
cmp r4, r0 ; if step <= N | |
blt fibonacci_loop ; branch to loop | |
mov r0, r1 | |
ldmia sp!, {r4} ; pop r4 off the stack | |
bx lr ; return to caller | |
main: | |
stmdb sp!, {lr} | |
; scanf("%d", &input_num) | |
ldr r0, addr_infmt | |
ldr r1, addr_in | |
bl scanf | |
; fib(N) | |
ldr r0, addr_in | |
ldr r0, [r0] | |
bl fibonacci | |
; printf("%d\n", input_num) | |
mov r1, r0 | |
ldr r0, addr_outfmt | |
bl printf | |
ldmia sp!, {lr} | |
bx lr | |
addr_infmt: .word in_format | |
addr_outfmt: .word out_format | |
addr_in: .word input_num | |
.global main | |
.global scanf | |
.global printf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment