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 to read string
- 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 char
- beqz $t0,strend # if null, go to strend
- addi $gp,$gp,-4 # move down the stack
- sw $t0,($gp) # store the char
- addi $t1,1 # add 1 to the index
- j push # jump back to push for loop
- strend:
- # pop chars from stack back into the buffer
- li $t1,0 # index of first byte of str
- pop:
- lw $t0,($gp) # pop char from stack
- addi $gp,$gp,4
- beqz $t0,done # if null, go to done (end of string)
- sb $t0,str($t1) # store byte in $t1
- addi $t1,1 # add 1 to the index
- j pop # jump back to pop for 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