Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##write a program that is menu driven:
- #1 enter 2 numbers
- #2 add, 3 subtract, 4 multiply, 5 divide, 6 modulus, 7 exit
- #have a function for each entry
- #start without error checking, but if I finish this at home (hah) then add error checking
- #make sure to display the results via another function in menu
- .globl main
- .text
- ##define t0 as being the menu choice, t1 and t2 being the numbers you're operating on,
- #t3 holding the output
- # s0 through S6 are going to be our values that we will be comparing our t#s to for menu tests
- main:
- li $s0, 1
- li $s1, 2
- li $s2, 3
- li $s3, 4
- li $s4, 5
- li $s5, 6
- li $s6, 7
- li $s7, 8
- ##These are immutable and now we can compare user input (stored in the "t" registers).
- menu: #this is going to be our menu loop
- li $v0, 4 #call a string that will output the menu
- la $a0, choices
- syscall
- li $v0, 5 #prep for user input
- syscall
- move $t0, $v0 #move the user input from $v0 to $t0 where it will stay until we reiterate the loop
- #this is our primary menu control structure and needs to be in the menu
- #compare user input to our constants, and then go do the section that does it
- beq $t0, $s0, two_number_input
- beq $t0, $s1, addition
- beq $t0, $s2, subtraction
- beq $t0, $s3, multiplication
- beq $t0, $s4, division
- beq $t0, $s5, modulus
- beq $t0, $s6, results
- beq $t0, $s7, exit
- two_number_input:
- li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
- la $a0, first_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
- syscall #go do it
- #now, we should read in an integer as done by the user, with syscall 5
- li $v0, 5 #syscall for code 5 (reading in an integer)
- syscall #go do it
- move $t1, $v0 #moving the stored integer from $v0 to $t1 this is our first input value
- li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
- la $a0, second_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
- syscall #go do it
- #now, we should read in an integer as done by the user, with syscall 5
- li $v0, 5 #syscall for code 5 (reading in an integer)
- syscall #go do it
- move $t2, $v0 #moving the stored integer from $v0 to $t2
- j menu #go back to the menu
- addition:
- add $t3, $t1, $t2 #this should do: $t1+$t2 = something, which we put into $t3
- j menu #go back to the menu
- subtraction:
- sub $t3, $t1, $t2 #this should do: $t1-$t2 = something, which we put into $t3
- j menu #go back to the menu
- multiplication:
- mul $t3, $t1, $t2 #this should do: $t1*$t2 = something, which we put into $t3
- j menu #go back to the menu
- division:
- bltz $t2, error_div
- div $t3, $t1, $t2 #this should do: $t1/$t2 = something, which we put into $t3
- j menu #go back to the menu
- modulus:
- rem $t3, $t1, $t2 #this should do: $t1%$t2 = something, which we put into $t3
- j menu #go back to the menu
- ouput:
- li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
- la $a0, ouput #we're telling the program where to look for our message that we defined at the bottom of the program
- syscall #go do it
- move $a0, $t3 #move the value stored $t0 to $a0
- li $v0, 1 #tell it we're about to print an integer
- syscall #print the integer
- j menu #go back to the menu
- exit:
- li $v0, 4 #call a string that will output the menu
- la $a0, terminate
- syscall
- li $v0, 10
- syscall
- error_div:
- li $v0, 4 #call a string that will output the menu
- la $a0, div_error
- syscall
- j menu
- .data
- choices:
- .asciiz "\n\n1) Enter 2 numbers\n2) Add the two numbers\n3) Subtract the second number from the first\n4) Multiply the two numbers\n5) Divide the first number by the second\n6) Find the first number modulus the second number\n7) Output the results\n8) Exit\n\nEntry: "
- first_prompt:
- .asciiz "\nPlease input your first number: "
- second_prompt:
- .asciiz "\nPlease input your second number: "
- results:
- .asciiz "\nThe answer is: "
- terminate:
- .asciiz "\nTermination code received. Good bye."
- div_error:
- .asciiz "\nCannot divide by 0. Bringing you back to the menu."
Advertisement
Add Comment
Please, Sign In to add comment