MIPS 编码作业:通过重复加法转换乘法
我有一个作业,必须将一行 C++ 代码转换为 MIPS,然后运行该代码。我一直在不断地遇到同样的错误,并且关于如何修复它的一些建议会很棒。所需信息如下。 我已经得到了这个 C++ 代码来转换为 MIPS。
int multiply(int x, int y)
{
int product=0;
if(x>y)
for(int i=0;i<y;i++)
product+=x;
if(y>x)
for(int i=0;i<x;i++)
product+=y;
return product;
}
我已将其转换为 MIPS 代码:
multiply(int, int):
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
move $3,$4
move $2,$5
sll $3,$3,0
sw $3,16($fp)
sll $2,$2,0
sw $2,20($fp)
sw $0,0($fp)
lw $3,16($fp)
lw $2,20($fp)
slt $2,$2,$3
beq $2,$0,.L2
nop
sw $0,4($fp)
b .L3
nop
.L4:
lw $3,0($fp)
lw $2,16($fp)
addu $2,$3,$2
sw $2,0($fp)
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
.L3:
lw $3,4($fp)
lw $2,20($fp)
slt $2,$3,$2
bne $2,$0,.L4
nop
.L2:
lw $3,20($fp)
lw $2,16($fp)
slt $2,$2,$3
beq $2,$0,.L5
nop
sw $0,8($fp)
b .L6
nop
.L7:
lw $3,0($fp)
lw $2,20($fp)
addu $2,$3,$2
sw $2,0($fp)
lw $2,8($fp)
addiu $2,$2,1
sw $2,8($fp)
.L6:
lw $3,8($fp)
lw $2,16($fp)
slt $2,$3,$2
bne $2,$0,.L7
nop
.L5:
lw $2,0($fp)
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
jr $31
nop
我继续收到此错误代码,我正在尝试找出如何解决此问题:
spim: (parser) syntax error on line 1 of file D:/Sophomore Year ('21-'22)/Spring Semester - 2022/CS-3365 (Intro to Comp Org and Arch)/MIPS Assignment 2.asm
multiply(int, int):
^
有人有任何建议吗?
I have an assignment and I have to convert a line of C++ code into MIPS and then run the code. I have been continuously getting the same error, and some suggestions on how to fix it would be great. The info needed is below.
I have been given this C++ Code to convert to MIPS.
int multiply(int x, int y)
{
int product=0;
if(x>y)
for(int i=0;i<y;i++)
product+=x;
if(y>x)
for(int i=0;i<x;i++)
product+=y;
return product;
}
I have converted it to MIPS Code here:
multiply(int, int):
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
move $3,$4
move $2,$5
sll $3,$3,0
sw $3,16($fp)
sll $2,$2,0
sw $2,20($fp)
sw $0,0($fp)
lw $3,16($fp)
lw $2,20($fp)
slt $2,$2,$3
beq $2,$0,.L2
nop
sw $0,4($fp)
b .L3
nop
.L4:
lw $3,0($fp)
lw $2,16($fp)
addu $2,$3,$2
sw $2,0($fp)
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
.L3:
lw $3,4($fp)
lw $2,20($fp)
slt $2,$3,$2
bne $2,$0,.L4
nop
.L2:
lw $3,20($fp)
lw $2,16($fp)
slt $2,$2,$3
beq $2,$0,.L5
nop
sw $0,8($fp)
b .L6
nop
.L7:
lw $3,0($fp)
lw $2,20($fp)
addu $2,$3,$2
sw $2,0($fp)
lw $2,8($fp)
addiu $2,$2,1
sw $2,8($fp)
.L6:
lw $3,8($fp)
lw $2,16($fp)
slt $2,$3,$2
bne $2,$0,.L7
nop
.L5:
lw $2,0($fp)
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
jr $31
nop
I continue to receive this error code, and I am trying to figure out how to fix this:
spim: (parser) syntax error on line 1 of file D:/Sophomore Year ('21-'22)/Spring Semester - 2022/CS-3365 (Intro to Comp Org and Arch)/MIPS Assignment 2.asm
multiply(int, int):
^
Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您正在使用 MIPS64 编译器生成的程序集,而不是 MIPS32 编译器生成的程序集。如果这是一项家庭作业,您可能应该手动编译 C 函数,而不是使用编译器。
使用 godbolt 您可能会看到几个 MIPS32 编译器的程序集。
下面是其中之一(mips gcc 5.4)以及示例调用:
It seems you are using the assembly generated by a MIPS64 compiler instead the one generated by a MIPS32 one. If this is a homework assignment you probably should just compile the C function manually instead of using a compiler.
Using godbolt you may see the assembly for a couple of MIPS32 compilers.
Here goes one of them (mips gcc 5.4) along with a sample call: