Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # COMP1521 18s2 Week 04 Lab
  2. # Compute factorials, iterative function
  3.  
  4.  
  5. ### Global data
  6.  
  7. .data
  8. msg1:
  9. .asciiz "n = "
  10. msg2:
  11. .asciiz "n! = "
  12. eol:
  13. .asciiz "\n"
  14.  
  15. ### main() function
  16. .text
  17. .globl main
  18. main:
  19. # set up stack frame
  20. sw $fp, -4($sp) # push $fp onto stack
  21. la $fp, -4($sp) # set up $fp for this function
  22. sw $ra, -4($fp) # save return address
  23. sw $s0, -8($fp) # save $s0 to use as ... int n;
  24. addi $sp, $sp, -12 # reset $sp to last pushed item
  25.  
  26. # code for main()
  27. li $s0, 0 # n = 0;
  28.  
  29. la $a0, msg1
  30. li $v0, 4
  31. syscall # printf("n = ");
  32.  
  33. li $v0, 5
  34. syscall # scanf("%d", into $v0)
  35.  
  36. #set the argument from the scanned value
  37. move $a0, $v0
  38.  
  39. #call the function
  40. jal fac
  41.  
  42. #assign the function return to tmp
  43. move $t0, $v0
  44.  
  45. # ... TODO: add your code here for tmp = fac(n); ...
  46. # ... place the parameter in $a0 and get the result from $v0 ...
  47.  
  48. la $a0, msg2
  49. li $v0, 4
  50. syscall # printf("n! = ");
  51.  
  52. move $a0, $t0 # assume $t0 holds n!
  53. li $v0, 1
  54. syscall # printf("%d\n",tmp);
  55.  
  56. la $a0, eol
  57. li $v0, 4
  58. syscall # printf("\n");
  59.  
  60. # clean up stack frame
  61. lw $s0, -8($fp) # restore $s0 value
  62. lw $ra, -4($fp) # restore $ra for return
  63. la $sp, 4($fp) # restore $sp (remove stack frame)
  64. lw $fp, ($fp) # restore $fp (remove stack frame)
  65.  
  66. li $v0, 0
  67. jr $ra # return 0
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. # fac() function
  85.  
  86. fac:
  87. # setup stack frame
  88. sw $fp, -4($sp) # push $fp/ n < 1 or not a number produce n! = 1 onto stack
  89. la $fp, -4($sp) # set up $fp for this function
  90. sw $ra, -4($fp) # save return address
  91. sw $s0, -8($fp) # save $s0 to use as ... int i;
  92. sw $s1, -12($fp) # save $s1 to use as ... int prod;
  93. addi $sp, $sp, -16 # reset $sp to last pushed item
  94.  
  95. # code for fac()
  96.  
  97. # This is where the s2 computers
  98.  
  99.  
  100. li $s0, 1 # the counter/multiplier
  101. li $s1, 1 # the final value
  102.  
  103. while:
  104. # if $s0 = 0 finish the loop by moving to the branch label
  105. bgt $s0, $a0, end_while
  106.  
  107.  
  108. mul $s1, $s1, $s0 # multiply total by input number decremented
  109.  
  110. add $s0, $s0, 1 # i++
  111. j while
  112.  
  113. end_while:
  114.  
  115. move $v0, $s1 # place the return value into the v0
  116.  
  117. # ... TODO: place your code for the body of fac() here ...
  118. # ... use the value of n in $a0, place n! in $v0 ...
  119.  
  120. # clean up stack frame
  121. lw $s1, -12($fp) # restore $s1 value
  122. lw $s0, -8($fp) # restore $s0 value
  123. lw $ra, -4($fp) # restore $ra for return
  124. la $sp, 4($fp) # restore $sp (remove stack frame)
  125. lw $fp, ($fp) # restore $fp (remove stack frame)
  126.  
  127. jr $ra # return prod;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement