1
votes

Trying to use Shift-Left instruction for multiplication.

The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with 0.

SHL instruction

I don't understand how to use register or memory for N, because when I attempt to do so, I am told "invalid instruction operand"

  1. The user inputs a number N in the RequestNumber procedure (which returns EAX).
  2. The calculation I am attempting is: result = 1 * 2^N

My code:

mov ebx, 1   
call RequestNumber      ; returns eax   
shl ebx, eax   
mov result, ebx

The assignment I am working on requests to use the Shift to get result = 2^N As far as I've seen, you can only use imm8 numbers with this register CL (tried using the lower ECX with the same result).

Operand types for SHL

Question: How do I properly use the Shift instruction if I have to get N from the user?

1
If it's really inconvenient to use cl for the shift count, and your CPU has the BMI2 instructions, you could use shlx. Larger code size though (5 bytes versus 2). - Nate Eldredge

1 Answers

5
votes

The shift amount has to be in cl. So to get shift input from the user, place it in cl. Recall that cl is the low 8 bit of ecx, so if you place the shift amount in ecx and then shift by cl, it works as expected:

mov ebx, 1   
call RequestNumber      ; returns eax
mov ecx, eax
shl ebx, cl
mov result, ebx