Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. .data
  2. str: .space 41 # create char buffer, 40 chars + null
  3. nl: .asciiz "\n" # newline
  4. .text
  5. .globl main
  6. main:
  7. # get input
  8. li $v0,8 # service code to read string
  9. la $a0,str # address of buffer
  10. li $a1,41 # buffer length
  11. syscall
  12. li $t0,0 # push null value onto stack. used to identify end of string
  13. addi $gp,$gp,-4 # alternatively can use sub 4
  14. sw $t0,($gp) # bottom of stack
  15. li $t1,0 # the index of the first char
  16.  
  17. # push each character onto the stack
  18. push:
  19.  
  20. lb $t0,str($t1) # get char
  21. beqz $t0,strend # if null, go to strend
  22.  
  23. addi $gp,$gp,-4 # move down the stack
  24. sw $t0,($gp) # store the char
  25.  
  26. addi $t1,1 # add 1 to the index
  27. j push # jump back to push for loop
  28. strend:
  29.  
  30. # pop chars from stack back into the buffer
  31. li $t1,0 # index of first byte of str
  32. pop:
  33. lw $t0,($gp) # pop char from stack
  34. addi $gp,$gp,4
  35. beqz $t0,done # if null, go to done (end of string)
  36.  
  37. sb $t0,str($t1) # store byte in $t1
  38. addi $t1,1 # add 1 to the index
  39. j pop # jump back to pop for loop
  40. done:
  41.  
  42. # print the reversed string
  43. li $v0,4 # service code
  44. la $a1,str # address of string
  45. syscall
  46.  
  47. #returns to new line after reverse printed
  48. li $v0, 4
  49. la $a0, nl
  50. syscall
  51.  
  52. #exits the program
  53. li $v0,10
  54. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement