xref: /reactos/sdk/lib/ucrt/string/amd64/strlen.asm (revision 04e0dc4a)
1        page    ,132
2        title   strlen - return the length of a null-terminated string
3;***
4;strlen.asm - contains strlen() routine
5;
6;       Copyright (c) Microsoft Corporation. All rights reserved.
7;
8;Purpose:
9;       strlen returns the length of a null-terminated string,
10;       not including the null byte itself.
11;
12;*******************************************************************************
13include ksamd64.inc
14        subttl  "strlen"
15;***
16;strlen - return the length of a null-terminated string
17;
18;Purpose:
19;       Finds the length in bytes of the given string, not including
20;       the final null character.
21;
22;       Algorithm:
23;       int strlen (const char * str)
24;       {
25;           int length = 0;
26;
27;           while( *str++ )
28;                   ++length;
29;
30;           return( length );
31;       }
32;
33;Entry:
34;       const char * str - string whose length is to be computed
35;
36;Exit:
37;       EAX = length of the string "str", exclusive of the final null byte
38;
39;Uses:
40;       EAX, ECX, EDX
41;
42;Exceptions:
43;
44;*******************************************************************************
45
46LEAF_ENTRY_ARG1 strlen, _TEXT, buf:ptr byte
47
48    OPTION PROLOGUE:NONE, EPILOGUE:NONE
49
50    mov   rax, rcx
51    neg   rcx          ; for later
52    test  rax, 7
53    jz    main_loop_entry
54
55byte 066h, 090h
56
57byte_loop_begin:
58    mov   dl, [rax]
59    inc   rax
60    test  dl, dl
61    jz    return_byte_7
62    test  al, 7
63    jnz   byte_loop_begin
64
65main_loop_entry:
66    mov   r8, 7efefefefefefeffh
67    mov   r11, 8101010101010100h
68
69main_loop_begin:
70    mov   rdx, [rax]
71
72    mov   r9,  r8
73    add   rax, 8
74    add   r9,  rdx
75    not   rdx
76    xor   rdx, r9
77    and   rdx, r11
78    je    main_loop_begin
79
80main_loop_end:
81
82    mov   rdx, [rax-8]
83
84    test  dl, dl
85    jz    return_byte_0
86    test  dh, dh
87    jz    return_byte_1
88    shr   rdx, 16
89    test  dl, dl
90    jz    return_byte_2
91    test  dh, dh
92    jz    return_byte_3
93    shr   rdx, 16
94    test  dl, dl
95    jz    return_byte_4
96    test  dh, dh
97    jz    return_byte_5
98    shr   edx, 16
99    test  dl, dl
100    jz    return_byte_6
101    test  dh, dh
102    jnz   main_loop_begin
103
104return_byte_7:
105    lea   rax, [rax+rcx-1]
106    ret
107return_byte_6:
108    lea   rax, [rax+rcx-2]
109    ret
110return_byte_5:
111    lea   rax, [rax+rcx-3]
112    ret
113return_byte_4:
114    lea   rax, [rax+rcx-4]
115    ret
116return_byte_3:
117    lea   rax, [rax+rcx-5]
118    ret
119return_byte_2:
120    lea   rax, [rax+rcx-6]
121    ret
122return_byte_1:
123    lea   rax, [rax+rcx-7]
124    ret
125return_byte_0:
126    lea   rax, [rax+rcx-8]
127    ret
128
129LEAF_END strlen, _TEXT
130    end
131