作业要求我们在 MIPS 中完成一个代码。最初的 MIPS 代码需要一个我们必须从 C++ 代码转换的函数。我根据需要将该函数从 C++ 转换为 MIPS。
当我运行 MIPS 中的代码时,它不会显示任何错误。但是,代码不显示任何内容。
我试图调试它,但我仍然看不出问题出在哪里。我使用 Notepad++ 作为编辑器和 QtSpim 来运行 MIPS 代码。我在下面发布了代码(首先在 C++ 中,然后在 MIPS 中)。您的帮助将不胜感激。
//*************C++ Code***********************
#include <stdio.h>
void update(float *, float *);
int main()
{
float x[5] = {.1, .2, .4, .6, .85};
float vx[5] = {-.2, -.15, .05, -.2, .2};
int i, steps;
for (i=0; i<5; i++)
{
printf("%d %f %f\n", i, x[i], vx[i]);
}
for (steps=0; steps<3; steps++)
{
update(x, vx);
printf("Step %d\n", steps);
for (i=0; i<5; i++)
{
printf("%d %f %f\n", i, x[i], vx[i]);
}
}
}
void update(float pos[], float vel[])
{
int i;
for (i=0; i<5; i++)
{
pos[i] += vel[i];
if (pos[i] < 0.)
{
pos[i] = 0.;
vel[i] = - vel[i];
}
else if (pos[i] > 1.)
{
pos[i] = 1.;
vel[i] = - vel[i];
}
}
}
//************************End of C++ Code*************************
//**************************Code Translated in MIPS*************
.data
x: .float 0.1
.float 0.2
.float 0.4
.float 0.6
.float 0.85
vx: .float -0.2
.float -0.15
.float 0.05
.float -0.2
.float 0.2
zero: .float 0.
one: .float 1.
space: .asciiz " "
endl: .asciiz "\n"
outStep:.asciiz "Step "
# i $s0
# steps $s1
# &x[0] $s2
# &vx[0] $s3
.globl for
.text
main: la $s2, x
la $s3, vx
move $t2, $s2
move $t3, $s3
li $s0, 0 # for (i=0; i<5; i++) {
ploop: move $a0, $s0 # printf("%d %f %f\n", i, x[i], vx[i]);
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t2)
li $v0, 2
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t3)
li $v0, 2
syscall
la $a0, endl
li $v0, 4
syscall
addi $t2, $t2, 4
addi $t3, $t3, 4
addi $s0, $s0, 1 # }
blt $s0, 5, ploop
li $s1, 0 # for (steps=0; steps<3; steps++) {
loop: move $a0, $s2 # update(x, vx);
move $a1, $s3
jal update
la $a0, outStep # printf("Step %d\n", steps);
li $v0, 4
syscall
move $a0, $s1
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
move $t2, $s2
move $t3, $s3
li $s0, 0 # for (i=0; i<5; i++) {
ploop1: move $a0, $s0 # printf("%d %f %f\n", i, x[i], vx[i]);
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t2)
li $v0, 2
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t3)
li $v0, 2
syscall
la $a0, endl
li $v0, 4
syscall
addi $t2, $t2, 4
addi $t3, $t3, 4
addi $s0, $s0, 1
blt $s0, 5, ploop1
addi $s1, $s1, 1
blt $s1, 3, loop
li $v0, 10
syscall
# void update(float pos[], float vel[]) {
# arg0 $a0
# arg1 $a1
# i $s0
update: l.s $f30, zero
l.s $f31, one
# The function i translated in MIPS is right bellow
addi $s0,$s0,0 # i=0
add.s $f0, $f0, $f1 # pos[i] += vel[i];
c.lt.s $f0, $f30 # if (pos[i] < 0.) {
bc1f else
add.s $f0, $f0, $f30 # pos[i]=0;
sub.s $f1, $f30 , $f1
sub.s $f31, $f30, $f31
add.s $f1, $f1, $f31 # vel[i] = - vel[i];
blt $s0, 5, update
else : c.lt.s $f31, $f0 #else if (pos[i] > 1.) {
bc1f skip
add.s $f0, $f0, $f31
sub.s $f31, $f30, $f31
sub.s $f1, $f1, $f31
blt $s0, 5, update
skip : add $s0, $s0, 0
addi $s0, $s0, 1
slti $t1, $s0, 5
blt $s0, 5, update
end: jr $31 #}