cs270 sp22 hw05 code
unknown
plain_text
3 years ago
2.2 kB
4
Indexable
Never
# File: hw05-assem.s # Done by: Carmen Bland Jr. .text .globl main main: addi sp, sp -8 #add space for 2 words sw ra, 0(sp) #push return address to stack sw x10, 4(sp) #push x10 (param for fibonacci/input in main) addi x10, zero, 10 #input = 10 jal ra, fibonacci #call fibonacci add x18, zero, x10 #result = fib(input) lw ra 0(sp) #load return address addi sp, sp, 8 #pop stack addi x10, zero, 0 #return 0 jalr zero, ra, 0 #exit program fibonacci: addi sp, sp -4 #add space for 1 word sw ra, 0(sp) #push return address to stack addi x5, zero, 2 #set up x5 as the comparison register addi x6, zero, 0 #set up x6 as zero (for reset) bge x10, x5, else #if !(n < 2), goto else beq zero, zero, exit #skip else else: addi sp, sp, -12 #add space for 3 words addi x10, x10, -1 #compute n-1 sw x10, 0(sp) #push x10 (n-1) to stack sw x7, 4(sp) #push x7 to stack sw x8, 8(sp) #push x8 to stack jal ra, fibonacci #call fib add x7, zero, x10 #copy result from fib(n-1) lw x10, 0(sp) #restore x10 addi x10, x10, -1 #compute n-2 jal ra, fibonacci #call fibonacci (recursive call) add x8, zero, x10 #copy result from fib(n-2) add x10, x7, x8 #put n = (n-1) + (n-2) lw x7, 4(sp) #restore x7 lw x8, 8(sp) #restore x8 addi sp, sp, 12 #pop stack exit: lw ra, 0(sp) #load return address addi sp, sp, 4 #pop stack jalr zero, ra, 0 #exit function