home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / platform / strncpy.asm < prev    next >
Assembly Source File  |  1998-06-17  |  8KB  |  235 lines

  1.         page    ,132
  2.         title   strncpy - copy at most n characters of string
  3. ;***
  4. ;strncpy.asm - copy at most n characters of string
  5. ;
  6. ;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ;       defines strncpy() - copy at most n characters of string
  10. ;
  11. ;*******************************************************************************
  12.  
  13.         .xlist
  14.         include cruntime.inc
  15.         .list
  16.  
  17. page
  18. ;***
  19. ;char *strncpy(dest, source, count) - copy at most n characters
  20. ;
  21. ;Purpose:
  22. ;       Copies count characters from the source string to the
  23. ;       destination.  If count is less than the length of source,
  24. ;       NO NULL CHARACTER is put onto the end of the copied string.
  25. ;       If count is greater than the length of sources, dest is padded
  26. ;       with null characters to length count.
  27. ;
  28. ;       Algorithm:
  29. ;       char *
  30. ;       strncpy (dest, source, count)
  31. ;       char *dest, *source;
  32. ;       unsigned count;
  33. ;       {
  34. ;         char *start = dest;
  35. ;
  36. ;         while (count && (*dest++ = *source++))
  37. ;             count--;
  38. ;         if (count)
  39. ;             while (--count)
  40. ;                 *dest++ = '\0';
  41. ;         return(start);
  42. ;       }
  43. ;
  44. ;Entry:
  45. ;       char *dest     - pointer to spot to copy source, enough space
  46. ;                        is assumed.
  47. ;       char *source   - source string for copy
  48. ;       unsigned count - characters to copy
  49. ;
  50. ;Exit:
  51. ;       returns dest, with the character copied there.
  52. ;
  53. ;Uses:
  54. ;
  55. ;Exceptions:
  56. ;
  57. ;*******************************************************************************
  58.  
  59.         CODESEG
  60.  
  61.         public  strncpy
  62. strncpy proc
  63.  
  64.         .FPO    ( 0, 3, 0, 0, 0, 0 )
  65.  
  66.         mov     ecx,[esp + 0ch]     ; ecx = count
  67.         push    edi                 ; preserve edi
  68.         test    ecx,ecx
  69.         jz      short finish        ; leave if count is zero
  70.  
  71.         push    esi                 ; preserve edi
  72.         push    ebx                 ; preserve ebx
  73.         mov     ebx,ecx             ; store count for tail loop
  74.         mov     esi,[esp + 14h]     ; esi -> source string
  75.         test    esi,3               ; test if source string is aligned on 32 bits
  76.         mov     edi,[esp + 10h]     ; edi -> dest string
  77.         jnz     short src_misaligned    ; (almost always source is aligned)
  78.  
  79.         shr     ecx,2               ; convert ecx to dword count
  80.         jnz     short main_loop_entrance
  81.         jmp     short copy_tail_loop    ; 0 < count < 4
  82.  
  83. ; simple byte loop until string is aligned
  84.  
  85. src_misaligned:
  86.         mov     al,byte ptr [esi]   ; copy a byte from source to dest
  87.         inc     esi
  88.         mov     [edi],al
  89.         inc     edi
  90.         dec     ecx
  91.         jz      fill_tail_end1      ; if count == 0, leave
  92.         test    al,al               ; was last copied byte zero?
  93.         jz      short align_dest    ; if so, go align dest and pad it out
  94.                                     ; with zeros
  95.         test    esi,3               ; esi already aligned ?
  96.         jne     short src_misaligned
  97.         mov     ebx,ecx             ; store count for tail loop
  98.         shr     ecx,2
  99.         jnz     short main_loop_entrance
  100.  
  101. tail_loop_start:
  102.         and     ebx,3               ; ebx = count_before_main_loop%4
  103.         jz      short fill_tail_end1    ; if ebx == 0 then leave without
  104.                                         ; appending a null byte
  105.  
  106. ; while ( EOS (end-of-string) not found and count > 0 ) copy bytes
  107.  
  108. copy_tail_loop:
  109.         mov     al,byte ptr [esi]   ; load byte from source
  110.         inc     esi
  111.         mov     [edi],al            ; store byte to dest
  112.         inc     edi
  113.         test    al,al               ; EOS found?
  114.         je      short fill_tail_zero_bytes  ; '\0' was already copied
  115.         dec     ebx
  116.         jnz     copy_tail_loop
  117. fill_tail_end1:
  118.         mov     eax,[esp + 10h]     ; prepare return value
  119.         pop     ebx
  120.         pop     esi
  121.         pop     edi
  122.         ret
  123.  
  124. ; EOS found. Pad with null characters to length count
  125.  
  126. align_dest:
  127.         test    edi,3               ; dest string aligned?
  128.         jz      dest_align_loop_end
  129. dest_align_loop:
  130.         mov     [edi],al
  131.         inc     edi
  132.         dec     ecx                 ; count == 0?
  133.         jz      fill_tail_end       ; if so, finished
  134.         test    edi,3               ; is edi aligned ?
  135.         jnz     dest_align_loop
  136. dest_align_loop_end:
  137.         mov     ebx,ecx             ; ebx > 0
  138.         shr     ecx,2               ; convert ecx to count of dwords
  139.         jnz     fill_dwords_with_EOS
  140.         ; pad tail bytes
  141. finish_loop:                        ; 0 < ebx < 4
  142.         mov     [edi],al
  143.         inc     edi
  144. fill_tail_zero_bytes:
  145.         dec     ebx
  146.         jnz     finish_loop
  147.         pop     ebx
  148.         pop     esi
  149. finish:
  150.         mov     eax,[esp + 8]       ; return in eax pointer to dest string
  151.         pop     edi
  152.         ret
  153.  
  154. ; copy (source) string to (dest). Also look for end of (source) string
  155.  
  156. main_loop:                          ; edx contains first dword of source string
  157.         mov     [edi],edx           ; store one more dword
  158.         add     edi,4               ; kick dest pointer
  159.         dec     ecx
  160.         jz      tail_loop_start
  161.  
  162. main_loop_entrance:
  163.         mov     edx,7efefeffh
  164.         mov     eax,dword ptr [esi] ; read 4 bytes (dword)
  165.         add     edx,eax
  166.         xor     eax,-1
  167.         xor     eax,edx
  168.         mov     edx,[esi]           ; it's in cache now
  169.         add     esi,4               ; kick dest pointer
  170.         test    eax,81010100h
  171.         je      short main_loop
  172.  
  173.         ; may have found zero byte in the dword
  174.  
  175.         test    dl,dl               ; is it byte 0
  176.         je      short byte_0
  177.         test    dh,dh               ; is it byte 1
  178.         je      short byte_1
  179.         test    edx,00ff0000h       ; is it byte 2
  180.         je      short byte_2
  181.         test    edx,0ff000000h      ; is it byte 3
  182.         jne     short main_loop     ; taken if bits 24-30 are clear and bit
  183.                                     ; 31 is set
  184.  
  185. ; a null character was found, so dest needs to be padded out with null chars
  186. ; to count length.
  187.  
  188.         mov     [edi],edx
  189.         jmp     short fill_with_EOS_dwords
  190.  
  191. byte_2:
  192.         and     edx,0ffffh          ; fill high 2 bytes with 0
  193.         mov     [edi],edx
  194.         jmp     short fill_with_EOS_dwords
  195.  
  196. byte_1:
  197.         and     edx,0ffh            ; fill high 3 bytes with 0
  198.         mov     [edi],edx
  199.         jmp     short fill_with_EOS_dwords
  200.  
  201. byte_0:
  202.         xor     edx,edx             ; fill whole dword with 0
  203.         mov     [edi],edx
  204.  
  205. ; End of string was found. Pad out dest string with dwords of 0
  206.  
  207. fill_with_EOS_dwords:               ; ecx > 0   (ecx is dword counter)
  208.         add     edi,4
  209.         xor     eax,eax             ; it is instead of ???????????????????
  210.         dec     ecx
  211.         jz      fill_tail           ; we filled all dwords
  212.  
  213. fill_dwords_with_EOS:
  214.         xor     eax,eax
  215. fill_with_EOS_loop:
  216.         mov     [edi],eax
  217.         add     edi,4
  218.         dec     ecx
  219.         jnz     short fill_with_EOS_loop
  220. fill_tail:                          ; let's pad tail bytes with zero
  221.         and     ebx,3               ; ebx = ebx % 4
  222.         jnz     short finish_loop   ; taken, when there are some tail bytes
  223. fill_tail_end:
  224.         mov     eax,[esp + 10h]
  225.         pop     ebx
  226.         pop     esi
  227.         pop     edi
  228.         ret
  229.  
  230. strncpy endp
  231.         end
  232.  
  233.  
  234.  
  235.