Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. main: #This is the main program.
  2. #It first asks user to enter the price of each BobCat Bar.
  3. #It then asks user to enter the number of bar wrappers needed to exchange for a new bar.
  4. #It then asks user to enter how much money he/she has.
  5. #It then calls maxBars function to perform calculation of the maximum BobCat Bars the user will receive based on the information entered.
  6. #It then prints out a statement about the maximum BobCat Bars the user will receive.
  7.  
  8. addi $sp, $sp, -8 # Feel free to change the increment if you need for space.
  9. sw $ra, 0($sp)
  10. # Implement your main here
  11.  
  12. li $v0, 4
  13. la $a0, str0
  14. syscall
  15.  
  16. li $v0, 4
  17. la $a0, str1
  18. syscall
  19.  
  20. li $v0, 5
  21. syscall
  22. add $a1, $v0, $0 # storing the price into a1, which will be one of the arguments for maxBars
  23.  
  24. li $v0, 4
  25. la $a0, str2
  26. syscall
  27.  
  28. li $v0, 5
  29. syscall
  30. add $a0, $v0, $0 # storing n (# of wrappers) into a0 which will be the first argument of maxbars
  31. sw $a0, 4($sp) # saving it in the stack because it will be changed when I do another syscall
  32.  
  33. li $v0, 4
  34. la $a0, str3
  35. syscall
  36.  
  37. li $v0, 5
  38. syscall
  39. add $a2, $v0, $0 # a2 = money
  40.  
  41. li $v0, 4
  42. la $a0, str4
  43. syscall
  44.  
  45. lw $a0, 4($sp) # restoring a0
  46. jal maxBars # Call maxBars(a0, a1, a2) to calculate the maximum number of BobCat Bars
  47. add $t0, $v0, $0 # storing the return value in a temp
  48.  
  49. # Print out final statement here
  50. li $v0, 4
  51. la $a0, str7
  52. syscall
  53.  
  54. li $v0 1
  55. add $a0, $a2, $0
  56. syscall
  57.  
  58. li $v0, 4
  59. la $a0, str9
  60. syscall
  61.  
  62. li $v0, 1
  63. add $a0, $t0, $0
  64. syscall
  65.  
  66. li $v0, 4
  67. la $a0, str10
  68. syscall
  69.  
  70. j end # Jump to end of program
  71.  
  72.  
  73.  
  74. maxBars: # This function calculates the maximum number of BobCat Bars.
  75. # It takes in 3 arguments ($a0, $a1, $a2) as n, price, and money. It returns the maximum number of bars
  76.  
  77. addi $sp, $sp, -16
  78. sw $ra, 0($sp)
  79. sw $v0, 4($sp)
  80. sw $a0, 8($sp)
  81.  
  82. add $v0, $0, $0
  83. bgt $a1, $a2, zero # if price > money, branch to zero. Return value of v0 will be zero
  84.  
  85. div $s0, $a2, $a1 # Local variable s0 will be the initial amount of bars
  86. sw $s0, 12($sp)
  87.  
  88. li $v0, 4 # Print initial amount of bars
  89. la $a0, str5
  90. syscall
  91.  
  92. li $v0, 1
  93. add $a0, $s0, $0
  94. syscall
  95.  
  96. li $v0, 4
  97. la $a0, str6
  98. syscall
  99.  
  100. lw $a0, 8($sp) # argument when calling newBars (n)
  101. lw $a1, 12($sp) # second argument (wrappers/bars)
  102. jal newBars # Call a helper function to keep track of the number of bars.
  103.  
  104. add $t0, $v0, $zero # saving the return value from newBars into $t0
  105. lw $v0, 4($sp)
  106. lw $s0, 12($sp)
  107. add $v0, $s0, $t0 # return (v0) = initial bars (s0) + new bars (t0)
  108.  
  109. lw $ra, 0($sp)
  110. addi $sp, $sp, 16
  111. jr $ra
  112. # End of maxBars
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement