RosettaCodeData/Task/String-length/Z80-Assembly/string-length.z80

19 lines
668 B
Z80 Assembly

; input: HL - pointer to the 0th char of a string.
; outputs length to B. HL will point to the last character in the string just before the terminator.
; length is one-indexed and does not include the terminator. A null string will return 0 in B.
; "Terminator" is a label for a constant that can be configured in the source code. My code uses 0.
; Sample Usage:
; ld hl,MyString
; call GetStringLength
GetStringLength:
ld b,0
loop_getStringLength:
ld a,(hl) ;load the next char
cp Terminator ;is it the terminator?
ret z ;if so, exit.
inc hl ;next char
inc b ;increment the byte count
jr loop_getStringLength