home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / LIB / STRSOURC.ZIP / STCPY.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-03-07  |  3.8 KB  |  81 lines

  1.  
  2. ; *******************************************************
  3. ; *                                                     *
  4. ; *     Turbo Pascal Runtime Library Version 7.0        *
  5. ; *     String Copy Routine                             *
  6. ; *                                                     *
  7. ; *     Copyright (C) 1990-1993 Norbert Juffa           *
  8. ; *                                                     *
  9. ; *******************************************************
  10.  
  11.              TITLE   STCPY
  12.  
  13.  
  14. CODE         SEGMENT BYTE PUBLIC
  15.  
  16.              ASSUME  CS:CODE
  17.  
  18. ; Publics
  19.  
  20.              PUBLIC  SCopy
  21.  
  22. ;-------------------------------------------------------------------------------
  23. ; Copy standard function: Copy (VAR S: STRING; Index, Count: INTEGER): STRING
  24. ;
  25. ; Copy copies Count characters from S, starting at position Index to a 256 byte
  26. ; temporary string buffer. If there are less than Count characters in S after
  27. ; Index, the routine copies all the characters from Index to the end of S (note
  28. ; that this number can be zero).
  29. ;
  30. ; On entry:  [SP+4]  Count
  31. ;            [SP+6]  Index
  32. ;            [SP+8]  Address of source string S
  33. ;            [SP+12] Address of destination string
  34. ;
  35. ; On exit:   [SP]    Address of destination string
  36. ;-------------------------------------------------------------------------------
  37.  
  38. SCopy        PROC    FAR
  39.              CLD                       ; auto increment for string instructions
  40.              PUSH    DS                ; save caller's data segment
  41.              MOV     BX, SP            ; make new frame pointer
  42.              LES     DI, SS:[BX+14]    ; load pointer to destination
  43.              LDS     SI, SS:[BX+10]    ; load pointer to source
  44.              MOV     AX, SS:[BX+6]     ; get copy count
  45.              MOV     BX, SS:[BX+8]     ; get index
  46.              CWD                       ; DX = FFFF if count negative, else DX=0
  47.              NOT     DX                ; DX = FFFF if count positive, else DX=0
  48.              AND     AX, DX            ; Max (count, 0)
  49.              DEC     BX                ; index - 1
  50.              CMP     BH, 80h           ; index-1 < 0 ?
  51.              SBB     DX, DX            ; DX = FFFF if index-1 pos., else DX = 0
  52.              AND     BX, DX            ; Max (index-1, 0)
  53.              MOV     CL, [SI]          ; source length
  54.              INC     SI                ; pointer to 1st char of source
  55.              XOR     CH, CH            ; zero-extend source length to word
  56.              SUB     BX, CX            ; index - 1 - length
  57.              SBB     DX, DX            ; DX=0, if index-1 > length, else DX=FFFF
  58.              AND     BX, DX            ; - max # of chars that could be copied
  59.              NEG     BX                ; maxcount
  60.              SUB     AX, BX            ; count - maxcount
  61.              SBB     DX, DX            ; DX=0, if count > maxcount, else DX=FFFF
  62.              AND     AX, DX            ; count - maxcount = 0, if count>maxcount
  63.              ADD     AX, BX            ; AX = Min (Count, MaxCount)
  64.              SUB     CX, BX            ; CX = Min (Index-1, Length)
  65.              ADD     SI, CX            ; pointer to first char to copy
  66.              STOSB                     ; store length of result
  67.              MOV     CX, AX            ; need it in CX for string instruction
  68.              SHR     CX, 1             ; number of chars odd ?
  69.              JNC     $even_copy        ; no, even
  70.              MOVSB                     ; move single char
  71. $even_copy:  REP     MOVSW             ; copy chars two at a time
  72.              POP     DS                ; restore caller's data segment
  73.              RET     8                 ; leave destination pointer on stack
  74. SCopy        ENDP
  75.  
  76.              ALIGN   4
  77.  
  78. CODE         ENDS
  79.  
  80.              END
  81.