Silver_Smoulder

[ASM][Functional]Loops & Output

Apr 3rd, 2019
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. ##do something similar to the previous program but instead, have there be a while loop that allows you to input as
  2. ##many numbers as you want, but will exit when the last number is less than the first and if the number is 0
  3.  
  4. .globl main
  5. .text
  6. main:
  7. ##overall, we want to create a loop that is going to compare our two testing values, execute them, and then re-do it
  8. ##unless the 2nd number > 1st, at which point we exit with an error message, but we do it in a loop
  9.  
  10. #copied from the previous program
  11.  
  12. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  13. 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
  14. syscall #go do it
  15.  
  16. #now, we should read in an integer as done by the user, with syscall 5
  17. li $v0, 5 #syscall for code 5 (reading in an integer)
  18. syscall #go do it
  19.  
  20. move $t0, $v0 #moving the stored integer from $v0 to $t0 so that we can free up the space for the termination code 10
  21. #we now stored the value that the user input in $t0
  22.  
  23. ##loop will initialize here: if our 2nd number is larger than the first, we exit
  24. loop:
  25.  
  26. #this is the block of code we want repeated in our code
  27. #now for the second number, code is going to be pretty identical
  28. #we can just copypaste this block because the new value in #v0/#a0 is going to bverwrite the old stuff
  29. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  30. 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
  31. syscall #go do it
  32.  
  33. #now, we should read in an integer as done by the user, with syscall 5
  34. li $v0, 5 #syscall for code 5 (reading in an integer)
  35. syscall #go do it
  36.  
  37. move $t1, $v0 #moving the stored integer from $v0 to $t1 so that we can free up the space for the termination code 10
  38.  
  39. ##at this point we have prompted the user for at least 2 numbers and have stored them in $t0 and $t1
  40.  
  41. ##we will do our first conditional for the exit conditional here
  42. blt $t1, $t0, end
  43.  
  44. ##We will be storing the sum in t3, the difference in t4, the quotient in t5, and the product in t6
  45.  
  46.  
  47. #arithmetic block
  48. add $t3, $t0, $t1 #this should do: $t0+$t1 = something, which we put into $t3
  49. sub $t4, $t0, $t1 #this should do: $t0-$t1 = something, which we put into $t4
  50. div $t5, $t0, $t1 #this should do: $t0/$t1 = something, which we put into $t5
  51. mul $t6, $t0, $t1 #this should do: $t0*$t1 = something, which we put into $t6
  52.  
  53. #output block
  54. li $v0, 4 #tell the system we are going to load a string
  55. la $a0, sum #tell it that the address of the string is in $a0
  56. syscall #go do it
  57.  
  58. move $a0, $t3 #move the value stored $t0 to $a0
  59. li $v0, 1 #tell it we're about to print an integer
  60. syscall #go do it
  61.  
  62. #subtraction
  63. li $v0, 4 #tell the system we are going to load a string
  64. la $a0, diff #tell it that the address of the string is in $a0
  65. syscall #go do it
  66.  
  67. move $a0, $t4 #move the value stored $t0 to $a0
  68. li $v0, 1 #tell it we're about to print an integer
  69. syscall #go do it
  70.  
  71. #division
  72. li $v0, 4 #tell the system we are going to load a string
  73. la $a0, quot #tell it that the address of the string is in $a0
  74. syscall #go do it
  75.  
  76. move $a0, $t5 #move the value stored $t0 to $a0
  77. li $v0, 1 #tell it we're about to print an integer
  78. syscall #go do it
  79.  
  80. #multiplication
  81. li $v0, 4 #tell the system we are going to load a string
  82. la $a0, prod #tell it that the address of the string is in $a0
  83. syscall #go do it
  84.  
  85. move $a0, $t6 #move the value stored $t0 to $a0
  86. li $v0, 1 #tell it we're about to print an integer
  87. syscall #go do it
  88.  
  89. ##this is where we will terminate our loop and go back to the initialize
  90. li $v0, 4 #prep string
  91. la $a0, rinit #load address of string
  92. syscall #go do it
  93.  
  94. ble $t0, $t1, loop #since our input values haven't changed, we tell it to go back to the loop init
  95.  
  96. end:
  97. #output the exit message and terminate the program
  98.  
  99. #exit message
  100. li, $v0, 4 #prep a string
  101. la $a0, terminate #load string address
  102. syscall #go do it
  103. #and now we're done
  104. li $v0, 10 #prep exit
  105. syscall #go do it
  106.  
  107.  
  108.  
  109. .data #no longer sure what exactly we store here
  110.  
  111. prompt_first_number:
  112. .asciiz "Enter your first number: \n"
  113. prompt_second_number:
  114. .asciiz "Enter your second number: \n"
  115. sum:
  116. .asciiz "\nThe sum is: "
  117. diff:
  118. .asciiz "\nThe difference is: "
  119. quot:
  120. .asciiz "\nThe quotient is: "
  121. prod:
  122. .asciiz "\nThe product is: "
  123. rinit:
  124. .asciiz "\nWe are now re-engaging the loop, please stand by.\n"
  125. terminate:
  126. .asciiz "Termination condition triggered. Have a nice day. The program will now terminate. Please restart to reinitialize."
Advertisement
Add Comment
Please, Sign In to add comment