In certain situations when I want to print some string to stdout we need the length for the write syscall in linux. So we can’t always depend on the $-string macro, which is valid for a defined string.
We use the REPNE (REPeat while Not Equal) instruction which will loop as long as CX != 0. Along with REPNE we use SCASB (scan byte string). It compares the content of the accumulator (AL, AX, or EAX) against the current value pointed at by ES:[EDI]. In the end we calculate the difference between offsets of the scanned string (EDI) and the original string (EBX) to find the length.
_strlen:push ebxpush ecxmov ebx, edixor al, almov ecx, 0xffffffffrepne scasb ; REPeat while Not Equal [edi] != alsub edi, ebx ; length = offset of (edi - ebx)mov eax, edipop ebxpop ecxret