Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 这是在Mul中使用cbw的正确方法吗?_Assembly_X86 16_Sign Extension - Fatal编程技术网

Assembly 这是在Mul中使用cbw的正确方法吗?

Assembly 这是在Mul中使用cbw的正确方法吗?,assembly,x86-16,sign-extension,Assembly,X86 16,Sign Extension,我从8位和8位寄存器得到乘法。但是,当16位中有一位,8位中有一位时,我们如何在乘法之前进行转换: 问题:需要提供260*19的代码片段,并打印结果。我做到了: mov Ax,260 mov Al,19 cbw; Mul Ax PutInt Ax AL寄存器是AX寄存器的最低一半 AX /-----------------\ MSB xxxx xxxx xxxx xxxx LSB \-------/ \-------/ AH

我从8位和8位寄存器得到乘法。但是,当16位中有一位,8位中有一位时,我们如何在乘法之前进行转换:

问题:需要提供260*19的代码片段,并打印结果。我做到了:

mov Ax,260
mov Al,19
cbw;
Mul Ax 
PutInt Ax
AL
寄存器是
AX
寄存器的最低一半

            AX
    /-----------------\
MSB xxxx xxxx xxxx xxxx LSB
    \-------/ \-------/
        AH        AL
因此,第二条指令
mov al,19
错误地覆盖了您的第一个数字

字节大小的
mul
指令将
AL
寄存器乘以指定的字节大小的操作数。
字大小的
mul
指令将
AX
寄存器乘以指定的字大小的操作数

然后使用
mul-ax
将计算
ax*ax
,这不是您想要的。你想把不同的数字相乘

AX
与另一个寄存器如
BX
一起使用可以解决这两个问题


如何处理
cbw
取决于我们如何看待代码

  • 如果您将数字作为直接数(260,19)使用,则即使对于较小的数字19,也只需使用字大小的寄存器:

    mov     bx, 260
    mov     ax, 19
    mul     bx       ; Product AX * BX is in DX:AX but here DX=0
    PutInt  ax
    
    甚至让汇编程序执行乘法:

    mov     ax, 260 * 19
    PutInt  ax
    
  • 如果数字来自内存(不同大小的变量),则需要扩展较小的变量

    • 无符号数,使用
      mul
      进行无符号乘法

      mov     bx, wNum  ; [0,65535]
      mov     al, bNum  ; [0,255]
      mov     ah, 0
      mul     bx        ; Product AX * BX is in DX:AX
      PutInt  ax
      
      mov     bx, wNum  ; [-32768,32767]
      mov     al, bNum  ; [-128,127]
      cbw
      imul    bx        ; Product AX * BX is in DX:AX
      PutInt  ax
      
      我建议在这种情况下不要使用
      cbw
      !使用
      cbw
      将从128向上复制所有数字

    • 有符号的数字,使用
      imul
      进行有符号的乘法

      mov     bx, wNum  ; [0,65535]
      mov     al, bNum  ; [0,255]
      mov     ah, 0
      mul     bx        ; Product AX * BX is in DX:AX
      PutInt  ax
      
      mov     bx, wNum  ; [-32768,32767]
      mov     al, bNum  ; [-128,127]
      cbw
      imul    bx        ; Product AX * BX is in DX:AX
      PutInt  ax
      
      cbw
      是从
      AL
      扩展带符号字节的正确选择


否,
cbw
符号将AL扩展到AX,覆盖其他输入。使用调试器。将16位输入放在不同的寄存器中,如DX(它将被16位
mul
的高半输出覆盖)。或者在组装时进行扩展,比如
mov-cx,19
/
mul-cx
顺便说一句,mul在这里没有什么特别之处,只是它和CBW都隐式使用AX。通常您使用CBW/CWD/CDQ作为
div
的设置:。当然,如果你在AL中有一个数字,你想对扩展进行签名,那么CBW是最有效的方法,MUL需要两个输入的宽度相同。