strlen

Purpose: Determine the number of BYTEs in an ASCIIz string in SRAM.
Assumes:  The string is between 0 and 2,048 BYTEs in length (which is all available SRAM in a 328P).
Passed:     R25:R24  CHAR *   szBuffer  pointer to the SRAM buffer to examine
Returns: R25:R24  WORD wCount the BYTE count in the ASCIIz string, which may be zero.
FLAGS  ZERO flag set (TRUE) when length is zero (and R25:R24 = 0) OR
clear (FALSE) when length is > 0 (and R25:R24 > 0)
Alters: Only R24, R25 and the FLAGs
Example: 
.ORG  0x1000

CheckLength:
   ADIW    R26, 0                 ; If 'X' is zero, the command line has nothing
   BREQ    ACheckLength_Ret       ;  to be checked, so just exit immediately
   MOVW    R24, R26
   LDI     R22, '"'               ; Specify the string starting delimiter BYTE
   CALL    Astrchr                ; Search for the first occurrence of a double
   BREQ    ACheckLength_Ret       ;  quote character; exit if none found
   ADIW    R24, 1                 ; Advance to the BYTE after the first double
   CALL    Astrlen                ;  quote character and get the string length
   CALL    APrintWORD             ; Should only need PrintBYTE, but since it
   LDI     R25, ACSZ_BYTEs >> 8   ;  suppresses leading zeros, use PrintWORD
   LDI     R24, ACSZ_BYTEs & 0xFF
   CALL    APrintFLASHASCIIz           ; This string below, ending in CR, LF

CheckLength_Ret:
   RET

CSZ_BYTEs:  .STRING  " BYTEs in the string\n"
Test it:
m>DF+ 0x1000 64    ; Verify the FLASH memory range is unused
FLASH (program memory) contents:
1000:  FF FF FF FF FF FF FF FF-FF FF FF FF FF FF FF FF  ................
1010:  FF FF FF FF FF FF FF FF-FF FF FF FF FF FF FF FF  ................
1020:  FF FF FF FF FF FF FF FF-FF FF FF FF FF FF FF FF  ................
1030:  FF FF FF FF FF FF FF FF-FF FF FF FF FF FF FF FF  ................
m>:10100000109671F0CD0162E20E941D3049F0019608
m>:101010000E9420300E94543090E182E20E944930C8
m>:10102000089520425954457320696E2074686520E4
m>:08103000737472696E670A0017
m>:00000001FF
m>DF+ 0x1000 64      ; Verify that it loaded
FLASH (program memory) contents:
1000:  10 96 71 F0 CD 01 62 E2-0E 94 1D 30 49 F0 01 96  ..q...b....0I...
1010:  0E 94 20 30 0E 94 54 30-90 E1 82 E2 0E 94 49 30  .. 0..T0......I0
1020:  08 95 20 42 59 54 45 73-20 69 6E 20 74 68 65 20  .. BYTEs in the
1030:  73 74 72 69 6E 67 0A 00-FF FF FF FF FF FF FF FF  string..........
m>@ 0x1000 "
0 BYTEs in the string
m>@ 0x1000 "Test
4 BYTEs in the string
m>@ 0x1000 "A longer string...
18 BYTEs in the string
m>                                                                              
Notes: An ASCIIz string is a series of printable characters, the end of which is defined by a NULL (= 0) BYTE.
The string may begin with a NULL, which is valid and will return zero length. The ZERO flag has already been set or cleared, so no comparison is needed.
Dropin: 
(setup
code)
;   Setup for the strlen() call:
; Passed:  R25:R24 CHAR * szBuffer pointer to the SRAM buffer to check
; Returns: R25:R24 WORD   wCount, number of BYTEs before the first NULL BYTE
;  (Also:)    ZERO FLAG   set (TRUE) when length is zero (and R25:R24 = 0)
;                         clear (FALSE) when length is > 0 (and R25:R24 > 0)
; Alters:  Only R24, R25, and the FLAGs
Also see:  The strcpy, strlenEEPROM, and strlenPGM System Functions.