.model small
.stack 100h
if1
include MACROS.H
endif
.data 
msg_course DB 0ah, 0dh, "Select Sort Algorithm $" 
msg_myname DB 0ah, 0dh, "Nothing New Here $"
msg_uc DB 0ah, 0dh, "Doing upper case:$"
msg_sort DB 0ah, 0dh, "Doing sort:$"
CRLF DB 0dh, 0ah,'$'
string1 DB 'abcde$'
string2 DB 5 dup (?),'$'
array DB 55,52,51,53,54, '$'
empty DB ?
.code

MAIN proc
mov AX, @data
mov DS, AX
mov ES, AX

; print header info
mov AH,9
lea DX, msg_course
int 21h
lea DX, msg_myname
int 21h
lea DX, msg_uc
int 21h

;--------------------------------------------------------
; String Stuff
;--------------------------------------------------------

; load string
lea DX, CRLF
int 21h
lea DX, string1
int 21h

; do the string thing
cld
mov CX,5 ; set up loop
lea SI,string1
lea DI,string2
STRING_LOOP:
	lodsb ; puts letter from string1 in AL
	sub AL, 20h ; subtract 26d or 20h to get the upper case value
	stosb ; puts letter from AL in string2
	loop STRING_LOOP

mov AH,9
lea DX, CRLF
int 21h
;mov DX, DI
lea DX,string2
int 21h

;--------------------------------------------------------
; Array Stuff
;--------------------------------------------------------

; print current array
lea DX, msg_sort
int 21h
lea DX, CRLF
int 21h
lea DX, array
int 21h

; do the sort
lea SI, array
mov BX,5
call SELECT

; print sort result
mov AH,9
lea DX, CRLF
int 21h
mov DX, SI
int 21h


QUT
MAIN endp

; -------------------------------------------------------------
; SELECT
; -------------------------------------------------------------

SELECT proc
; sorts a byte array by the select sort method
; INPUT: SI = array offset address
;        BX = number of elements
; OUTPUT: SI = offset of sorted array
; uses SWAP
	push BX
	push CX
	push DX
	push SI

	dec BX		; n-1
	je END_SORT
	mov DX,SI
SORT_LOOP:
	mov SI,DX   ; si points to array
	mov CX,BX	; # of comparisons to make
	mov DI,SI	; di points to largest element
	mov AL,[DI]	; al has largest element
FIND_BIG:
	inc SI		; si points to next element
	cmp [SI],AL	; get value
	jng NEXT	; is new element greater than largest?  if not, loop
	mov DI,SI	; if yes, move to di
	mov AL,[DI]	; al has largest element again
NEXT:
	loop FIND_BIG	; loop until done comparing all elements
	call SWAP		; swap 'em
	dec BX			; n-1
	jne SORT_LOOP	; n > 0 loop
END_SORT:
	pop SI
	pop DX
	pop CX
	pop BX
	ret
SELECT endp

SWAP proc
; swaps two array elements
; INPUT:  SI = one element
;         DI = other element
; OUTPUT: SI, DI exchanged
	push AX
	mov AL,[SI]   ; get a[i]
	xchg AL, [DI] ; place in a[j]
	mov [SI], AL  ; mov a[j] to a[i]
	pop AX
	ret
SWAP endp

end main