Skip to content
Assembly

strlen — 扫描至 NUL

通过扫描内存直到零字节来计算 C 字符串长度。

#string#sse#optimization

Code

assembly
; strlen(s) -> length   rdi = s, result in rax
section .text
global strlen

strlen:
    mov     rax, rdi        ; save start pointer
.loop:
    cmp     byte [rdi], 0   ; is *rdi == '\0'?
    je      .done
    inc     rdi
    jmp     .loop
.done:
    sub     rdi, rax        ; length = end - start
    mov     rax, rdi
    ret

; Optimized: process 8 bytes at a time
; (the famous 'determine if a word has a zero byte' bit trick)
global strlen_fast
strlen_fast:
    mov     rax, rdi
    and     rdi, -8         ; align to 8-byte boundary
    pxor    xmm0, xmm0
.loop:
    movdqu  xmm1, [rax]     ; load 16 bytes (unaligned)
    pcmpeqb xmm1, xmm0      ; compare with 0 -> 0xFF bytes on match
    pmovmskb ecx, xmm1      ; pack match bits into ecx
    test    ecx, ecx
    jnz     .found
    add     rax, 16
    jmp     .loop
.found:
    bsf     ecx, ecx        ; bit index of first 0
    add     rax, rcx
    ret