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.

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

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.