Silver_Smoulder

[ASM] Input and Store an Integer [Non-Functional]

Mar 28th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 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. #li $t0, 1 #tell it to print the integer stored in $t0
  19. syscall #go do it
  20.  
  21. move $a0,$t0 #move the value stored $t0 to $a0
  22. li $v0,1 #tell it we're about to print an integer
  23. syscall #go do it
  24.  
  25. li $v0, 10 #terminate the program
  26. syscall #go do it
  27.  
  28.  
  29.  
  30. .data
  31. prompt:
  32. .asciiz "\nPlease input an integer.\n" #this is going to be our prompt
  33. spit:
  34. .asciiz "\nYour integer is: " #We will now need to append the integer to this.
  35.  
  36. #get rid of this weak shit
  37. #li $a0, $v0 #print the integer stored in $v0, but it needs to print it from $a0???
  38. #syscall #go do it
  39.  
  40. #move $t0, $v0 #moving the stored integer from $v0 to $t0 so that we can free up the space for the termination code 10
  41. #syscall #go do it
  42. #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)
  43.  
  44. #li $v0, 4 #tell the system we are going to load a string
  45. #la $a0, spit #tell it that the address of the string is in $a0
  46. #li $t0, 1 #tell it to print the integer stored in $t0
  47. #syscall #go do it
  48.  
  49. #move $a0, $t0 #move the integer to where it can be output from
  50. #la $a0, 1 #print the integer theoretically on the same line as the 'spit' message
  51. #lw $a0, int #tell it to convert an integer to a word?
  52. #syscall #go do it
  53.  
  54. #code copied from Quora: https://www.quora.com/How-do-you-print-integers-in-MIPS
  55. #li $v0,1 #tell it we're about to print an integer
  56.  
  57.  
  58. #li $v0, 4 #tell the system we are going to load a string
  59. #la $a0, spit #tell it that the address of the string is in $a0
  60. #li $t0, 1 #tell it to print the integer stored in $t0
  61. #syscall #go do it
  62.  
  63. #li $a0, #tell the code to print the integer
  64. #syscall # go do it
  65.  
  66. li $v0, 10 #terminate the program
  67. syscall #go do it
  68.  
  69.  
  70.  
  71. .data
  72. prompt:
  73. .asciiz "\nPlease input an integer.\n" #this is going to be our prompt
  74. spit:
  75. .asciiz "\nYour integer is: " #We will now need to append the integer to this.
Advertisement
Add Comment
Please, Sign In to add comment