# Confused about the use of the shl instruction in this disassembly

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.

Here's the source code:

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.

• Hi, I'd say it's still a good idea to swap your screenshot for actual text even now after you got an answer. Commented Apr 14, 2021 at 7:59

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.

• Thanks for the help! Commented Apr 13, 2021 at 12:20