[英]MIPS : loop goes to infinity
我正在嘗試將此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
用作循環計數器,然后將t3
破壞到byteToDec
函數中。 MIPS約定是t
寄存器是“臨時的”,並且不能在這樣的函數調用中使用。 您應該將循環變量放在s
寄存器(“保存”寄存器)中,如果調用的函數需要重用相同的s
寄存器,則它需要將其保存到堆棧或其他東西中,並在返回被調用方之前恢復值。 。
我有一個程序,它的第一個循環工作正常,但第二個到第6個循環進入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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.