MIPS32火星 - 将算法放在功能中

发布于 2025-02-04 19:54:02 字数 561 浏览 2 评论 0原文

因此,我在火星大会上有一个小计划,该计划获得了最大的共同除数:

.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 技术交流群。

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

发布评论

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

评论(1

洋洋洒洒 2025-02-11 19:54:02

以下是将代码段转换为函数的工作:

  1. 在开始时给它一个标签,例如myFunc
  2. 将返回放在结尾处:jr $ ra
  3. 制作一个调用您的功能的测试主体,并将其放置在.Text部分的开头 - 您希望此测试主是模拟器首先运行的,因此它将称呼您的功能 - 您不don' T需要您的功能,因为它不再是片段,但是现在是一个函数,应适当调用它:传递参数并返回地址。 主体还必须正确终止程序,否则它将从边缘运行并再次运行该功能,但这次是意外的。
    .text
    # test main: call MyFunc
    #
    # call function, pass some parameters
    li $a0, 20          # load first arg register
    li $a1, 15          # load second arg register
    jal MyFunc          # the actual invocation 
                        # this jumps to the function, 
                        #   while also passing the return address in $ra

                        # here's where come back to when the function is done:
    move $a0, $v0       # move return value into $a0, so we can print it

    li $v0, 1           # and print
    syscall

    li $v0, 10          # and stop the program
    syscall
#
#
#
MyFunc:                 # start of MyFunc
    # ...               # code of function goes here
    jr $ra              # jump back to the caller by using the return address register

建议使用友好的寄存器名称而不是简单的数字名称。 (而且我们绝不应该将友好名称与同一程序中的简单数字名称混合。)

Here's what to do in order to turn a snippet of code into a function:

  1. Give it a label at the beginning, e.g. MyFunc.
  2. Put a return at the end: jr $ra.
  3. Make a test main that calls your function and place this at the beginning of the .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.
    .text
    # test main: call MyFunc
    #
    # call function, pass some parameters
    li $a0, 20          # load first arg register
    li $a1, 15          # load second arg register
    jal MyFunc          # the actual invocation 
                        # this jumps to the function, 
                        #   while also passing the return address in $ra

                        # here's where come back to when the function is done:
    move $a0, $v0       # move return value into $a0, so we can print it

    li $v0, 1           # and print
    syscall

    li $v0, 10          # and stop the program
    syscall
#
#
#
MyFunc:                 # start of MyFunc
    # ...               # code of function goes here
    jr $ra              # jump back to the caller by using the return address register

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.)

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