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.
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.