在程序集中添加数组时找到数组的最小值

问题描述

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

让最低值 = 0

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

解决方法

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

  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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...