;NAME_SORT--G6_17
;**********************************************************
stack	segment
	dw	32 dup(?)
stack	ends
;**********************************************************
datasg  segment			;define data segment
  namepar	label	byte	;name parameter list:
  maxnlen	db	21	;max. length
  namelen	db	?	;no. chars entered
  namefld	db	21 dup(?) ;name
  crlf		db	13,10,'$'
  endaddr	dw	?
  messg1	db	'Name?','$'
  namectr	db	0
  nametab	db	30 dup(20 dup(' ')) ;name table
  namesav	db	20 dup(?),13,10,'$'
  swapped	db	0
datasg  ends
;**********************************************************
codesg  segment			;defint code segment
;----------------------------------------------------------
begin	proc	far		;main part of program

	assume cs:codesg,ds:datasg,ss:stack,es:datasg

;set up stack for return
	push	ds		;save old data segment
	sub	ax,ax		;put zero in AX	
	push	ax		;save it on stack

;set DS and ES register to current data segment
	mov	ax,datasg	;data segment addr
	mov	ds,ax	  	;  into DS register
  	mov	es,ax		;    and ES register

;MAIN PART OF PROGRAM GOES HERE
	cld	
	lea	di,nametab	
	call	q10clr		;clear screen
	call	q20curs		;set cursor
a20loop:
	call	b10read		;accept name
	cmp	namelen,0	;any more names?
	jz	a30		;no,go to sort
	cmp	namectr,30	;30 names entered?
	je	a30		;yes,go to sort
	call	d10stor		;store entered name in table
	jmp	a20loop		;end of input
a30:	call	q10clr		;clear screen
	call	q20curs		;  &set cursor
	cmp	namectr,1	;one or no name entered?
	jbe	a40		;yes,exit
	call	g10sort		;sort stored names
	call	k10disp		;display sorted names
a40:	ret			;terminate

begin	endp			;end of main part of program
;----------------------------------------------------------
;	Accept name as input:

b10read	proc	near
	mov	ah,09
	lea	dx,messg1	;display prompt
	int	21h
	mov	ah,0ah
	lea	dx,namepar	;accept name
	int	21h
	mov	ah,09
	lea	dx,crlf		;return/linefeed
	int	21h
;
	mov	bh,0		;clear chars after name
	mov	bl,namelen	;get count of chars
	mov	cx,21
	sub	cx,bx		;calc remaining length
b20:	mov	namefld[bx],20h	;set to blank
	inc	bx
	loop	b20
	ret

b10read	endp
;-----------------------------------------------------------
; 	Store name in table:

d10stor	proc	near
	inc	namectr		;add to number of name
	cld
	lea	si,namefld
	mov	cx,10
	rep	movsw		;move name to table
	ret

d10stor	endp
;-----------------------------------------------------------
;	Sort names in table:

g10sort	proc	near
	sub	di,40		;set up stop address
	mov	endaddr,di
g20:	mov	swapped,0	
	lea	si,nametab	;set up start of table
g30:	mov	cx,20		;length of compare
	mov	di,si	
	add	di,20		;next name for compare
	mov	ax,di
	mov	bx,si
	repe	cmpsb		;compare name to next
	jbe	g40		;no exchange
	call	h10xchg		;exchange
g40:	mov	si,ax
	cmp	si,endaddr	;end of table?
	jbe	g30		;no,continue
	cmp	swapped,0	;any swaps?
	jnz	g20		;yes,continue
	ret			;no,end of sort

g10sort	endp
;-----------------------------------------------------------
;	Exchange table entries:

h10xchg	proc	near
	mov	cx,10
	lea	di,namesav
	mov	si,bx
	rep	movsw		;move lower item to save
;
	mov	cx,10
	mov	di,bx
	rep	movsw		;move higher item to lower
;
	mov	cx,10
	lea	si,namesav
	rep	movsw		;move save to higher item
	mov	swapped,1	;signal that exchange made
	ret

h10xchg	endp
;-----------------------------------------------------------
;	Display sorted names:

k10disp	proc	near
	lea	si,nametab
k20:	lea	di,namesav	;initialize start of table
	mov	cx,10
	rep	movsw
	mov	ah,9
	lea	dx,namesav
	int	21h		;display
	dec	namectr		;is this last one?
	jnz	k20		;no,loop
	ret			;yes,exit

k10disp	endp
;-----------------------------------------------------------
;	Clear screen:

q10clr	proc 	near
	mov	ax,0600h
	mov	bh,61h		;color(07 for BW)
	sub	cx,cx
	mov	dx,184fh
	int	10h
	ret

q10clr	endp
;-----------------------------------------------------------
;	Set cursor:

q20curs	proc	near
	mov	ah,2
	sub	bh,bh
	sub	dx,dx		;set cursor to 0,0
	int	10h
	ret

q20curs	endp
;-----------------------------------------------------------
codesg	ends
;***********************************************************
	end	begin


