2012-03-08 32 views
0

作爲一項任務的一部分,我一直在試圖乘以兩個32位數並將結果存儲在一個64位的地方。但是,我的結果不正確。請幫我弄清楚爲什麼擴展乘法與納斯姆

[org 0x0100] 
jmp start 
multiplicand:  dd 100122,0 
multiplier:   dd 66015 
result:    dd 0,0 
start: 
initialize:   mov cl,16 

        mov bl,1 
checkbit:   test bl,[multiplier] 
        jz decrement 

multiply:   mov ax, [multiplicand] 
        add [result],ax 
        mov ax, [multiplicand+2] 
        adc [result+2], ax 
        mov ax, [multiplicand+4] 
        adc [result+4], ax 


decrement:   shl bl,1 
        shl [multiplicand],1 
        rcl [multiplicand+2],1 
        rcl [multiplicand+4],1 
        dec cl 
        jnz checkbit 

        mov ax, 0x4c00 
        int 0x21 

在AFD調試答案是F6B3A6(16587802 IN DEC),而應該是189F5C9A6(12月6609553830)。我已經通過調試器,但無法找到任何代碼錯誤。

+0

你就不能使用32位×32位= 64位(我)MUL?你沒有在pre-i80386 CPU上運行它,對嗎? – 2012-03-08 11:13:21

+0

它的一個課程要求,現在在16位工作,32位是在最後一課(從底部方法建立,我知道它已棄用) – 2012-03-08 12:41:51

回答

2

查看評論幾D'哦的:

[org 0x0100] 
jmp start 

multiplicand: dd 100122,0 
multiplier: dd 66015 
result:  dd 0,0 

start: 
initialize: mov cl,32 ; multipliers are 32-bit, so 32 iterations, not 16 

       mov bl,1 
checkbit:  test bl,[multiplier] 
       jz decrement 

multiply:  mov ax, [multiplicand] 
       add [result],ax 
       mov ax, [multiplicand+2] 
       adc [result+2], ax 
       mov ax, [multiplicand+4] 
       adc [result+4], ax 
       mov ax, [multiplicand+6] ; forgot this 
       adc [result+6], ax  ; forgot this 

decrement: ; shl bl,1    ; bl is 8-bit, but you need to test 32 
       shr word [multiplier+2],1 ; so, shift multiplier right instead 
       rcr word [multiplier],1 ; of shifting bl left 

       shl word [multiplicand],1 ; this is NASM, I'd rather tell 
       rcl word [multiplicand+2],1 ; the operand size here 
       rcl word [multiplicand+4],1 ; because it's unclear 
       rcl word [multiplicand+6],1 ; forgot this 
       dec cl 
       jnz checkbit 

       mov ax, 0x4c00 
       int 0x21 
+0

謝謝,是的,我忽略了很多錯綜複雜的問題 – 2012-03-08 18:07:55

+0

並且「無法發現代碼有問題「。 :) – 2012-03-08 18:11:31

0

mov cl,32替代mov cl,16

別忘了[multiplicand+6]