Cannot move 8 bit address to 16 bit register

馋奶兔 提交于 2019-11-28 00:22:16

why can’t I move 8 bit variable into much larger 16 bit register?

Becase the machine code MOV instruction needs both the source operand and destination operand to be the same size. This is needed because a MOV instruction by itself doesn't specify how to fill the remaining bits of the larger destination register.

To allow for different size move operations, Intel added MOVZX and MOVSX in the 80386 CPU, which allow a smaller source operand (the destination is always a 32 bit register). -SX and -ZX suffixes denotes what the previously unused bits of the destination register should be filled with.

On 16-bit Intel processors, there is the instruction CBW (Convert Byte to Word), which does a sign extend from 8 to 16 bits. Unfortunately, this only works for the accumulator (register AL/AX), so you'd have to do something like:

mov al,var1
cbw
mov bx,ax

cbw does a sign extension. If your var1 is unsigned, you can do simply as this:

mov bl,var1
xor bh,bh  ; equivalent to mov bh,0 but faster and only one byte opcode

Or, as Peter Cordes states:

xor bx,bx    ;clear the whole destination register
mov bl,var1  ;update the least significant byte of the register with the 8-bit value

You're using an assembler that keeps track of how you declare symbols to figure out what operand size to use.

var1 isn't an 8 bit address, it's a 16bit address (not counting the segment) that points at the first of two 8-bit variables. So the assembler error message is badly worded and confusing.

NASM would do what you said, and do a 16bit load. You'd find var1 in bl and var2 in bh. Presumably you could write mov bx, word [var1], or word ptr or whatever, to get your assembler to emit the 16bit load.

(Actually, NASM would assemble mov BX, var1 into a mov r16, imm16, putting the address into the register. Always use [] around memory references, for consistency, because that works in NASM and MASM variants of Intel syntax. NASM doesn't support the mov BX, offset var1 syntax for writing the mov-immediate form, though.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!