Silver_Smoulder

[ASM] [Functional] Arithmetic, Output, Errors

Apr 3rd, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #write a program that is going to take user input of two numbers (test it only with the 1st # > 2nd #) and then
  2. #and then output: the sum/difference/quotient/product is and then actually do it, using the functions from the appendix
  3. #furthermore when I am done, make it do an error message if 1st # < 2nd #
  4.  
  5. .globl main
  6. .text
  7.  
  8. main:
  9. #copied from the first program
  10. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  11. la $a0, prompt_first_number #we're telling the program where to look for our message that we defined at the bottom of the program
  12. syscall #go do it
  13.  
  14. #now, we should read in an integer as done by the user, with syscall 5
  15. li $v0, 5 #syscall for code 5 (reading in an integer)
  16. syscall #go do it
  17.  
  18. move $t0, $v0 #moving the stored integer from $v0 to $t0 so that we can free up the space for the termination code 10
  19. #we now stored the value that the user input in $t0
  20.  
  21. #now for the second number, code is going to be pretty identical
  22. #we can just copypaste this block because the new value in #v0/#a0 is going to bverwrite the old stuff
  23. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  24. la $a0, prompt_second_number #we're telling the program where to look for our message that we defined at the bottom of the program
  25. syscall #go do it
  26.  
  27. #now, we should read in an integer as done by the user, with syscall 5
  28. li $v0, 5 #syscall for code 5 (reading in an integer)
  29. syscall #go do it
  30.  
  31. move $t1, $v0 #moving the stored integer from $v0 to $t1 so that we can free up the space for the termination code 10
  32.  
  33. ##at this point we have prompted the user for 2 numbers and have stored them in $t0 and $t1
  34.  
  35. ##at this point, we want to test the two numbers and give the user an error message if the input is wrong
  36. ##not at this point, because we need to determine the difference of the two values, so it will need to happen after the arithmetic block
  37. #probably with blez (see pg 17 and 79 in Britton)
  38. #start at line 49 with ##error test here
  39.  
  40.  
  41. ##We will be storing the sum in t3, the difference in t4, the quotient in t5, and the product in t6
  42.  
  43. #arithmetic block
  44. add $t3, $t0, $t1 #this should do: $t0+$t1 = something, which we put into $t3
  45. sub $t4, $t0, $t1 #this should do: $t0-$t1 = something, which we put into $t4
  46. div $t5, $t0, $t1 #this should do: $t0/$t1 = something, which we put into $t5
  47. mul $t6, $t0, $t1 #this should do: $t0*$t1 = something, which we put into $t6
  48.  
  49. ##error test here
  50. #whoops forgot to tell it we're gonna load a string
  51. #li $v0, 4
  52. #nope wasn't it
  53. #try evaluating it at #v0?
  54. #move $v0, $t4
  55. #nope
  56. #stuff like replacing sub with subu and blez with bltzal doesn't either
  57. blez $t4, brexit
  58.  
  59.  
  60. #output block
  61. #adzal dition
  62. li $v0, 4 #tell the system we are going to load a string
  63. la $a0, sum #tell it that the address of the string is in $a0
  64. syscall #go do it
  65.  
  66. move $a0, $t3 #move the value stored $t0 to $a0
  67. li $v0, 1 #tell it we're about to print an integer
  68. syscall #go do it
  69.  
  70. #subtraction
  71. li $v0, 4 #tell the system we are going to load a string
  72. la $a0, diff #tell it that the address of the string is in $a0
  73. syscall #go do it
  74.  
  75. move $a0, $t4 #move the value stored $t0 to $a0
  76. li $v0, 1 #tell it we're about to print an integer
  77. syscall #go do it
  78.  
  79. #division
  80. li $v0, 4 #tell the system we are going to load a string
  81. la $a0, quot #tell it that the address of the string is in $a0
  82. syscall #go do it
  83.  
  84. move $a0, $t5 #move the value stored $t0 to $a0
  85. li $v0, 1 #tell it we're about to print an integer
  86. syscall #go do it
  87.  
  88. #multiplication
  89. li $v0, 4 #tell the system we are going to load a string
  90. la $a0, prod #tell it that the address of the string is in $a0
  91. syscall #go do it
  92.  
  93. move $a0, $t6 #move the value stored $t0 to $a0
  94. li $v0, 1 #tell it we're about to print an integer
  95. syscall #go do it
  96.  
  97. #and now we're done
  98. li $v0, 10
  99. syscall
  100.  
  101. brexit: #we put it here because any actual code needs to be in .text, but I am putting it after the "program exit" command
  102. #so that we don't accidentally run it during our program phase
  103. li $v0, 4 #ready to output a string
  104. la $a0, error_msg #give it the address of my string
  105. syscall # go do it
  106.  
  107. li $v0, 10 #get ready to leave
  108. syscall #leave
  109.  
  110. .data #gonna be storing our text and prompts here
  111.  
  112. prompt_first_number:
  113. .asciiz "Enter your first number: \n"
  114. prompt_second_number:
  115. .asciiz "Enter your second number: \n"
  116. sum:
  117. .asciiz "\nThe sum is: "
  118. diff:
  119. .asciiz "\nThe difference is: "
  120. quot:
  121. .asciiz "\nThe quotient is: "
  122. prod:
  123. .asciiz "\nThe product is: "
  124. error_msg:
  125. .asciiz "Please terminate the program and input the numbers in the proper sequence - the first number must be greater than the second number."
Advertisement
Add Comment
Please, Sign In to add comment