MIPS 编码作业:通过重复加法转换乘法

发布于 2025-01-11 08:03:52 字数 2020 浏览 0 评论 0原文

我有一个作业,必须将一行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2025-01-18 08:03:53

看来您正在使用 MIPS64 编译器生成的程序集,而不是 MIPS32 编译器生成的程序集。如果这是一项家庭作业,您可能应该手动编译 C 函数,而不是使用编译器。

使用 godbolt 您可能会看到几个 MIPS32 编译器的程序集。
下面是其中之一(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

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:

.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文