Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main: #This is the main program.
- #It first asks user to enter the price of each BobCat Bar.
- #It then asks user to enter the number of bar wrappers needed to exchange for a new bar.
- #It then asks user to enter how much money he/she has.
- #It then calls maxBars function to perform calculation of the maximum BobCat Bars the user will receive based on the information entered.
- #It then prints out a statement about the maximum BobCat Bars the user will receive.
- addi $sp, $sp, -8 # Feel free to change the increment if you need for space.
- sw $ra, 0($sp)
- # Implement your main here
- li $v0, 4
- la $a0, str0
- syscall
- li $v0, 4
- la $a0, str1
- syscall
- li $v0, 5
- syscall
- add $a1, $v0, $0 # storing the price into a1, which will be one of the arguments for maxBars
- li $v0, 4
- la $a0, str2
- syscall
- li $v0, 5
- syscall
- add $a0, $v0, $0 # storing n (# of wrappers) into a0 which will be the first argument of maxbars
- sw $a0, 4($sp) # saving it in the stack because it will be changed when I do another syscall
- li $v0, 4
- la $a0, str3
- syscall
- li $v0, 5
- syscall
- add $a2, $v0, $0 # a2 = money
- li $v0, 4
- la $a0, str4
- syscall
- lw $a0, 4($sp) # restoring a0
- jal maxBars # Call maxBars(a0, a1, a2) to calculate the maximum number of BobCat Bars
- add $t0, $v0, $0 # storing the return value in a temp
- # Print out final statement here
- li $v0, 4
- la $a0, str7
- syscall
- li $v0 1
- add $a0, $a2, $0
- syscall
- li $v0, 4
- la $a0, str9
- syscall
- li $v0, 1
- add $a0, $t0, $0
- syscall
- li $v0, 4
- la $a0, str10
- syscall
- j end # Jump to end of program
- maxBars: # This function calculates the maximum number of BobCat Bars.
- # It takes in 3 arguments ($a0, $a1, $a2) as n, price, and money. It returns the maximum number of bars
- addi $sp, $sp, -16
- sw $ra, 0($sp)
- sw $v0, 4($sp)
- sw $a0, 8($sp)
- add $v0, $0, $0
- bgt $a1, $a2, zero # if price > money, branch to zero. Return value of v0 will be zero
- div $s0, $a2, $a1 # Local variable s0 will be the initial amount of bars
- sw $s0, 12($sp)
- li $v0, 4 # Print initial amount of bars
- la $a0, str5
- syscall
- li $v0, 1
- add $a0, $s0, $0
- syscall
- li $v0, 4
- la $a0, str6
- syscall
- lw $a0, 8($sp) # argument when calling newBars (n)
- lw $a1, 12($sp) # second argument (wrappers/bars)
- jal newBars # Call a helper function to keep track of the number of bars.
- add $t0, $v0, $zero # saving the return value from newBars into $t0
- lw $v0, 4($sp)
- lw $s0, 12($sp)
- add $v0, $s0, $t0 # return (v0) = initial bars (s0) + new bars (t0)
- lw $ra, 0($sp)
- addi $sp, $sp, 16
- jr $ra
- # End of maxBars
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement