更直接的方法来做到这一点?
这是一个家庭作业问题。我的代码给出了正确的答案。像我一样写出解决方案非常令人困惑,并且花了我一段时间。有更好的编码方法吗?
# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到你的代码中评论了这个问题。
是的,您可以以更直接的方式做到这一点。您只需按照发生的顺序为每个操作创建一个不同的部分即可。但这会让你的代码变得更长,而且可读性也没有那么高
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
如果您有 MIPS C 编译器,则始终可以在关闭优化的情况下对其进行编译:
然后对生成的目标代码运行 objdump。这不是你的教授希望你这样做的方式,但你已经这样做了,你会学到更多。
编辑:
我通常在将汇编语言文件传递给汇编器之前运行 C 预处理器。当特定寄存器用于特定事物时,它可以使事物更容易阅读:
If you have a MIPS C compiler, you can always compile this with optimizations turned off:
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: