MIPS组装中J对JAL(以及JR对JALR)的必要性 [英] Necessity of J vs. JAL (and JR vs. JALR) in MIPS assembly

查看:1223
本文介绍了MIPS组装中J对JAL(以及JR对JALR)的必要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注册是因为我一直在谷歌上搜索该问题的答案,却找不到答案.

我想知道在MIPS中是否必须严格使用无链接的跳转指令?

我可以想象,例如,在不需要时使用"AL"版本会带来一定的功耗损失,但是在任何情况下(JN/JR不能完全做到这一点或可以相对简单地进行编码)仅可以使用J/JR吗? /p>

谢谢!

解决方案

将评论正式化为答案


可以用JAL/JALR模拟

J/JR,因为后者执行前者操作的超集.

正如@Jester指出的那样,例程(C术语中的函数)必须小心保留在$ra中存在的返回地址. 除非该例程是叶例程(不执行任何调用的叶例程),否则无论如何都必须将$ra保存在某个地方.

实际上JAL/JALRJ/JR都可以在另一个方面实现:

  • J/JR

    模拟JAL/JALR

    Original               Emulated
    
    jal foo                la $ra, ret_label
                           j foo
                          ret_label:
    

  • JAL/JALR

    模拟J/JR

    Original                Emulated
    
    j foo                   prolog:
                              addi $sp, $sp, -4
                              sw $ra, ($sp)   
    
                              jal foo
    
                            epilog:
                              lw $ra, ($sp)
                              addi $sp, $sp, 4
    

    为此,代码必须返回到epilog.假定$ra大多数保留在例程中(因此标签名称). 非常感谢@EOF指出了此代码段中的错误.

正如@Peter指出的那样,对$pc的访问导致对JAL/JALR的更容易的仿真(对于人类).

正如@EOF所指出的,鉴于其固有的纠缠,某些RISC机器实际上仅对JAL/JALRJ/JR使用一条指令.

考虑到跳转和调用在一个典型程序中经常发生,因此对于任何成功的ISA来说,必须易于实现(并快速执行它们).

I signed up because I've been googling forever for an answer to this question and can't find one.

I'd like to know if the jump instructions WITHOUT linking are strictly necessary in MIPS?

I can imagine for example that using "AL" versions when not required would incur some power penalty, but is there any situation (that's not completely contrived or could be coded around relatively simply) where ONLY J/JR would work?

Thank you!

解决方案

Formalizing the comments into an answer


J/JR can be emulated with JAL/JALR as the latter performs a super-set of the operations of the former.

As @Jester pointed out, routines (functions in C jargon) must be careful to preserve their return address present in $ra.
Unless the routine is a leaf routine (one that doesn't do any call) $ra must be saved somewhere anyway.

Actually both JAL/JALR and J/JR can be implemented one in terms of the other:

  • Emulate JAL/JALR with J/JR

    Original               Emulated
    
    jal foo                la $ra, ret_label
                           j foo
                          ret_label:
    

  • Emulate J/JR with JAL/JALR

    Original                Emulated
    
    j foo                   prolog:
                              addi $sp, $sp, -4
                              sw $ra, ($sp)   
    
                              jal foo
    
                            epilog:
                              lw $ra, ($sp)
                              addi $sp, $sp, 4
    

    For this to work, the code must return to epilog. It is assumed that $ra is mostly preserved in routines (hence the names of the labels). Many thanks to @EOF for point out a mistake in this snippet.

As @Peter pointed out, the access to the $pc leads to an easier (for humans) emulation of JAL/JALR.

As @EOF pointed out, some RISC machine actually have only one instruction for JAL/JALR and J/JR given their innate entanglement.

Considering that jumps and call happen very often in a typical program, being able to implement easily (and executing them fast) is mandatory for any successful ISA.

这篇关于MIPS组装中J对JAL(以及JR对JALR)的必要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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