MIPS32火星 - 将算法放在功能中
因此,我在火星大会上有一个小计划,该计划获得了最大的共同除数:
.text
addiu $4,$zero,49
addiu $5,$zero,42
add $2,$zero,$5
beq $4,$zero,label10
loop:
beq $5,$zero,label9
slt $9,$5,$4
bne $9,$zero,if
slt $10,$4,$5
bne $10,$zero,else
beq $10,$zero,else##if $4 and $5 are equal.
if:
sub $4,$4,$5
j endif
else:
sub $5,$5,$4
j endif
endif:
j loop
label9:
add $2,$0,$4
label10:
结果将保存在注册2中。 现在,我的第二个任务是,我应该更改程序,以便我的算法是一些“函数”(子程序),您可以在其中设置寄存器4和5的参数,并在寄存器2中设置返回值。 我现在的问题是,我不知道该怎么做。
so i have a little program in mars assembly which gets the greatest common divisor:
.text
addiu $4,$zero,49
addiu $5,$zero,42
add $2,$zero,$5
beq $4,$zero,label10
loop:
beq $5,$zero,label9
slt $9,$5,$4
bne $9,$zero,if
slt $10,$4,$5
bne $10,$zero,else
beq $10,$zero,else##if $4 and $5 are equal.
if:
sub $4,$4,$5
j endif
else:
sub $5,$5,$4
j endif
endif:
j loop
label9:
add $2,$0,$4
label10:
The result will be saved in register 2.
Now my second task is, i should change my program so that my algorithm is some "function"(subprogram), where you can set arguments for register 4 and 5 and the return value in register 2.
My problem now is, i dont know how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是将代码段转换为函数的工作:
myFunc
。jr $ ra
。.Text
部分的开头 - 您希望此测试主是模拟器首先运行的,因此它将称呼您的功能 - 您不don' T需要您的功能,因为它不再是片段,但是现在是一个函数,应适当调用它:传递参数并返回地址。 主体还必须正确终止程序,否则它将从边缘运行并再次运行该功能,但这次是意外的。建议使用友好的寄存器名称而不是简单的数字名称。 (而且我们绝不应该将友好名称与同一程序中的简单数字名称混合。)
Here's what to do in order to turn a snippet of code into a function:
MyFunc
.jr $ra
..text
section — you want this test main to be what the simulator runs first, so it will call your function — you don't want your function first, because as it is no longer a snippet, but now a function, it should be properly invoked: by passing parameters and return address. The main also has to properly terminate the program otherwise it will run off the edge and run the function again, but accidentally this time.Suggest using friendly register names instead of the simple numeric names. (And we should never mix friendly names with the simple numeric ones in the same program.)