在数组排序中使用相对值 (asm)
我需要对数组进行排序,并将数组中的每一行按升序排序。我似乎进展得不太顺利(惊讶!),因为我不断遇到两个错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下几行是有问题的:
与 HLL 不同,汇编不允许用任意表达式代替常量或变量。这就是 HLL 最初被发明的原因。使用之前计算寄存器中的偏移量:
此外,您不使用任何分支; cmp 指令完全没有意义;你浪费了它的结果,并且 xcng 不会有条件地执行。阅读条件跳转命令(jz/jnz 等)。
另外,我真心希望这是一次练习,而不是一个真正的项目。如果是真的,请重新考虑使用汇编。对于这样一件微不足道的事情,组装是一个错误、错误的选择。特别规格。考虑到你在这方面有多糟糕。
The following lines are problematic:
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:
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.