home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_06_03 / v6n3039a.txt < prev    next >
Text File  |  1989-09-28  |  4KB  |  175 lines

  1. ;
  2. ; David D. Clark
  3. ;
  4. ; 9 May 1987
  5. ; 11 December 1987 -- changes for DLC 3.12 (save DI and SI registers)
  6. ;
  7.  
  8.         Include macros.asm
  9.  
  10. begcode
  11.  
  12.  
  13. ;
  14. ; memmove -- move a block of data len bytes long from src to dest.
  15. ; The function determines whether the move should be done from
  16. ; the left or right end of the blocks.  After deciding, a jump is
  17. ; made into either moveleft() or moveright(), as appropriate, to
  18. ; actually perform word optimized moves.  A pointer to dest is
  19. ; returned.
  20. ;
  21. ; The Datalight C declaration would look like:
  22. ;
  23. ;    void *memmove(dest, src, len)
  24. ;    void *dest, *src;
  25. ;    unsigned int len;
  26. ;
  27.  
  28.         Public    memmove
  29.  
  30. func        memmove
  31.  
  32.         PUSH    BP        ; save old frame pointer
  33.         MOV    BP,SP        ; set new frame pointer
  34.         PUSH    DI        ; NEW with DLC 3.12
  35.         PUSH    SI        ; "               "
  36.         MOV    CX,P+SIZEPTR+SIZEPTR[BP]    ; len
  37.     If SPTR
  38.         MOV    DI,P+[BP]    ; ES:DI = dest
  39.         MOV    SI,P+SIZEPTR[BP]; DS:SI = src
  40.         MOV    AX,DI        ; original dest is returned
  41.     ELSE
  42.         LES    DI,P+[BP]    ; ES:DI = dest
  43.         PUSH    DS        ; save data segment
  44.         LDS    SI,P+SIZEPTR[BP]; DS:SI = src
  45.         MOV    AX,ES
  46.         MOV    BX,DI        ; AX:BX = original value of dest
  47.     ENDIF
  48.  
  49.         CMP    SI,DI        ; is src above dest?
  50.         JAE    ml1        ; move from left
  51.         JMP    Short mr1    ; move from right
  52.  
  53. memmove        EndP
  54.  
  55.  
  56. ;
  57. ; moveleft -- move a block of data len bytes long from src to dest
  58. ; starting at the left end.  The move is optimized to word moves
  59. ; and a (possible) single byte move.  A pointer to dest is returned.
  60. ;
  61. ; The Datalight C declaration would look like:
  62. ;
  63. ;    void *moveleft(dest, src, len)
  64. ;    void *dest, *src;
  65. ;    unsigned int len;
  66. ;
  67.  
  68.         Public    moveleft
  69.  
  70. func        moveleft
  71.  
  72.         PUSH    BP        ; save old frame pointer
  73.         MOV    BP,SP        ; set new frame pointer
  74.         PUSH    DI        ; NEW with DLC 3.12
  75.         PUSH    SI        ; "               "
  76.         MOV    CX,P+SIZEPTR+SIZEPTR[BP]    ; len
  77.     If SPTR
  78.         MOV    DI,P+[BP]    ; ES:DI = dest
  79.         MOV    SI,P+SIZEPTR[BP]; DS:SI = src
  80.         MOV    AX,DI        ; original dest is returned
  81.     ELSE
  82.         LES    DI,P+[BP]    ; ES:DI = dest
  83.         PUSH    DS        ; save data segment
  84.         LDS    SI,P+SIZEPTR[BP]; DS:SI = src
  85.         MOV    AX,ES
  86.         MOV    BX,DI        ; AX:BX = original value of dest
  87.     ENDIF
  88.  
  89. ml1:                    ; alternate entry point
  90.  
  91.         CLD            ; set direction forward (needed?)
  92.         SHR    CX,1        ; convert bytes to words
  93.         JZ    mlb        ; jump if no full words to move
  94.     REP    MOVSW            ; move the words
  95. mlb:        JNC    mldone        ; no bytes to move
  96.         MOVSB            ; move the left over byte
  97. mldone:
  98.     If LPTR
  99.         POP    DS        ; recover DS
  100.     ENDIF
  101.         POP    SI        ; NEW with DLC 3.12
  102.         POP    DI        ; "               "
  103.         POP    BP        ; restore old frame pointer
  104.         RET
  105.  
  106. moveleft    EndP
  107.  
  108.  
  109. ;
  110. ; moveright -- move a block of data len bytes long from src to dest
  111. ; starting at the right end.  The move is optimized to word moves
  112. ; and a (possible) single byte move.  A pointer to dest is returned.
  113. ;
  114. ; The Datalight C declaration would look like:
  115. ;
  116. ;    void *moveright(dest, src, len)
  117. ;    void *dest. *src;
  118. ;    unsigned int len;
  119. ;
  120.  
  121.         Public    moveright
  122.  
  123. func        moveright
  124.  
  125.         PUSH    BP        ; save old frame pointer
  126.         MOV    BP,SP        ; set new frame pointer
  127.         PUSH    DI        ; NEW with DLC 3.12
  128.         PUSH    SI        ; "               "
  129.         MOV    CX,P+SIZEPTR+SIZEPTR[BP]    ; len
  130.     If SPTR
  131.         MOV    DI,P+[BP]    ; ES:DI = dest
  132.         MOV    SI,P+SIZEPTR[BP]; DS:SI = src
  133.         MOV    AX,DI        ; original dest is returned
  134.     ELSE
  135.         LES    DI,P+[BP]    ; ES:DI = dest
  136.         PUSH    DS        ; save data segment
  137.         LDS    SI,P+SIZEPTR[BP]; DS:SI = src
  138.         MOV    AX,ES
  139.         MOV    BX,DI        ; AX:BX = original value of dest
  140.     ENDIF
  141.  
  142. mr1:                    ; alternate entry point
  143.  
  144.         ADD    SI,CX        ; start at high (right) end
  145.         DEC    SI        ; last source byte
  146.         DEC    SI        ; last source word
  147.         ADD    DI,CX        ; start at high (right) end
  148.         DEC    DI        ; last target byte
  149.         DEC    DI        ; last target word
  150.         STD            ; set direction backward
  151.         SHR    CX,1        ; convert bytes to words
  152.         JZ    mrb        ; jump if no full words to move
  153.     REP    MOVSW            ; move the words
  154. mrb:        JNC    mrdone        ; no bytes to move
  155.         INC    SI        ; point to source byte
  156.         INC    DI        ; point to target byte
  157.         MOVSB            ; move the left over byte
  158.  
  159. mrdone:        CLD            ; reset direction flag before exit
  160.     If LPTR
  161.         POP    DS        ; recover DS
  162.     ENDIF
  163.         POP    SI        ; NEW with DLC 3.12
  164.         POP    DI        ; "               "
  165.         POP    BP        ; restore old frame pointer
  166.         RET
  167.  
  168. moveright    EndP
  169.  
  170.         endcode
  171.  
  172.         End
  173.  
  174. 
  175.