MEM_COPY.ASM 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ;
  2. ; Command & Conquer Red Alert(tm)
  3. ; Copyright 2025 Electronic Arts Inc.
  4. ;
  5. ; This program is free software: you can redistribute it and/or modify
  6. ; it under the terms of the GNU General Public License as published by
  7. ; the Free Software Foundation, either version 3 of the License, or
  8. ; (at your option) any later version.
  9. ;
  10. ; This program is distributed in the hope that it will be useful,
  11. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ; GNU General Public License for more details.
  14. ;
  15. ; You should have received a copy of the GNU General Public License
  16. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;
  18. ;***************************************************************************
  19. ;** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  20. ;***************************************************************************
  21. ;* *
  22. ;* Project Name : LIBRARY *
  23. ;* *
  24. ;* File Name : MEM_COPY.ASM *
  25. ;* *
  26. ;* Programmer : Scott Bowen *
  27. ;* *
  28. ;* Last Update : September 8, 1994 [IML] *
  29. ;* Ported to watcom c32 : 01/03/96 [JRJ] *
  30. ;*-------------------------------------------------------------------------*
  31. ;* Functions: *
  32. ;* Mem_Copy -- Copies from one pointer to another. *
  33. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  34. IDEAL
  35. P386
  36. MODEL USE32 FLAT
  37. LOCALS ??
  38. ;******************************************************************************
  39. ; Much testing was done to determine that only when there are 14 or more bytes
  40. ; being copied does it speed the time it takes to do copies in this algorithm.
  41. ; For this reason and because 1 and 2 byte copies crash, is the special case
  42. ; used. SKB 4/21/94. Tested on 486 66mhz.
  43. OPTIMAL_BYTE_COPY equ 14
  44. ;******************************************************************************
  45. ; External declares so these functions can be called
  46. ;
  47. GLOBAL C Mem_Copy : NEAR
  48. GLOBAL C Largest_Mem_Block : near
  49. CODESEG
  50. ;***************************************************************************
  51. ;* MEM_COPY -- Copies from one pointer to another. *
  52. ;* This routine copies bytes from source to dest. It takes care of *
  53. ;* overlapped memory, and unsigned long copies. *
  54. ;* *
  55. ;* *
  56. ;* *
  57. ;* *
  58. ;* INPUT: *
  59. ;* *
  60. ;* OUTPUT: *
  61. ;* *
  62. ;* WARNINGS: *
  63. ;* *
  64. ;* HISTORY: *
  65. ;* 04/18/1994 SKB : Created. *
  66. ;*=========================================================================*
  67. ; void Mem_Copy(void *source, void *dest, unsigned long bytes_to_copy);
  68. PROC Mem_Copy C near
  69. USES ecx , esi , edi , ebx
  70. ARG source:DWORD
  71. ARG dest:DWORD
  72. ARG bytes:DWORD
  73. ;********************************* Setup ******************************************
  74. cld
  75. mov esi,[source]
  76. mov edi,[dest]
  77. mov ecx,[bytes] ; get number of bytes to copy.
  78. ; check pointers for singularities
  79. cmp esi,edi ; Compare source with dest.
  80. je ??done ; No sence in copying the same pointer.
  81. or esi,0
  82. jz ??done
  83. or edi,0
  84. jz ??done
  85. cmp ecx,OPTIMAL_BYTE_COPY ; see notes above about equate.
  86. jge ??normal ; If >= MAX(2,OPTIMAL_BYTE_COPY), do normal dword copy.
  87. ;******************************** Special case <= 2 *******************************
  88. ;
  89. ; This section must be called for bytes <= 2 since the other case will crash. It
  90. ; optionally uses OPTIMAL_BYTE_COPY for the cut off point. This is because after
  91. ; extensive testing, it was proved that only at that point (14 or more bytes) does
  92. ; it become quicker to use the dword copy method.
  93. cmp esi,edi ; Compare source with dest.
  94. jge ??do_move ; if source greater do forward copy.
  95. lea esi,[esi+ecx-1]
  96. std ; Opps, wrong, force the pointers to decrement.
  97. lea edi,[edi+ecx-1]
  98. ??do_move:
  99. rep movsb ; move the one or two bytes.
  100. cld
  101. ??done:
  102. ret
  103. ;************************** back or forth, that is the question *******************
  104. ??normal:
  105. mov ebx,ecx
  106. cmp esi,edi ; Compare source with dest.
  107. jge ??forward ; if source greater do forward copy.
  108. ;********************************* Backward ***************************************
  109. ??backward:
  110. lea ecx,[edi+ebx]
  111. std
  112. lea edi,[edi+ebx-1]
  113. and ecx,3 ; Get non aligned bytes.
  114. lea esi,[esi+ebx-1]
  115. sub ebx,ecx ; remove that from the total size to be copied later.
  116. rep movsb ; do the copy.
  117. sub esi,3
  118. mov ecx,ebx ; Get number of bytes left.
  119. sub edi,3
  120. shr ecx,2 ; Do 4 bytes at a time.
  121. rep movsd ; do the dword copy.
  122. mov ecx,ebx
  123. add esi,3
  124. add edi,3
  125. and ecx,03h
  126. rep movsb ; finnish the remaining bytes.
  127. cld
  128. ret
  129. ;********************************* Forward ***************************************
  130. ??forward:
  131. cld
  132. mov ecx,edi ; get destination pointer.
  133. neg ecx
  134. and ecx,3 ; Get non aligned bytes.
  135. sub ebx,ecx ; remove that from the total size to be copied later.
  136. rep movsb ; do the copy.
  137. mov ecx,ebx ; Get number of bytes left.
  138. shr ecx,2 ; Do 4 bytes at a time.
  139. rep movsd ; do the dword copy.
  140. mov ecx, ebx
  141. and ecx,03h
  142. rep movsb ; finnish the remaining bytes.
  143. ret
  144. ENDP Mem_Copy
  145. PROC Largest_Mem_Block C near
  146. uses esi , edi , ebx , ecx , edx
  147. local mem_struct : dword : 16
  148. mov eax , 0500h
  149. lea edi , [ mem_struct ]
  150. int 31h
  151. mov eax , [ mem_struct ]
  152. ret
  153. ENDP Largest_Mem_Block
  154. END
  155.