Author Topic: A doubt about "shl" instruction  (Read 14696 times)

Offline TomodaMaki

  • Jr. Member
  • *
  • Posts: 2
A doubt about "shl" instruction
« on: March 18, 2013, 02:44:22 PM »
There are two instructions:

Code: [Select]
shl ax,1
shl ax,2

and then I have assembly the above source file to *.bin file.  then I use ndisasm command to disassembly it. I have got the
below code:

Code: [Select]
D1E0              shl ax,1
C1E002            shl ax,byte 0x2

if the number is equal or bigger than 2. it will add "byte" in front of the number. I don't know why?

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: A doubt about "shl" instruction
« Reply #1 on: March 18, 2013, 03:41:34 PM »
It all comes down to instruction encoding, and that is why we have assemblers like NASM so we don't have to worry about those intricacies.

Even C1 E0 01

Offline TomodaMaki

  • Jr. Member
  • *
  • Posts: 2
Re: A doubt about "shl" instruction
« Reply #2 on: March 18, 2013, 03:55:33 PM »
thanks very much . the instruction "shl" means shift to the left by bit. not by byte. why it add byte in front of the number?

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: A doubt about "shl" instruction
« Reply #3 on: March 18, 2013, 04:17:24 PM »
For a lot of instructions there is a particular spot in the opcode that indicates when using 8, 16 or 32 bit operands.  NDISASM simply looks at these bits and even though redundant, as this particular set of instructions will only use an 8 bit value, byte is prepended.  As an example

Code: [Select]
        mov     byte [ Next_Index], 41

is required, otherwise the assembler doesn't know what type of data Next_Index is pointing to. Whereas;

Code: [Select]
       mov     [ Next_Index], al

isn't, because assembler knows AL is 8 bits

MASM you'd have to use;

Code: [Select]
     mov     byte ptr [ Next_Index], 41



Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: A doubt about "shl" instruction
« Reply #4 on: March 18, 2013, 05:19:36 PM »
There are actually a bunch of "shl"s.

http://home.myfairpoint.net/fbkotler/nasmdocc.html#section-A.4.290

The variety with a specific second operand of 1, and with cl as a second operand, have been with us since the 8086. The variety with a "byte" operand wasn't introduced until the 80186 (for practical purposes, 286 - there weren't many 186's made). So it's a completely different instruction.

Note that "shl" and "sal" are the same instruction, but that "shr" and "sar" are different - the latter being for signed numbers...

As TightCoderEx points out, we can mostly trust Nasm to do the right thing (or one of 'em)... and not worry too much about what Ndisasm says. Sharp eyes to have spotted that difference!

Best,
Frank