更直接的方法来做到这一点?

发布于 2024-12-29 06:52:20 字数 776 浏览 0 评论 0原文

这是一个家庭作业问题。我的代码给出了正确的答案。像我一样写出解决方案非常令人困惑,并且花了我一段时间。有更好的编码方法吗?

# Convert to MIPS: (((5 + 1) - (1 + 3) + (5 - 3)) - 1) – 5 + 3

main:
    li $t0 5
    li $t1 1
    li $t2 3
    #         (((5   + 1)   - (1   + 3)   + (5   - 3))   - 1)   – 5   + 3
    # becomes ((($t0 + $t1) - ($t1 + $t2) + ($t0 - $t2)) - $t1) - $t0 + $t2
    #         (((  $t3    ) - (  $t4    ) + (  $t5    )) - $t1) - $t0 + $t2

    add $t3, $t0, $t1
    add $t4, $t1, $t2
    sub $t5, $t0, $t2
    sub $t6, $t3, $t4
    add $t6, $t6, $t5
    sub $t6, $t6, $t1
    sub $t6, $t6, $t0
    add $a0, $t6, $t2

    li $v0, 1             # print $a0
    syscall

    li $v0, 10            # system call 10... lets us exit, load 10 in $v0
    syscall               # call the exit.

This is a homework problem. My code is giving the correct answer. Writing out the solution like I did was terribly confusing and took me a while. Is there a better way of coding this?

# Convert to MIPS: (((5 + 1) - (1 + 3) + (5 - 3)) - 1) – 5 + 3

main:
    li $t0 5
    li $t1 1
    li $t2 3
    #         (((5   + 1)   - (1   + 3)   + (5   - 3))   - 1)   – 5   + 3
    # becomes ((($t0 + $t1) - ($t1 + $t2) + ($t0 - $t2)) - $t1) - $t0 + $t2
    #         (((  $t3    ) - (  $t4    ) + (  $t5    )) - $t1) - $t0 + $t2

    add $t3, $t0, $t1
    add $t4, $t1, $t2
    sub $t5, $t0, $t2
    sub $t6, $t3, $t4
    add $t6, $t6, $t5
    sub $t6, $t6, $t1
    sub $t6, $t6, $t0
    add $a0, $t6, $t2

    li $v0, 1             # print $a0
    syscall

    li $v0, 10            # system call 10... lets us exit, load 10 in $v0
    syscall               # call the exit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

守护在此方 2025-01-05 06:52:20

我看到你的代码中评论了这个问题。

是的,您可以以更直接的方式做到这一点。您只需按照发生的顺序为每个操作创建一个不同的部分即可。但这会让你的代码变得更长,而且可读性也没有那么高

li $t0 5
li $t1 1
add $t2, $t0, $t1

li $t0 1
li $t1 3
add $t3, $t0, $t1

sub $t4, t2, t3


li $t0 5
li $t1 3
sub $t2, $t0, $t1

add $t3, $t4, $t2

li $t0 1
sub $t2, $t3, $t0
#etc.

I saw the problem commented in your code.

Yes, you can do it in a more straight forward way. You just make a different section for each operation that happens in the order that they happen. but that would make your code longer, and not really that much more readable either

li $t0 5
li $t1 1
add $t2, $t0, $t1

li $t0 1
li $t1 3
add $t3, $t0, $t1

sub $t4, t2, t3


li $t0 5
li $t1 3
sub $t2, $t0, $t1

add $t3, $t4, $t2

li $t0 1
sub $t2, $t3, $t0
#etc.
早乙女 2025-01-05 06:52:20

如果您有 MIPS C 编译器,则始终可以在关闭优化的情况下对其进行编译:

int main()
{
    int one, three, five;
    int result;

    one = 1;
    three = 3;
    five = 5;

    result = (((five + one) - (one + three) + (five - three)) - one) -
        five + three;
    return result;
}

然后对生成的目标代码运行 objdump。这不是你的教授希望你这样做的方式,但你已经这样做了,你会学到更多。

编辑

我通常在将汇编语言文件传递给汇编器之前运行 C 预处理器。当特定寄存器用于特定事物时,它可以使事物更容易阅读:

#define FIVE  $t0
#define ONE   $t1
#define THREE $t2
#define FIVE_PLUS_ONE    $t3
#define ONE_PLUS_THREE   $t4
#define FIVE_MINUS_THREE $t5
#define ACCUM $t6

main:
    li FIVE 5
    li ONE 1
    li THREE 3
    //         (((5   + 1)   - (1   + 3)   + (5   - 3))   - 1)   – 5   + 3
    // becomes ((($t0 + $t1) - ($t1 + $t2) + ($t0 - $t2)) - $t1) - $t0 + $t2
    //         (((  $t3    ) - (  $t4    ) + (  $t5    )) - $t1) - $t0 + $t2

    add FIVE_PLUS_ONE, FIVE, ONE
    add ONE_PLUS_THREE, ONE, THREE
    sub FIVE_MINUS_THREE, FIVE, THREE
    sub ACCUM, FIVE_PLUS_ONE, ONE_PLUS_THREE
    add ACCUM, ACCUM, FIVE_MINUS_THREE
    sub ACCUM, ACCUM, ONE
    sub ACCUM, ACCUM, FIVE
    add $a0, ACCUM, THREE

    li $v0, 1             // print $a0
    syscall

    li $v0, 10            // system call 10... lets us exit, load 10 in $v0
    syscall               // call the exit.

If you have a MIPS C compiler, you can always compile this with optimizations turned off:

int main()
{
    int one, three, five;
    int result;

    one = 1;
    three = 3;
    five = 5;

    result = (((five + one) - (one + three) + (five - three)) - one) -
        five + three;
    return result;
}

then run objdump on the resultant object code. It's not the way your professor wants you to do it, but you've already done that and you'll learn more.

Edit:

I usually run the C preprocessor on my assembly language files before passing them to the assembler. It can make things much easier to read, when particular registers are used for particular things:

#define FIVE  $t0
#define ONE   $t1
#define THREE $t2
#define FIVE_PLUS_ONE    $t3
#define ONE_PLUS_THREE   $t4
#define FIVE_MINUS_THREE $t5
#define ACCUM $t6

main:
    li FIVE 5
    li ONE 1
    li THREE 3
    //         (((5   + 1)   - (1   + 3)   + (5   - 3))   - 1)   – 5   + 3
    // becomes ((($t0 + $t1) - ($t1 + $t2) + ($t0 - $t2)) - $t1) - $t0 + $t2
    //         (((  $t3    ) - (  $t4    ) + (  $t5    )) - $t1) - $t0 + $t2

    add FIVE_PLUS_ONE, FIVE, ONE
    add ONE_PLUS_THREE, ONE, THREE
    sub FIVE_MINUS_THREE, FIVE, THREE
    sub ACCUM, FIVE_PLUS_ONE, ONE_PLUS_THREE
    add ACCUM, ACCUM, FIVE_MINUS_THREE
    sub ACCUM, ACCUM, ONE
    sub ACCUM, ACCUM, FIVE
    add $a0, ACCUM, THREE

    li $v0, 1             // print $a0
    syscall

    li $v0, 10            // system call 10... lets us exit, load 10 in $v0
    syscall               // call the exit.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文