Jump to content

TASM program help : Sorting

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Nagasaki

Nagasaki

    Newbie

  • Members
  • Pip
  • 1 posts
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...
.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...

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
It would be helpful if you try some debugging tactics and figure out, exactly, where the error happens.


What I would usually do if I get such an error, is, let's say here's the (rough) code:
do action1 

call function1 

do action2 

call function2 

do action3 

If the program fails somewhere there, I'd usually try editing the code and inserting a debug piece of code:
do action1 

print "debug 1" 

call function1 

do action2 

call function2 

do action3 

If "debug 1" is on the screen, after the program fails, then I know that the first part of the code has been successfully executed, in which case I move the debug piece of code forward:
do action1 

call function1 

print "debug 1" 

do action2 

call function2 

do action3 
Now if "debug 1" is printed before failing, then I know that both of those parts of the code don't cause the error. And I continue until I find where the error occurs.

If I keep moving the debug piece of code forward, I would eventually not get the "debug 1" output text, and that's when I can stop and focus on the code just before the part that has the print statement (or piece of code).

Meaning, if the following does print "debug 1" :
do action1 

call function1 

do action2 

print "debug 1" 

call function2 

do action3 
and the following doesn't :
do action1 

call function1 

do action2 

call function2 

print "debug 1" 

do action3 
, then I know that there must be something wrong with function2(), so I now go into function2() and try to figure out what the problem is from there.

Just giving you some advice, from my own experience. :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users