Antoine Dahan
Antoine Dahan

Reputation: 713

shrl vs sarl .. x86 Assembly gnu

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?

Upvotes: 4

Views: 21005

Answers (1)

John Szakmeister
John Szakmeister

Reputation: 47022

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.

Upvotes: 10

Related Questions