• Aucun résultat trouvé

Glen Colbert

Dans le document 64/128 (Page 33-37)

• •

• •

• •

• •

• •

String Search

Glen Colbert

Use "String Search" to search through string arrays look-ing for a match. This utility is much faster than its BASIC

equivalent. For the Commodore 64 and 128 in 64 mode.

Although machine language is fast, BASIC is generally pref-erable when you're writing a program to handle lots of strings:

names, addresses, recipes, lists in general. BASIC has built-in string and array functions that make it easy to handle large volumes of information.

It's frustrating, however, to have to wait while the pro-gram searches through a few hundred entries looking for a match. The longer the list, the slower BASIC becomes.

String Search is fast because it's written in machine lan-guage (ML), although you don't have to understand machine language to use it.

Special Instructions

There are two things you must do before using the program:

1. The first and second variables defined in the target pro-gram must be strings. To be safe, put them in the first few lines. And the second string must be the "match" you're looking for.

2. The string array to be searched must be the first array DIMensioned. An integer array containing the same number of elements must be the second array DIMensioned. The integer array will contain flags that indicate a match has been found.

Program 1 is the BASIC loader for String Search. When you run it, the ML routine is located to the top-of-BASIC memory and the pointers are reset, protecting it from BASIC.

Program 1 can be incorporated into your own programs or loaded and run as a separate program before you load your own data-management program.

25

CHAPTER 1

To access the search routine, enter

SYS (PEEK(55)

+

256"'PEEK(56»

The ML routine is relocatable. If you prefer, you can put it up at $COOO (49152).

Program 2 is a test of String Search. After you have en-tered, saved, and run Program 1, run Program 2. First, an ar-ray containing 300 elements is set up. BASIC then searches for a match, and you see how many jiffies it takes. (A jiffy is 1/60 second.) Next, the ML routine is used. You may be surprised at how much faster you get the results.

How It Works

The search method used in this routine is quite simple. When it's called, the first operation is to swap out a portion of the zero page ($D9-$E9) into the cassette buffer. The length of the string to be checked for is put into $D9, and the address of the string is set into $DA-$DB. Next, addresses $DC-$DD are set to point to the zero element of the integer array. Addresses

$EO-$El are set to point to the three bytes of string array information (length, low byte of address, and high byte of ad-dress) for the zero element of the string array. Things are now in order for the processing loop.

The first step in the processing loop is to increment the pointers for the arrays that are being worked to the next ele-ment. For this reason, the zero element is not searched. The information for the string array element being worked is moved to $E5-$E7. Then $E5 is checked for a null (string = I I " ) ; if it is null, the zero page information is put back in and returned to BASIC. A counter for the search string ($E2) and one for the searched string ($E3) are set to zero, and the search begins.

These counters determine whether or not there is a match.

If the search string counter is equal to the length of the search string, there has been a match. If the searched string counter is equal to the length of the searched string, there has been no match. In either case, the routine sets the value in the integer array and returns to the main loop to try the next element of the array.

If the counters do not match, the accumulator is loaded with the first character of the search string. This is compared against each element of the searched string until a match is 26

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

• •

BASIC PROGRAMMING UTILITIES

found. Then the second character of the search string is com-pared against the next character in the searched string, and so on, until the counter equals the length of the search string. If a match is not found, the search string counter is reset (but the searched string counter isn't), and the program loops back.

Program 1. String Search-BASIC Loader

For mistake-proof program entry, use "The Automatic Proofreader," Appendix B, to type in this program.

PG 10 REM*****************{3 SPACES}BASIC LOADER F OR **{5 SPACES}STRING SEARCH{2 SPACES}******

*****************

AB 100 PRINT" {CLR} {4 DOWN} (3 SPACES}STRING SEARCHE R"

CJ 11O PRINT"{2 DOWN}ONE MOMENT PLEASE"

BH 12O TP=PEEK(55)+256*PEEK(56)

SR 13O TP=TP-186:H=INT«TP)/256):L=TP-H*256:POKE55 ,L:POKE56,H

JH 14O IN=PEEK(55)+256*PEEK(56):FORC=IN TO IN+185:

READI:POKEC,I:CK=CK+I:NEXT:

RR 15O IFCK<>26449 THEN PRINT"ERROR IN DATA":END KH 16O REM****************** STRING SEARCH DATA***

*******************

PK 18O DATA 16O,17,185,216,O,153,6O,3,136,208 KH 19O DATA 247,16O,9,177,45,133,217,2OO,177,45 GF 2OO DATA 133,218,2OO,177,45,133,219,24,16O,2 BF 21O DATA 177,47,101,47,1O5,7,133,22O,2OO,177 SK 22O DATA 47,101,48,133,221,16O,O,24,165,47 FF 23O DATA 105,7,133,224,165,48,1O5,O,133,225 RB 24O DATA 169,O,24O,12,16O,17,185,6O,3,153 BA 25O DATA 216,O,136,208,247,96,24,165,224,1O5 RC 26O DATA 3,133,224,165,225,105,O,133,225,16O HK 27O DATA O,177,224,153,229,O,2OO,192,3,208 GD 28O DATA 246,24,165,22O,105,2,133,22O,165,221 QD 29O DATA 105,O,133,221,165,229,24O,2O2,2O8,2 BP 3OO DATA 24O,21O,162,O,134,227,134,226,24,165 XK 31O DATA 217,197,226,24O,31,24,165,229,197,227 MS 32O DATA 144,37,164,226,177,218,164,227,209,23O DC 33O DATA 208,6,23O,227,23O,226,2O8,226,23O,227 PQ 34O DATA 169,O,133,226,24O,218,16O,O,169,1 KE 35O DA'I'A 145,22O,2OO,169,O,145,22O,24O,197,16O EJ 36O DATA O,152,145,22O,24O,242

RX 999 PRINT" DONE" : NEW

27

CHAPTER 1

Program 2. String Search-Demo Program

For mistake-proof program entry, use "The Automatic Proofreader," Appendix B, to type in this program.

ES 45 ML=PEEK(55)+256*PEEK(56):REM START ADDRESS GB 50 PRINT" { CLR} {2 DOWN} {2 SPACES} STRING SEARCH

EMO"

QE 100 PRINT"BUILDING ARRAY"

QG 110 Q$="GOOD"

DX 120 FORL=lT0299 DK 130

QQ 140 :A$(L)="ABCDEFGHIJKLMNOPQRSTUVWX:YZ"

PM 150

BB 160 NEXTL

BP 170 A$(l)="GARBAGE GOOD MORE GARBAGE"

DM 180 A$ (10 )="GARB GOOD MORE GARB"

SM 185 A$(70)="GOOD GARBAGE"

DB 190 A$(100)="GARBAGE GOOD"

MM 195 A$(250)="GARBAGE GOOD MORE GARBAGE"

XE 212J12J PRINT"ARRAY FINISHED"

JQ 300 REM***************{8 SPACES}BASIC SEARCH

REM***************{10 SPACES}ML SEARCH {3 SPACES}**********************

• •

• •

• •

• •

• •

• •

Dans le document 64/128 (Page 33-37)