;----------------------------------------------------------
; 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.
; Uses TASM's extended procedure call instruction.
;----------------------------------------------------------
.MODEL SMALL
EXTRN C  find_avg:PROC
.CODE
PUBLIC C stats
stats   PROC    
	ARG     marks:PTR WORD, class_size:WORD, min:PTR WORD,\
		max:PTR WORD, avg:PTR WORD
	push    BP
	mov     BP,SP
	push    SI
	push    DI
	; AX keeps minimum number and DX maximum
	; Marks total is maintained in SI
	mov     BX,[marks]   ; BX := marks array address
	mov     AX,[BX]      ; min := first element
	mov     DX,AX        ; max := first element
	xor     SI,SI        ; total := 0
	mov     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,[min]     ; return minimum
	mov     [BX],AX
	mov     BX,[max]     ; return maximum
	mov     [BX],DX
	; now call find_avg C function to compute average
	; returns the rounded average value in AX
	call    find_avg C, SI, class_size
	mov     BX,[avg]     ; return average
	mov     [BX],AX
	pop     DI
	pop     SI
	pop     BP
	ret
stats   ENDP
	ENDs
