Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 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
  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 current char into
  21. # a full word
  22. beqz $t0,strend # null byte: end of string
  23.  
  24. addi $gp,$gp,-4 # push the full word
  25. sw $t0,($gp) # holding the char
  26.  
  27. addi $t1,1 # increment the index
  28. b push # loop
  29. strend:
  30.  
  31. # pop chars from stack back into the buffer
  32. li $t1,0 # index of first byte of str
  33. pop:
  34. lw $t0,($gp) # pop a char off the stack
  35. addi $gp,$gp,4
  36. beqz $t0,done # null means empty stack
  37.  
  38. sb $t0,str($t1) # store at string[$t1]
  39. addi $t1,1 # inc the index
  40. b pop # loop
  41. done:
  42.  
  43. # print the reversed string
  44. li $v0,4 # service code
  45. la $a1,str # address of string
  46. syscall
  47.  
  48. #returns to new line after reverse printed
  49. li $v0, 4
  50. la $a0, nl
  51. syscall
  52.  
  53. #exits the program
  54. li $v0,10
  55. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement