TASM program help : Sorting

Discussion in 'Assembly Language Programming (ALP) Forum' started by Nagasaki, Oct 12, 2011.

  1. Nagasaki

    Nagasaki New Member

    Joined:
    Oct 12, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi. I am new to programming, and I am required to make a sorting program. Here is the problem

    Create an AL program that will input a string 10 characters long then print the
    characters in decending or accending order.The program will also prompt the user
    if he/she wants to repeat the program, if the answer is 'Y' repeat the program if
    the answer is 'N' display the word "Thank You then exit the program.


    Example:
    Enter String :GDFEWPSHBA

    Choice
    [1]Accending
    [2]Decending

    Enter Choice :1

    Accending Order :ABDEFGHPSW

    Enter Another[Y/N] :N

    Thank You !!!

    So, here is my plan... I will first get each character of the 10-letter line the user inputted, and allocate it into an array.
    Then, sort it using any of the sorts (I used bubble sort for the program), then show it to the user and prompt him if he wants to continue.

    I am making a code for this using several references, and I made a program.The problem is...
    when I start running the program, DOS crashes and the program terminates!!!

    Here is my code...

    Code:
    .model small
    .stack 100h
    .data
       prompt db 0dh,0ah, "Enter String          : $"
       choice db 0dh,0ah,0dh,0ah,"Choice",0dh,0ah,"[1]Ascending",0dh,0ah,"[2]Descending",0dh,0ah,0dh,0ah,"Enter Choice          :$"
       ascending db 0dh,0ah,0dh,0ah,"Ascending Order      :$"
       descending db 0dh,0ah,0dh,0ah,"Descending Order     :$"
       decision db 0dh,0ah,0dh,0ah,"Enter Another[Y/N]   :$"
       thank db 0dh,0ah,0dh,0ah,"Thank You !!!"
       array db 10 dup (0)
    .code
     main proc
       mov ax, @data
       mov ds, ax
    
       @start:
       mov ax, offset prompt
    
       call print_array
    
       mov ax, offset array
    
       call read_array
    
       lea dx, choice
       mov ah, 9
       int 21h
    
       mov ah, 1
       int 21h
    
       lea si, array
       cmp dl, 1
         call descend
    
         call ascend
    
       mov ax, offset array
    
       call print_array
    
       lea dx, decision
       mov ah, 9
       int 21h
    
       mov ah, 1
       int 21h
    
       mov bl, al
       and bl, 0dfh
    
       cmp bl, "N"
       jmp @start
    
       mov ah, 9
       lea dx, thank
       int 21h
    
       mov ah, 4ch
       int 21h
     main endp
    
     read_array proc
       push ax
       push bx
       push cx
       push dx
    
       mov bx, ax
       call getc
       mov byte ptr[bx], al
       @get_loop:
         cmp al, 0dh
         je @get_fin
         inc bx
         call getc
         mov byte ptr[bx], al
         jmp @put_loop
       @get_fin:
         pop dx
         pop cx
         pop bx
         pop ax
         
       ret
     read_array endp
    
     ascend proc
       push ax
       push bx
       push cx
       push dx
       push di
    
       lea dx, ascending
       mov ah, 9
       int 21h
    
       mov ax, si
       mov cx, bx
       dec cx
    
       @outer_loop:
         mov bx, cx
         mov si, ax
         mov di, ax
         inc di
    
         @inner_loop:
           mov dl, [si]
    
           cmp dl, [di]
           jng @skip
    
           xchg dl, [di]
           mov [si], dl
    
           @skip:
             inc si
             inc di
    
             dec bx
         jnz @inner_loop
       loop @outer_loop
         pop di
         pop dx
         pop cx
         pop bx
         pop ax
       ret
     ascend endp
    
     descend proc
       push ax
       push bx
       push cx
       push dx
       push di
    
       lea dx, descending
       mov ah, 9
       int 21h
    
       mov ax, si
       mov cx, bx
       dec cx
    
       @outer_loop2:
         mov bx, cx
         mov si, ax
         mov di, ax
         inc di
    
         @inner_loop2:
           mov dl, [si]
    
           cmp dl, [di]
           jng @skip2
    
           xchg dl, [di]
           mov [si], dl
    
           @skip2:
             inc si
             inc di
    
             dec bx
         jnz @inner_loop2
       loop @outer_loop2
         pop di
         pop dx
         pop cx
         pop bx
         pop ax
       ret
     descend endp
    
     print_array proc
       push ax
       push bx
       push cx
       push dx
    
       mov bx, ax
       mov al, byte ptr[bx]
       @put_loop:
         cmp al, 0
         je @put_fin
         call putc
         inc bx
         mov al, byte ptr[bx]
         jmp @put_loop
       @put_fin:
         pop dx
         pop cx
         pop bx
         pop ax
       ret
     print_array endp
    
     putc proc
       push ax
       push bx
       push cx
       push dx
    
       mov dl, al
       mov ah, 2
       int 21h
    
       pop dx
       pop cx
       pop bx
       pop ax
       ret
     putc endp
    
     getc proc
       push ax
       push bx
       push cx
       push dx
    
       int 21h
    
       pop dx
       pop cx
       pop bx
       pop ax
     ret
     getc endp  
    end main
    Can you help me in modifying this program? Any suggestions? Please help me...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice