Untitled
unknown
plain_text
3 years ago
1.9 kB
2
Indexable
Never
## fibonacci.S .text # code section .global fibonacci_asm # declar the sum_asm function as a global function .type fibonacci_asm, @function # define sum_asm as a function fibonacci_asm: mv a0, s0 addi sp, sp, -16 # allocate stack sw ra, 0(sp) sw s0, 4(sp) sw s1, 8(sp) sw s2,12(sp) jal ra, fibonacci # call fib(n) mv a0, s0 # a0 = s0 li a1, 0 # a1 = 0 li a2, 0 # a2 = 0 j epilogue ## write assembly code here. ## call fibonacci function and get return value. fibonacci: ## fibonacci function blt a0, s2, condition # if a0 < 2 then condition addi s0, a0, -1 blt s0, s2, condition # if s0 < 2 then condition addi sp, sp, -8 # PUSH stack sw ra, 0(sp) # save ret stack sw a0, 4(sp) # save n(a0) of f(n) addi a0, a0, -1 # n-1 in a0 jal fibonacci # call fib(n-1) lw s0, 4(sp) # restore n sw a0, 4(sp) # save fib(n-1) addi a0, s0, -2 # n-2 in a0 jal fibonacci # call fib(n-2) lw s0, 4(sp) # get fib(n-1) add a0, a0, s0 # a0 = fib(n-1) + fib(n-2) lw ra, 0(sp) # Get ret addr addi sp, sp, 8 # POP Stack condition: ret # jr ra epilogue: lw ra, 0(sp) lw s0, 4(sp) lw s1, 8(sp) lw s2,12(sp) addi sp, sp, 16 # allocate stack ret # Write your assembly code funtion here. # Please notice the rules of calling convention. .size fibonacci_asm, .-fibonacci_asm