开发者

Using relative values in array sorting ( asm )

开发者 https://www.devze.com 2023-02-15 07:10 出处:网络
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:

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开发者_StackOverflow社区'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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消