title: Selection Sort on x86 Assembly
body: <p>This is a homework if anyone's curious.</p>
  
  <p>The task is to have a C program create a 2D array and pass that array into assembly which it should be sorted using selection sort. </p>
  
  <p>The error I'm having when trying to run it is segmentation fault. I've tried putting</p>
  
  <pre><code>push tesst
  push sout
  call printf
  </code></pre>
  
  <p>to print out "test" on the terminal window but I couldn't figure out where it's coming from. I also tried putting it right after "enter 0,0" and I still havent gotten a print out of test. So i'm completely out of ideas as to where i'm getting the segmentation fault. </p>
  
  <p>Any assistance would be very much appreciated. :( Due tomorrow night and i've been stuck at this for a couple of days. Someone shed some light please</p>
  
  <p>-Chris</p>
  
  <p>The codes will be below</p>
  
  <p>my c program</p>
  
  <blockquote>
  <pre><code>#include &lt;stdio.h&gt;
  
  int ssort(char * hi[], int x, int y);
  
  int main(){
  
    int i, j;
  
  
    char hi[3][5] = {"Yoshi", "Annie", "Chris"};
  
    printf("Display Unsorted Strings\n");
    for(i = 0; i &lt; 3; i++){
      for(j = 0; j &lt; 5; j++){
        printf("%c", hi[i][j]);
      }
      printf("\n");
    }
    printf("It Got Here\n");
    ssort((char*)hi, 5, 3);
    printf("Sorted\n");
    for(i = 0; i &lt; 3; i++){
      for(j = 0; j &lt; 5; j++){
        printf("%c", hi[i][j]);
      }
      printf("\n");
    }
  }
  </code></pre>
  </blockquote>
  
  <p>And my Assembly Code</p>
  
  <blockquote>
  <pre><code>extern    printf
  
  segment   .data
  
  tesst db  't','e','s','t',0
  sout  db  "%s", 10, 0
  
  segment   .text
  
    global  ssort
  
  ssort:    
    enter   0,0
  
  ;;; for(i = 0; i &lt; namecount; i++){
  ;;;   for(j = 1; j &lt; namecount; j++){
  ;;;       if(array[i] &lt; array[j])
  ;;;           do nothing
  ;;;       else
  ;;;           swap
  ;;;   }
  ;;; }
  
  
    mov esi,    [ebp+8] 
    mov edi,    esi
    add edi,    [ebp+8]
    mov ecx,    0   ;i
    mov edx,    0
    add edx,    1   ;j
    ;; [ebp+16] = namecount
  
  iloop:
    push    esi
    cmp ecx,    [ebp+16]
    je  done_sorting
  jloop:
    cmp edx,    [ebp+16]
    je  j_done
  
  ;;; compare here now
  compare:
    mov al, [esi]
    mov bl, [edi]
    cmp al, bl
    jg  alisbigger
    jl  blisbigger
    inc esi
    inc edi
    jmp compare
  alisbigger:   
    jmp swap
  blisbigger:   
    jmp done
  
  swap:
    mov ebx,    0
    mov ebx,    [ebp+12]
  swap_loop:
    dec ebx
  
    mov al, [esi]
    mov bl, [edi]
    mov [esi],  bl
    mov [edi],  al
  
    inc esi
    inc edi
  
    cmp ebx,    0
    je  done
    jmp swap_loop
  
  done:
    inc edx
    jmp jloop
  
  j_done:
    add ecx, 1
    add esi, [ebp+12]
    mov edx, 1
    jmp iloop   
  
  
  done_sorting:
    leave
    ret
  </code></pre>
  </blockquote>