
 1000  *SAVE S.HEX.SEARCH
 1010  *--------------------------------
 1020  *   MEMORY SEARCH FOR HEX STRING
 1030  *      BY BOB BERNARD, MAY 17, 1985
 1040  *      MODIFIED BY BOB S-C, MAY 27TH
 1050  *   ADR1.ADR2^YXXXXXXXXXXXX
 1060  *      ("^Y" MEANS CONTROL-Y)
 1070  *
 1080  *   SEARCH MEMORY FROM ADR1 THRU ADR2
 1090  *   LOOKING FOR REFERENCES TO
 1100  *   THE HEX STRING, XXXXXXXXXX
 1110  *
 1120  *--------------------------------
 1130  YSAV        .EQ $34
 1140  A1L         .EQ $3C,3D   START OF SEARCH AREA
 1150  A2L         .EQ $3E,3F   END OF SEARCH AREA
 1160  KEY.LENGTH  .EQ $40      (MONITOR'S A3L)
 1170  *--------------------------------
 1180  KBDBUF      .EQ $0200 THRU $2CF
 1190  DELTA.TABLE .EQ $02D0 THRU $3CF
 1200  USRADR      .EQ $03F8   CTL-Y JUMPS HERE
 1210  *--------------------------------
 1220  PRINTAX     .EQ $F941
 1230  CROUT       .EQ $FD8E   NEW LINE
 1240  MONZ        .EQ $FF69   MONITOR, NO BELL
 1250  *--------------------------------
 1260           .OR $0800  
 1270           .TF B.HEX.SEARCH 
 1280  *--------------------------------
 1290  HEX.SEARCH 
 1300           LDA #$4C       JMP OPCODE
 1310           STA USRADR       STUFF INTO CNTL-Y EXIT LOC
 1320           LDA #SEARCH      LO ADR
 1330           STA USRADR+1
 1340           LDA /SEARCH      HI ADR
 1350           STA USRADR+2
 1360           JMP MONZ         MONITOR, NO BELL
 1370  *--------------------------------
 1380  SEARCH
 1390  *---SKIP LEADING BLANKS----------
 1400           LDY YSAV         NEXT VALID KBDBUF CHAR
 1410  .1       LDA KBDBUF,Y     GET CHAR FROM
 1420           INY              KEYBOARD BUFFER
 1430           CMP #" "         SKIP LEADING BLANKS
 1440           BEQ .1
 1450  *---MARK KEY START---------------
 1460           DEY
 1470           STY YSAV         WHERE SCAN STARTS
 1480  *---FIND END OF KEY--------------
 1490  .2       JSR NXTCHAR
 1500           BCC .2           ...HEX DIGIT
 1510           CPY YSAV         CHECK FOR NULL KEY
 1520           BNE .3           ...NOT NULL
 1530           RTS              NULL KEY
 1540  *---COMPUTE KEY LENGTH-----------
 1550  .3       TYA
 1560           SBC YSAV
 1570           
 1580           LSR 
 1590           STA KEY.LENGTH
 1600           LDY YSAV
 1610           LDX #0
 1620           STX KBDBUF       (IN CASE ODD COUNT)
 1630           BCS .5           ...ODD NUMBER OF BYTES
 1640  *---ADJUST FOR EVEN LENGTH-------
 1650           DEC KEY.LENGTH MAKE EVEN LENGTH ONE LESS
 1660  *---LEFT NYBBLE------------------
 1670  .4       JSR NXTCHAR
 1680           BCS .6           END OF KEY
 1690           ASL
 1700           ASL
 1710           ASL
 1720           ASL
 1730           STA KBDBUF,X     LEFT HALF DEST CHAR 
 1740  *---RIGHT NYBBLE-----------------
 1750  .5       JSR NXTCHAR
 1760           AND #$0F
 1770           ORA KBDBUF,X     MERGE HI NIBBLE
 1780           STA KBDBUF,X
 1790           INX
 1800           BNE .4           ...ALWAYS
 1810  .6       STY YSAV
 1820  *---INIT ALL DELTAS=-1 ----------
 1830           LDX #0
 1840           LDA #-1
 1850  .7       STA DELTA.TABLE,X 
 1860           INX
 1870           BNE .7     ...256 OF THEM
 1880  *---DELTA(KEY(I))=I--------------
 1890           LDY #0           FOR I=0 TO KEYLEN
 1900  .8       LDA KBDBUF,Y    DELTA(K) = DISTANCE FROM LEFT END
 1910           TAX                  OF RIGHT-MOST OCCURENCE OF
 1920           TYA                  8-BIT VALUE "K" IN KEY.
 1930           STA DELTA.TABLE,X
 1940           INY              NEXT I
 1950           CPY KEY.LENGTH
 1960           BCC .8
 1970           BEQ .8
 1980  *---ADJUST END OF SEARCH---------
 1990           SEC
 2000           LDA A2L
 2010           SBC KEY.LENGTH
 2020           STA A2L
 2030           BCS SEARCH.LOOP
 2040           DEC A2L+1
 2050  *--------------------------------
 2060  SEARCH.LOOP
 2070         LDA A1L+1    <<<DEBUG>>>
 2080         LDX A1L      <<<DEBUG>>>
 2090         JSR PRINTAX  <<<DEBUG>>>
 2100         LDA #"-"     <<<DEBUG>>>
 2110         JSR $FDED    <<<DEBUG>>>
 2120           LDA A2L          CHECK AGAINST
 2130           CMP A1L           UPPER BOUND
 2140           LDA A2L+1         FOR SEARCH
 2150           SBC A1L+1
 2160           BCS .1           A1<=A2, NOT FINISHED
 2170           RTS              A1>A2, FINISHED
 2180  *---COMPARE IN THIS POSITION-----
 2190  .1       LDY KEY.LENGTH   FOR I=KEYLEN TO 0
 2200  .2       LDA (A1L),Y      CHECK BYTES FROM
 2210           CMP KBDBUF,Y        RIGHT TO LEFT
 2220           BNE .3           ...DID NOT MATCH
 2230           DEY              NEXT I
 2240           BPL .2
 2250  *---MATCH FOUND------------------
 2260           LDX A1L          PRINT ADR
 2270           LDA A1L+1        WHERE MATCH
 2280           JSR PRINTAX         WAS FOUND
 2290           JSR CROUT        NEW LINE
 2300           JMP .4
 2310  *---ADVANCE SEARCH POINTER-------
 2320  .3       TAX              STRING CHAR JUST LOOKED AT 
 2330           TYA              I
 2340           CLC
 2350           SBC DELTA.TABLE,X   
 2360           BPL .5           ...VALUE IS POSITIVE
 2370  .4       LDA #0           ...ADVANCE BY 1
 2380  .5       SEC              COMPENSATE
 2390           ADC A1L
 2400           STA A1L
 2410           BCC SEARCH.LOOP
 2420           INC A1L+1
 2430           BNE SEARCH.LOOP  ...ALWAYS, UNLESS WE
 2440           RTS              ...RAN OFF THE END OF MEMORY
 2450  *--------------------------------
 2460  NXTCHAR
 2470           LDA KBDBUF,Y     NEXT ACTIVE CHAR
 2480           INY
 2490           EOR #$B0         CONVERT ASCII TO DIGIT
 2500           CMP #10          0..9?
 2510           BCC .1           YES  
 2520           ADC #$88         SHIFT RANGE FOR A-F TEST
 2530           CMP #$FA         A..F?
 2540           BCS .1           YES. EXIT CC
 2550           SEC              NOT HEX CHAR
 2560           DEY              BACK UP INDEX
 2570           RTS
 2580  .1       CLC              SIGNAL HEX CHAR
 2590           RTS
 2600  *--------------------------------
 2610  END    .BS $A00-*
 2620  TEST.STRING
 2630         .AS /XXXXXXXCOCACACACACACACACACACACAC/
 2640  * TRY A00.A1F^Y  43414341434143
 2650  * SHOULD GET A09-A0B-A0D-A0F-A11-A13-A15-A17-A19
 2660  *--------------------------------
 2670  TS2    .AS /A STRING SEARCHING EXAMPLE CONSISTING OF SIMPLE TEXT/
 2680  * TRY A20.A53^Y48494E47
 2690  *--------------------------------

