如何将整数值加载到 MIPS 中的双精度寄存器?
How to load an integer value to a double register in MIPS?
我有这个代码:
void test(int x)
{
cout<<x;
double y=x+4.0;
cout<<y;
}
void main ()
{
test(7); // call the test in main
}
在 MIPS 中:
在我将参数 x 的值放入堆栈中的 0($fp) 并跳转到测试之后:
lw $a0,0($fp) // load value of x and print it
li $v0,1
syscall
lw $t1,0($fp)
sw $t1,0($sp) // put the value of x in stack and point it by $sp
li.d $f0,4.0
s.d $f0,4($sp) // put the value 4.0 in stack and point it by $sp
l.d $f0,0($sp)
l.d $f2,4($sp)
add.d $f4,$f0,$f2
s.d $f4,8($sp) // put the result of add
l.d $f12,8($sp) // print the value of y
li $v0,3
syscall
我的问题是 QTSPIM 中 y 的结果是 4 ....这个问题是因为我在双寄存器中加载了一个整数值...我该如何解决这个问题???
您需要将整数值加载到 fp 寄存器中,然后将其转换为浮点格式。你有一个 8 字节的加载(它将加载你的值的 4 个字节,加上后面的 4 个字节,无论它们是什么),所以你可能想改变它,然后做一个 cvt:
l.w $f0,0($fp)
cvt.d.w $f0,$f0
我有这个代码:
void test(int x)
{
cout<<x;
double y=x+4.0;
cout<<y;
}
void main ()
{
test(7); // call the test in main
}
在 MIPS 中: 在我将参数 x 的值放入堆栈中的 0($fp) 并跳转到测试之后:
lw $a0,0($fp) // load value of x and print it
li $v0,1
syscall
lw $t1,0($fp)
sw $t1,0($sp) // put the value of x in stack and point it by $sp
li.d $f0,4.0
s.d $f0,4($sp) // put the value 4.0 in stack and point it by $sp
l.d $f0,0($sp)
l.d $f2,4($sp)
add.d $f4,$f0,$f2
s.d $f4,8($sp) // put the result of add
l.d $f12,8($sp) // print the value of y
li $v0,3
syscall
我的问题是 QTSPIM 中 y 的结果是 4 ....这个问题是因为我在双寄存器中加载了一个整数值...我该如何解决这个问题???
您需要将整数值加载到 fp 寄存器中,然后将其转换为浮点格式。你有一个 8 字节的加载(它将加载你的值的 4 个字节,加上后面的 4 个字节,无论它们是什么),所以你可能想改变它,然后做一个 cvt:
l.w $f0,0($fp)
cvt.d.w $f0,$f0