循环到无穷远 我试图把这个C++函数转换成MIPS。我想我在循环中遇到了问题,因为当我运行它时,它会给我13..1.17.5。。但我的输出应该是两个ip地址:130.52.0.10和171.9.50.186
C++函数代码:循环到无穷远 我试图把这个C++函数转换成MIPS。我想我在循环中遇到了问题,因为当我运行它时,它会给我13..1.17.5。。但我的输出应该是两个ip地址:130.52.0.10和171.9.50.186,c++,assembly,mips,C++,Assembly,Mips,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 +
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;
}
请帮我一下。我试了很多,但没能使它正常运行
编辑:
BytToDEC 的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
寄存器是“temp”,不能在这样的函数调用中使用。应该将循环变量放入s
寄存器(“保存”寄存器),如果调用的函数需要重用相同的s
寄存器,它需要将其保存到堆栈或其他地方,并在返回到被调用方之前还原值。我有一个程序,第一个循环运行正常,但第二到第六个循环转到inf。我是初学者,需要一些帮助
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
ofstream myfile ("Atomseries.txt");
int main()
{
float rconstant=109677.58;
int nstart, nend;
int length;
for (nstart = 1; nstart <= 6; nstart++){
for (nend= 2; nend <=nstart + 10; nend++)
length = 1/(rconstant*(1/(nstart*nstart)- 1/(nend*nend)));
myfile << length * 10000000 << endl;
}
if (nstart = 1)
myfile << "Lyman Series"<< endl;
else
;
if (nstart = 2)
myfile << "B Series" << endl;
else
;
if (nstart= 3)
myfile << " series"<< endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
流myfile(“Atomseries.txt”);
int main()
{
浮子rconstant=109677.58;
内德内斯特;
整数长度;
对于(nstart=1;nstart)您应该使用unsigned int作为ip地址。您的循环控制应该重复4次,除非您在变量i上跺脚。您没有提供byteToDec(),因此我们无法查看。我注意到寄存器$t3在循环的“blt$t3,4,loop”中使用,并且在byteToDec中也设置为0,“li$ti,0”将FROLUP中的变量名改为其他的,然后尝试它。嗨,谢谢你的帮助。我确实把我的寄存器登记到S登记器,它停止了无限循环,但是我仍然有很多异常4和异常7错误。我添加了BytToDC++函数作为参考。通常,我从不使用“I”作为变量名,但我使用ndx、jdx、ctr或其他东西-很难找到“I”。嗨..谢谢你的提示。我确实在s寄存器中保存了I,它停止了无限循环,但它仍然会给我很多错误,如异常4和异常7,以及许多错误地址。如果我在两个函数中使用不同的所有临时寄存器?如果程序的其余部分是用C/C++编写的,它希望您负责任地使用s
寄存器。如果您使用s
寄存器,您必须在堆栈上节省一些额外的空间,将s
寄存器保存到堆栈中,然后使用s
寄存器进行w然后从堆栈中还原s
寄存器的值。听起来你可能在破坏被调用方的s
寄存器。但是,是的,另一种选择是确保在两个函数之间使用唯一的t
寄存器。
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
ofstream myfile ("Atomseries.txt");
int main()
{
float rconstant=109677.58;
int nstart, nend;
int length;
for (nstart = 1; nstart <= 6; nstart++){
for (nend= 2; nend <=nstart + 10; nend++)
length = 1/(rconstant*(1/(nstart*nstart)- 1/(nend*nend)));
myfile << length * 10000000 << endl;
}
if (nstart = 1)
myfile << "Lyman Series"<< endl;
else
;
if (nstart = 2)
myfile << "B Series" << endl;
else
;
if (nstart= 3)
myfile << " series"<< endl;
return 0;
}