home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRECPY.ASM < prev    next >
Assembly Source File  |  1997-07-05  |  4KB  |  97 lines

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