TITLE MASM Template						(main.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc
.data
msg1	BYTE 0dh,0ah,"looping",0
msgMin	BYTE 0dh,0ah,"current min: ",0
msgMax	BYTE 0dh,0ah,"current max: ",0
array	DWORD 2,8,4,16

.code
main PROC
	call Clrscr
	call DumpRegs

	mov EDX, OFFSET array
	mov ECX, LENGTHOF array
	call DumpRegs
	call Midrange
	call DumpRegs
	exit
main ENDP

Midrange PROC
;-----------------------------------
; calc the midrange of an unsigned int array of DWORDS
; IN:   EDX - offset to array
;       ECX - length of array
; OUT:  EAX :EDX - midrange value
;       FLAGS - set according to ADD & DIV rules
;-----------------------------------
	push ECX	;save 4 later	
	push EBX	;save 4 later
	push ESI	;save 4 later

	mov ESI,EDX
	mov EAX,[ESI] ; init min
	mov EBX,[ESI] ; init max

	lp:
		cmp EAX,[ESI]
		jbe s1
		mov EAX,[ESI] ; new min
	s1:
		cmp EBX,[ESI]
		jae s2
		mov EBX,[ESI] ; new max
	s2:
		add ESI,4
		loop lp

	add EAX,EBX 
	mov ECX,2h
	cdq
	div ECX
	pop ESI	;restore ESI
	pop EBX	;restore EBX
	pop ECX	;restore ECX
    ret
Midrange ENDP
END main
