;----------------------------------------------------------
; Assembly program example to show call to a C function.
; This procedure receives a marks array and class size
; and returns minimum, maximum, and rounded average marks.
;----------------------------------------------------------
.MODEL SMALL
EXTRN   _find_avg:PROC
.CODE
PUBLIC _stats
_stats  PROC    
	push    BP
	mov     BP,SP
	push    SI
	push    DI
	; AX keeps minimum number and DX maximum
	; Marks total is maintained in SI
	mov     BX,[BP+4]    ; BX := marks array address
	mov     AX,[BX]      ; min := first element
	mov     DX,AX        ; max := first element
	xor     SI,SI        ; total := 0
	mov     CX,[BP+6]    ; CX := class size
repeat1:
	mov     DI,[BX]      ; DI := current mark
	; compare and update minimum
	cmp     DI,AX
	ja      skip1
	mov     AX,DI
skip1:
	; compare and update maximum
	cmp     DI,DX
	jb      skip2
	mov     DX,DI
skip2:
	add     SI,DI        ; update marks total
	add     BX,2
	loop    repeat1
	mov     BX,[BP+8]    ; return minimum
	mov     [BX],AX
	mov     BX,[BP+10]   ; return maximum
	mov     [BX],DX
	; now call find_avg C function to compute average
	push    WORD PTR[BP+6] ; push class size
	push    SI           ; push total marks
	call    _find_avg    ; returns average in AX
	add     SP,4         ; clear stack
	mov     BX,[BP+12]   ; return average
	mov     [BX],AX
	pop     DI
	pop     SI
	pop     BP
	ret
_stats  ENDP
	END
