Reputation: 43
Say I have an array defined by:
array DW 1,1,3,0,3,3,4,4,-1
The array is terminated by -1, how would I be able to sort the array in pairs of descending order based on the first number in the pair (if first number is the same then it's sorted by the second number) as such:
4, 4; 3, 3; 3, 0; 1, 1;
Upvotes: 0
Views: 395
Reputation: 39166
array DW 1,1, 3,0, 3,3, 4,4, -1
The first number in each pair of word-sized numbers is the most significant for your task.
Each of these pairs can be seen as a dword, but on x86 (little endian) the first word will be the least significant. That's just the opposite of what you need. What if you temporarily swapped the words? Then you could sort the array as normal dwords.
This could be the swap procedure (32-bit):
Swap:
mov ebx, array
jmp First
Next:
rol dword [ebx], 16
add ebx, 4
First:
cmp word [ebx], -1
jne Next
ret
This could be the swap procedure (16-bit):
Swap:
mov bx, array
jmp First
Next:
xchg ax, [bx+2]
mov [bx], ax
add bx, 4
First:
mov ax, [bx]
cmp ax, -1
jne Next
ret
A solution where you do these pre-swap and post-swap operations within the dword sorting algorithm would be just as easy.
Upvotes: 1