Assembler with LCC-Win32

The routine below does a fair job a counting the chars in a null terminated string.

StrLen

size_t __declspec(naked) str_len(char * s)
{
    _asm("movl 4(%esp),%ecx");      // first arg is at 4(%esp)
    _asm("jecxz exit");             // check for '0' NULL in ecx
    _asm("movl $-1,%eax");          // -1 to start
_asm("L1:");
    _asm("inc %eax");
    _asm("cmpb $0,(%ecx,%eax)");    // compares '0' with the byte pointed to
                                    // by ecx with an offfset of eax.
    _asm("jne L1");                 // not finished yet?
_asm("exit:");
    _asm("ret");                    // return is in eax
}

Back to main page