2
votes

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.

For example if our input strings are:

eab    
abe    
cbd    
cda    
adb

I will first sort the last character of every string and I come up with this:

cda    
eab    
adb    
cbd    
abe

Then I will compare them by the middle character:

eab    
cbd    
abe    
cda    
adb

and at last with the first character and everything is sorted:

abe
adb
cbd
cda    
eab

but it is actually what in my mind and I don't have any idea who to implement that for my job.

; 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.

Any other algorithm for sorting these whole strings also will be appreciated.

1
Your choice of radix (a.k.a. distribution) sort is fine for strings that are short and have a fixed length. In general, sorting is a complex enough problem that I would write a version in a high-level language first to get my algorithm straight and then port that to assembler.500 - Internal Server Error
Will you always sort exactly 5 strings, never more and never less? Then you should investigate sort networks. These are faster and simpler than general sorting algorithms.Dour High Arch
Yeah, for input it is just 5 strings that need to be sorted, no more and no less.Haiku
I am not insisting on this radix sort and I know how to implement this sorting in Java for example but in Assembly everything turns to a nightmare. Any other easier algorithm will be good.Haiku

1 Answers

3
votes

I actually figure out the answer myself, I use string commands to compare the strings 2 by 2 with each other to see if they're bigger, smaller or equal. Something like the code below in the specific macro that takes two strings to check them and do the required operation like swapping the strings to make them sorted:

check macro a, b
    local next, finish
    cld
    mov cx, 64  ; the size of our buffer that saves the string
    mov si, a
    mov di, b

    repe cmpsb  ; comparing two strings with each other
    ja next
    jmp finish

next:
    ; swaping our strings if needed
    mov cx, 64
    mov si, a
    lea di, change
    rep movsb 

    mov cx, 64
    mov si, b
    mov di, a
    rep movsb

    mov cx, 64
    lea si, change
    mov di, b
    rep movsb 

finish:

endm