Silver_Smoulder

[ASM] Basic Arithmetic and Output [Functional]

Apr 2nd, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 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.  
  37. #probably with blez (see pg 17 and 79 in Britton)
  38.  
  39. ##We will be storing the sum in t3, the difference in t4, the quotient in t5, and the product in t6
  40.  
  41. #arithmetic block
  42. add $t3, $t0, $t1 #this should do: $t0+$t1 = something, which we put into $t3
  43. sub $t4, $t0, $t1 #this should do: $t0-$t1 = something, which we put into $t4
  44. div $t5, $t0, $t1 #this should do: $t0/$t1 = something, which we put into $t5
  45. mul $t6, $t0, $t1 #this should do: $t0*$t1 = something, which we put into $t6
  46.  
  47. #output block
  48. #addition
  49. li $v0, 4 #tell the system we are going to load a string
  50. la $a0, sum #tell it that the address of the string is in $a0
  51. syscall #go do it
  52.  
  53. move $a0, $t3 #move the value stored $t0 to $a0
  54. li $v0, 1 #tell it we're about to print an integer
  55. syscall #go do it
  56.  
  57. #subtraction
  58. li $v0, 4 #tell the system we are going to load a string
  59. la $a0, diff #tell it that the address of the string is in $a0
  60. syscall #go do it
  61.  
  62. move $a0, $t4 #move the value stored $t0 to $a0
  63. li $v0, 1 #tell it we're about to print an integer
  64. syscall #go do it
  65.  
  66. #division
  67. li $v0, 4 #tell the system we are going to load a string
  68. la $a0, quot #tell it that the address of the string is in $a0
  69. syscall #go do it
  70.  
  71. move $a0, $t5 #move the value stored $t0 to $a0
  72. li $v0, 1 #tell it we're about to print an integer
  73. syscall #go do it
  74.  
  75. #multiplication
  76. li $v0, 4 #tell the system we are going to load a string
  77. la $a0, prod #tell it that the address of the string is in $a0
  78. syscall #go do it
  79.  
  80. move $a0, $t6 #move the value stored $t0 to $a0
  81. li $v0, 1 #tell it we're about to print an integer
  82. syscall #go do it
  83.  
  84. .data #gonna be storing our text and prompts here
  85.  
  86. prompt_first_number:
  87. .asciiz "Enter your first number: \n"
  88. prompt_second_number:
  89. .asciiz "Enter your second number: \n"
  90. sum:
  91. .asciiz "\nThe sum is: "
  92. diff:
  93. .asciiz "\nThe difference is: "
  94. quot:
  95. .asciiz "\nThe quotient is: "
  96. prod:
  97. .asciiz "\nThe product is: "
  98. error:
  99. .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