home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / m / mmpf.zip / LSTR.ASM < prev    next >
Assembly Source File  |  1991-09-10  |  3KB  |  152 lines

  1. ; lstr.asm:
  2. ;
  3. ; Copyright (C) Microsoft Corporation 1990, 1991.
  4. ; All Rights reserved.
  5. ;
  6. ; masm -Mx -Zi -DSEGNAME=????? lstr.asm
  7. ;
  8.     TITLE LSTR.ASM - asm code dude
  9.  
  10. ;****************************************************************
  11. ;* LSTR.ASM - ASM string copies                    *
  12. ;*                                *
  13. ;****************************************************************
  14. ;
  15.  
  16. ?PLM=1        ; PASCAL Calling convention is DEFAULT
  17. ?WIN=1        ; Windows calling convention
  18.  
  19. .xlist
  20. include cmacros.inc
  21. .list
  22.  
  23. ; The following structure should be used to access high and low
  24. ; words of a DWORD.  This means that "word ptr foo[2]" -> "foo.hi".
  25.  
  26. LONG    struc
  27. lo      dw      ?
  28. hi      dw      ?
  29. LONG    ends
  30.  
  31. FARPOINTER      struc
  32. off     dw      ?
  33. sel     dw      ?
  34. FARPOINTER      ends
  35.  
  36. ; -------------------------------------------------------
  37. ;               DATA SEGMENT DECLARATIONS
  38. ; -------------------------------------------------------
  39.  
  40. ifndef SEGNAME
  41.     SEGNAME equ <_TEXT>
  42. endif
  43.  
  44. .286
  45. createSeg %SEGNAME, CodeSeg, word, public, CODE
  46.  
  47. sBegin Data
  48. sEnd Data
  49.  
  50. sBegin CodeSeg
  51. assumes cs,CodeSeg
  52. assumes ds,DATA
  53.  
  54.  
  55.  
  56. cProc lstrncpy,<FAR,PUBLIC,NODATA>,<>
  57.     ParmD    lpDest
  58.     ParmD    lpSource
  59.     ParmW    n
  60. cBegin
  61.     mov    cx, n
  62.     or    cx, cx
  63.     jz    ncpy_exit    ; if 0 given for size, bail out now
  64.  
  65.     push    si        ; Preserve C registers
  66.     push    di
  67.     push    ds
  68.  
  69.     cld
  70.     les    di, lpDest    ; Load source, dest, and count
  71.     lds    si, lpSource
  72.  
  73. ncpy_copy:
  74.     lodsb            ; byte from source to al, inc src
  75.     stosb            ; al into dest, inc dest
  76.     or    al,al        ; load zero flag for the byte
  77.     loopnz    ncpy_copy    ; dec cx, if cx=0 or byte=0, break;
  78.  
  79. if 0
  80.     ; Pad the rest of dest string with NULLs
  81.     xor    al, al            ; zero out the byte to store
  82.     rep stosb byte ptr es:[di]    ; zero fill
  83. endif
  84.  
  85.     pop    ds        ; note: pop regs before the lstrncpy_exit
  86.     pop    di        ; label, which is called before regs pushed
  87.     pop    si
  88. ncpy_exit:
  89.         mov     ax,lpDest.off   ; return something consistent
  90.         mov     dx,lpDest.sel
  91. cEnd
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. cProc lstrncat,<FAR,PUBLIC,NODATA>,<>
  99.     ParmD    lpDest
  100.     ParmD    lpSource
  101.     ParmW    n
  102. cBegin
  103.     test    n, 0ffffh        ; check if zero count specified
  104.     jz    ncat_nocopy_exit
  105.  
  106.     ; preserve appropriate registers
  107.     push    di
  108.     push    si
  109.     push    ds
  110.  
  111.     ; Find end of the destination string
  112.     cld
  113.     les    di, lpDest
  114.     mov    cx, 0ffffh        ; is this bogus?
  115.  
  116.     xor    al, al            ; test for zero
  117.  
  118.     repnz    scasb            ; find first NULL character
  119.  
  120.     or    cx, cx            ; check overrun.  We've probably GP
  121.     jz    ncat_exit        ; faulted by now...
  122.  
  123.     ; Now copy the first n chars of lpSource onto lpDest
  124.     lds    si, lpSource
  125.  
  126.     dec    di            ; copy over src's \0
  127.     mov    cx, n            ; mbr: gee, maybe this line would help
  128.  
  129. ncat_copy:
  130.     lodsb            ; byte from source to al, inc src
  131.     stosb            ; al into dest, inc dest
  132.     or    al,al        ; load zero flag if the byte == \0
  133.     loopnz    ncat_copy    ; dec cx, if cx=0 or byte=0, break;
  134.  
  135.     jz    ncat_exit    ; if last character copied was NULL, we don't
  136.                 ; need to null terminate the dest string.
  137.     mov    byte ptr es:[di], 0h
  138.  
  139. ncat_exit:
  140.     pop    ds
  141.     pop    si
  142.     pop    di
  143.  
  144. ncat_nocopy_exit:
  145.         mov     ax,lpDest.off   ; return something consistent
  146.         mov     dx,lpDest.sel
  147. cEnd    
  148.  
  149.  
  150. sEnd CodeSeg
  151. end
  152.