1#include <asm.inc> 2#include <ksamd64.inc> 3.code64 4#if 0 5 page ,132 6 title strncpy - copy at most n characters of string 7;*** 8;strncpy.asm - copy at most n characters of string 9; 10; Copyright (c) Microsoft Corporation. All rights reserved. 11; 12;Purpose: 13; defines strncpy() - copy at most n characters of string 14; 15;******************************************************************************* 16; Look at strncat.asm for this file 17include ksamd64.inc 18 subttl "strncpy" 19#endif 20 21LEAF_ENTRY_ARG3 strncpy, _TEXT, dst_ptr_byte, src_ptr_byte, count_dword 22//OPTION PROLOGUE:NONE, EPILOGUE:NONE 23 24// align the SOURCE so we never page fault 25// dest pointer alignment not important 26 27 mov r11, rcx 28 or r8, r8 29 jz strncpy_exit 30 sub rcx, rdx // combine pointers 31 test dl, 7 32 jz qword_loop_entrance 33 34copy_head_loop_begin: 35 mov al, [rdx] 36 test al, al 37 mov [rdx+rcx], al 38 jz filler 39 inc rdx 40 dec r8 41 jz strncpy_exit 42 test dl, 7 43 jnz copy_head_loop_begin 44 jmp qword_loop_entrance 45 46strncpy_exit: 47 mov rax, r11 48 ret 49 50qword_loop_begin: 51 mov [rdx+rcx], rax 52 add rdx, 8 53qword_loop_entrance: 54 mov rax, [rdx] 55 sub r8, 8 56 jbe qword_loop_end 57 mov r9, HEX(7efefefefefefeff) 58 add r9, rax 59 mov r10, rax 60 xor r10, -1 61 xor r10, r9 62 mov r9, HEX(8101010101010100) 63 test r10, r9 64 jz qword_loop_begin 65 66qword_loop_end: 67 add r8, 8 68 jz strncpy_exit_2 69 70 test al, al 71 mov [rdx+rcx], al 72 jz filler 73 inc rdx 74 dec r8 75 jz strncpy_exit_2 76 test ah, ah 77 mov [rdx+rcx], ah 78 jz filler 79 inc rdx 80 dec r8 81 jz strncpy_exit_2 82 shr rax, 16 83 test al, al 84 mov [rdx+rcx], al 85 jz filler 86 inc rdx 87 dec r8 88 jz strncpy_exit_2 89 test ah, ah 90 mov [rdx+rcx], ah 91 jz filler 92 inc rdx 93 dec r8 94 jz strncpy_exit_2 95 shr rax, 16 96 test al, al 97 mov [rdx+rcx], al 98 jz filler 99 inc rdx 100 dec r8 101 jz strncpy_exit_2 102 test ah, ah 103 mov [rdx+rcx], ah 104 jz filler 105 inc rdx 106 dec r8 107 jz strncpy_exit_2 108 shr eax, 16 109 test al, al 110 mov [rdx+rcx], al 111 jz filler 112 inc rdx 113 dec r8 114 jz strncpy_exit_2 115 test ah, ah 116 mov [rdx+rcx], ah 117 jz filler 118 inc rdx 119 dec r8 120 jnz qword_loop_entrance 121 122strncpy_exit_2: 123 mov rax, r11 124 ret 125 126//this is really just memset 127filler: 128 add rcx, rdx 129 xor rdx, rdx 130 cmp r8, 16 131 jb tail // a quickie 132aligner1: 133 test cl, 7 134 jz aligned 135 inc rcx 136 mov [rcx], dl 137 dec r8 138 jmp aligner1 139aligned: 140 sub r8, 32 141 jb tail_8_enter 142loop32: 143 mov [rcx], rdx 144 mov [rcx+8], rdx 145 mov [rcx+16], rdx 146 mov [rcx+24], rdx 147 add rcx, 32 148 sub r8, 32 149 jae loop32 150 151tail_8_enter: 152 add r8, 32 // get back the value 153tail_8_begin: 154 sub r8, 8 155 jb tail_enter 156 mov [rcx], rdx 157 add rcx, 8 158 jmp tail_8_begin 159 160tail_enter: 161 add r8, 8 // get back the value 162tail: 163 sub r8, 1 164 jb tail_finish 165 mov [rcx], dl 166 inc rcx 167 jmp tail 168tail_finish: 169 mov rax, r11 170 ret 171 172LEAF_END strncpy, _TEXT 173 174 end 175