Many changes:
[Marmot.git] / util.S
blobc4c59056cda983236ef0d732584f3447cefa6a2a
1         /*
2          * util.S --
3          *
4          *      Various generic utility functions.
5          */
8         .code64
9         .section .text
11         /*
12          * bzero --
13          * bfill --
14          *
15          *      void bzero(void *s, uint64 n);
16          *      void bfill(void *s, uint64 n, uint8 val);
17          *
18          *      Set the first n bytes of s to 0 or to val.
19          */
21         .global bzero
22 bzero:  pushq   %rbp
23         movq    %rsp, %rbp
25         xorq    %rdx, %rdx
26         jmp     1f
29         .global bfill
30 bfill:  pushq   %rbp
31         movq    %rsp, %rbp
33         /* copy %dl to each byte of %rdx */
34         movzbq  %dl, %rdx
35         movq    %rdx, %rax
36         clc
37         shll    $8, %edx
38         orl     %eax, %edx
39         /* 2 bytes done */
40         movl    %edx, %eax
41         shll    $16, %edx
42         orl     %eax, %edx
43         /* 4 bytes done */
44         movq    %rdx, %rax
45         shlq    $32, %rdx
46         orq     %rax, %rdx
47         /* all 8 done */
49         /* set bytes while adjusting s up to 8 byte boundary */
50 1:      testl   $7, %esi
51         jz      1f
53         movb    %dl, (%rdi)
54         decq    %rsi
55         incq    %rdi
56         jmp     1b
58 1:      /* zero bytes 8 at a time */
59         
60         testq   $-8, %rsi
61         jz      1f
63         movq    %rdx, (%rdi)
64         subq    $8, %rsi
65         addq    $8, %rdi
66         jmp     1b
68 1:      /* zero out the remainder */
69         testl   $7, %esi
70         jz      1f
72         movb    %dl, (%rdi)
73         decq    %rsi
74         incq    %rdi
75         jmp     1b
77 1:      leave
78         ret
81         /*
82          * bcopy --
83          *
84          *      void bcopy(void *s, void *d, uint64 n);
85          *
86          *      Copy n bytes from s to d.
87          */
89         .global bcopy
90 bcopy:  pushq   %rbp
91         movq    %rsp, %rbp
93         cld
94         xchgq   %rdi, %rsi
95         movq    %rdx, %rcx
96         rep movsb
98         leave
99         ret
102         /*
103          * strlen --
104          *
105          *      Return length of a string.
106          *
107          *      uint64 strlen(char *s);
108          */
109         .global strlen
110 strlen: pushq   %rbp
111         movq    %rsp, %rbp
113         pushq   %rdi
114         xorl    %eax, %eax
115         cld
116         repne scasb
117         popq    %rax
118         subq    %rax, %rdi      # d - d - s
119         movq    %rdi, %rax
121         leave
122         ret