title: Sorting strings in 8086 Assembly
body: <p>I want to write a 8086 assembly program that takes 5 strings from the user as an input and then sorts these strings and prints the sorted result as an output. I actually do everything but I have a big problem with the sorting part. I know how to use a for example bubble sort to sort the items in an array that start from a specific address but here I have 5 different strings that are not in the same array. each string has its own address and its own characters. I try to compare last character of each string with each other and then if one is bigger that another one i swap the whole string and then I go on and do that for the whole characters of all string to the first. </p>
  
  <p>For example if our input strings are:</p>
  
  <pre><code>eab    
  abe    
  cbd    
  cda    
  adb
  </code></pre>
  
  <p>I will first sort the last character of every string and I come up with this:</p>
  
  <pre><code>cda    
  eab    
  adb    
  cbd    
  abe
  </code></pre>
  
  <p>Then I will compare them by the middle character:</p>
  
  <pre><code>eab    
  cbd    
  abe    
  cda    
  adb
  </code></pre>
  
  <p>and at last with the first character and everything is sorted:</p>
  
  <pre><code>abe
  adb
  cbd
  cda    
  eab
  </code></pre>
  
  <p>but it is actually what in my mind and I don't have any idea who to implement that for my job.</p>
  
  <pre>
  ; multi-segment executable file template.
  
  data segment 
      data1 db 64,?,64 dup(?)
      data2 db 64,?,64 dup(?)
      data3 db 64,?,64 dup(?)
      data4 db 64,?,64 dup(?)
      data5 db 64,?,64 dup(?)
  
      change db 66 dup(?)
  
      msg db 0ah,0dh,"You enter a wrong option",0ah,0dh,"try again",0ah,0dh,"$" 
      prompt db 0ah,0dh,"Choose an option:",0ah,0dh,"$"
      prompt1 db ".a: Sort in ascending order",0ah,0dh,"$" 
      prompt2 db ".d: Sort in descending order",0ah,0dh,"$"
      prompt3 db ".q: Quit",0ah,0ah,0dh,"$" 
      enter db 0ah,0ah,0dh,"Enter 5 strings:",0ah,0dh,"$"
      pkey db 0ah,0dh,"press any key...$"
  ends
  
  stack segment
      dw   128  dup(0)
  ends
  
  code segment
  main proc far
  ; set segment registers:
      mov ax, data
      mov ds, ax
      mov es, ax
  
  again:
  ; printing the prompts for the user
      lea dx, prompt
      mov ah, 09h
      int 21h   
  
      lea dx, prompt1
      mov ah, 09h
      int 21h
  
      lea dx, prompt2
      mov ah, 09h
      int 21h
  
      lea dx, prompt3
      mov ah, 09h
      int 21h   
  
  ; getting a character from the user as an input
      mov ah, 01h
      int 21h
  
  ; determining which option the user selects    
      cmp al, 'a'
      je ascending
      cmp al, 'd'
      je descending
      cmp al, 'q'
      je quit
  
  ; this is for the time that the user enters a wrong char    
      lea dx, msg
      mov ah, 09h
      int 21h
      jmp again     ; again calling the application to start
  
  ascending:
      call input
      call AscendSort
      jmp again     ; again calling the application to start
  
  descending:
      call input
      call DescendSort
      jmp again     ; again calling the application to start
  
  quit:            
      lea dx, pkey
      mov ah, 9
      int 21h        ; output string at ds:dx
  
      ; wait for any key....    
      mov ah, 1
      int 21h
  
      mov ax, 4c00h ; exit to operating system.
      int 21h  
  main endp
  ;.................................................
  ; this subroutine gets input from user
  input proc
  
      lea dx, enter
      mov ah, 09h
      int 21h
      call newline
  
      mov ah, 0ah
      lea dx, data1
      int 21h      
      call newline
  
      mov ah, 0ah
      lea dx, data2
      int 21h
      call newline
  
      mov ah, 0ah
      lea dx, data3
      int 21h
      call newline
  
      mov ah, 0ah
      lea dx, data4
      int 21h
      call newline
  
      mov ah, 0ah
      lea dx, data2
      int 21h
      call newline
  
      ret 
  input endp
  ;................................................
  ; sorting the strings in the ascending order
  AscendSort proc         
  
      mov si, 65
      lea dx, change
      mov al, data1[si]
      cmp al, data2[si]
      ja l1    
      ?????
  
      ret
  AscendSort endp 
  ;................................................
  ; sorting the strings in the descending order
  DescendSort proc
  
  
  
      ret
  DescendSort endp 
  ;................................................
  ; newline
  newline proc
  
      mov ah, 02h
      mov dl, 0ah
      int 21h
  
      mov dl, 0dh
      int 21h   
  
      ret        
  newline endp    
  ends
  
  end main ; set entry point and stop the assembler.
  </pre>
  
  <p>Any other algorithm for sorting these whole strings also will be appreciated.</p>