gpt4 book ai didi

c++ - MIPS : loop goes to infinity

转载 作者:行者123 更新时间:2023-11-28 07:22:04 24 4
gpt4 key购买 nike

我正在尝试将此 C++ 函数转换为 mips。我想我在循环中遇到了问题,因为当我运行它时,它给了我 13..1.17.5.. 但我的输出应该是两个 ip 地址:130.52.0.10 和 171.9.50.186

C++函数代码:

      void IPtoDD(int arg0, char *arg1)
{
int temp, numChar, shift = 24;

for (int i=0; i<4; i++) {
temp = arg0 >> shift;
temp = temp & 0x000000ff;
numChar = byteToDec(temp,arg1);
arg1 += numChar;
*arg1++ = '.';
shift -= 8;
}
arg1--;
*arg1 = 0;

return;
}

MIPS 代码:

      IPtoDD: addi $sp, $sp, -20
sw $ra, ($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
move $s0, $a0
move $s1, $a1
li $s3, 24 #s3=shift
li $s2, 0 #s2=i
li $t5, 0 #t5=temp
li $t3, 0
move $s1, $a1 #s1=*arg1
loop: srl $t5, $s0, $s3 #t3= numChar
and $t5, $t5, 0xff #t4= (*arg1)
move $a0, $t5
move $a1, $s1
jal byteToDec
move $t3, $v0
add $s1, $s1, $t3
li $t5, '.'
sb $t5, ($a1)
addi $a1, $a1, 1
addi $s3, $s3, -8
addi $s2, $s2, 1
blt $s2, 4, loop
addi $s1, $s1, -1
sb $0, ($a1)
lw $s3, 16($sp)
ra: lw $s2, 12($sp)
lw $s1, 8($sp)
lw $s0, 4($sp)
lw $ra, ($sp)
addi $sp, $sp, 20
jr $ra

请帮帮忙。我尝试了很多但无法使其正常运行。

编辑:byteToDec 的 C++ 函数

    int byteToDec(int arg0, char *arg1)
{
int temp, flag = 0, count = 0;
if (arg0==0) {
*arg1 = '0';
return 1;
}
else {
temp = arg0/100;
if (temp != 0) {
*arg1++ = (char) temp + 0x30;
count++;
flag = 1;
}
temp = (arg0 % 100) / 10;
if ((flag!=0) || (temp != 0)) {
*arg1++ = (char) temp + 0x30;
count++;
}
temp = arg0 % 10;
*arg1 = (char) temp + 0x30;
count++;
return count;
}
}

MIPS 中的 byteToDec:

    byteToDec:      #t0= temp
#t1= flag
#v0= count

#t3= (*arg1)

bne $a0, $0, else
li $t3, '0'
sb $t3, ($a1)
li $v0, 1
jr $ra
else: div $t0, $a0, 100
beq $t0, 0, cont
bp2: addi $t3, $t0, 0x30
sb $t3, ($a1)
addi $a1, $a1, 1
addi $v0, $v0, 1
li $t1, 1
cont: rem $t3, $a0, 100
div $t0, $t3, 10
bne $t1, 0, nxtIf
beq $t0, 0, endElse
nxtIf: addi $t3, $t0, 0x30
sb $t3, ($a1)
addi $a1, $a1, 1
addi $v0, $v0, 1
endElse:rem $t0, $a0, 10
bp1: addi $t3, $t0, 0x30
sb $t3, ($a1)
addi $v0, $v0, 1
ra1: jr $ra

最佳答案

您正在使用 t3 作为循环计数器,然后在 byteToDec 函数中丢弃 t3。 MIPS 惯例是 t 寄存器是“临时”的,不能跨函数调用使用。你应该把你的循环变量放在 s 寄存器(一个“保存”寄存器)中,如果你的被调用函数需要重用相同的 s 寄存器,它需要保存它到堆栈或其他东西并在返回给被调用者之前恢复值。

关于c++ - MIPS : loop goes to infinity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com