home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tasm / ibytecpy.asm < prev    next >
Assembly Source File  |  1988-08-28  |  4KB  |  107 lines

  1.  
  2. ;   FILENAME: IBYTECPY.ASM
  3. ;
  4. ;   Copyright (c) 1988 by Borland International
  5. ;
  6. ;   DESCRIPTION: This module implements the routine ByteCopy. The routine
  7. ;   copys an array of byte values from one location in memory to another.
  8. ;   Note that the routine checks which direction it should do the copy in,
  9. ;   thus avoiding overwriting any data during the copy. This module uses
  10. ;   ideal mode syntax and simplified segment directives.
  11. ;
  12. ;   ASSEMBLY INSTRUCTIONS: To assemble this module use the following
  13. ;   TASM command line.
  14. ;
  15. ;       TASM /dMDL=memorymodel ibytecpy
  16. ;
  17. ;   'memorymodel' in the above command line may be replaced by TINY, SMALL,
  18. ;   MEDIUM, COMPACT, LARGE or HUGE. If assembling this module to run on
  19. ;   a 286/386 machine, turn on the P286 directive in order to take advantage of
  20. ;   286/386 specific instructions. For example:
  21. ;
  22. ;       TASM /dMDL=memorymodel /jP286 ibytecpy
  23.  
  24. %tabsize 4
  25.  
  26. ifndef  MDL
  27.     display "Error: This module requires that you provide a memory model"
  28.     display "       definition on the command line. I.E. /dMDL=SMALL."
  29.     err ; Force a fatal error
  30. else
  31.  
  32.     ideal                               ; Use TASM's Ideal mode
  33.     model   MDL                         ; Define the memory model
  34.  
  35.     include "ibios.mac"
  36.     include "imacros.mac"
  37.     include "bios.inc"
  38.  
  39.     codeseg
  40.  
  41.     global  ByteCopy:proc               ; Define public proc
  42.  
  43.     proc    ByteCopy
  44.  
  45.     ;
  46.     ;   This function copies an array of bytes from one location in memory
  47.     ;   to another. Before doing the copy the routine compares the absolute
  48.     ;   addresses of the two memory locations and sets the direction flag
  49.     ;   appropriately to avoid destroying any data. Note that this routine
  50.     ;   assumes that the pointers passed to it are normalized if the arrays
  51.     ;   being copied are longer than 0FFF0h bytes. I.E. Offset+ax is
  52.     ;   <= 0FFFFh.
  53.     ;
  54.     ;   Input
  55.     ;       SourcePtr - far pointer to source array
  56.     ;       DestPtr   - far pointer to destination array
  57.     ;       ax - number of bytes to copy
  58.     ;   Output
  59.     ;       es:di - points to 1 position past last destination byte
  60.     ;       ds:si - points to 1 position past last source byte
  61.     ;   Calling convention
  62.     ;       Pascal
  63.     ;   Registers modified
  64.     ;       bx, cx, dx, di, si, es, Flags
  65.     ;
  66.  
  67.     ; Define parameters passed on stack
  68.  
  69.     arg DestPtr:dword, SourcePtr:dword=ParmSize
  70.  
  71.         push    bp              ; Set up the stack to access parameters
  72.         mov     bp, sp
  73.         push    ds
  74.         lds     si, [SourcePtr] ; Get the pointers off the stack
  75.         les     di, [DestPtr]
  76.  
  77.         ; Compare absolute addresses of pointers
  78.  
  79.         push    ax
  80.         CompareFarPointers  ds, si, es, di
  81.         pop     ax
  82.         jl      short HighToLow ; Move from high address to low address
  83.         cld     ; Move from low address to high address
  84.         jmp     short DoMove
  85.  
  86.     HighToLow:
  87.         std
  88.         add     si, ax          ; Adjust offsets so they can be decremented
  89.         dec     si
  90.         add     di, ax          ; instead of incremented.
  91.         dec     di
  92.  
  93.     DoMove:
  94.         mov     cx, ax          ; Copy # of bytes to move into cx
  95.         jcxz    DontDoMove
  96.         rep     movsb           ; Copy the bytes
  97.     DontDoMove:
  98.         pop     ds              ; restore ds if necessary
  99.         pop     bp              ; Restore the stack pointer
  100.         ret     ParmSize        ; Clean up stack since we're using Pascal
  101.                                 ; calling conventions.
  102.     endp    ByteCopy
  103.  
  104. endif   ; ifndef MDL
  105.  
  106. end
  107.