Skip to content

Instantly share code, notes, and snippets.

@merryhime
Created January 10, 2017 01:58
Show Gist options
  • Save merryhime/a62afe51f9125880c0a93e128110a72d to your computer and use it in GitHub Desktop.
Save merryhime/a62afe51f9125880c0a93e128110a72d to your computer and use it in GitHub Desktop.
// As with usual x64 ABI, rdi contains pointer to string
strlen:
xor eax, eax
test dil, 7
je .skip_align
// rax contains our return value, as per usual x64 ABI
xor eax, eax
// Line up to 8-byte alignment
.align_loop:
cmp byte ptr [rdi + rax], 0
je .end
inc rax
lea ecx, [rdi + rax]
test cl, 7
jne .align_loop
add rdi, rax
// Load up some stuff for the loop to happen
.skip_align:
movabs r8, 0x8080808080808080
movabs rdx, 0xfefefefefefefeff
mov rcx, qword ptr [rdi]
jmp .loop_entry
// Here we're testing 8 bytes at at time for zeros.
.loop:
add rax, 8
mov rcx, qword ptr [rdi + 8]
add rdi, 8
.loop_entry:
mov rsi, rcx
not rsi
add rcx, rdx
and rsi, r8
test rsi, rcx
je .loop
// Ok we detected a zero, but which byte was it?
cmp byte ptr [rdi], 0
je .end
inc rdi
.final_bytes:
inc rax
cmp byte ptr [rdi], 0
lea rdi, [rdi + 1]
jne .final_bytes
.end:
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment