|
DataMuseum.dkPresents historical artifacts from the history of: CP/M |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about CP/M Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - download
Length: 3328 (0xd00) Types: TextFile Names: »POS.MAC«
└─⟦01b5c9619⟧ Bits:30005906 Microsoft Multiplan v1.05 og HELP └─ ⟦this⟧ »POS.MAC«
; ; ; Title Find the position of a substring on a string ; Name: POS ; ; ; Purpose: Search for the first occurrence of a substring ; within a string and return its starting index. ; If the substring is not found a 0 is returned. ; ; Entry: Register pair HL = base address of string ; Register pair DE = base address of substring ; ; A string is a maximum of 255 bytes long plus ; a lenght byte witch precdes it. ; ; Exit: If the substring is found then ; Register A = its starting index ; else ; Register A = 0 ; ; Registers used: AF,BC,DE,HL ; ; Time: Since the algorithm is so data-dependent, ; a simple formula is impossible; but the ; following statements are true, and a ; worst case is given. ; ; 154 bytes overhead ; Each match of 1 character takes 56 cycles ; a mismatch takes 148 cycles ; ; worst case timing will be when the ; string and substring always match ; except for the last character of the ; substring, such as ; string = 'AAAAAAAAAB' ; substring = 'AAB' ; ; Size: Program 69 bytes ; Data 7 bytes ; ; POS: ;Set up temporaries ;Exit if string of substring has zero length ld (string),hl ;Save string address ex de,hl ld a,(hl) ;Test length of substring or a jr z,notfnd ;Exit if length of substring = 0 inc hl ;Move past length byte of substring ld (substg),hl ;Save substring address ld (sublen),a ld c,a ;C = substring length ld a,(de) ;Test length of string or a jr z,notfnd ;Exit if length of string = 0 ;Number of searches = string length - substring length ; + 1. After that, no use searching since there aren't ; enough characters left to hold substring ; ;If substring is ower than string. Exit immedialy and ; indicate substring not found sub c ;A = string length - substring length jr c,notfnd ;Exit if string shorter than substring inc a ;Count = difference in lenghts + 1 ld b,a sub a ;Initial starting index = 0 ld (index),a ;Search until remaining string shorter than substring slp1: ld hl,index ;Increment starting index inc (hl) ld hl,sublen ;C = length of substring ld c,(hl) ld hl,(string) ;Incerment to next byte of string inc hl ld (string),hl ;HL = next address in string ld de,(substg) ;DE = starting value of substring ;Try to match substring starting at index ;match involves comparing corresponding characters ; one at a time cmplp: ld a,(de) ;Get a character of substring cp (hl) ;Compare to character of string jr nz,slp2 ;Jump if not same dec c jr z,found ;Jump if substring found inc hl ;Proceed to next characters inc de jr cmplp ;Arrive here if match fails, substring not yet found slp2: djnz slp1 ;Try next higher index if ; enough string left jr notfnd ;Else exit not found ;Found substring, return starting index found: ld a,(index) ;Substring found, A = starting index ret ;Could not find substring, return 0 as index notfnd: sub a ;Substring not found, A = 0 ret ;DATA string: ds 2 ;Base address of string substg: ds 2 ;Base address of substring slen: ds 1 ;Length of string sublen: ds 1 ;Length of substring index: ds 1 ;Current index into string «eof»