【问题标题】:finding the lowest value of an array while adding them in assembly在汇编中添加数组时找到数组的最小值
【发布时间】:2020-12-24 14:44:50
【问题描述】:

我正在学习汇编,最近转到签名数字主题。在我坚持的一项练习中。有一个数组array DB +53, -87, -45, +23, -79, +28, -90, +75, -39,我想在添加它们时找到最小值。 例如:

让最小值 = 0

在前 2 次迭代中 --> 53+(-87) = -34 变为最低。 -34 +(-45) = -79 变为低点等。 我试图实现一个循环,但由于我溢出了代码无法处理。 任何帮助表示赞赏。

【问题讨论】:

  • 让你的循环使用 word 大小的寄存器,这样就不会溢出。

标签: arrays assembly sum tasm emu8086


【解决方案1】:

鉴于数组被定义为包含有符号字节,因此很容易使用字节大小的寄存器。这可能会迅速产生溢出。因此最好使用字大小的寄存器。只需使用CBW 指令将字节值从AL 符号扩展为AX

  mov si, 9             ; Number of elements
  xor dx, dx            ; Sum
  mov cx, 32767         ; Current lowest
  mov bx, offset array  ; Address of array
again:
  mov al, [bx]          ; Fetch next byte element
  cbw                   ; Sign-extend to word
  add dx, ax            ; Add to the sum
  cmp dx, cx            ; Is it less (signed comparison) ?
  jnl NotLess
  mov cx, dx            ; Only replace if less than previous lowest
NotLess:
  inc bx                ; To next byte element
  dec si
  jnz Again

CX 寄存器 (-197) 具有 LowestValueWhileAdding
DX 寄存器 (-161) 具有 Sum

【讨论】:

  • 非常感谢您的宝贵时间和明确的答复!
  • @opia 这是真正的圣诞节精神!也感谢您接受答案。
  • 圣诞快乐! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 2018-07-04
  • 2013-03-30
  • 1970-01-01
相关资源
最近更新 更多