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

  1.         page    ,132
  2.         title   strcat - concatenate (append) one string to another
  3. ;***
  4. ;strcat.asm - contains strcat() and strcpy() routines
  5. ;
  6. ;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ;       STRCAT concatenates (appends) a copy of the source string to the
  10. ;       end of the destination string, returning the destination string.
  11. ;
  12. ;*******************************************************************************
  13.  
  14.         .xlist
  15.         include cruntime.inc
  16.         .list
  17.  
  18.  
  19. page
  20. ;***
  21. ;char *strcat(dst, src) - concatenate (append) one string to another
  22. ;
  23. ;Purpose:
  24. ;       Concatenates src onto the end of dest.  Assumes enough
  25. ;       space in dest.
  26. ;
  27. ;       Algorithm:
  28. ;       char * strcat (char * dst, char * src)
  29. ;       {
  30. ;           char * cp = dst;
  31. ;
  32. ;           while( *cp )
  33. ;                   ++cp;           /* Find end of dst */
  34. ;           while( *cp++ = *src++ )
  35. ;                   ;               /* Copy src to end of dst */
  36. ;           return( dst );
  37. ;       }
  38. ;
  39. ;Entry:
  40. ;       char *dst - string to which "src" is to be appended
  41. ;       const char *src - string to be appended to the end of "dst"
  42. ;
  43. ;Exit:
  44. ;       The address of "dst" in EAX
  45. ;
  46. ;Uses:
  47. ;       EAX, ECX
  48. ;
  49. ;Exceptions:
  50. ;
  51. ;*******************************************************************************
  52.  
  53. page
  54. ;***
  55. ;char *strcpy(dst, src) - copy one string over another
  56. ;
  57. ;Purpose:
  58. ;       Copies the string src into the spot specified by
  59. ;       dest; assumes enough room.
  60. ;
  61. ;       Algorithm:
  62. ;       char * strcpy (char * dst, char * src)
  63. ;       {
  64. ;           char * cp = dst;
  65. ;
  66. ;           while( *cp++ = *src++ )
  67. ;                   ;               /* Copy src over dst */
  68. ;           return( dst );
  69. ;       }
  70. ;
  71. ;Entry:
  72. ;       char * dst - string over which "src" is to be copied
  73. ;       const char * src - string to be copied over "dst"
  74. ;
  75. ;Exit:
  76. ;       The address of "dst" in EAX
  77. ;
  78. ;Uses:
  79. ;       EAX, ECX
  80. ;
  81. ;Exceptions:
  82. ;*******************************************************************************
  83.  
  84.  
  85.         CODESEG
  86.  
  87. %       public  strcat, strcpy      ; make both functions available
  88. strcpy  proc
  89.         push    edi                 ; preserve edi
  90.         mov     edi,[esp+8]         ; edi points to dest string
  91.         jmp     short copy_start
  92.  
  93. strcpy  endp
  94.  
  95.         align   16
  96.  
  97. strcat  proc
  98.  
  99.         .FPO    ( 0, 2, 0, 0, 0, 0 )
  100.  
  101.         mov     ecx,[esp+4]         ; ecx -> dest string
  102.         push    edi                 ; preserve edi
  103.         test    ecx,3               ; test if string is aligned on 32 bits
  104.         je      short find_end_of_dest_string_loop
  105.  
  106. dest_misaligned:                    ; simple byte loop until string is aligned
  107.         mov     al,byte ptr [ecx]
  108.         inc     ecx
  109.         test    al,al
  110.         je      short start_byte_3
  111.         test    ecx,3
  112.         jne     short dest_misaligned
  113.  
  114.         align   4
  115.  
  116. find_end_of_dest_string_loop:
  117.         mov     eax,dword ptr [ecx] ; read 4 bytes
  118.         mov     edx,7efefeffh
  119.         add     edx,eax
  120.         xor     eax,-1
  121.         xor     eax,edx
  122.         add     ecx,4
  123.         test    eax,81010100h
  124.         je      short find_end_of_dest_string_loop
  125.         ; found zero byte in the loop
  126.         mov     eax,[ecx - 4]
  127.         test    al,al               ; is it byte 0
  128.         je      short start_byte_0
  129.         test    ah,ah               ; is it byte 1
  130.         je      short start_byte_1
  131.         test    eax,00ff0000h       ; is it byte 2
  132.         je      short start_byte_2
  133.         test    eax,0ff000000h      ; is it byte 3
  134.         je      short start_byte_3
  135.         jmp     short find_end_of_dest_string_loop
  136.                                     ; taken if bits 24-30 are clear and bit
  137.                                     ; 31 is set
  138. start_byte_3:
  139.         lea     edi,[ecx - 1]
  140.         jmp     short copy_start
  141. start_byte_2:
  142.         lea     edi,[ecx - 2]
  143.         jmp     short copy_start
  144. start_byte_1:
  145.         lea     edi,[ecx - 3]
  146.         jmp     short copy_start
  147. start_byte_0:
  148.         lea     edi,[ecx - 4]
  149. ;       jmp     short copy_start
  150.  
  151. ;       edi points to the end of dest string.
  152. copy_start::
  153.         mov     ecx,[esp+0ch]       ; ecx -> sorc string
  154.         test    ecx,3               ; test if string is aligned on 32 bits
  155.         je      short main_loop_entrance
  156.  
  157. src_misaligned:                     ; simple byte loop until string is aligned
  158.         mov     dl,byte ptr [ecx]
  159.         inc     ecx
  160.         test    dl,dl
  161.         je      short byte_0
  162.         mov     [edi],dl
  163.         inc     edi
  164.         test    ecx,3
  165.         jne     short src_misaligned
  166.         jmp     short main_loop_entrance
  167.  
  168. main_loop:                          ; edx contains first dword of sorc string
  169.         mov     [edi],edx           ; store one more dword
  170.         add     edi,4               ; kick dest pointer
  171. main_loop_entrance:
  172.         mov     edx,7efefeffh
  173.         mov     eax,dword ptr [ecx] ; read 4 bytes
  174.  
  175.         add     edx,eax
  176.         xor     eax,-1
  177.  
  178.         xor     eax,edx
  179.         mov     edx,[ecx]           ; it's in cache now
  180.  
  181.         add     ecx,4               ; kick dest pointer
  182.         test    eax,81010100h
  183.  
  184.         je      short main_loop
  185.         ; found zero byte in the loop
  186. ; main_loop_end:
  187.         test    dl,dl               ; is it byte 0
  188.         je      short byte_0
  189.         test    dh,dh               ; is it byte 1
  190.         je      short byte_1
  191.         test    edx,00ff0000h       ; is it byte 2
  192.         je      short byte_2
  193.         test    edx,0ff000000h      ; is it byte 3
  194.         je      short byte_3
  195.         jmp     short main_loop     ; taken if bits 24-30 are clear and bit
  196.                                     ; 31 is set
  197. byte_3:
  198.         mov     [edi],edx
  199.         mov     eax,[esp+8]         ; return in eax pointer to dest string
  200.         pop     edi
  201.         ret
  202. byte_2:
  203.         mov     [edi],dx
  204.         mov     eax,[esp+8]         ; return in eax pointer to dest string
  205.         mov     byte ptr [edi+2],0
  206.         pop     edi
  207.         ret
  208. byte_1:
  209.         mov     [edi],dx
  210.         mov     eax,[esp+8]         ; return in eax pointer to dest string
  211.         pop     edi
  212.         ret
  213. byte_0:
  214.         mov     [edi],dl
  215.         mov     eax,[esp+8]         ; return in eax pointer to dest string
  216.         pop     edi
  217.         ret
  218.  
  219. strcat  endp
  220.  
  221.         end
  222.  
  223.