Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. .include "java_util4.1-2.asm"
  2.  
  3. .data
  4.  
  5. .text
  6.  
  7. li $t0, 0 #comparison for number of times to print
  8. li $t2, 1 #$t2 will be our line number variable
  9. li $t3, 0 #$t3 will be number of times printed
  10. System_out_print("How many times should the message be printed?")
  11. Keyboard_next_int($t1) #This is the number of times to be printed. Subtract 1 from it repeatedly to get to 0
  12. sub $t1, $t1, 1 #Since bge counts for equals to as well, take 1 away to count for $t1 = 0 (otherwise 1 extra will print)
  13.  
  14. bge $t1, $t0, ifGreaterThan
  15.  
  16.  
  17. ifGreaterThan:
  18. System_out_print("\n") #start a new line
  19. printInt($t2) #print line number
  20. System_out_print(" ") #spacing for format
  21. add $t2, $t2, 1 #increment line number
  22. System_out_print("I love Computer Science!!") #print message
  23. add $t3, $t3, 1
  24. sub $t1, $t1, 1 #take 1 away from number of times to print
  25. bge $t1, $t0, ifGreaterThan #check if there's still more times to print
  26. b endDisplay
  27.  
  28. endDisplay:
  29. System_out_print("\n") #formatting
  30. System_out_print("Number of times printed: ")
  31. printInt($t3) #print $t3, the register with number of times printed
  32. b summationDisplay
  33.  
  34. summationDisplay:
  35. System_out_print("\n")
  36. System_out_print("The sum of the numbers from 1 to ")
  37. printInt($t3)
  38. System_out_print(" is: ")
  39. li $t4, 0 #the actual summation
  40. li $t5, 1 #the first number included in the summation. This will be incremented each time
  41. li $t6, 1 #count for the summation
  42. b summation
  43.  
  44. summation:
  45. add $t4, $t4, $t5
  46. add $t5, $t5, 1
  47. add $t6, $t6, 1
  48. ble $t6, $t3, summation #if the index of number of numbers added is less than total number of times displayed, continue
  49. b endProgram
  50.  
  51. endProgram:
  52. printInt($t4)
  53. li $v0, 10
  54. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement