16 位 x 32 位的汇编乘法 =>48 位 [英] Assembly multiplication of 16-bit x 32-bit => 48-bit

查看:50
本文介绍了16 位 x 32 位的汇编乘法 =>48 位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在汇编中将一个大数乘以另一个(可能是小)数.大数(被乘数)保存在DX:AX中,乘数保存在BX中.MUL 指令只对 AX 进行操作.那么如何处理DX?

Assume I want to multiply a large number by another (maybe small) number in assembly. The big number (multiplicand) is saved in DX:AX and the multiplier is saved in BX. The MUL instruction only operates on AX. So what to do with DX?

例如,数字是 0001:0000H (65536),我想乘以 2.

For example, the number is 0001:0000H (65536) and I want to multiply it by 2.

number     dw   0000h, 0001h
...
mov    ax, [number]
mov    dx, [number+2]
mov    bx, 2
mul    bx   ; it is ax*2 or 0000*2

因此结果为零!对此有什么想法吗?

Therefore the result is zero! Any idea on that?

推荐答案

假设这是 286,所以你没有 eax.

Let's pretend this is 286, so you don't have eax.

number dd 0x12345678      ; = dw 0x5678, 0x1234
result dw 0, 0, 0         ; 32b * 16b = 48b needed
    ...
    mov    ax,[number]    ; 0x5678
    mov    cx,[number+2]  ; 0x1234 ; cx, dx will be used later
    mov    bx,0x9ABC
    ; now you want unsigned 0x12345678 * 0x9ABC (= 0xB00DA73B020)
    mul    bx             ; dx:ax = 0x5678 * 0x9ABC
    ; ^ check instruction reference guide why "dx:ax"!
    xchg   cx,ax
    mov    di,dx          ; di:cx = intermediate result
    mul    bx             ; dx:ax = 0x1234 * 0x9ABC
    ; put the intermediate multiplication results together
    ; into one 48b number dx:di:cx
    add    di,ax
    adc    dx,0
    ; notice how I added the new result as *65536 to old result
    ; by using different 16bit registers

    ; store the result
    mov    [result],cx
    mov    [result+2],di
    mov    [result+4],dx

这与纸上数字相乘的方式相同,只是您不移动 *10 个分量,而是利用 16b 寄存器大小的性质移动 *65536 (0x10000) 个分量以减少步骤.

It's the same way as when you multiply numbers on paper, just you don't move by *10 components, but exploit the 16b register size nature to move by *65536 (0x10000) components to make it in less steps.

  13
* 37
----
  91 (13 * 7)
 39_ (13 * 3, shifted left by *base (=10))
---- (summing the intermediate results, the 39 "shifted")
 481 (13 * 37)

这篇关于16 位 x 32 位的汇编乘法 =>48 位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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