shrl vs sarl .. x86 Assembly gnu

16,381

Assuming that sarl 1, %eax is really supposed to be sarl $1, %eax, then the whole thing equates to:

x = ((unsigned int) x) >> 20;
y = (x + y) >> 1

The leal instruction means: eax = eax + edx. This link might be useful to you, as well as this one.

Share:
16,381
toinetoine
Author by

toinetoine

Updated on June 04, 2022

Comments

  • toinetoine
    toinetoine almost 2 years

    I'm compiling my code with gcc and looking at the assembly, what is this code exactly doing?

    shrl $20, %edx
    leal (%edx,%eax), %eax
    sarl 1, %eax 
    

    Say that the variable X is at the edx register, and Y is at eax and both are (32-bit int). What is going on here??

    I know 'shrl $20, %edx' is shifting %eax right 20 bits, so is same as: eax/(2^20) and then sarl is the same so 'sarl 1, %eax' = eax/(2^1). Is that right, and if so what does leal do?