汇编为什么lea快? [英] Assembly why lea is fast?

查看:79
本文介绍了汇编为什么lea快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的教授谈话,他说:

I had a conversation with my professor and he said:

leaq (%rax,%rax,8)

比:

imulq $9, %rax

我问他为什么(在两种情况下,我们都用几乎相同的数字进行乘法运算),他说我们不会介入.

I asked him why (in both cases we are doing multiplication with nearly same numbers) and he said we won't get into that.

有人可以通过简单的方式帮助我理解leaq为何速度很快吗?

Can Someone help me understand in simple way why leaq is fast in general?

评论中提出的一个问题是:

a question that rose from the comments, is:

imulq $9, %rax

比执行2条命令快,一个命令向左移动,另一个命令添加一个%rax(我们以前可以将其保存在寄存器中)

faster than doing 2 commands, one to shift left and other to add one %rax (which we could previously save in a register)

为什么?

推荐答案

lea (加载有效地址)是一种执行指针算术通用操作的方式.指令如何引用其操作数称为其寻址模式 lea 支持已缩放

The lea (load effective address) is a way to perform the common operation of pointer arithmetic. How an instruction refers to its operands is called its addressing mode and lea supports scaled or base plus index plus offset addressing modes (among others).

address = base address + index * scaling + offset

其中缩放比例值可以是2(1、2、4、8)的几个幂之一.这些值对于字节,字符,整数,指针等的数组很有用.它不能对任意值进行编码或执行乘法运算.在硬件中,这几个选项可以通过几个多路复用器来实现,而延迟周期只有一小部分.

where the scaling value can be one of few powers of two (1, 2, 4, 8). These values are useful for arrays of bytes, characters, integers, pointers etc. It is not capable of encoding or performing multiplication with arbitrary values. In the hardware, these few options can be implemented with a couple of multiplexers, with a fraction of a cycle of delay.

另一方面,乘法指令通过乘法电路,该电路可以将两个任意全角(64位)操作数相乘.这是一个复杂性高得多的操作.即使并行使用多个全角加法器,它的延迟也大约是全角加法器的六倍(log n)(尽管该设计可能包含优化功能,使它可以更快地乘以较简单的值).

A multiplication instruction on the other hand, goes through a multiplication circuitry that can multiply two arbitrary full-width (64 bits) operands. This is an operation with significantly higher complexity. Even with multiple full-width adders in parallel, it has about six times (log n) the delay of a full-width addition (although the design might incorporate an optimization that allows it to multiply simpler values quicker).

这篇关于汇编为什么lea快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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