How can I fix the problem of moving 8 bit value to the BX register (16 bit)?
mov al, 10h
mov bx, al
for this I get:
operands do not match: 16 bit and 8 bit register
How can I fix the problem of moving 8 bit value to the BX register (16 bit)?
mov al, 10h
mov bx, al
for this I get:
operands do not match: 16 bit and 8 bit register
Copyright © 2021 Jogjafile Inc.
The answer depends on whether you want to zero extend or sign extend the value and on whether you can or cannot use instructions available starting with the 80386. For better performance, the 80386 or later code should be used if
movzxandmovsxare available.zero extend on an 8086 or 80286
Zero the upper half, then move to the low half.
(Or equivalently, and more efficient on some later CPUs,
xor bx, bxto zero the whole 16 bits before replacing the low 8.)sign extend on an 8086 or 80286
Without
movsx, you probably want to usecbwwhich only works withAL->AX. The simplest way overwritesAHbefore copying toBX, since your value was already inAL.If you want to preserve
AH, you could copy the oldAXfirst then swap aftercbw:Saving instructions on 8086 can involve planning what you keep in which register so it's already in the right place for an instruction like
cbwormulthat uses an implicit register. By 386, Intel added versions of some of these that work with any register.zero extend on an 80386 or newer
Use
movzx.For best performance, zero extend all the way to 32 bit.
sign extend on an 80386 or newer
Use
movsx, which is likecbwbut works for any dst, src, even including a memory source.If possible, sign extend all the way to 32 bit for better performance.
Other methods: setting the top half with
neg/sbbis also possible, and so are arithmetic shifts or logical shifts for sign or zero extension. (Especially if your value started in a register like AH). See MASM Assembly move 8 bit register to the 16 bit register (ie. mov cx, ch)