| 1 |
;
|
| 2 |
; String functions
|
| 3 |
;
|
| 4 |
; Steve Maddison, 27/02/2007
|
| 5 |
;
|
| 6 |
; Although the Z80's block instructions would be useful here, they cannot
|
| 7 |
; be used effectively either due to:
|
| 8 |
; 1) having to check for the terminating '\0', or
|
| 9 |
; 2) we want BC to count upwards and not down.
|
| 10 |
|
| 11 |
; Name: strcmp
|
| 12 |
; Desc: Compare strings
|
| 13 |
; In: HL, DE = addresses of strings to compare
|
| 14 |
; Out: Zero flag set if strings match
|
| 15 |
strcmp:
|
| 16 |
push bc
|
| 17 |
ld bc,0xffff ; max out counter
|
| 18 |
call strncmp
|
| 19 |
pop bc
|
| 20 |
ret
|
| 21 |
|
| 22 |
; Name: strncmp
|
| 23 |
; Desc: Compare first n characters of strings
|
| 24 |
; In: HL, DE = addresses of strings to compare
|
| 25 |
; BC = number of characters to check (n)
|
| 26 |
; Out: Zero flag set if strings match
|
| 27 |
strncmp:
|
| 28 |
ld a,b ; check if counter is 0
|
| 29 |
or c
|
| 30 |
jp z,strncmp_end
|
| 31 |
ld a,(de) ; compare bytes
|
| 32 |
cp (hl)
|
| 33 |
jp nz,strncmp_end
|
| 34 |
cp 0 ; end of first string?
|
| 35 |
jp z,strncmp_check
|
| 36 |
inc de
|
| 37 |
inc hl
|
| 38 |
dec bc
|
| 39 |
jp strncmp
|
| 40 |
strncmp_check: ; check end of second string
|
| 41 |
or (hl) ; has been reached too
|
| 42 |
cp 0
|
| 43 |
strncmp_end:
|
| 44 |
ret
|
| 45 |
|
| 46 |
; Name: strncpy
|
| 47 |
; Desc: Copy a string
|
| 48 |
; In: HL = address of source string
|
| 49 |
; DE = address of destination buffer
|
| 50 |
; Out: none
|
| 51 |
strcpy:
|
| 52 |
push bc
|
| 53 |
ld bc,0xffff ; max out counter
|
| 54 |
call strncpy
|
| 55 |
pop bc
|
| 56 |
ret
|
| 57 |
|
| 58 |
; Name: strncpy
|
| 59 |
; Desc: Copy first n characters of a string
|
| 60 |
; In: HL = address of source string
|
| 61 |
; DE = address of destination buffer
|
| 62 |
; BC = number of characters to copy (n)
|
| 63 |
; Out: none
|
| 64 |
strncpy:
|
| 65 |
ld a,b
|
| 66 |
or c
|
| 67 |
jp z,strncpy_end
|
| 68 |
ld a,(hl)
|
| 69 |
ld (de),a
|
| 70 |
inc hl
|
| 71 |
inc de
|
| 72 |
dec bc
|
| 73 |
cp 0
|
| 74 |
jp nz,strncpy
|
| 75 |
strncpy_end:
|
| 76 |
ret
|
| 77 |
|
| 78 |
; Name: strlen
|
| 79 |
; Desc: Find length of string (excluding terminating \0)
|
| 80 |
; In: HL = address of string
|
| 81 |
; Out: BC = length of string
|
| 82 |
strlen:
|
| 83 |
ld bc,0
|
| 84 |
strlen_loop:
|
| 85 |
ld a,(hl)
|
| 86 |
cp 0
|
| 87 |
jp z,strlen_end
|
| 88 |
inc hl
|
| 89 |
inc bc
|
| 90 |
jp strlen_loop
|
| 91 |
strlen_end:
|
| 92 |
ret
|
| 93 |
|