2012-03-08 69 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]