Advertisement
foreverfugazi

fact without recur

Oct 17th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. .data
  2. num: .word 6 # Number for factorial
  3. fact: .word 0 # Store result
  4.  
  5. .text
  6. _start:
  7. la x5, num # Load address of num
  8. lw x5, 0(x5) # Load num into x5
  9.  
  10. li x6, 1 # Initialize result to 1
  11.  
  12. factorial_loop:
  13. li x7, 0 # Load 0 for comparison
  14. bne x5, x7, multiply # If x5 != 0, jump to multiply
  15.  
  16. j end_loop # If x5 == 0, exit loop
  17.  
  18. multiply:
  19. mul x6, x6, x5 # result *= n (x5)
  20. addi x5, x5, -1 # n--
  21. j factorial_loop # Loop again
  22.  
  23. end_loop:
  24. la x8, fact # Load address of fact
  25. sw x6, 0(x8) # Store result in memory
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement