Silver_Smoulder

[ASM] [Inc] Arrays and Strings

Apr 16th, 2019
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. ##output the contents of an "array" which you initialize via .byte, .word, .half
  2.  
  3. #then, input a string and output the string backwards
  4.  
  5. .data
  6. string_array:
  7. .space 1000 #define an array 1000 bytes big
  8.  
  9. reverse_array:
  10. .space 1000 #define the array that will hold the reverse
  11.  
  12. .globl main
  13. .text
  14.  
  15.  
  16. main:
  17.  
  18. la $a0, user_input #user input
  19. li $v0, 4
  20. syscall
  21.  
  22. la $a0, string_array #prepare for user input into the string array
  23. li $a1, 1000 #telling it to feed it into those 1000 bytes of memory + 1(why +1?)
  24. li $v0, 8 #the actual syscall code
  25. syscall
  26.  
  27.  
  28. la $t0, string_array #set t1 to store an address of our string array
  29. la $t2, reverse_array #set t2 to be the address of our reverse array
  30.  
  31. repeat:
  32.  
  33. lb $t1, ($t0) #store the BYTE indicated by the address in t0
  34. beqz $t1, finish #if the address becomes 0, terminate
  35.  
  36. ##get the maximium of the reverse array and feed byte by byte from the back-to-front
  37.  
  38.  
  39.  
  40. addi $t0, $t0, 4 #increase the address by 4, which moves us to the next byte
  41.  
  42. finish:
  43. la $a0, terminate
  44. li $v0, 4
  45. syscall
  46.  
  47. li $v0, 10
  48. syscall
  49.  
  50.  
  51.  
  52. .data
  53.  
  54. user_input:
  55. .asciiz "Input your string: "
  56.  
  57. terminate:
  58. .asciiz "Terminating the program."
  59.  
  60.  
  61.  
  62. #DO: hybrid assignment 7
  63. #http://courses.cs.vt.edu/~cs2505/summer2011/Notes/pdf/T23.MIPSArrays.pdf
  64. #https://rosettacode.org/wiki/Reverse_a_string#MIPS_Assembly
Advertisement
Add Comment
Please, Sign In to add comment