I'm getting errors when I'm running my program. Here are the errors I'm getting.
I'm getting an exception error. An error saying "Bad address in data/stack read"
An error saying "Instruction references undefined symbol"
In the console it says "String to be reversed :
COSC 300 Exception 7 [Bad data address] occurred and ignored
The program
.data
str: .asciiz "String to be reversed : \n"
msg: .asciiz "COSC 300"
ans: .asciiz "The string reversed is : "
.text
.globl main
main:
la $a0, str #print string
li $v0, 4
syscall
la $a0, msg #print string
li $v0, 4
syscall
la $t0, msg #load a string to be reversed
loop:
lb $t0, 0 ($t0) #load char from msg
beqz $t0, stringEnd # if null end loop
addi $sp, $sp -1 # reduce stack pointer
sb $t0, 0 ($sp) # store t0 into stack
addi $t1, $t1, 1 # gets next char
j loop
stringEnd:
la $t1, msg1
storeLoop:
lb $t0, 0($t0)
beqz $t0, end
lb $t4, 0($sp)
sb $t4, 0 ($t0)
addi $t1, $t1, 1
addi $sp, $sp, 1
j storeLoop
end:
la $a0, ans
li $v0, 4
syscall
move $a0, $t4
li $v0, 4
syscall
li $v0, 10
syscall
One thing that looks a bit funny:
In both loop and storeLoop you are using $t0 as a pointer and also as the character to be loaded. This means that the first time you load a byte you will be corrupting your pointer.
Pseudocode:
print(intro)
text = readString()
// find the end of the string
for (addr = text; *addr != NUL; addr++) { }
// now print it backward
while (--addr >= text) {
printChar(*addr)
}
Registers:
addr => $t0, tmp => $t1, orig => $t2
li $v0, 4 # print(intro)
la $a0, intro
syscall
li $v0, 8 # text = readString()
la $a0, text
li $a1, 256 # (size of input buffer)
syscall
la $t0, text # addr = text
move $t2, $t0 # orig = addr (backup of original address)
find: lb $t1, 0($t0) # tmp = *addr
beq $t1, 0, print # while(tmp != NUL)
addi $t0, $t0, 1 # addr++
j find # (end loop)
print: subi $t0, $t0, 1 # --addr
blt $t0, $t2, end # while (addr >= orig)
li $v0, 11 # printChar(
lb $a0, 0($t0) # *addr
syscall # )
j print # (end loop)
end: li $v0, 10 # exit cleanly
syscall
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With