[getbits.asm]

	extrn	NBits,BitCnt,WB,InByte

GetBits:
	lxi	h,0		; Ergebnis Register
	lda	NBits
	mov	c,a		; c:=NBits
	lda	BitCnt
	cmp	c
	jnc	ok		; BitCnt>=NBits
	mov	b,a		; b:=BitCnt
	mov	a,c		; a:=NBits
	sub	b		; a:=NBits - BitCnt
	sta	NBits
	lda	WB
	call	shfl		; WB shl BitCnt
	push	h		; Zwischenergebnis
	call	InByte		; Neues Byte in l
	lda	NBits
	mov	b,a		; b:=NBits
	mvi	a,8
	sub	b		; a:=8 - NBits
	sta	BitCnt
	mov	a,l		; Neues Byte
	pop	h
	call	shfl		; WB shl NBits
	sta	WB
	ret
ok:
	mov	b,c		; b:=NBits
	sub	b		; a:=BitCnt - NBits
	sta	BitCnt
	lda	WB
	call	shfl
	sta	WB
	ret

shfl:
	add	a		; MSB in carry
	mov	e,a
	mov	a,l
	adc	a
	mov	l,a
	mov	a,e
	dcr	b
	jnz	shfl
	ret

	end	GetBits

