	PUBLIC	CALCPI


plus	=	255
minus	=	0


DATA	SEGMENT PUBLIC WORD

	EXTRN	a:BYTE, b:BYTE, c:BYTE, d:BYTE, e:BYTE, f:BYTE, n:WORD
sign	DB	?

DATA	ENDS



CODE	SEGMENT PUBLIC BYTE
	ASSUME	CS:CODE, DS:DATA, ES:DATA

clear	MACRO	x
	MOV	DI,OFFSET x
	MOV	CX,n
	MOV	AX,0
	REP	STOSB
	ENDM


move	MACRO	src, dst
	MOV	SI,OFFSET src
	MOV	DI,OFFSET dst
	MOV	CX,n
	REP	MOVSB
	ENDM

leng	MACRO	x
	LOCAL	leng1, leng2
	MOV	BX,n
leng1:
	CMP	x[BX],0
	JNZ	leng2
	DEC	BX
	OR	BX,BX
	JNZ	leng1
leng2:
	ENDM

linc2	MACRO	x
	LOCAL	linc21, linc22
	ADD	x[1],2
	MOV	BX,1
linc21:	CMP	x[BX],10
	JC	linc22
	SUB	x[BX],10
	INC	BX
	INC	x[BX]
	JMP	linc21
linc22:
	ENDM

ladd	MACRO	x, y
	LOCAL	ladd1, ladd2
	MOV	BX,1
ladd1:	MOV	al,x[BX]
	ADD	al,y[BX]
	CMP	al,10
	JC	ladd2
	SUB	al,10
	INC	x[BX][1]
ladd2:	MOV	x[BX],al
	INC	BX
	CMP	BX,n
	JNA	ladd1
	ENDM

lsub	MACRO	x, y
	LOCAL	lsub1, lsub2
	MOV	BX,1
lsub1:	MOV	al,x[BX]
	SUB	al,y[BX]
	CMP	al,10
	JC	lsub2
	ADD	al,10
	DEC	x[BX][1]
lsub2:	MOV	x[BX],al
	INC	BX
	CMP	BX,n
	JNA	lsub1
	ENDM

ldiv	MACRO	x, y, z, l
	LOCAL	ldiv1, ldiv2, ldiv3, ldiv4, ldiv5, ldiv6
	clear	z[1]
	MOV	DX,l
	MOV	SI,n
	INC	SI
	SUB	SI,DX
ldiv1:	MOV	BX,DX
ldiv2:	MOV	AL,x[SI][BX][-1]
	SUB	AL,y[BX]
	OR	AL,AL
	JNZ	ldiv3
	DEC	BX
	OR	BX,BX
	JNZ	ldiv2
ldiv3:	JS	ldiv4
	MOV	BX,1
ldiv5:	MOV	AL,x[SI][BX][-1]
	SUB	AL,y[BX]
	CMP	AL,10
	JC	ldiv6
	ADD	AL,10
	DEC	x[SI][BX]
ldiv6:	MOV	x[SI][BX][-1],AL
	INC	BX
	CMP	BX,DX
	JNA	ldiv5
	INC	z[SI]
	JMP	ldiv1
ldiv4:	DEC	SI
	OR	SI,SI
	JNZ	ldiv1
	move	z[1], x[1]
	ENDM

rpt	MACRO	address
	local	r1, r2, r3
	leng	e
	INC	BX
	ldiv	c, e, d, BX
	linc2	e
	CMP	sign,plus
	JNZ	r1
	ladd	a, d
	JMP	SHORT r2
r1:
	lsub	a, d
r2:
	NOT	sign
	leng	d
	OR	BX,BX
	JZ	r3
	JMP	address
r3:
	ENDM


calcpi	PROC	NEAR

	PUSH	DS
	POP	ES
	CLD

	clear	a[1]
	clear	b[1]
	MOV	BX,n
	MOV	b[BX][-3],8
	clear	e[1]
	MOV	e[1],1
	MOV	sign,plus

l01:
	move	b[1], c[1]
	ladd	b[1], c[1]
	move	b[1], c[1]
	ladd	b[1], c[1]
	MOV	SI,offset b[3]
	MOV	DI,offset b[1]
	MOV	CX,n
	SUB	CX,3
	REP	MOVSB
	MOV	WORD PTR [DI],0
	move	b[1], c[1]
	rpt	l01

	clear	b[1]
	MOV	BX,n
	MOV	b[BX][-2],9
	MOV	b[BX][-3],5
	MOV	b[BX][-4],6
	clear	e[1]
	MOV	e[1],1
	MOV	sign,minus

l02:
	ldiv	b, f, c, 6
	rpt	l02

	RET

calcpi	ENDP

CODE	ENDS

	END
