home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / STRECPY.ASM < prev    next >
Assembly Source File  |  1994-04-03  |  3KB  |  93 lines

  1.         page    ,132
  2.         title   STRECPY
  3. COMMENT $
  4.         Author: Leslie Satenstein
  5.         A form of strcpy() where the result returned is the
  6.         nul terminating character of the first argument. In many cases
  7.         this function is move efficient than the equivalent
  8.         strcpy, followed by strcat.
  9.  
  10.         The assembler code does a strlen followed by a memcpy,
  11.             and makes use of the special string move instructions
  12.             intrinsic to the intel 8088, 80186, 80286, '386 and '486s.
  13.  
  14.         Use as: strecpy(strecpy(target,"first part"),"Second part");
  15.         in place of
  16.                 strcat(strcpy(target,"first part"),"Second part");
  17.  
  18.         One of the ways the C code appears is:
  19.         char *strecpy(char *target,const char *src)
  20.         {
  21.            return target+strlen(strcpy(target,src));
  22.         }
  23.         Another way is to do your own looping:
  24.  
  25.         char *strecpy(char *target,const char *src)
  26.         {
  27.           char *cpt,*cps;        /* many compilers optimise better
  28.                                   * when local pointers are declared
  29.                                   * (locals can be assigned registers)
  30.                                   */
  31.           cpt=target-1;
  32.           cps=src-1;
  33.           do
  34.           {
  35.             *++cpt = *++src;     /* copy first, ask questions later */
  36.           } while(*cpt!='\0');
  37.           return cpt;
  38.         }
  39.  
  40.         $
  41.  
  42. ;  Requires MASM 5.1 or later or equivalent
  43. ;
  44. ;  Assemble with:       MASM /Mx /z ...
  45. ;                       TASM /jMASM /mx /z ...
  46.  
  47. %       .MODEL  memodel,C               ;Add model support via command
  48.                                         ;line macros, e.g.
  49.                                         ;MASM /Dmemodel=LARGE,
  50.                                         ;MASM /Dmemodel=SMALL, etc.
  51.         PUBLIC STRECPY
  52.  
  53.         .DATA
  54.         .CODE
  55. if    @DataSize
  56. strecpy  proc uses si di ds,target:FAR ptr byte,src:FAR ptr byte
  57.         les     di,src          ; load es:di with src
  58.         mov     si,di           ; put copy to bx:si
  59.         mov     bx,es
  60. else
  61. strecpy  proc uses si di,target:NEAR ptr byte,src:NEAR ptr byte
  62.         mov     di,ds           ; make es same as ds
  63.         mov     es,di
  64.         mov     di,src
  65.         mov     si,di           ; put copy to es:si
  66. endif
  67.         xor     ax,ax           ; scan for the nul at end of string
  68.         mov     cx,-1
  69.         repne   scasb
  70.         not     cx              ; cx =    strlen(src)+1 ;
  71. if    @Datasize
  72.         les     di,target       ; this is where copy is to begin
  73.         mov     dx,es           ; dx has segment, di has offset to target
  74.         mov     ds,bx           ; ds:si have pointer to src
  75. else
  76.         mov     di,target       ; this is where copy is to begin
  77. endif
  78.         test    di,1            ; if we are on odd address, do one byte move
  79.         jz      on_even
  80.         movsb                   ; now, prepare to move words at a
  81.         dec     cx              ;time to target
  82. on_even:
  83.         shr     cx,1            ; carry flag has remainder after divide by 2
  84.         rep     movsw           ; move this many words
  85.         jnc     fini            ; if no carry, we are finished
  86.         movsb
  87. fini:
  88.         xchg    di,ax           ; set up return value, dx has seg value
  89.         dec     ax              ; backup to the nul character
  90.         ret
  91. strecpy endp
  92.         end
  93.