1

I would like to ask for help!

The c code is


void bubble_sort(int a[], int n) {
   int i = 0, j = 0, tmp;
   for (i = 0; i < n; i++) {  
       for (j = 0; j < n - i - 1; j++)             
            if (a[j] > a[j + 1]) {  
               tmp = a[j];
               a[j] = a[j + 1];
               a[j + 1] = tmp;
           }
       }
   }
}

My Assembly code so far from the main part is :


main:

    push ebp    //func prologue
    mov ebp, esp

    mov esi, [ebp + 8]  // array address
    mov edi, [ebp + 12]  //array length

    mov eax, 0 //i
    mov ecx, 0 //j    


outerloop:               
        inc eax // i++ 
        cmp eax, edi // comparing i with length
        jge end            



insideloop:                  
           mov edx, edi         
           sub edx, eax          
           dec edx          
           inc ecx      
           cmp ecx, edx    
           je  outerloop
comparison:



I'm lost at this comparison part, I've been doing research the past day about how would I do it, but I simply don't get how would I do this part:

 if (a[j] > a[j + 1]) {  
 tmp = a[j];

I'm really confused as to how I move my j value into the array, then check if everytime a[j] is higher than a[j +1], and then move a[j] into tmp, any help or tips are appreciated, thanks!

Wassssap
  • 11
  • 2
  • What exactly is causing you problem? Indexing the array? Rewrite the code using pointer arithmetic, that should make things more clear. But remember C scales by item size automatically, in assembly you will need to do that by hand. Also read about effective address forms which can be helpful for accessing your array. – Jester May 04 '19 at 15:46
  • Yes, I don't rly get how would I index my array in assembly, and compare them so I can make the array into ascending order – Wassssap May 04 '19 at 15:54
  • Hoist `tmp = a[j];` out of the `if` body, so you have a value in a register you can compare against a memory location. – Peter Cordes May 05 '19 at 00:34

0 Answers0