MOV 8位至16位寄存器(AL至BX) [英] MOV 8 bit to 16 bit register (al to bx)

查看:360
本文介绍了MOV 8位至16位寄存器(AL至BX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决将8位值移至BX寄存器(16位)的问题?

How can I fix the problem of moving 8 bit value to the BX register (16 bit)?

mov al, 10h
mov bx, al

为此,我得到:

operands do not match: 16 bit and 8 bit register 

推荐答案

答案取决于是否要对值进行零扩展或符号扩展以及是否可以使用以80386开头的可用指令.为获得更好的性能,如果movzxmovsx可用,则应使用80386或更高版本的代码.

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 movzx and movsx are available.

将上半部分调零,然后移至下半部分.

Zero the upper half, then move to the low half.

xor bh, bh
mov bl, al

(或者等效地,并且在以后的某些CPU上效率更高,xor bx, bx在替换低8位之前将整个16位清零).

(Or equivalently, and more efficient on some later CPUs, xor bx, bx to zero the whole 16 bits before replacing the low 8.)

没有movsx,您可能要使用 cbw 仅适用于AL-> AX.最简单的方法是在复制到BX之前覆盖AH,因为您的值已经在AL中.

Without movsx, you probably want to use cbw which only works with AL -> AX. The simplest way overwrites AH before copying to BX, since your value was already in AL.

 cbw               ; AH = 0 or 0xFF according to top bit of AL
 mov  bx, ax

如果要保留AH,则可以先复制旧的AX,然后在cbw之后交换:

If you want to preserve AH, you could copy the old AX first then swap after cbw:

 mov  bx, ax      ; save the old AH (and AL)
 cbw              ; sign-extend AL into AX
 xchg bx, ax      ; BX = sign-extended result, restore original AX

在8086上保存指令可能涉及计划保存在哪个寄存器中的内容,因此对于使用隐式寄存器的cbwmul这样的指令,它已经在正确的位置.到386年,英特尔增加了其中一些可与任何寄存器一起使用的版本.

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 cbw or mul that uses an implicit register. By 386, Intel added versions of some of these that work with any register.

使用 movzx .

movzx bx, al

为获得最佳性能,零应一直扩展到32位.

For best performance, zero extend all the way to 32 bit.

movzx ebx, al

符号在80386或更高版本上扩展

使用 movsx ,它类似于cbw,但适用于任何 dst,src ,甚至包括内存源.

sign extend on an 80386 or newer

Use movsx, which is like cbw but works for any dst, src, even including a memory source.

movsx bx, al

如果可能的话,将符号一直扩展到32位,以获得更好的性能.

If possible, sign extend all the way to 32 bit for better performance.

movsx ebx, al


其他方法:也可以用neg/sbb设置上半部分,符号或零扩展的算术移位或逻辑移位也可以. (特别是如果您的值始于AH之类的寄存器中).参见 MASM程序集将8位寄存器移到16位寄存器(即mov cx,ch)


Other methods: setting the top half with neg/sbb is 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)

这篇关于MOV 8位至16位寄存器(AL至BX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆