![](/img/trans.png)
[英]Assembly 8088: 32-bit signed Multiplication with addition and bit manipulation using 16-bit registers
[英]Assembly multiplication of 16-bit x 32-bit => 48-bit
假設我想在匯編中將一個大數乘以另一個(可能很小)的數。 大數(被乘數)保存在DX:AX
,乘數保存在BX
。 MUL
指令僅對AX
。 那么DX
怎么辦呢?
例如,數字是0001:0000H
(65536),我想將它乘以 2。
number dw 0000h, 0001h
...
mov ax, [number]
mov dx, [number+2]
mov bx, 2
mul bx ; it is ax*2 or 0000*2
所以結果為零! 對此有什么想法嗎?
讓我們假設這是 286,所以你沒有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) 個分量以減少步驟。
即
13
* 37
----
91 (13 * 7)
39_ (13 * 3, shifted left by *base (=10))
---- (summing the intermediate results, the 39 "shifted")
481 (13 * 37)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.