BBC Basic Line Numbers ---------------------- Within BBC BASIC programs line numbers (eg after GOTO, GOSUB, RESTORE) are encoded to make them easily found by RENUMBER, and to prevent them appearing to be control characters or tokens by forcing them into the range &40-&7F. Encoding line number -------------------- 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: \ On entry num 16-bit line number \ (ptr),Y => insertion point \ On exit (ptr),Y => after inserted bytes LDA #&8D:STA (ptr),Y :\ &8D line number token LDA num+0:AND #&C0 LSR A:LSR A INY:STA (ptr),Y LDA num+1:AND #&C0 LSR A:LSR A LSR A:LSR A ORA (ptr),Y:EOR #&54 STA (ptr),Y:INY :\ first encoded byte LDA num+0:AND #&3F ORA #&40:STA (ptr),Y:INY :\ second encoded byte LDA num+1:AND #&3F ORA #&40:STA (ptr),Y:INY :\ third encoded byte Decoding line number -------------------- Detokenising byte 0,1,2,3: lsb : byte 2 EOR ((byte 1 * 4) AND &C0) msb : byte 3 EOR (byte 1 * 16) In 6502 code: \ On entry (ptr),Y => 141 byte \ On exit num = 16-bit line number \ (ptr),Y => after encoded number INY:LDA (ptr),Y ASL A:ASL A:TAX:AND #&C0 INY:EOR (ptr),Y:STA num+0 :\ b0-b7 TXA:ASL A:ASL A INY:EOR (ptr),Y:STA num+1 :\ b8-b15 INY :\ step past last byte In 6502 code: \ On entry byte1/2/3=encoded number \ On exit num = 16-line number \ byte1/2/3=corrupted ASL byte1:ASL byte1 LDA byte1:AND #&C0 EOR byte2:STA num+0 :\ lsb of line number ASL byte1:ASL byte1 LDA byte1 EOR byte3:STA num+1 :\ msb of line number In BASIC: REM On entry ptr% => &8D byte REM Returns 16-bit line number DEFFNline(ptr%) =((ptr%?3 EOR (ptr%?1 * 16))*256 + (ptr%?2 EOR ((ptr%?1 * 4) AND &C0))) AND &FFFF