themennice
themennice

Reputation: 187

x86-64 Assembly multiplication by 4

I am trying to find an alternative to using imull and thus have a question that follows.

Would sall $2, %eax have the same effect on %eax as leal 0(,%eax,4), %eax, which should be multiplication by 4 to be exact?

Upvotes: 3

Views: 2261

Answers (1)

jfMR
jfMR

Reputation: 24738

You are right, sall $2, %eax multiplies eax by four, i.e., it shifts the bits of register eax two positions to the left.

Note that sal and shl are actually synonyms, whereas sar and shr are not. There is no distinction between signed and unsigned for the left shift.

In your case you are simply shifting the bits of the register to the left, signedness of the operand to be shifted does not matter, since the most significant bit (i.e., the sign bit) does not need to be interpreted as such as it is the case for the right arithmetic shift.

Upvotes: 3

Related Questions