Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- str: .space 41 # create char buffer, 40 chars + null
- nl: .asciiz "\n" # newline
- .text
- .globl main
- main:
- # get input
- li $v0,8 # service code
- la $a0,str # address of buffer
- li $a1,41 # buffer length
- syscall
- li $t0,0 # push null value onto stack. used to identify end of string
- addi $gp,$gp,-4 # alternatively can use sub 4
- sw $t0,($gp) # bottom of stack
- li $t1,0 # the index of the first char
- # push each character onto the stack
- push:
- lb $t0,str($t1) # get current char into
- # a full word
- beqz $t0,strend # null byte: end of string
- addi $gp,$gp,-4 # push the full word
- sw $t0,($gp) # holding the char
- addi $t1,1 # increment the index
- b push # loop
- strend:
- # pop chars from stack back into the buffer
- li $t1,0 # index of first byte of str
- pop:
- lw $t0,($gp) # pop a char off the stack
- addi $gp,$gp,4
- beqz $t0,done # null means empty stack
- sb $t0,str($t1) # store at string[$t1]
- addi $t1,1 # inc the index
- b pop # loop
- done:
- # print the reversed string
- li $v0,4 # service code
- la $a1,str # address of string
- syscall
- #returns to new line after reverse printed
- li $v0, 4
- la $a0, nl
- syscall
- #exits the program
- li $v0,10
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement