;
;
; File registers
;
		org	08h
al		ds	1			;ax
ah		ds	1
bl		ds	1			;bx
bh		ds	1
cl		ds	1			;cx
ch		ds	1
		org	0h
;
;
; Multiply cx by bx
; product in ax:cx
;
function_mul	mov	fsr,#-10h
		clr	al
		clr	ah

:loop		clc
		jnb	cl.0,:not
		call	add_bx_ax
:not		rr	ah
		rr	al
		rr	ch
		rr	cl
		ijnz	fsr,:loop
		ret
;
;
; Divide ax by bx
; quotient in cx, remainder in ax
;
function_div	mov	fsr,#-10h
		clr	cl
		clr	ch

:position	jb	bh.7,:loop
		rl	bl
		rl	bh
		ijnz	fsr,:position
		ret

:loop		dec	fsr
		call	neg_bx
		call	add_bx_ax
		call	neg_bx
		rl	cl
		rl	ch
		sb	cl.0
		call	add_bx_ax
		clc
		rr	bh
		rr	bl
		jb	fsr.4,:loop
		ret
;
;
; Negate/Increment bx, make z
;
neg_bx		not	bl
		not	bh
inc_bx		inc	bl
		snz
		inc	bh
		ret
;
;
; Add bx to ax, make c and z
;
add_bx_ax	add	al,bl			;add low bytes, make c
		mov	w,bh			;get bh into w
		snc				;if no overflow, add w to ah
		incsz	ah			;overflow, inc ah, skip 0, c=1
		add	w,ah			;add ah to w, make c
		mov	ah,w			;store w in ah
		or	w,al			;make z
		ret
