8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly

Page Last Altered:

80CD Search for Program Line

Submitted by Steve Fewell

Name: Search for Program Line (in IWA)
Starting Address: &80CD
Entry criteria: The IWA contains the 2-byte Program line number to search for (&2A-&2B).
Exit: &3D-&3E contain a pointer to the Program Line that is greater than or equal to the
Line Number in the IWA. C (carry flag) = 1 and Y = 2 if the Line number in the IWA was found (equal).
C = 0 and Y = 2 if the Line number wasn't found and &3D-&3E point to the next existing line after
the Line Number contained in the IWA>.


Set &3D-&3E to PAGE (by zeroing &3D and setting &3E to &18).
[&80D3] Compare line number MSB [the value pointed to by (&3D-&3E) + 1] with &2B (MSB of the Line
Number to search for).

If the Line Number MSB at &3D-&3E is less than the Line Number MSB in the IWA (&2B) then:
* [&80DB] Update (&3D-&3E) pointer to the next Line Number by adding the Number of Bytes in the current
Line [the value pointed to by (&3D-&3E) + 3] to the (&3D-&3E) pointer.
Note: Program Line numbers are stored as follows [1st Byte = Line Number LSB, 2nd Byte = Line Number MSB,
   3rd Byte = Number of Bytes in Line].
* Jump back to &80D3 to Compare the next Line Number.
[&80E9] If the MSB Line Number bytes were not equal then (no exact line number match) exit with C = 0 and Y = 2.
Compare line number LSB [the value pointed to by (&3D-&3E) + 2] with &2A (LSB of the Line Number
to search for).
If the current line number is less than the one we are searching for (in the IWA) then jump back to &80DB to update the
pointer (&3D-&3E) to point to the next Line Number in the Program, and compare the next line number with
the IWA MSB Byte again.

Now we have found a line number that is >= the Line Number in the IWA (or reached the end of the program [#&FF marker]).
If the Line Number is equal to the IWA line, then exit with C = 1; Otherwise, exit with Y = 2 and C = 0.

Disassembly for the Search for Program Line routine

80CD d= 100 061 64 3D STZ &3D
80CF   165 024 A5 18 LDA &18
80D1 > 133 062 85 3E STA &3E
80D3   160 001 A0 01 LDY#&01
80D5 = 177 061 B1 3D LDA (&3D),Y
80D7 + 197 043 C5 2B CMP &2B
80D9   176 014 B0 0E BCS 14 --> &80E9
80DB   160 003 A0 03 LDY#&03
80DD = 177 061 B1 3D LDA (&3D),Y
80DF e= 101 061 65 3D ADC &3D
80E1 = 133 061 85 3D STA &3D
80E3   144 238 90 EE BCC -18 --> &80D3
80E5 > 230 062 E6 3E INC &3E
80E7   128 234 80 EA BRA -22 --> &80D3
80E9   208 010 D0 0A BNE 10 --> &80F5
80EB   200 C8 INY
80EC = 177 061 B1 3D LDA (&3D),Y
80EE * 197 042 C5 2A CMP &2A
80F0   144 233 90 E9 BCC -23 --> &80DB
80F2   208 001 D0 01 BNE 1 --> &80F5
80F4 ` 096 60 RTS
80F5   160 002 A0 02 LDY#&02
80F7   024 18 CLC
80F8 ` 096 60 RTS


 Back to 8BS