Click to See Complete Forum and Search --> : What's wrong with my program?


Nagasaki_01
October 11th, 2011, 08:59 PM
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...

Eri523
October 12th, 2011, 12:13 PM
I didn't examine your program down to every single line, but at least these issues hit my eye:

All your display strings are $-terminated (except for thank where you apparently simply forgot that), yet you're attempting to output prompt using your print_array routine that expects a null-terminated string. You could easily output that with INT 21h, function 09h, what you're already doing with at least some of the others.
Your array isn't null-terminated either, so print_array won't even work with that.
A stack of merely 256 bytes perhaps is a bit small. Why so stingy?

This list is definitely incomplete and I don't think any of these issues except for the small stack size could cause the program to crash entirely.

I have some notion you didn't try to use the debugger to inspect your program and see what it's actually doing. I strongly recommend doing that now. That way you'd have been able to find out at least two of the bugs I pointed out above, and probably at least most of the others.

Ah, and... Welcome to CodeGuru! :)