Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .text
- li s0, 6 # Load 6 (input)
- mv a0, s0 # Move input to a0
- jal x1, fact # Call factorial function
- beq x0, x0, exit # End program
- fact:
- addi sp, sp, -8 # Stack space for s0, x1
- sw, s0, 0(sp) # Save s0
- sw, x1, 4(sp) # Save return address
- li t0, 3 # Load base case value
- blt a0, t0, skip_call # If a0 < 3, skip recursion
- mv s0, a0 # Store current a0
- addi a0, a0, -1 # a0 = a0 - 1
- jal x1, fact # Recursively call fact
- mul a0, a0, s0 # Multiply result with s0
- skip_call:
- lw s0, 0(sp) # Restore s0
- lw, x1, 4(sp) # Restore return address
- addi sp, sp, 8 # Clean up stack
- jalr x0, x1, 0 # Return
- exit:
- nop # Program end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement