Silver_Smoulder

[ASM][Non-Func] Menu Driven Arithmetic

Apr 10th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. ##write a program that is menu driven:
  2. #1 enter 2 numbers
  3. #2 add, 3 subtract, 4 multiply, 5 divide, 6 modulus, 7 exit
  4. #have a function for each entry
  5. #start without error checking, but if I finish this at home (hah) then add error checking
  6. #make sure to display the results via another function in menu
  7.  
  8. .globl main
  9. .text
  10.  
  11. ##define t0 as being the menu choice, t1 and t2 being the numbers you're operating on,
  12. #t3 holding the output
  13. # s0 through S6 are going to be our values that we will be comparing our t#s to for menu tests
  14.  
  15. main:
  16.  
  17. li $s0, 1
  18. li $s1, 2
  19. li $s2, 3
  20. li $s3, 4
  21. li $s4, 5
  22. li $s5, 6
  23. li $s6, 7
  24. li $s7, 8
  25. ##These are immutable and now we can compare user input (stored in the "t" registers).
  26.  
  27. menu: #this is going to be our menu loop
  28. li $v0, 4 #call a string that will output the menu
  29. la $a0, choices
  30. syscall
  31.  
  32. li $v0, 5 #prep for user input
  33. syscall
  34. move $t0, $v0 #move the user input from $v0 to $t0 where it will stay until we reiterate the loop
  35.  
  36. #this is our primary menu control structure and needs to be in the menu
  37. #compare user input to our constants, and then go do the section that does it
  38. beq $t0, $s0, two_number_input
  39. beq $t0, $s1, addition
  40. beq $t0, $s2, subtraction
  41. beq $t0, $s3, multiplication
  42. beq $t0, $s4, division
  43. beq $t0, $s5, modulus
  44. beq $t0, $s6, results
  45. beq $t0, $s7, exit
  46.  
  47.  
  48. two_number_input:
  49.  
  50. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  51. la $a0, first_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
  52. syscall #go do it
  53.  
  54. #now, we should read in an integer as done by the user, with syscall 5
  55. li $v0, 5 #syscall for code 5 (reading in an integer)
  56. syscall #go do it
  57.  
  58. move $t1, $v0 #moving the stored integer from $v0 to $t1 this is our first input value
  59.  
  60. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  61. la $a0, second_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
  62. syscall #go do it
  63.  
  64. #now, we should read in an integer as done by the user, with syscall 5
  65. li $v0, 5 #syscall for code 5 (reading in an integer)
  66. syscall #go do it
  67.  
  68. move $t2, $v0 #moving the stored integer from $v0 to $t2
  69.  
  70. j menu #go back to the menu
  71.  
  72.  
  73. addition:
  74. add $t3, $t1, $t2 #this should do: $t1+$t2 = something, which we put into $t3
  75. j menu #go back to the menu
  76.  
  77. subtraction:
  78. sub $t3, $t1, $t2 #this should do: $t1-$t2 = something, which we put into $t3
  79. j menu #go back to the menu
  80.  
  81. multiplication:
  82. mul $t3, $t1, $t2 #this should do: $t1*$t2 = something, which we put into $t3
  83. j menu #go back to the menu
  84.  
  85. division:
  86. bltz $t2, error_div
  87. div $t3, $t1, $t2 #this should do: $t1/$t2 = something, which we put into $t3
  88. j menu #go back to the menu
  89.  
  90. modulus:
  91. rem $t3, $t1, $t2 #this should do: $t1%$t2 = something, which we put into $t3
  92. j menu #go back to the menu
  93.  
  94. ouput:
  95. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  96. la $a0, ouput #we're telling the program where to look for our message that we defined at the bottom of the program
  97. syscall #go do it
  98.  
  99. move $a0, $t3 #move the value stored $t0 to $a0
  100. li $v0, 1 #tell it we're about to print an integer
  101. syscall #print the integer
  102.  
  103. j menu #go back to the menu
  104.  
  105. exit:
  106. li $v0, 4 #call a string that will output the menu
  107. la $a0, terminate
  108. syscall
  109.  
  110. li $v0, 10
  111. syscall
  112.  
  113. error_div:
  114. li $v0, 4 #call a string that will output the menu
  115. la $a0, div_error
  116. syscall
  117. j menu
  118.  
  119. .data
  120.  
  121. choices:
  122. .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: "
  123.  
  124. first_prompt:
  125. .asciiz "\nPlease input your first number: "
  126.  
  127. second_prompt:
  128. .asciiz "\nPlease input your second number: "
  129.  
  130. results:
  131. .asciiz "\nThe answer is: "
  132.  
  133. terminate:
  134. .asciiz "\nTermination code received. Good bye."
  135.  
  136. div_error:
  137. .asciiz "\nCannot divide by 0. Bringing you back to the menu."
Advertisement
Add Comment
Please, Sign In to add comment