1

I'm trying to figure out whether the following piece of assembly code is invalid.

movb $0xF, (%bl)

Is it invalid? If so, why? Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
amorimluc
  • 1,661
  • 5
  • 22
  • 32

2 Answers2

7

You don't say what processor. bl is a 8-bit register at least in x86 processors, but it cannot be used for addressing.

Why is it invalid instruction? Well, the reason an assembly instruction is invalid is that there's no such instruction for the given processor. There is no possible way to encode this instruction. In this case (assuming x86), using bl or any other 8-bit register neither for addressing has not been considered necessary. In 16-bit code only 16-bit registers bx, bp, si and di can be used for memory addressing. Wikipedia has a useful list of all possible addressing modes (please do note it's using Intel syntax, your code is in AT&T syntax).

Edit: In AT&T syntax the letter b in movb defines it deals with an 8-bit operand.

To obtain more or less your goal (to use bl for addressing), you could do one of these (these are in Intel YASM/NASM syntax, MASM-style assemblers including GNU .intel_syntax noprefix want byte ptr):

For 16-bit code:

xor   bh,bh
mov   byte [bx], 0x0f

For 32-bit code:

movzx  ebx,bl
mov    byte [ebx], 0x0f

For 64-bit code:

movzx  ebx,bl               ; with implicit zero-extension to 64-bit
mov    byte [rbx], 0x0f

It's rare that you ever want to store anything to a linear address from 0..255 (one byte). In 64-bit mode where segmentation is mostly disabled so the DS base is fixed at 0, this is definitely what the instruction is doing, but especially in 16-bit mode, the DS base could be non-zero.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
nrz
  • 10,435
  • 4
  • 39
  • 71
0

This question is probably from the CSAPP book, Practice Problem 3.3. You cannot use a 8 bit register (%bl) as address register on AT&T assembly (I'm not sure about Intel's). Moreover it is not possible to determine whether $0xF is a 8, 16 or 32 bit value.

  • AT&T and Intel syntax can both express everything that x86 machine code can do. It's a hardware limitation (not encodeable), which is why assemblers won't accept it. – Peter Cordes Oct 02 '18 at 16:38
  • Does this hardware limitation apply to 16-bit registers as well, in this case %bx? – Rodrigo Camargos Oct 02 '18 at 21:23
  • Only in 64-bit mode. In 16 and 32-bit mode, `(%bx)` is a valid addressing mode. nrz's answer on this question already covers this, just read it. – Peter Cordes Oct 02 '18 at 21:25