mmutils.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. {=========================================================================}
  2. procedure _SwapSmall(var a, b: SmallInt);
  3. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  4. var
  5. Temp: SmallInt;
  6. begin
  7. Temp := a;
  8. a := b;
  9. b := Temp;
  10. end;
  11. {=========================================================================}
  12. procedure _SwapInt(var a, b: integer);
  13. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  14. var
  15. Temp: integer;
  16. begin
  17. Temp := a;
  18. a := b;
  19. b := Temp;
  20. end;
  21. {=========================================================================}
  22. procedure _SwapLong(var a, b: Longint);
  23. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  24. var
  25. Temp: Longint;
  26. begin
  27. Temp := a;
  28. a := b;
  29. b := Temp;
  30. end;
  31. {=========================================================================}
  32. function _Min(a, b: Longint): Longint;
  33. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  34. begin
  35. if a > b then Result := b
  36. else Result := a;
  37. end;
  38. {=========================================================================}
  39. function _Max(a, b: Longint): Longint;
  40. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  41. begin
  42. if a > b then Result := a
  43. else Result := b;
  44. end;
  45. {=========================================================================}
  46. function _MinMax(X, Min, Max: Longint): Longint;
  47. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  48. begin
  49. if (X < Min) then X := Min
  50. else if (X > Max) then X := Max;
  51. Result := X;
  52. end;
  53. {=========================================================================}
  54. function _Limit(X, Min, Max: Longint): Longint;
  55. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  56. begin
  57. if (Max >= Min) then
  58. begin
  59. if (X < Min) then X := Min
  60. else if (X > Max) then X := Max;
  61. end
  62. else
  63. begin
  64. if (X < Max) then X := Max
  65. else if (X > Min) then X := Min;
  66. end;
  67. Result := X;
  68. end;
  69. {=========================================================================}
  70. function _InMinMax(X,Min,Max: Longint): Boolean;
  71. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  72. begin
  73. { if Min > Max then Result is never true }
  74. if (X < Min) then Result := False
  75. else if (X > Max) then Result := False
  76. else Result := True;
  77. end;
  78. {=========================================================================}
  79. function _InRange(X, Min, Max: Longint): Boolean;
  80. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}
  81. begin
  82. if (Max >= Min) then
  83. begin
  84. if (X < Min) then Result := False
  85. else if (X > Max) then Result := False
  86. else Result := True;
  87. end
  88. else
  89. begin
  90. if (X < Max) then Result := False
  91. else if (X > Min) then Result := False
  92. else Result := True;
  93. end;
  94. end;
  95. {=========================================================================}
  96. procedure _incHuge(var Pointer; nBytes: Longint);
  97. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}assembler;
  98. asm
  99. {$IFDEF WIN32}
  100. add [eax], edx
  101. {$ELSE}
  102. mov ax, nBytes.Word[0]
  103. mov dx, nBytes.Word[2]
  104. xor bx, bx
  105. les di, Pointer
  106. add es:[di].Word[0], ax
  107. adc bx, dx
  108. shl bx, 3 { inc Selector by SelectorInc = 8 }
  109. add es:[di].Word[2], bx
  110. {$ENDIF}
  111. end;
  112. {=========================================================================}
  113. procedure _GlobalFillMem(var X; Cnt: Longint; Value: Byte);
  114. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}assembler;
  115. asm
  116. {$IFDEF WIN32}
  117. { ->EAX Pointer to destination }
  118. { EDX count }
  119. { CL value }
  120. test eax, eax
  121. jz @@error
  122. test edx, edx
  123. jz @@error
  124. push edi
  125. mov edi, eax { Point EDI to destination }
  126. mov ch, cl { Fill EAX with value repeated 4 times }
  127. mov eax, ecx
  128. shl eax, 16
  129. mov ax, cx
  130. mov ecx, edx
  131. sar ecx, 2
  132. js @@exit
  133. cld
  134. rep stosd { Fill count DIV 4 dwords }
  135. mov ecx, edx
  136. and ecx, 3
  137. rep stosb { Fill count MOD 4 bytes }
  138. @@exit:
  139. pop edi
  140. @@error:
  141. {$ELSE}
  142. db 66h
  143. xor di, di
  144. les di, X { Point ES:[EDI] to destination }
  145. mov cl, Value
  146. mov ch, cl { Fill EAX with value repeated 4 times }
  147. mov ax, cx
  148. db 66h
  149. shl ax, 16
  150. mov ax, cx
  151. db 66h
  152. mov cx, word ptr Cnt
  153. db 66h
  154. sar cx, 2
  155. js @@exit
  156. db 66h
  157. rep
  158. db 67h
  159. stosw { Fill count DIV 4 dwords }
  160. db 66h
  161. mov cx, word ptr Cnt
  162. db 66h
  163. db 83h
  164. db 0E1h
  165. db 03h { and ecx, 3 }
  166. rep
  167. db 67h
  168. stosb { Fill count MOD 4 bytes }
  169. @@exit:
  170. {$ENDIF}
  171. end;
  172. {=========================================================================}
  173. procedure _GlobalFillLong(var X; Cnt: Longint; Value: Longint);
  174. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}assembler;
  175. asm
  176. {$IFDEF WIN32}
  177. { ->EAX Pointer to destination }
  178. { EDX count }
  179. { CL value }
  180. push edi
  181. mov edi, eax { Point EDI to destination }
  182. mov eax, ecx
  183. mov ecx, edx
  184. sar ecx, 2
  185. js @@exit
  186. cld
  187. rep stosd { Fill count DIV 4 dwords }
  188. mov ecx, edx
  189. and ecx, 3
  190. rep stosb { Fill count MOD 4 bytes }
  191. @@exit:
  192. pop edi
  193. {$ELSE}
  194. db 66h
  195. xor di, di
  196. les di, X { Point ES:[EDI] to destination }
  197. db 66h
  198. mov ax, word ptr Value
  199. db 66h
  200. mov cx, word ptr Cnt
  201. db 66h
  202. sar cx, 2
  203. js @@exit
  204. db 66h
  205. rep
  206. db 67h
  207. stosw { Fill count DIV 4 dwords }
  208. db 66h
  209. mov cx, word ptr Cnt
  210. db 66h
  211. db 83h
  212. db 0E1h
  213. db 03h { and ecx, 3 }
  214. rep
  215. db 67h
  216. stosb { Fill count MOD 4 bytes }
  217. @@exit:
  218. {$ENDIF}
  219. end;
  220. {=========================================================================}
  221. procedure _GlobalMoveMem(const Source; var Dest; Cnt: Longint);
  222. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}assembler;
  223. asm
  224. {$IFDEF WIN32}
  225. { ->EAX Pointer to source }
  226. { EDX Pointer to destination }
  227. { ECX Count }
  228. test ecx, ecx
  229. jz @@error
  230. push esi
  231. push edi
  232. mov esi, eax
  233. mov edi, edx
  234. mov eax, ecx
  235. cmp edi, esi
  236. jg @@down
  237. je @@exit
  238. sar ecx, 2 { copy count DIV 4 dwords }
  239. js @@exit
  240. cld
  241. rep movsd
  242. mov ecx, eax
  243. and ecx, 3
  244. rep movsb { copy count MOD 4 bytes }
  245. jmp @@exit
  246. @@down:
  247. lea esi, [esi+ecx-4] { point ESI to last dword of source }
  248. lea edi, [edi+ecx-4] { point EDI to last dword of dest }
  249. sar ecx, 2 { copy count DIV 4 dwords }
  250. js @@exit
  251. std
  252. rep movsd
  253. mov ecx, eax
  254. and ecx, 3 { copy count MOD 4 bytes }
  255. add esi, 4-1 { point to last byte of rest }
  256. add edi, 4-1
  257. rep movsb
  258. cld
  259. @@exit:
  260. pop edi
  261. pop esi
  262. @@error:
  263. {$ELSE}
  264. mov dx, ds { save DS in DX }
  265. db 66h
  266. xor si, si
  267. lds si, Source { DS:[esi] point to Source }
  268. db 66h
  269. xor di, di
  270. les di, Dest { ES:[EDI] point to destination }
  271. db 66h
  272. mov cx, word ptr Cnt
  273. db 66h
  274. cmp di, si
  275. jg @@down
  276. cld
  277. db 66h
  278. sar cx, 2
  279. js @@exit
  280. db 66h
  281. rep
  282. db 67h
  283. movsw { move count DIV 4 dwords }
  284. db 66h
  285. mov cx, word ptr Cnt
  286. db 66h
  287. db 83h
  288. db 0E1h
  289. db 03h { and ecx, 3 }
  290. db 66h
  291. rep
  292. db 67h
  293. movsb { move count MOD 4 bytes }
  294. jmp @@exit
  295. @@down:
  296. db 66h
  297. add si, cx
  298. db 66h
  299. add di, cx
  300. db 66h
  301. sub si, 4
  302. db 66h
  303. sub di, 4
  304. std
  305. db 66h
  306. sar cx, 2
  307. js @@exit
  308. db 66h
  309. rep
  310. db 67h
  311. movsw { move count DIV 4 dwords }
  312. db 66h
  313. mov cx, word ptr Cnt
  314. db 66h
  315. db 83h
  316. db 0E1h
  317. db 03h { and ecx, 3 }
  318. db 66h
  319. add si, 4-1 { point to last byte of rest }
  320. db 66h
  321. add di, 4-1
  322. db 66h
  323. rep
  324. db 67h
  325. movsb { move count MOD 4 bytes }
  326. @@exit:
  327. cld
  328. mov ds, dx { restore DS }
  329. {$ENDIF}
  330. end;
  331. {=========================================================================}
  332. function _GlobalCmpMem(const p1, p2; Cnt: Longint): Boolean;
  333. {$IFDEF USEDLL}export;{$ELSE}Far;{$ENDIF}assembler;
  334. asm
  335. {$IFDEF WIN32}
  336. { ->EAX Pointer to source }
  337. { EDX Pointer to destination }
  338. { ECX Count }
  339. push edi
  340. push esi
  341. cld
  342. mov edi, eax
  343. mov esi, edx
  344. xor eax, eax
  345. mov edx, ecx
  346. shr ecx, 2
  347. or ecx, ecx
  348. jz @@Rest
  349. repz cmpsd
  350. jnz @@NotEqual
  351. @@Rest:
  352. mov ecx, edx
  353. and ecx, 3
  354. or ecx, ecx
  355. jz @@Equal
  356. repz cmpsb
  357. jnz @@NotEqual
  358. @@Equal:
  359. mov ax, True
  360. @@NotEqual:
  361. pop esi
  362. pop edi
  363. {$ELSE}
  364. mov bx, ds { save DS in BX }
  365. cld
  366. xor ax, ax
  367. db 66h
  368. xor si, si
  369. lds si, p1 { DS:[esi] point to p1 }
  370. db 66h
  371. xor di, di
  372. les di, p2 { ES:[EDI] point to p2 }
  373. db 66h
  374. mov cx, word ptr Cnt
  375. db 66h
  376. mov dx, cx
  377. db 66h
  378. shr cx, 2
  379. db 66h
  380. or cx, cx
  381. jz @@Rest
  382. db 66h
  383. repz
  384. db 67h
  385. cmpsw { compare count DIV 4 dwords }
  386. jnz @@NotEqual
  387. @@Rest:
  388. db 66h
  389. mov cx, dx
  390. db 66h
  391. db 83h
  392. db 0E1h
  393. db 03h { and ecx, 3 }
  394. db 66h
  395. or cx, cx
  396. jz @@Equal
  397. repz
  398. db 67h
  399. cmpsb
  400. jnz @@NotEqual
  401. @@Equal:
  402. mov ax, True
  403. @@NotEqual:
  404. mov ds, bx { restore ds }
  405. {$ENDIF}
  406. end;