编写汇编时使用哪个寄存器有关系吗? [英] Does it matter which registers you use when writing assembly?

查看:119
本文介绍了编写汇编时使用哪个寄存器有关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要编写程序集,将值分配给哪个寄存器是否重要?假设您将累积/中间值存储在%ebx中,而不是通常用于此目的的%eax中。那是不好的做法吗?会影响性能吗?

If you're writing assembly, does it matter which registers you allocate values to? Say, you store an accumulated/intermediate value in %ebx instead of %eax, which was traditionally used for that purpose. Is that bad practice? Will it affect performance?

换句话说,您能否将它们等同地视为存储空间,还是应该坚持将它们用于特定目的?

In other words, can you treat them equally as storage space, or should you stick to using them for specific purposes?

推荐答案

首先,您必须使用支持您要使用的指令的寄存器。   x86上的许多指令(以及其他体系结构,虽然不那么少)在如何支持寄存器方面都有一些限制。

First and foremost, you have to use registers that support the instructions you want to use.  Many instructions on x86 (and other architectures, though less so) have some restrictions on how registers are supported.

例如,使用某些双寄存器乘法和除法指令,

Take certain double register multiply and divide instructions, for example, which specifically involve eax and edx in particular uses.

接下来,您想要使用高效的寄存器,即寄存器:

Next, you want to use registers that are efficient, i.e. registers:


  • 的编码较短(此处是有关x64指令长度的很好的讨论)。短编码可以更好地利用缓存资源,从而可以使大型程序更高效地运行。

  • for which encodings are shorter (here is a good discussion on x64 around instruction length).  Short encodings make better usage of cache resources, which can allow larger programs to run more efficiently.

不受限制,即由于调用约定,也就是说,它们不会为使用产生额外的开销(定义了软件/呼叫会议)–除非已经支付了开销!

that are un-encumbered, i.e. due to the calling conventions, which is to say they don't incur extra (software/calling-convention defined) overhead for their usage — unless that overhead has already been paid!

是产生值的最终目的地:例如如果是第二个参数,则对应于要传递的第二个值的寄存器(同样根据调用约定)。如果我们可以将值放在正确的寄存器中(根据传递或返回值的需要),那么我们可以放弃数据移动(即复制)指令。

that are the eventual destinations of the values being produced: e.g. if the second parameter, then the register that corresponds to the second value to be passed (again according to the calling convention).  If we can place the value in the right register (as needed for passing or returning values), then we can forgo a data move (aka copy) instruction.

这篇关于编写汇编时使用哪个寄存器有关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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