.z80 external NBits,BitCnt,WB,InByte getbts: ld HL,0 ;Results here ld A,(NBits) ld C,A ; C := NBits ld A,(BitCnt) cp C jr NC,ok ;BitCnt>=NBits ld B,A ;B := BitCnt ld A,C ;A := NBits sub B ;A := NBits - BitCnt ld (NBits),A ld A,(WB) call shl ;WB shl BitCnt push HL ;intermediate result call InByte ;new Byte in L ld A,(NBits) ld B,A ;B := NBits ld A,8 ; SUB B ;A := 8 - NBits ld (BitCnt),A ld A,L ;New byte pop HL call shl ;WB shl NBits ld (WB),A ret ok: ld B,C ;B := NBits sub B ;A := BitCnt - NBits ld (BitCnt),A ld A,(WB) call shl ld (WB),A ret shl: sla A ;MSB in Carry rl L djnz shl ret end getbts