从DX:AX寄存器移动到单个32位寄存器

时间:2015-10-16 23:34:00

标签: assembly x86 masm

我在添加16位乘法产品时遇到问题。我希望将2015年这样的年份乘以365,这样做我

mov dx, 0    ; to clear the register
mov ax, cx   ; cx holds the year such as 2015
mov dx, 365  ; to use as multiplier
mul dx       ; multiply dx by ax into dx:ax

检查寄存器后,我得到了正确的解决方案但是有一种方法可以将这个产品存储到一个寄存器中。我想为产品添加单独的值,因此我想将此产品移动到一个32位寄存器中。 谢谢你的帮助!

2 个答案:

答案 0 :(得分:4)

通常的方法是使用32位乘法开始。如果你的因子是一个常数,那就特别容易了:

movzx ecx, cx      ; zero extend to 32 bits
                   ; you can omit if it's already 32 bits
                   ; use movsx for signed
imul ecx, ecx, 365 ; 32 bit multiply, ecx = ecx * 365

您当然也可以组合16位寄存器,但不建议这样做。无论如何它在这里:

shl edx, 16 ; move top 16 bits into place
mov dx, ax  ; move bottom 16 bits into place

(显然还有其他可能性。)

答案 1 :(得分:1)

mov dx, 0    ; to clear the register
mov ax, cx   ; cx holds the year such as 2015
mov dx, 365  ; to use as multiplier
mul dx       ; multiply dx by ax into dx:ax

您可以从简化此代码开始(在进行乘法之前,您不需要清除任何寄存器):

mov  ax, 365
mul  cx       ; result in dx:ax

接下来回答您的标题问题并得到结果DX:AX移动到32位寄存器,如EAX,您可以写:

push dx
push ax
pop  eax
相关问题