Silver_Smoulder

[ASM] Input and Store an Integer [Functional]

Apr 2nd, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. ##write a program that prompts the user for input (integer), and stores it#
  2.  
  3. .globl main
  4. .text
  5.  
  6. main:
  7. li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
  8. la $a0, prompt #we're telling the program where to look for our message that we defined at the bottom of the program
  9. syscall #go do it
  10.  
  11. #now theoretically, we should read in an integer as done by the user, with syscall 5
  12. li $v0, 5 #syscall for code 5 (reading in an integer)
  13. syscall #go do it
  14.  
  15. move $t0, $v0 #moving the stored integer from $v0 to $t0 so that we can free up the space for the termination code 10
  16. li $v0, 4 #tell the system we are going to load a string
  17. la $a0, spit #tell it that the address of the string is in $a0
  18. syscall #go do it
  19.  
  20. move $a0,$t0 #move the value stored $t0 to $a0
  21. li $v0,1 #tell it we're about to print an integer
  22. syscall #go do it
  23.  
  24. li $v0, 10 #terminate the program
  25. syscall #go do it
  26.  
  27.  
  28.  
  29. .data
  30. prompt:
  31. .asciiz "\nPlease input an integer.\n" #this is going to be our prompt
  32. spit:
  33. .asciiz "\nYour integer is: " #We will now need to append the integer to this.
  34.  
  35. #get rid of this weak shit
  36. #li $a0, $v0 #print the integer stored in $v0, but it needs to print it from $a0???
  37. #syscall #go do it
  38.  
  39. #move $t0, $v0 #moving the stored integer from $v0 to $t0 so that we can free up the space for the termination code 10
  40. #syscall #go do it
  41. #in this case, the syscall is unnecessary as the system will not recognize the code that's currently being stored in $v0; and since we are not doing anything to the integer we are storing, it doesn't actually need to be syscalled (though if we did want to do something to it, we'd need to)
  42.  
  43. #li $v0, 4 #tell the system we are going to load a string
  44. #la $a0, spit #tell it that the address of the string is in $a0
  45. #li $t0, 1 #tell it to print the integer stored in $t0
  46. #syscall #go do it
  47.  
  48. #move $a0, $t0 #move the integer to where it can be output from
  49. #la $a0, 1 #print the integer theoretically on the same line as the 'spit' message
  50. #lw $a0, int #tell it to convert an integer to a word?
  51. #syscall #go do it
  52.  
  53. #code copied from Quora: https://www.quora.com/How-do-you-print-integers-in-MIPS
  54. #li $v0,1 #tell it we're about to print an integer
  55.  
  56.  
  57. #li $v0, 4 #tell the system we are going to load a string
  58. #la $a0, spit #tell it that the address of the string is in $a0
  59. #li $t0, 1 #tell it to print the integer stored in $t0
  60. #syscall #go do it
  61.  
  62. #li $a0, #tell the code to print the integer
  63. #syscall # go do it
  64.  
  65. li $v0, 10 #terminate the program
  66. syscall #go do it
  67.  
  68.  
  69.  
  70. .data
  71. prompt:
  72. .asciiz "\nPlease input an integer.\n" #this is going to be our prompt
  73. spit:
  74. .asciiz "\nYour integer is: " #We will now need to append the integer to this.
Advertisement
Add Comment
Please, Sign In to add comment