title: How to sort an array using stack in assembly
body: <p>I want to sort an array while using the stack:</p>
  <p>so first things first I pushed it all into the stack.</p>
  <p>then I'm locating the minimum element in the stack, swapping it with the top element popping it out and move it back to the array.</p>
  <p>but something isn't right and about halfway there it got messed up (DI is one index below than what it should really be)</p>
  <pre><code>data segment
     a db 5h,12h,6h,4h,9h
     len equ $-a
     loop_counter db len
     min_index dw 0
     min db ?
  ends
  
  stack segment
      dw   128  dup(0)   
  ends
  
  code segment ;TODO: Locate min in stack xchg it with top element copy to array and pop it :|    
  start:
      mov ax, data
      mov ds, ax
      mov bx,0
      mov cx,0
      mov cl,loop_counter
  push_arr_to_stack:
  
      mov al,a[bx]
      push ax
      inc bx      
      loop push_arr_to_stack
    
      mov bx,len-1
      mov ax,0
      mov cl,loop_counter
      mov bp,sp
      mov ax,[bp]
      mov bx,[bp]
      mov di,0 
      mov dx,0
      mov si,2
      mov di_check,len-loop_counter 
      
  find_min_to_arr:
  
      cmp bx,ax
      jb  new_min
      mov bx,[bp+si]
      add si,2  
      
      loop find_min_to_arr
      mov min,al
      
      xchg a[di],al
      jmp pop_min
      
  new_min:
         mov ax,bx
         mov bx,[bp+si]
         mov min_index,si
         sub min_index,2
         add si,2
         dec cx
         jmp find_min_to_arr
          
  pop_min:
          mov dx,[bp]
          xchg di,min_index
          mov [bp+di],dx
          xchg di,min_index
          pop dx
          mov dx,0
          inc di
          mov si,0
          cmp di,len
          je b
          dec loop_counter
          mov cl,loop_counter
          jmp find_min_to_arr
          
  b:              
      mov ax, 4c00h ; exit to operating system.
      int 21h
    
  ends
  end start 
  </code></pre>