String length: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
|||
Line 3,645: | Line 3,645: | ||
===Character Length=== |
===Character Length=== |
||
<lang yorick>strlen("Hello, world!")</lang> |
<lang yorick>strlen("Hello, world!")</lang> |
||
=={{header|Z80 Assembly}}== |
|||
The majority of Z80-based hardware predates Unicode so only byte length will be demonstrated for now. |
|||
===Byte Length=== |
|||
Code is called as a subroutine, i.e. <code>CALL getStringLength</code> |
|||
<lang z80>; 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</lang> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |