【问题标题】:Assembly 8088: 32-bit signed Multiplication with addition and bit manipulation using 16-bit registers汇编 8088:使用 16 位寄存器进行加法和位操作的 32 位有符号乘法
【发布时间】:2015-03-16 16:25:50
【问题描述】:

我正在尝试编写汇编程序来将两个 32 位有符号数字相乘并将答案存储在 64 位数字中,但我的代码只能给我最多 32 位的正确答案。我已经护目镜,搜索了很多网站,但不能满足自己。尽管我调试了很多次,但我无法解决问题。这是我作为学生的第一个问题,所以如果我不清楚,我真的很抱歉。谢谢:)

; 32bit multiplication 
[org 0x0100]
multiplicand dd 0,9000008             ; 32bit multiplicand 64bit space 
multiplier: dd 45009                  ; 32bit multiplier 
result: dd 0,0                        ; 64bit result 
start:
    mov cx,32                         ; initialize bit count to 32
    mov bx, [multiplier]              ; load multiplier in dx and bx
    mov dx, [multiplier+2]            

checkbit:
    sar dx,1                          ; move right most bit in carry 
    rcr bx,1
    jnc skip                          ; skip addition if bit is zero
    mov ax,[multiplicand]
    add word[result],ax
    mov ax,[multiplicand+2]
    adc word[result+2],ax
    mov ax,[multiplicand+4]
    adc word[result+4],ax
    mov ax,[multiplicand+6]
    adc word[result+6],ax
    mov ax,[multiplicand+8]
    adc word[result+8],ax
    mov ax,[multiplicand+10]
    adc word[result+10],ax
    mov ax,[multiplicand+12]
    adc word[result+12],ax
    mov ax,[multiplicand+14]
    adc word[result+14],ax

skip:
    shl word [multiplicand],1
    rcl word [multiplicand+2],1
    rcl word [multiplicand+4],1
    rcl word [multiplicand+6],1
    rcl word [multiplicand+8],1
    rcl word [multiplicand+10],1
    rcl word [multiplicand+12],1
    rcl word [multiplicand+14],1
    dec cx
    jnz checkbit

mov ax,0x4c00                         ; terminate program
int 0x21

我认为我的逻辑是正确的,但我无法理解错误是什么。任何帮助表示赞赏。 :)

【问题讨论】:

  • 调试程序时发现了什么?
  • 我明白你的意思。我在 AFD 调试器上按 F1 键,直到 cx 变为零,然后将答案与计算器匹配。我知道它在 32 位之后就出错了。我应该彻底做到这一点:P.....好吧,谢谢@RaymondChen ..:)
  • 如果只在最后检查值,那并不是真正的调试。尝试更频繁地检查该值,例如,每次执行达到checkbit,或者甚至在每条指令之后,因为程序是如此简短。 (它只执行几百条指令,你应该能够单步执行。)

标签: assembly 32-bit computer-architecture x86-16


【解决方案1】:
multiplicand dd 0,9000008             ; 32bit multiplicand 64bit space  

您设置了 64 位空间,但您的代码修改了 128 位空间!!

另外,由于很少有顺序,你应该交换被乘数的双字。

【讨论】:

  • 谢谢..我检查了它......它已经完成了..实际上我认为二进制中的每四位,十六进制添加一位,但逻辑上我犯了一个错误,每个添加两位四位,现在谢谢你。我明白了..还有小端的东西..
猜你喜欢
  • 1970-01-01
  • 2017-03-11
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多