BBC Basic Line Numbers ---------------------- Within BBC Basic programs lines line numbers (eg after GOTO, GOSUB, RESTORE) are encoded to make them easily found by RENUMBER, and to prevent them appearing tp be control characters or tokens by forcing them into the range &40-&7F. Tokenising line number lsb, msb: Byte 0 : 141 Byte 1 :(((lsb AND &C0) DIV 4) OR ((msb AND &C0) DIV 16)) EOR &54 Byte 2 : (lsb AND &3F) OR &40 Byte 3 : (msb AND &3F) OR &40 In 6502 code: LDA #&8D:STA byte0 :\ &8D line number token LDA lsb:AND #&C0 LSR A:LSR A STA byte1 LDA msb:AND #&C0 LSR A:LSR A LSR A:LSR A ORA byte1:EOR #&54 STA byte1 :\ first encoded byte LDA lsb:AND #&3F ORA #&40:STA byte2 :\ second encoded byte LDA msb:AND #&3F ORA #&40:STA byte3 :\ third encoded byte Detokenising byte 0,1,2,3: lsb : byte 3 EOR (byte 1 * 16) msb : byte 2 EOR ((byte 1 AND &30) * 4) In 6502 code: ASL byte1:ASL byte1 LDA byte1:AND #&C0 EOR byte2:STA lsb :\ lsb of line number ASL byte1:ASL byte1 LDA byte1 EOR byte3:STA msb :\ msb of line number