0

What does this code do in assembly language? Please help.

movl %eax, %edx shrl $31, %edx addl %edx, %eax sarl %eax The program was written in C and complied on a 64-bit Intel Pentium CPU.

19th Mar 2020, 10:04 AM
Jay
2 Respuestas
+ 2
movl %eax, %edx Copies eax to edx. shrl $31, %edx Shifts the value in edx to the right by 31 bits and store in edx. Since edx is a 32 bit register this effectively reads the sign bit. addl %edx, %eax Adds edx to eax. Since the previous operation stored the sign bit in edx, this adds 1 to the original value if eax is negative, otherwise it does nothing. sarl %eax Shift eax to the right by 1 and preserves the sign bit. Basically a division by 2. So for example if the input( eax ) is -5, the result is: Copy eax(-5) to edx. Get the sign bit, edx(-5) is negative, so edx is 1. Add edx(1) to eax(-5), eax is -4. Divide eax by 2, eax is -2. Final result: -2. If the input is 5. Copy eax(5) to edx. Get the sign bit, edx(5) is positive, so edx is 0. Add edx(0) to eax(5), eax is 5. ( do nothing ) Divide eax by 2, eax is 2. Final result: 2.
19th Mar 2020, 10:30 AM
Dennis
Dennis - avatar
0
Dennis thank you so much.. Now I understand what it means. I truly appreciate your help
19th Mar 2020, 12:45 PM
Jay