0

I am creating a BubbleSort that will be dynamic with any size array. I have figured out the first iteration through the array. How would I rest the counter to re-iterate through the list enough times to sort the entire array. Below is what I have so far:

    segment .data
array   dd  14, 10, 23, 45, 17, 9, 54, 22, 1, 76
size    dd  10

    segment .text
    global main
main:
xor ebx, ebx
xor eax, eax
xor ecx, ecx
mov ebx, 0  ;counter

compare:
mov eax, dword[ebx + array]
cmp eax, dword[ebx + array + 4]
jg swap
jl notswap

swap:
mov ecx, dword[ebx + array + 4]
mov dword[ebx + array + 4], eax
mov [ebx + array], ecx

notswap:
add ebx, 4  
jmp compare  

end: 
    ret
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Defqon
  • 117
  • 11
  • An idea was to decrement my size and check for 0 but I do not know how to achieve that in assembly – Defqon Mar 06 '20 at 01:33
  • Your `jg swap` is useless; just let `jl notswap` fall through or not like a normal person. (And use `jle`; you don't need to swap on equal). – Peter Cordes Mar 06 '20 at 02:04
  • 1
    [Bubble sort on array on Assembly Language](https://stackoverflow.com/a/34126074) is a 16-bit bubble sort of byte elements; adapting for your case should be easy. Also [Sort an array in assembly?](https://stackoverflow.com/q/43103359) has code with some bugfixes in comments. Basically you just need an outer loop around the inner loop, with a separate register for the counter. – Peter Cordes Mar 06 '20 at 02:14

0 Answers0