



;COUNTER.ASM--Counts number of occurrences of a character 
;  in a character in a string
;For use with BASIC program
;**********************************************************
pro_nam segment			;defint code segment
;----------------------------------------------------------
counter	proc	far		

	assume cs:pro_nam

;Address of three variables are on the stack in the form

stack_form	struc
	save_bp		dw	?
	save_cs_ip	dw	2 dup(?)
	strdes_addr	dw	?
	char_addr	dw	?
	count_addr	dw	?
stack_form	ends

;MAIN PART OF PROGRAM GOES HERE
	push	bp		;save calling prog BP
	mov	bp,sp		;establish new BP
;get address of string descriptor,then
; length of string and address of string
	mov	bx,[bp].strdes_addr;addr of string desc
	mov	cl,[bx]		;string length in CL
	mov	dx,[bx+1]	;string addr in DX
;get addr of character,then character
	mov	bx,[bp].char_addr;addr of character
	mov	ax,[bx]		;character in AL
;search for char in string,count matches
	mov	bx,dx		;addr of string in BX
	mov	dx,0		;DX holds count
	mov	ch,0		;zero out hi half CX
next_char:
	cmp	al,[bx]		;match?
	jne     not_equal	;no
	inc	dx		;yes,increment count
not_equal:
	inc	bx		;increment pointer
	loop	next_char	;done string?
;put count into address of count
	mov	bx,[bp].count_addr;addr of count in BX
	mov	[bx],dx		;put count in address
;restore base point,return to BASIC
	pop	bp		;restore BP reg
	ret	6		;return,pop 6 bytes

counter	endp			;end procedure
;----------------------------------------------------------
pro_nam ends			;end of code segment
;**********************************************************

	end			;end assembly





