mysort
page 56, 132
title MYSORT
subttl Sort example
.model small
.586
.listif
comment \
How to use : MYSORT num1 num2 num3 ,,,
all of num must be positive decimal integer less than 65536
\
s_type equ 1 ; 0 for bubble sort, 1 for shell sort
cmdline equ 81h ; PSP command line
CR equ 0dh
LF equ 0ah
HTAB equ 09h
SPACE equ ' '
data segment ; Data segment
errm db CR,LF
db 'Bad input'
crlf db CR,LF,'$'
message db CR,LF
db 'Sorted result',CR,LF,'$'
displn db ' '
result db 5 dup (?),'$'
indat dw 100 dup (?)
endat label word
data ends
code segment
assume cs:code, ds:data, ss:code
sizak: mov ax,data ; set data segment
mov ds,ax
mov ax,code
cli ; disable interrupt
mov ss,ax
mov sp,offset prend + 512
sti ; back on interrupt
mov di,offset indat
mov si,cmdline ; SI to command line
getin: call clparse ; Parse one term from the command line into AX
jnb goodin
errt: mov dx,offset errm ; error in command line
mov ah,9
int 21h
mov ax,4c00h ; exit dos
int 21h
goodin: jz endin ; if end of command line
mov [di],ax ; store term
inc di
inc di
cmp di,offset endat ; buffer is full
je errt
jmp getin ; get next term
endin: mov si,offset indat
cmp di,si ; error if no valid data
jz errt
if s_type
shell1: xor dl, dl
mov bx, si
shell2: inc bx
inc bx
cmp bx, di
jz shell4
cld
mov ax, [si]
mov cx, [bx]
cmp ax, cx
jbe shell3
mov [si], cx
mov [bx], ax
mov dl, 01h
shell3: inc si
inc si
jmp shell2
shell4: mov si, offset indat
or dl, dl
jnz shell1
else
; Bubble sort
bubbl1: mov bx,si
bubbl2: inc bx
inc bx
cmp bx,di ; end of array ?
jz bubbl3
mov ax,[si] ; compare two terms
mov cx,[bx]
cmp ax,cx
jbe bubbl2 ; if in order
mov [si],cx ; if out of order, exchange terms
mov [bx],ax
jmp bubbl2
bubbl3: inc si
inc si
cmp si,di
jnz bubbl1 ; end of bubble sort ?
mov dx,offset message
mov ah,9
int 21h
endif
mov si,offset indat ; display sorted result
dispit: call dispwd
cmp si,di
jnz dispit
mov dx,offset crlf
mov ah,9
int 21h
mov ax,4c00h
int 21h
clparse proc near ; parse term
xor ax,ax
xor bh,bh
clp1: mov bl,es:[si] ; discard leading blank chararcters
cmp bl,SPACE
jz clp2
cmp bl,HTAB ; Tab ?
jnz clp3
clp2: inc si
jmp clp1
clp3: cmp bl,CR ; if CR, end of line
jz clpr
clp4: sub bl,'0' ; convert ASCII numeric into hexa in AX
jb clpe
cmp bl,10
jnb clpe
mov cx,ax
add ax,ax
jc clpr
add ax,ax
jc clpr
add ax,cx
jc clpr
add ax,ax
jc clpr
add ax,bx
jc clpr
inc si ; end of numeric term ?
mov bl,es:[si]
cmp bl,SPACE
jz clp5
cmp bl,HTAB
jz clp5
cmp bl,CR
jnz clp4
clp5: or bl,bl
clpr: ret
clpe: stc
ret
clparse endp
dispwd proc near ; display 16 bit binary
mov bx,[si]
inc si
inc si
xor ch,ch ; clear 5 digit packed BCD buffer in CH:DH:DL
xor dx,dx
mov cl,16 ; 16 bit binary
dispw1: add bx,bx ; convert 16 bit binary in BX
mov al,dl ; into 5 digit packed BCD in CH:DH:DL
adc al,al
daa
mov dl,al
mov al,dh
adc al,al
daa
mov dh,al
mov al,ch
adc al,al
daa
mov ch,al
dec cl
jnz dispw1
mov bx,offset result ; convert 4 digit packed BCD in CH:DH:DL
mov al,ch ; into 5 ASCII code in result
call lownb ; with leading zero deletion
mov al,dh
call highnb
mov al,dh
call lownb
mov al,dl
call highnb
mov cl,1
mov al,dl
call lownb
mov dx,offset displn ; display converted result
mov ah,9
int 21h
ret
dispwd endp
highnb proc near
shr al,1
shr al,1
shr al,1
shr al,1
lownb proc near
and al,0fh
or cl,al
jz lownb2
or al,'0'
lownb1: mov [bx],al
inc bx
ret
lownb2: mov al,SPACE
jmp lownb1
lownb endp
highnb endp
even
prend label byte ; end of program, bottom of stack
code ends
end sizak
History
Last edited on 05/15/2007 11:16 by persona
Comments (0)