MASM Assembly将8位寄存器移动到16位寄存器(即mov cx,ch)

Bar*_*ach 5 x86 assembly masm mov zero-extension

我决定学习汇编编程语言.我正在使用这个.在底部练习它在一些指令中找到错误,其中之一是

mov cx, ch 
Run Code Online (Sandbox Code Playgroud)

我在SO上发现了一些类似的问题,解释了如何实现它,但现在我想知道为什么禁止这个操作?

假设我在CH中有10d = 00001010b并且想要将其置于CL并同时擦除CH.mov cx, ch似乎这样做是因为它将10d显示为16bit 00000000 00001010并将其分别放入CH和CL(整个CX)

有什么问题,为什么给定的教程要求在这个表达式中找到错误?

Mic*_*ael 7

mov指令用于在相同大小的操作数之间移动.你想要的是 8位扩展ch到16位cx.有两个指令可用于此目的:

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
Run Code Online (Sandbox Code Playgroud)

在这种特殊情况下完成同样事情的另一种方法是:

shr cx,8  ; zero-extend
sar cx,8  ; sign-extend
Run Code Online (Sandbox Code Playgroud)