TransWikia.com

Confused about the use of the shl instruction in this disassembly

Reverse Engineering Asked by Constructive on August 8, 2021

I created a code construct in C to see how it looks in x86. I’m confused about the use of the shl instructions. I’m confused about what is happening in between the lines <+39> and <+51> I don’t get how those instructions translate to the source code.

Code construct

Here’s the source code:
Source

It’s obviously compiler optimisation but I’m not understanding how that would be equal to multiplying by 21. [It’s bit shifting eax, 0x2, or multiplying by 4 twice, but I don’t understand how the add instructions make it go from multiplying by 16 to 21]

Thanks for any help! Rohail.

One Answer

Let's break it down line by line. Assuming x is the thing you want to multiply by 21 and it's stored in eax (as it is in this example after line 34).

<39>: mov edx, eax  ; so copy the x to edx
<41>: mov eax, edx  ; it's pointless to do this mov; after those two lines eax & edx has the value of x
<43>: shl eax, 2    ; so eax = x * 4
<46>: add eax, edx  ; so eax = x * 5  (x * 4 + x)
<48>: shl eax, 2    ; so eax = x * 20 (x * 5 * 4)
<51>: add eax, edx  ; so eax = x * 21 (x * 5 * 4 + x)

PS. For the future please post code as a text. Much easier to copy than from the image.

Correct answer by Paweł Łukasik on August 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP