	.title	lapse
;
; lapse calculates number of days elapsed since 1 jan, 1972
; call	lapse( date, ndays )
;
;
	.dsabl	gbl
	.globl	idadmy, leap
	.globl	r5idmy, day, month, year, tbnday

	.psect	user$i
lapse::
	tst	(r5)+

	mov	(r5)+,r5idmy+2		; copy address of date
	mov	r5,-(sp)		; save r5
	mov	#r5idmy,r5
	call	idadmy			; convert internal date to id, im, iy
	mov	(sp)+,r5		; restore r5

	sub	#72.,year
	mov	year,r1			; 365 days per year
	mul	#365.,r1

; + (year+3) / 4
	mov	year,r3
	add	#3,r3
	clr	r2
	div	#4,r2
	add	r2,r1			; add in extra days for any leap years

; + sumon(month)
	mov	month,r3
;	dec	r3			; 0->11.
;	asl	r3			; 0->22.
;	add	sumon(r3),r1
20$:	dec	r3			; sum days in months since start year
	beq	30$
	mov	r3,r4
	dec	r4
	asl	r4
	add	tbnday(r4),r1
	br	20$


; + day - 1
30$:	add	day,r1
	dec	r1

; check for leap year
	mov	year,r4
	call	leap			; leap year ?
	bcc	40$			; br, if false
; but is month > 2 ??
	cmp	month,#2
	ble	40$			; br, if false
; so is leap year and month > feb so add a day
	inc	r1

; r1 now contains days since 0 jan 72

40$:	mov	r1,@(r5)+
	return

	.end
