【问题标题】:extended multiplication with nasmnasm 扩展乘法
【发布时间】:2012-03-08 10:34:35
【问题描述】:

作为作业的一部分,我一直在尝试将两个 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 (6609553830 in dec)。我已经通过调试器,但找不到代码有什么问题。

【问题讨论】:

  • 你不能只使用 32 位 x 32 位 = 64 位 (i)mul 吗?您没有在 i80386 之前的 CPU 上运行它,是吗?
  • 目前在 16 位上工作的课程要求,最后一课涵盖了 32 位(从底部开始构建,我知道它已弃用)

标签: assembly x86 nasm multiplication


【解决方案1】:

查看 cmets 以获得一些 d'oh's:

[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

【讨论】:

  • 谢谢,是的,我忽略了很多错综复杂的事情
【解决方案2】:

mov cl,16 替换为mov cl,32

别忘了[multiplicand+6]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多