* = $c000 lda #<2049 sta $fc lda #>2049 sta $fd ;$fc refers to the standard start of BASIC ldy #$04 ;scan BASIC program area from the first byte ;of the first line content (skip the two bytes with the ;address of the second BASIC line, and skip the ;two bytes with the first line number). loop1 lda ($fc), y beq exit_loop1 ;if a zero has been found, exit loop1 iny ;else, scan another byte jmp loop1 exit_loop1 iny ;a zero has been found - end of first program line ;inc y so that we reach the address of second program line ;(before inc y, we are on the last byte of the first basic line) clc tya adc $fc sta $fc ;$fc = y + $fc skip1 lda $fc sta 2049 lda $fd sta 2050 ;restore the second line address that has been cleared by NEW ldy #$00 ;y will stay zero, we don't need it scan_loop lda ($fc), y ;scans the remaining part of BASIC program beq zero_found ;if it founds a zero, it jumps to "zero_found" lda #$00 sta zero_flag ;if a value different than 0 has been found, zero_flag ;counter must be reset return inc $fc ;prepare to scan next byte lda $fc bne scan_loop ;if low byte of address is not zero, no need to increment ;the high byte of address inc $fd jmp scan_loop ;we've crossed a memory page, so increment high byte of address zero_found inc zero_flag ;we've found a zero, so zero_flag must be incremented by 1 lda zero_flag cmp #3 ;three consecutive zeroes have been found? ;three zeroes = one byte (end of last line) + two bytes (end of program) bne return ;if not, keep on scanning the program inc $fc ;three zeroes have been reached, so we've reached program end ;now we need to store the final address + one on pointers ;on 45, 47, and 49 decimal lda $fc bne skip2 ;increment low byte of address by one, then check if it is 0 inc $fd ;low byte has wrapped to 0, so we need to increment the high byte ;of address skip2 lda $fc ;set pointers to required value, according to where the program ends sta 45 sta 47 sta 49 lda $fd sta 46 sta 48 sta 50 rts ;we are done zero_flag .byte 0