在数组排序中使用相对值 (asm)

发布于 2024-10-20 16:44:21 字数 900 浏览 4 评论 0原文

我需要对数组进行排序,并将数组中的每一行按升序排序。我似乎进展得不太顺利(惊讶!),因为我不断遇到两个错误:

a2101:无法添加两个可重定位标签 和 a2026:预期常量

这是我的排序,这对我来说很有意义,但我认为我仍在尝试将高级语言技术实现到汇编中。有没有办法解决无法使用相对值的问题? (顺便说一句,该数组为 7 行 x 9 列)。

mov cx, 7; cx = number of rows

outer: ; outer loop  walk through the rows

  push cx 
  mov cx, 9
  mov row, cx ;rows

    middle: ; middle-loop walk through the columns

      push cx
      sub cx, 1  ;cx = cx-1
      mov column, cx  ;columns
          inner:  ;inner loop - compare and exchange column values

                  cmp mArray[row*9 + column], mArray[row*9 + column+1]
                  xchg mArray[row*9 + column+1], mArray[row*9 + column]
                  ; compare and exchange values from mArray table
                  inc column
          loop inner

      pop cx
    loop middle ;end middle loop

  pop cx
 loop outer ; end outer loop

ret

感谢您的任何帮助。

I need to sort through an array and sort each individual row in array to be in ascending order. I doesn't seem to going so well (surprise!) as I keep getting hit with two errors:

a2101: cannot add two relocatable labels
and
a2026: constant expected

Here's my sort, it makes sense to me, but I think I'm still trying to implement high-lvl language techniqes into assembly. Is there a way to get around not being able to use relative values? (the array is 7 rows by 9 columns, btw).

mov cx, 7; cx = number of rows

outer: ; outer loop  walk through the rows

  push cx 
  mov cx, 9
  mov row, cx ;rows

    middle: ; middle-loop walk through the columns

      push cx
      sub cx, 1  ;cx = cx-1
      mov column, cx  ;columns
          inner:  ;inner loop - compare and exchange column values

                  cmp mArray[row*9 + column], mArray[row*9 + column+1]
                  xchg mArray[row*9 + column+1], mArray[row*9 + column]
                  ; compare and exchange values from mArray table
                  inc column
          loop inner

      pop cx
    loop middle ;end middle loop

  pop cx
 loop outer ; end outer loop

ret

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-10-27 16:44:21

以下几行是有问题的:

cmp mArray[row*9 + column], mArray[row*9 + column+1]
xchg mArray[row*9 + column+1], mArray[row*9 + column]

与 HLL 不同,汇编不允许用任意表达式代替常量或变量。这就是 HLL 最初被发明的原因。使用之前计算寄存器中的偏移量:

mov ax, row
mov bx, ax
shr bx, 3 ; bx = row*8 now
add bx, ax ; bx = row*9 now
add bx, column ; bx = row*8+column now
mov dx, [bx] ;first comparand
inc bx
cmd dx, [bx] ; that's your compare!

此外,您不使用任何分支; cmp 指令完全没有意义;你浪费了它的结果,并且 xcng 不会有条件地执行。阅读条件跳转命令(jz/jnz 等)。

另外,我真心希望这是一次练习,而不是一个真正的项目。如果是真的,请重新考虑使用汇编。对于这样一件微不足道的事情,组装是一个错误、错误的选择。特别规格。考虑到你在这方面有多糟糕。

The following lines are problematic:

cmp mArray[row*9 + column], mArray[row*9 + column+1]
xchg mArray[row*9 + column+1], mArray[row*9 + column]

Unlike HLL, assembly does NOT allow for arbitrary expressions in place of constants or variables. That's why HLL's were invented in the first place. Calculate the offset in the registers before using:

mov ax, row
mov bx, ax
shr bx, 3 ; bx = row*8 now
add bx, ax ; bx = row*9 now
add bx, column ; bx = row*8+column now
mov dx, [bx] ;first comparand
inc bx
cmd dx, [bx] ; that's your compare!

Also, you don't use any branching; the cmp instruction is utterly pointless; you waste its result, and xcng is not executed conditionally. Read up on conditional jump commands (jz/jnz etc.).

Also, I seriously hope this is an exercise, not a real project. If it's for real, please reconsider using assembly. For something as trivial as this, assembly is a wrong, wrong choice. Espec. considering how bad you are at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文