将c ++代码更改为mips时会遇到一些问题。
但是当我编译它时,该程序被卡在最里面的for循环中而无法退出。但是,我确定我每回合都会增加j,这是我可能犯的错误。
仅供参考,错误消息显示在sw $t6, 0($t8)
中。它说运行时异常。地址超出范围。
c ++代码
void intToRoman(int num) {
int index = 0;
for (int i = 0; num > 0; ++i) {
int count = 0;
//deduct from the biggest radix for any many number of times as possible
while (num >= roman_radix[i]){
num -= roman_radix[i];
count++; //record the number of each roman symbol of
//roman_radix[i]
}
//write roman symbols of roman_radix[i] into roman
for (; count > 0; --count){
for (int j = 0; j < roman_offset[i]; j++){
char temp = roman_symbol[roman_start[i] + j];
roman[index++] = temp;
}
}
}
}
我的MIPS代码
# $a1 is num
intToRoman:
add $t0 ,$zero, $zero #index
add $t1 ,$zero, $zero #i
add $t2 ,$zero, $zero #j
forlp1:
add $t7, $zero, $zero #count
sll $t3 ,$t1 ,2
add $t3, $t3, $s0
lw $t4, 0($t3) #$t4 = roman radix[i]
whilelp:
sub $t6,$a1, $t4 # $t6 = num - roman_radix[i]
bltz $t6, whileexit
add $t7, $t7, 1
add $a1, $t6, $zero
j whilelp
whileexit:
sll $t3 ,$t1 ,2
add $t3, $t3, $s2 #$s2 is address of roman start array
lw $t4, 0($t3)
sll $t3 ,$t1 ,2
add $t3, $t3, $s3 #$s3 is address of roman offset array
lw $t5, 0($t3)
forlp2:
forlp3:
add $t6, $t4, $t2 # $t6 is romanstart[i]+j
sll $t3 ,$t6 ,2
add $t3, $t3, $s1 #$s1 is address of roman symbol array
lw $t6, 0($t3)
sll $t8 ,$t0 ,2
add $t8, $t8, $s4 #$s1 is address of roman array
sw $t6, 0($t8) # roman[index++] = temp;
add $t0, $t0, 1
add $t2, $t2, 1 # count++
beq $t2, $t5, forlp3exit
j forlp3
forlp3exit:
sub $t7, $t7, 1
blez $t7, forlp2exit
j forlp2
forlp2exit:
blez $a1, forlp1exit
add $t1 ,$t1, 1
j forlp1
forlp1exit: