为什么会出现这种IA32组装code三莱亚尔说明? [英] Why is there three leal instructions for this IA32 assembly code?

查看:129
本文介绍了为什么会出现这种IA32组装code三莱亚尔说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编译这个C函数:

int calc(int x, int y, int z) {
   return x + 3*y + 19*z;
}

和我calc.s得到这个,而我注释发生了什么:

And I got this in calc.s, and I am annotating what is happening:

        .file   "calc.c"
        .text
.globl calc
        .type   calc, @function
calc:
        pushl   %ebp                    //Save paramaters
        movl    %esp, %ebp              //Move stack pointer into %ebp
        movl    12(%ebp), %eax          //Move y into %eax
        movl    16(%ebp), %ecx          //Move z into %ecx
        leal    (%eax,%eax,2), %eax     //%eax = 3*y
        addl    8(%ebp), %eax           //%eax = x+3y
        leal    (%ecx,%ecx,8), %edx     // ?
        leal    (%ecx,%edx,2), %edx     // ?
        addl    %edx, %eax              //%eax = (x+3*y)+(19*z)
        popl    %ebp                    //Pop the previous pointer
        ret
        .size   calc, .-calc
        .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
        .section        .note.GNU-stack,"",@progbits

我明白了一切到最后两个莱亚尔指令。为什么你需要19 2莱亚尔指令* Z,而3 * y是一个指令来完成的。

I understand everything up to the last two leal instructions. Why do you need two leal instructions for 19*z whereas 3*y is accomplished in one instruction.

推荐答案

莱亚尔是一个很小的常数对低价进行乘法运算的方式,如果常数的二加一的功率。这个想法是,利尔没有偏移相当于REG1 = REG2 + REG3 *比例。如果REG2和REG3正好匹配,这意味着REG1 = REG2 *(量表+ 1)。

leal is a way to perform a multiplication by a small constant on a cheap, if the constant is a power of two plus one. The idea is that leal without an offset is equivalent to "Reg1 = Reg2+Reg3*Scale". If Reg2 and Reg3 happen to match, that means "Reg1=Reg2*(Scale+1).

莱亚尔仅支持比例因子多达8个,所以19倍增,你需要两个。

leal only supports scale factors up to 8, so to multiply by 19, you need two.

的影响

leal   (%eax,%eax,2), %eax

是:

eax = eax + eax*2

它是由三个就是说,乘法

which is to say, multiplication by three.

第二两个莱亚尔取值一起进行了19乘法:

The second two leals together perform multiplication by 19:

leal    (%ecx,%ecx,8), %edx     // edx = ecx+ecx*8
leal    (%ecx,%edx,2), %edx     // edx = ecx+edx*2 (but edx is already z*9)

这篇关于为什么会出现这种IA32组装code三莱亚尔说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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