-2

I wrote some code for bubble sort in c++. Now I want to write it in flat assembler but it seems harder than I expected. Can someone help or provide relevant sources? Here is my c++ code:

int[] arr= {5,8,7,4,1,9}
int n =6    //array size

for (i = 0; i < n-1; i++){   
// Last i elements are already in place
             for (j = 0; j < n-i-1; j++){
                            if (arr[j] > arr[j+1]){
                                             int x=arr[j];
                                            arr[j] = arra[j+1];
                                             arr[j+1] = x
                            }
}
}

For (int i=0; I < n ;i++){
            Cout << “  ”+arr[i]+ “  ” ;
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 2
    There are several existing NASM BubbleSort implementations, and FASM syntax is close enough to NASM. (Unfortunately the first page of google results for `site:stackoverflow.com nasm bubble sort` is either unanswered, or only in comments.) But anyway, we're not going to do your homework for you; you need to at least make an attempt *in asm*; you can start by translating each C statement one at a time, like a debug-mode compiler would. Otherwise you might as well just use a C compiler. – Peter Cordes Jun 04 '21 at 13:45

1 Answers1

0

The (now classic) snippet for bubble sort is:

; by Andrew Howe

outerloop:  lea     ebx,[edi+ecx*4]
            mov     eax,[edi]
cmploop:    sub     ebx,4
            cmp     eax,[ebx]
            jle     notyet
            xchg    eax,[ebx]
notyet:     cmp     ebx,edi
            jnz     cmploop
            stosd
            loop    outerloop

You'd just define your array of data and pass the address in EDI, and the number of items in the array in ECX.

I first saw it at AZillionMonkeys.

bitRAKE
  • 391
  • 2
  • 9
  • `xchg` is good for code size, but very slow. (Implicit `lock` prefix, so it's an atomic RMW with a full memory barrier.) Also, it's weird to use EBX but not EDX; in most calling conventions, EBX is call-preserved but EDX can be clobbered. – Peter Cordes Jun 15 '21 at 10:01
  • This looks optimized for code-size over speed, mostly. I think it's mostly the same algorithm as my codegolf.SE answer on [Sort an Integer List](https://codegolf.stackexchange.com/a/149038) (but that version is more heavily golfed for size at the expense of speed, e.g. uses `scasd` in the inner loop, and copies ECX to count-down instead of using an end-pointer). – Peter Cordes Jun 15 '21 at 10:04
  • 2
    Anyway, **this algorithm is JumpDown Sort, not Bubble Sort** - it swaps every time you find a new min, not necessarily adjacent elements. See [Bubble Sort: An Archaeological Algorithmic Analysis](https://users.cs.duke.edu/~ola/papers/bubble.pdf). That's why one exchange between reg and memory in the inner loop can work, and why a store after the inner loop (`stosd`) is needed. It's a hybrid of Selection Sort and Bubble Sort. – Peter Cordes Jun 15 '21 at 10:05
  • Thanks for the clarification - I've been thinking this was bubble sort for 20+ years. I've never actually used it in software since quick sort is not that large code-wise, but have always enjoyed the code density of the creative mind. – bitRAKE Jun 19 '21 at 05:33