Talk:Detokenising in 6502 machine code

From BeebWiki
Revision as of 21:52, 9 March 2015 by Lurkio (talk | contribs) (Can't get code to run)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

I tried to make a listing (below) out of the code and run it in BeebEm, but it just prints out "128: AND" and then hangs. What am I doing wrong? Apologies if it's something incredibly obvious.

5 tbase=&70:tptr=&72:OSWRCH=&FFEE
10 P%=&4000:FORV%=0TO3STEP3:[
20 OPT V%
30 \ Detokenise BASIC tokens
40 \ =======================
50 \ needs, eg tbase=&70 tptr=&72 if calling from BASIC 
60 \ eg tbase=&A8 tptr=&AA if calling as a *command
70 \
80 \ ----------------------------------
90 \ TokenInit - Find BASIC token table
100 \ ----------------------------------
110 \ On entry, BASIC ROM must be paged in
120 \ On exit, tbase=>Start of token table
130 \ A,Y,tptr corrupted, X preserved
140 \
150 .TokenInit
160 LDY #0:STY tptr:LDA #&80:STA tptr+1 :\ tptr=>ROM start
170 .TokInitLp
180 LDA #1:CLC:ADC tptr:STA tptr :\ Inc. tptr
190 LDA #0:TAY:ADC tptr+1:STA tptr+1
200LDA(tptr),Y:CMP #&80:BEQ TokInit2:DEY
210 .TokInit2
220 INY:LDA (tptr),Y:CMP #ASC"A":BNE TokInitLp :\ Not "A"
230 INY:LDA (tptr),Y:CMP #ASC"N":BNE TokInitLp :\ Not "AN"
240 INY:LDA (tptr),Y:CMP #ASC"D":BNE TokInitLp :\ Not "AND"
250 .TokInitOk
260 LDA tptr:STA tbase:LDA tptr+1:STA tbase+1
270 RTS
280 :
290 :
300 \ -----------------------------
310 \ PrToken - Print a BASIC token
320 \ -----------------------------
330 \ On entry, A=token byte
340 \ BASIC ROM must be paged in
350 \ tbase=>Start of token table, set by TokenInit
360 \ On exit, A,Y,tptr corrupted, X preserved
370 \
380 .TokenPrint
390 PHA:LDA tbase:STA tptr :\ Point to start token table
400 LDA tbase+1:STA tptr+1
410 .TokPrLp1
420 LDY #&FF
430 .TokPrLp2
440 INY:LDA (tptr),Y:BPL TokPrLp2 :\ Loop until token byte found
450 PLA:CMP (tptr),Y:BEQ TokPrFound :\ Found matching token
460 PHA:TYA:BNE TokPrStep :\ Step to next token
470 .TokPrLp3
480 INY:LDA (tptr),Y:BPL TokPrLp3 :\ Find next token byte
490 DEY:DEY
500 .TokPrStep
510 INY:TYA:SEC:ADC tptr:STA tptr :\ Step past this token string
520 LDA #0:ADC tptr+1:STA tptr+1
530 BNE TokPrLp1 :\ Loop to keep searching
540 .TokPrFound
550 TYA:BEQ TokPrNxt:LDY #0 :\ Skip past leading token
560 .TokPrLp3
570 LDA (tptr),Y:BMI TokPrEnd :\ Token byte, end
580 CMP #32:BCC TokPrEnd:JSR OSWRCH :\ Flag byte, end
590 .TokPrNxt
600 INY:BNE TokPrLp3 :\ Loop back for next character
610 .TokPrEnd
620 RTS
630 ]:NEXT
640 CALL TokenInit
650 FOR A%=&80 TO 255:PRINT ;A%;": ";:CALL TokenPrint:PRINT:NEXT