Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 688
toinetoine Avatar asked Dec 26 '22 06:12

toinetoine


1 Answers

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.

like image 87
John Szakmeister Avatar answered Dec 31 '22 15:12

John Szakmeister