Silver_Smoulder

[ASM][Functional]For Loop Integer Compare

Apr 6th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. ##Write a program that asks the user how many sets of 2 numbers they have (n).
  2.  
  3. ##input n sets of 2 numbers.
  4.  
  5. ##Output in how many sets the numbers were equal
  6.  
  7. ## Implement a for loop with an iterator. Also, since there apparently isn't a "is exactly equal," I will need to implement a double condition of
  8. ## if not greater than, if not less than
  9.  
  10. ## t0 = number of iterations the user wants, t1 = our internal increment counter, t2 = first number user enters
  11. ## t3 = second number user enters, t4 = the count of how many equal t2/t3 pairs there are
  12.  
  13. .globl main
  14. .text
  15.  
  16. main:
  17.  
  18. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  19. la $a0, first_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
  20. syscall #go do it
  21.  
  22. #now, we should read in an integer as done by the user, with syscall 5
  23. li $v0, 5 #syscall for code 5 (reading in an integer)
  24. syscall #go do it
  25.  
  26. move $t0, $v0 #moving the stored integer from $v0 to $t0 this will be the number of times our loop executes
  27. #we now stored the value that the user input in $t0
  28.  
  29. li $t1, 0 #we are storing the value 0 in $t1, which will be our incrementor; every time we complete the loop, we increase it by 1
  30. li $t4, 0 #this is going to be how many "equal" values there are
  31. #syscall #go do it
  32.  
  33. ##at this point, we have determined how many sets of two integers the user will want. now we loop the input and "test if equal" step
  34.  
  35. loop:
  36. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  37. la $a0, second_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
  38. syscall #go do it
  39.  
  40. #now, we should read in an integer as done by the user, with syscall 5
  41. li $v0, 5 #syscall for code 5 (reading in an integer)
  42. syscall #go do it
  43.  
  44. move $t2, $v0 #moving the stored integer from $v0 to $t0 this is our first input value
  45. #we now stored the value that the user input in $t3
  46.  
  47. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  48. la $a0, third_prompt #we're telling the program where to look for our message that we defined at the bottom of the program
  49. syscall #go do it
  50.  
  51. #now, we should read in an integer as done by the user, with syscall 5
  52. li $v0, 5 #syscall for code 5 (reading in an integer)
  53. syscall #go do it
  54.  
  55. move $t3, $v0 #moving the stored integer from $v0 to $t0 this is our second input value
  56. #we now stored the value that the user input in $t3
  57.  
  58. ##for our conditional, we want to compare t2 and t3 for equality (by failing a greater than and less than test) and then incrementing t0,
  59. #and increment $t4 whenever the t2 and t3 are equal
  60.  
  61. addi $t1, $t1, 1 #increment $t1 (our counter) by 1
  62. beq $t2, $t3, pass #go to pass, increase our "equality" counter and proceed
  63. beq $t0, $t1, fail #if the increment is equal to our initial user init, exit
  64. b loop
  65.  
  66. pass:
  67. addi $t4, $t4, 1 #increment our "equals" variable by 1
  68. beq $t0, $t1, fail #if the increment is equal to our initial user init, exit
  69. b loop
  70.  
  71. fail:
  72. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  73. la $a0, exit_1 #we're telling the program where to look for our message that we defined at the bottom of the program
  74. syscall #go do it
  75.  
  76.  
  77. move $a0, $t4 #move the value stored $t0 to $a0
  78. li $v0, 1 #tell it we're about to print an integer
  79. syscall #go do it
  80.  
  81. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  82. la $a0, exit_2 #we're telling the program where to look for our message that we defined at the bottom of the program
  83. syscall #go do it
  84.  
  85. li $v0, 10 #terminate the program
  86. syscall #go do it
  87.  
  88. .data
  89. first_prompt:
  90. .asciiz "Please tell me how many sets of two integers you would like to compare: "
  91.  
  92. second_prompt:
  93. .asciiz "\nPlease input your first number: "
  94.  
  95. third_prompt:
  96. .asciiz "\nPlease input your second number: "
  97.  
  98. exit_1:
  99. .asciiz "\nThere were "
  100.  
  101. exit_2:
  102. .asciiz " entries that had equal values. The program is now terminating."
Advertisement
Add Comment
Please, Sign In to add comment