技巧翻译问题 [英] Mips translating problems

查看:113
本文介绍了技巧翻译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将此伪代码转换为mips程序集时遇到麻烦,我添加了我正在使用的寄存器以帮助了解发生了什么情况

I am having trouble translating this pseudo code to mips assembly i have added the registers i am using to help understand what is going on

# if (n == 1)
#   return 1
# else if (n == 2)
#   return 6
# else
#   return 2*hex(n-1) - hex(n-2) + 4

# (hex) Registers:
# $a0 - n, the argument
# $v0 - the result the n-th Hexamorphic number
# $t0 - holds hex(n-2)
# $t1 - holds constants 1
# $t2 - holds constant 2

这是我在代码中所处的位置,我对十六进制:和elseif充满信心;但是else:是问题开始的地方

here is where i am at in my code i feel confident about hex: and elseif: but the else: is where the problem starts

hex:    bne $a0,$t1,elseif #if n==1
        li $t1,1
        li $t2,2
        li $v0,1 
        jr $ra         #retu

elseif: bne $a0, $t2,else
        li $v0,6
        jr $ra

else:   addi $sp,$sp,-12
    sw $ra,$ra 0($sp)
    addi $t3,$a0,-1
    sll $t3, $t2,1
    sw $a0,$a0,4($sp)
    sw $t3,8($sp)
    lw $ra
    lw $a0
    addi $t3,4
    sub $t4,$t3,$t0
    lw $t4
    sw $v0,$t4
    lw $ra
    lw $a0
    j $ra

推荐答案

您的某些说明有误(swlwaddij).这些指令的定义可以在 MIPS32指令集中找到快速参考.

You have some instructions wrong (sw, lw, addi and j). The definitions of these intstructions can be found in the MIPS32 instruction set quick reference.

您在else块中的正确轨道上.您要做的是将要保留的所有值(在堆栈上)保存在整个递归调用中. jal十六进制(n-1),将其保存在堆栈中,然后jal再次十六进制(n-2).然后加载所有值,对它们和jr ra进行计算.不要忘记在返回之前还原$ sp.使用jal时,请记住分支延迟:

You're on the right track in the else block. What you want to do is save all the values (on the stack) that you want to be preserved throughout your recursive calls. jal to hex (n - 1), save it on the stack and jal to hex again (n - 2). Then load up all values, do the calculations on them and jr ra. Do not forget to restore $sp before returning. When using jal, remember the branch delay:

jal hex
The instruction here will be run "together" with jal, before taking the branch.
ra will point here

由于分支延迟,elseif中的jr $ra将运行addi $sp,$sp,-12.这不好.

The jr $ra in elseif will run addi $sp,$sp,-12 due to branch delay. This is not good.

这篇关于技巧翻译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆