;----------------------------------------------------------
; Assembly program for the min_max function -- called from
; the C program in file minmax_c.c. This function finds the
; minimum and maximum of the three integers received by it.
; Uses ARG to simplify offset calculations of arguments.
;----------------------------------------------------------
.MODEL SMALL
.CODE
PUBLIC _min_max
_min_max   PROC    
	ARG     v1:WORD, v2:WORD, v3:WORD,\
		min_ptr:PTR WORD, max_ptr:PTR WORD
	push    BP
	mov     BP,SP
	; AX keeps minimum number and DX maximum
	mov     AX,[v1]      ; get value 1
	mov     DX,[v2]      ; get value 2
	cmp     AX,DX        ; value 1 < value 2?
	jl      skip1        ; if so, do nothing
	xchg    AX,DX        ; else, exchange 
skip1:
	mov     CX,[v3]      ; get value 3
	cmp     CX,AX        ; value 3 < min in AX?
	jl      new_min
	cmp     CX,DX        ; value 3 < max in DX?
	jl      store_result
	mov     DX,CX
	jmp     store_result
new_min:
	mov     AX,CX
store_result:
	mov     BX,[min_ptr] ; BX := &minimum
	mov     [BX],AX
	mov     BX,[max_ptr] ; BX := &maximum
	mov     [BX],DX
	pop     BP
	ret
_min_max   ENDP
	END
