我正在将C代码转换为MIPS,找到字符串中子字符串的位置,我无法弄清楚是什么导致地址超出范围错误“strloop: lb $ t2,($ t1)“(第76行)。我该如何修复错误? 我的代码是:
.data
Text : .space 28
Key : .space 5
msg1 : .asciiz "Please enter the text:"
msg2 : .asciiz "\nPlease enter the key:"
msg3 : .asciiz "\n NOT FOUND"
msg4 : .asciiz "The key found at:"
.text
main:
la $a0, msg1
li $v0,4
syscall
la $a0, Text
li $a1, 28
li $v0,8
syscall
la $t0, Text
la $a0, msg2
li, $v0,4
syscall
la $a0, Key
li $a1,5
li $v0,8
syscall
la $t1, Key
add $a0, $t1 ,$zero
jal mstrlen
add $t2, $v0, $t2
add $a0, $t0, $zero
add $a1, $t1, $zero
add $a2, $t2, $zero
jal findPos
add $t0, $t0, $v0
add $a0, $v0, $zero
beq $a0, $zero,K
li $v0, 4
la $a0, msg4
syscall
li $t0, 1
move $a0, $t0
syscall
K: la $a1, msg3
li $v0,4
syscall
li $v0, 10
syscall
mstrlen:
addi $sp, $sp, -8
sw $t0,4($sp)
sw $t1,($sp)
li $t0,0 #$t0=0
add $t1,$a0,$zero
strloop:
lb $t2, ($t1)
beq $t2,28,endloop #$t2\n
addi $t0,$t0,1 #$t0++
addiu $t1,$t1,1
b strloop #next i
endloop:
add $v0, $t0, $0
lw $t0, 4($sp)
lw $t1,($sp)
addi $sp,$sp,20 #stack pointer restored
jr $ra
######################## 主功能
# $a0 text, $a1 key, $a2 keyInt (strleng(key)
findPos:
addi $sp, $sp, -20
sw $t0, 16($sp)
sw $t1, 12($sp)
sw $t2, 8($sp)
sw $t3, 4($sp)
sw $t4, ($sp)
loop:
add $t2,$a2, $zero
lb $t1,($a1)
lb $t0,($a0)
beq $t0,28,endfP
beq $t0, $t1, L
add $t1, $zero, $zero
beq $t1,$t2,M
addi $t0,$t0,1
b loop
L: addi $t1,$t1,1
M: add $t3, $t3,$t0
addi $t3, $t3,1
sub $t3,$t3,$t2
endfP: add $v0, $t3, $0
lw $t0,16($sp)
lw $t1,12($sp)
lw $t2,8($sp)
lw $t3,4($sp)
lw $t4,($sp)
addi $sp,$sp,20
jr $ra