Last active
April 1, 2022 09:42
-
-
Save tombasche/341e480d752f3c2a471f7212c7d2689d to your computer and use it in GitHub Desktop.
Run in Spim or Mars to reverse a string with assembler! (MIPS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mips program to reverse a string | |
# mips.s | |
.data | |
str: .space 128 # character buffer | |
.text | |
.globl main | |
main: # input the string | |
li $v0, 8 # service code | |
la $a0, str # address of buffer | |
li $a1, 128 # buffer length | |
syscall | |
li $t0, 0 # push a null | |
subu $sp, $sp, 4 # onto the stack | |
sw $t0, ($sp) # to signal its bottom | |
li $t1, 0 # index of first char in str | |
pushl: # push each character on the stack | |
lbu $t0, str($t1) # get current char into a full word | |
beqz $t0, stend # null byte: end of string | |
subu $sp, $sp, 4 # push the full word | |
sw $t0, ($sp) # holding the char | |
addu $t1, $t1 1 # increment the index | |
j pushl # loop | |
stend: # pop chars from stack back into the buffer | |
li $t1, 0 # index of first byte of str | |
popl: | |
lw $t0, ($sp) # pop a char off the stack | |
addu $sp, $sp, 4 | |
beqz $t0, done # null means empty stack | |
sb $t0, str($t1) # store at string[$t1] | |
addu $t1, $t1, 1 # increment the index | |
j popl | |
done: # print the reversed string | |
li $v0, 4 # service code | |
la $a1, str # address of string | |
syscall | |
li $v0, 10 # exit | |
syscall | |
#end of program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment