;average numbers in an array if they are negative
; note:  does not create the numbers in the array!
.model small
start:  mov     ax, @data
	mov     ds, ax
	mov     es, ax

	mov     si, 0
mo:     mov     bl, nums[si]    ; get the input
	test    bl, 10000000b   ;look for negative sign
	jz      check
	inc     count
;Yes!  It IS a negative number, therefore count it and add it
; into the total
; First, however, you must do a sign extension from bl to bx
;  this is done by putting all ONES into bh
	mov     bh, 0FFh
	add     total, bx       ;add to the accumulator
;  could check the overflow flag to see if the number is out of bounds
;       do:   JO <label>

check:  add     si, 1
	cmp     si, 10
	jb      mo
; added all negative numbers -- time for the average

	mov     ax, total
	mov     bl, count
	idiv    bl              ;  al := ax div bl
	mov     avg, al         ;move out the output

	mov     ah, 4ch         ;return to dos
	mov     al, 00          ;error level 0 (no error)
	int     21h             ;dos function call

nums    db  -128,0,100,50,-50,80,-80,90,100,-1,50
count   db 0
total   dw 0
avg     db 0

.stack 100h

	end start