[英]MIPS Coding Assignment: Translating multiplication via repeated addition
I have an assignment and I have to convert a line of C++ code into MIPS and then run the code.我有一个作业,我必须将一行 C++ 代码转换为 MIPS,然后运行代码。 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.我已获得此 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;
}
I have converted it to MIPS Code here:我在这里将它转换为 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
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?有没有人有什么建议?
It seems you are using the assembly generated by a MIPS64 compiler instead the one generated by a MIPS32 one.看来您使用的是 MIPS64 编译器生成的程序集,而不是 MIPS32 编译器生成的程序集。 If this is a homework assignment you probably should just compile the C function manually instead of using a compiler.如果这是家庭作业,您可能应该手动编译 C function 而不是使用编译器。
Using godbolt you may see the assembly for a couple of MIPS32 compilers.使用godbolt ,您可能会看到几个 MIPS32 编译器的程序集。 Here goes one of them (mips gcc 5.4) along with a sample call:下面是其中一个 (mips gcc 5.4) 以及一个示例调用:
.data
str: .asciiz "Result: "
.text
main:
# sample multiplication
la $a0, str
li $v0, 4
syscall # print msg
li $a0, 10 # test multiply 10 * 5
li $a1, 5
jal multiply_int_int # compute multiplication
move $a0, $v0
li $v0, 1
syscall # print result
li $v0, 10
syscall # exit
multiply_int_int:
addiu $sp,$sp,-32
sw $fp,28($sp)
move $fp,$sp
sw $4,32($fp)
sw $5,36($fp)
sw $0,8($fp)
lw $3,32($fp)
lw $2,36($fp)
nop
slt $2,$2,$3
beq $2,$0,$L2
nop
sw $0,12($fp)
$L4:
lw $3,12($fp)
lw $2,36($fp)
nop
slt $2,$3,$2
beq $2,$0,$L2
nop
lw $3,8($fp)
lw $2,32($fp)
nop
addu $2,$3,$2
sw $2,8($fp)
lw $2,12($fp)
nop
addiu $2,$2,1
sw $2,12($fp)
b $L4
nop
$L2:
lw $3,36($fp)
lw $2,32($fp)
nop
slt $2,$2,$3
beq $2,$0,$L5
nop
sw $0,16($fp)
$L7:
lw $3,16($fp)
lw $2,32($fp)
nop
slt $2,$3,$2
beq $2,$0,$L5
nop
lw $3,8($fp)
lw $2,36($fp)
nop
addu $2,$3,$2
sw $2,8($fp)
lw $2,16($fp)
nop
addiu $2,$2,1
sw $2,16($fp)
b $L7
nop
$L5:
lw $2,8($fp)
move $sp,$fp
lw $fp,28($sp)
addiu $sp,$sp,32
jr $31
nop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.