我在运行程序时遇到错误。 这是我得到的错误。
我收到异常错误。 错误“数据/堆栈读取中的错误地址”
错误说“指令引用未定义的符号”
在控制台中说 “要扭转的字符串:
COSC 300例外7 [错误的数据地址]发生并被忽略
该计划
.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
答案 0 :(得分:3)
有一点看起来有点滑稽:
在loop和storeLoop中,你使用$ t0作为指针,也作为要加载的字符。这意味着第一次加载一个字节时,您将损坏指针。
答案 1 :(得分:1)
伪代码:
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)
}
寄存器:
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