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

  1.         page    ,132
  2.         title   memccpy - copy bytes until character found
  3. ;***
  4. ;memccpy.asm - copy bytes until a character is found
  5. ;
  6. ;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ;       defines _memccpy() - copies bytes until a specifed character
  10. ;       is found, or a maximum number of characters have been copied.
  11. ;
  12. ;*******************************************************************************
  13.  
  14.         .xlist
  15.         include cruntime.inc
  16.         .list
  17.  
  18. page
  19. ;***
  20. ;char *_memccpy(dest, src, _c, count) - copy bytes until character found
  21. ;
  22. ;Purpose:
  23. ;       Copies bytes from src to dest until count bytes have been
  24. ;       copied, or up to and including the character _c, whichever
  25. ;       comes first.
  26. ;
  27. ;       Algorithm:
  28. ;       char *
  29. ;       _memccpy (dest, src, _c, count)
  30. ;             char *dest, *src, _c;
  31. ;             unsigned int count;
  32. ;             {
  33. ;             while (count && (*dest++ = *src++) != _c)
  34. ;                     count--;
  35. ;
  36. ;             return(count ? dest : NULL);
  37. ;             }
  38. ;
  39. ;Entry:
  40. ;       char *dest - pointer to memory to receive copy
  41. ;       char *src - source of bytes
  42. ;       char _c - character to stop copy at
  43. ;       int count - max number of bytes to copy
  44. ;
  45. ;Exit:
  46. ;       returns pointer to byte immediately after _c in dest;
  47. ;       returns NULL if _c was never found
  48. ;
  49. ;Uses:
  50. ;
  51. ;Exceptions:
  52. ;
  53. ;*******************************************************************************
  54.  
  55.         CODESEG
  56.  
  57.         public  _memccpy
  58. _memccpy proc
  59.  
  60.         .FPO    ( 0, 4, 0, 0, 0, 0 )
  61.  
  62.         mov     ecx,[esp + 10h] ; ecx = max byte count
  63.         push    ebx             ; save ebx
  64.  
  65.         test    ecx,ecx         ; if it's nothing to move
  66.         jz      ret_zero_len    ; restore ebx, and return NULL
  67.  
  68.         mov     bh,[esp + 10h]  ; bh = byte to look for
  69.         push    esi             ; save esi
  70.  
  71.         test    ecx,1           ; test if counter is odd or even
  72.  
  73.         mov     eax,[esp + 0ch] ; eax = dest   , don't affect flags
  74.         mov     esi,[esp + 10h] ; esi = source , don't affect flags
  75.  
  76. ;       nop
  77.         jz      lupe2           ; if counter is even, do double loop
  78.                                 ; else do one iteration, and drop into double loop
  79.         mov     bl,[esi]        ; get first byte into bl
  80.         inc     esi             ; kick src (esi points to src)
  81.  
  82.         mov     [eax],bl        ; store it in dest
  83.         inc     eax             ; kick dest
  84.  
  85.         cmp     bl,bh           ; see if we just moved the byte
  86.         je      short toend
  87.  
  88.         dec     ecx             ; decriment counter
  89.         jz      retnull         ; drop into double loop if nonzero
  90.  
  91. lupe2:
  92.         mov     bl,[esi]        ; get first byte into bl
  93.         add     esi,2           ; kick esi (src)
  94.  
  95.         cmp     bl,bh           ; check if we just moved the byte (from bl)
  96.         je      toend_mov_inc   ; store bl & exit
  97.  
  98.         mov     [eax],bl        ; store first byte from bl
  99.         mov     bl,[esi - 1]    ; get second byte  into bl
  100.  
  101.         mov     [eax + 1],bl    ; store second byte from bl
  102.         add     eax,2           ; kick eax (dest)
  103.  
  104.         cmp     bl,bh           ; see if we just moved the byte
  105.         je      short toend     ; end of string
  106.  
  107.         sub     ecx,2           ; modify counter, and if nonzero continue
  108.         jnz     lupe2           ; else drop out & return NULL
  109.  
  110. retnull:
  111.         pop     esi
  112. ret_zero_len:
  113.         xor     eax,eax         ; null pointer
  114.         pop     ebx
  115.  
  116.         ret                     ; _cdecl return
  117.  
  118. toend_mov_inc:
  119.         mov     [eax],bl        ; store first byte from bl
  120.         inc     eax             ; eax points rihgt after the value
  121.  
  122. toend:  pop     esi
  123.         pop     ebx
  124.  
  125.         ret                     ; _cdecl return
  126.  
  127. _memccpy endp
  128.  
  129.         end
  130.