MASM Assembly moves an 8 bit register to a 16 bit register (i.e. Mov cx, ch)

I decided to learn the assembly language programming. I am using this . At the bottom of the exercise, to find a mistake in some instructions and one of them,

mov cx, ch 

      

I found some similar question on SO on this topic explaining how to achieve it, but now I would like to know why this operation is prohibited?

Suppose I have 10d = 00001010b in CH and you want to put it in CL and erase CH at the same time. mov cx, ch

looks like because it shows 10d as 16 bit 00000000 00001010 and puts it in CH and CL respectively (whole CX)

What's wrong with this and why is this tutorial asking you to find the error in this expression?

+3


source to share


3 answers


The command is mov

used to move between operands of the same size. You want to expand 8-bit ch

to 16-bit cx

. There are two commands available for this purpose:

movzx cx,ch  ; zero-extends ch into cx. the upper byte of cx will be filled with zeroes
movsx cx,ch  ; sign-extends ch into cx. the upper byte of cx will be filled with the most significant bit of ch

      



Another way to achieve the same in this specific case would be:

shr cx,8  ; zero-extend
sar cx,8  ; sign-extend

      

+5


source


The problem is that you are trying to move the contents of an 8-bit register ch

to a 16-bit register cx

. You cannot do this because the registers are of different sizes.

So my guess is that you get an error like "Invalid opcode / operand combination".



ps : exchanged 8 and 16 above; the statement remains the same. Check out this review for example . As you can see, there is no combination of different register sizes. This means that there is no OPcode that represents mov cx, ch

.

+2


source


just do it with simple instructions

mov cl,ch  ; copy high bits to low
xor ch,ch  ; clear high-bits

      

it is common in 16-bit programming and only takes 2 clock cycles .

It takes 3 clock cycles to use movezx / movsx . Use

movsx cx,ch

      

to move byte to word with s ign-extension and

movzx cx,ch

      

Move byte to word with z ero-extension

0


source







All Articles