Update mult_recursive.s

This commit is contained in:
Sirin Puenggun 2024-03-04 15:27:17 +07:00 committed by GitHub
parent 4616b3681a
commit 86cb0c811a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,22 +12,27 @@ main:
mult:
# a0 = a, a1 = b
addi sp sp -12 # Move stack pointer down by 12 bytes to store a, b, ra
sw a0 8(sp) # Store a
sw a1 4(sp) # Store b
# Set Up
addi sp sp -8 # Move stack pointer down by 8 bytes to store a, ra
sw a0 4(sp) # Store a
sw ra 0(sp) # Store ra
addi t0 x0 1 # t0 = temporary 1
bne a1 t0 return # if b != then jump to return
addi sp sp 12
bne a1 t0 return # if b != 1 then jump to return
# Base Case
addi sp sp 8 # Move stack pointer up to previous ra
jr ra # return a
return:
# a + mult(a, b-1)
addi a1 a1 -1 # b = b-1
jal mult # call mult(a, b-1)
lw t1 8(sp) # load a into t1
# After get output from function (get a0)
lw t1 4(sp) # load a into t1
lw ra 0(sp) # Load ra
addi sp sp 12
addi sp sp 8 # Move stack pointer up to previous ra
add a0 a0 t1 # a + mult(a, b-1)
jr ra