scitech.mac 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. ;****************************************************************************
  2. ;*
  3. ;* ========================================================================
  4. ;*
  5. ;* The contents of this file are subject to the SciTech MGL Public
  6. ;* License Version 1.0 (the "License"); you may not use this file
  7. ;* except in compliance with the License. You may obtain a copy of
  8. ;* the License at http://www.scitechsoft.com/mgl-license.txt
  9. ;*
  10. ;* Software distributed under the License is distributed on an
  11. ;* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12. ;* implied. See the License for the specific language governing
  13. ;* rights and limitations under the License.
  14. ;*
  15. ;* The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
  16. ;*
  17. ;* The Initial Developer of the Original Code is SciTech Software, Inc.
  18. ;* All Rights Reserved.
  19. ;*
  20. ;* ========================================================================
  21. ;*
  22. ;* Language: NetWide Assembler (NASM) or Turbo Assembler (TASM)
  23. ;* Environment: Any Intel Environment
  24. ;*
  25. ;* Description: Macros to provide memory model independant assembly language
  26. ;* module for C programming. Supports the large and flat memory
  27. ;* models.
  28. ;*
  29. ;* The defines that you should use when assembling modules that
  30. ;* use this macro package are:
  31. ;*
  32. ;* __LARGE__ Assemble for 16-bit large model
  33. ;* __FLAT__ Assemble for 32-bit FLAT memory model
  34. ;* __NOU__ No underscore for all external C labels
  35. ;* __NOU_VAR__ No underscore for global variables only
  36. ;*
  37. ;* The default settings are for 16-bit large memory model with
  38. ;* leading underscores for symbol names.
  39. ;*
  40. ;* The main intent of the macro file is to enable programmers
  41. ;* to write _one_ set of source that can be assembled to run
  42. ;* in either 16 bit real and protected modes or 32 bit
  43. ;* protected mode without the need to riddle the code with
  44. ;* 'if flatmodel' style conditional assembly (it is still there
  45. ;* but nicely hidden by a macro layer that enhances the
  46. ;* readability and understandability of the resulting code).
  47. ;*
  48. ;****************************************************************************
  49. ; Include the appropriate version in here depending on the assembler. NASM
  50. ; appears to always try and parse code, even if it is in a non-compiling
  51. ; block of a ifdef expression, and hence crashes if we include the TASM
  52. ; macro package in the same header file. Hence we split the macros up into
  53. ; two separate header files.
  54. ifdef __NASM_MAJOR__
  55. ;============================================================================
  56. ; Macro package when compiling with NASM.
  57. ;============================================================================
  58. ; Turn off underscores for globals if disabled for all externals
  59. %ifdef __NOU__
  60. %define __NOU_VAR__
  61. %endif
  62. ; Define the __WINDOWS__ symbol if we are compiling for any Windows
  63. ; environment
  64. %ifdef __WINDOWS16__
  65. %define __WINDOWS__ 1
  66. %endif
  67. %ifdef __WINDOWS32__
  68. %define __WINDOWS__ 1
  69. %define __WINDOWS32_386__ 1
  70. %endif
  71. ; Macros for accessing 'generic' registers
  72. %ifdef __FLAT__
  73. %idefine _ax eax
  74. %idefine _bx ebx
  75. %idefine _cx ecx
  76. %idefine _dx edx
  77. %idefine _si esi
  78. %idefine _di edi
  79. %idefine _bp ebp
  80. %idefine _sp esp
  81. %idefine _es
  82. %idefine UCHAR BYTE ; Size of a character
  83. %idefine USHORT WORD ; Size of a short
  84. %idefine UINT DWORD ; Size of an integer
  85. %idefine ULONG DWORD ; Size of a long
  86. %idefine BOOL DWORD ; Size of a boolean
  87. %idefine DPTR DWORD ; Size of a data pointer
  88. %idefine FDPTR FWORD ; Size of a far data pointer
  89. %idefine NDPTR DWORD ; Size of a near data pointer
  90. %idefine CPTR DWORD ; Size of a code pointer
  91. %idefine FCPTR FWORD ; Size of a far code pointer
  92. %idefine NCPTR DWORD ; Size of a near code pointer
  93. %idefine FPTR NEAR ; Distance for function pointers
  94. %idefine DUINT dd ; Declare a integer variable
  95. %idefine intsize 4
  96. %idefine flatmodel 1
  97. %else
  98. %idefine _ax ax
  99. %idefine _bx bx
  100. %idefine _cx cx
  101. %idefine _dx dx
  102. %idefine _si si
  103. %idefine _di di
  104. %idefine _bp bp
  105. %idefine _sp sp
  106. %idefine _es es:
  107. %idefine UCHAR BYTE ; Size of a character
  108. %idefine USHORT WORD ; Size of a short
  109. %idefine UINT WORD ; Size of an integer
  110. %idefine ULONG DWORD ; Size of a long
  111. %idefine BOOL WORD ; Size of a boolean
  112. %idefine DPTR DWORD ; Size of a data pointer
  113. %idefine FDPTR DWORD ; Size of a far data pointer
  114. %idefine NDPTR WORD ; Size of a near data pointer
  115. %idefine CPTR DWORD ; Size of a code pointer
  116. %idefine FCPTR DWORD ; Size of a far code pointer
  117. %idefine NCPTR WORD ; Size of a near code pointer
  118. %idefine FPTR FAR ; Distance for function pointers
  119. %idefine DUINT dw ; Declare a integer variable
  120. %idefine intsize 2
  121. %endif
  122. %idefine invert ~
  123. %idefine offset
  124. %idefine use_nasm
  125. ; Convert all jumps to near jumps, since NASM does not so this automatically
  126. %idefine jo jo near
  127. %idefine jno jno near
  128. %idefine jz jz near
  129. %idefine jnz jnz near
  130. %idefine je je near
  131. %idefine jne jne near
  132. %idefine jb jb near
  133. %idefine jbe jbe near
  134. %idefine ja ja near
  135. %idefine jae jae near
  136. %idefine jl jl near
  137. %idefine jle jle near
  138. %idefine jg jg near
  139. %idefine jge jge near
  140. %idefine jc jc near
  141. %idefine jnc jnc near
  142. %idefine js js near
  143. %idefine jns jns near
  144. %ifdef DOUBLE
  145. %idefine REAL QWORD
  146. %idefine DREAL dq
  147. %else
  148. %idefine REAL DWORD
  149. %idefine DREAL dd
  150. %endif
  151. ; Boolean truth values (same as those in debug.h)
  152. %idefine False 0
  153. %idefine True 1
  154. %idefine No 0
  155. %idefine Yes 1
  156. %idefine Yes 1
  157. ; Macro to be invoked at the start of all modules to set up segments for
  158. ; later use. Does nothing for NASM.
  159. %imacro header 1
  160. %endmacro
  161. ; Macro to begin a data segment
  162. %imacro begdataseg 1
  163. %ifdef __GNUC__
  164. segment .data public class=DATA use32 flat
  165. %else
  166. %ifdef flatmodel
  167. segment _DATA public align=4 class=DATA use32 flat
  168. %else
  169. segment _DATA public align=4 class=DATA use16
  170. %endif
  171. %endif
  172. %endmacro
  173. ; Macro to end a data segment
  174. %imacro enddataseg 1
  175. %endmacro
  176. ; Macro to begin a code segment
  177. %imacro begcodeseg 1
  178. %ifdef __GNUC__
  179. segment .text public class=CODE use32 flat
  180. %else
  181. %ifdef flatmodel
  182. segment _TEXT public align=16 class=CODE use32 flat
  183. %else
  184. segment %1_TEXT public align=16 class=CODE use16
  185. %endif
  186. %endif
  187. %endmacro
  188. ; Macro to begin a near code segment
  189. %imacro begcodeseg_near 0
  190. %ifdef __GNUC__
  191. segment .text public class=CODE use32 flat
  192. %else
  193. %ifdef flatmodel
  194. segment _TEXT public align=16 class=CODE use32 flat
  195. %else
  196. segment _TEXT public align=16 class=CODE use16
  197. %endif
  198. %endif
  199. %endmacro
  200. ; Macro to end a code segment
  201. %imacro endcodeseg 1
  202. %endmacro
  203. ; Macro to end a near code segment
  204. %imacro endcodeseg_near 0
  205. %endmacro
  206. ; Macro for an extern C symbol. If the C compiler requires leading
  207. ; underscores, then the underscores are added to the symbol names, otherwise
  208. ; they are left off. The symbol name is referenced in the assembler code
  209. ; using the non-underscored symbol name.
  210. %imacro cextern 2
  211. %ifdef __NOU_VAR__
  212. extern %1
  213. %else
  214. extern _%1
  215. %define %1 _%1
  216. %endif
  217. %endmacro
  218. %imacro cexternfunc 2
  219. %ifdef __NOU__
  220. extern %1
  221. %else
  222. extern _%1
  223. %define %1 _%1
  224. %endif
  225. %endmacro
  226. ; Macro for a public C symbol. If the C compiler requires leading
  227. ; underscores, then the underscores are added to the symbol names, otherwise
  228. ; they are left off. The symbol name is referenced in the assembler code
  229. ; using the non-underscored symbol name.
  230. %imacro cpublic 1
  231. %ifdef __NOU_VAR__
  232. global %1
  233. %1:
  234. %else
  235. global _%1
  236. _%1:
  237. %define %1 _%1
  238. %endif
  239. %endmacro
  240. ; Macro for an global C symbol. If the C compiler requires leading
  241. ; underscores, then the underscores are added to the symbol names, otherwise
  242. ; they are left off. The symbol name is referenced in the assembler code
  243. ; using the non-underscored symbol name.
  244. %imacro cglobal 1
  245. %ifdef __NOU_VAR__
  246. global %1
  247. %else
  248. global _%1
  249. %define %1 _%1
  250. %endif
  251. %endmacro
  252. ; Macro for an global C function symbol. If the C compiler requires leading
  253. ; underscores, then the underscores are added to the symbol names, otherwise
  254. ; they are left off. The symbol name is referenced in the assembler code
  255. ; using the non-underscored symbol name.
  256. %imacro cglobalfunc 1
  257. %ifdef __NOU__
  258. global %1
  259. %else
  260. global _%1
  261. %define %1 _%1
  262. %endif
  263. %endmacro
  264. ; Macro to start a C callable function. This will be a far function for
  265. ; 16-bit code, and a near function for 32-bit code.
  266. %imacro cprocstatic 1
  267. %push cproc
  268. %1:
  269. %ifdef flatmodel
  270. %stacksize flat
  271. %define ret retn
  272. %else
  273. %stacksize large
  274. %define ret retf
  275. %endif
  276. %assign %$localsize 0
  277. %endmacro
  278. %imacro cprocstart 1
  279. %push cproc
  280. cglobalfunc %1
  281. %1:
  282. %ifdef flatmodel
  283. %stacksize flat
  284. %define ret retn
  285. %else
  286. %stacksize large
  287. %define ret retf
  288. %endif
  289. %assign %$localsize 0
  290. %endmacro
  291. ; This macro sets up a procedure to be exported from a 16 bit DLL. Since the
  292. ; calling conventions are always _far _pascal for 16 bit DLL's, we actually
  293. ; rename this routine with an extra underscore with 'C' calling conventions
  294. ; and a small DLL stub will be provided by the high level code to call the
  295. ; assembler routine.
  296. %imacro cprocstartdll16 1
  297. %ifdef __WINDOWS16__
  298. cprocstart _%1
  299. %else
  300. cprocstart %1
  301. %endif
  302. %endmacro
  303. ; Macro to start a C callable near function.
  304. %imacro cprocnear 1
  305. %push cproc
  306. cglobalfunc %1
  307. %1:
  308. %define ret retn
  309. %ifdef flatmodel
  310. %stacksize flat
  311. %else
  312. %stacksize small
  313. %endif
  314. %assign %$localsize 0
  315. %endmacro
  316. ; Macro to start a C callable far function.
  317. %imacro cprocfar 1
  318. %push cproc
  319. cglobalfunc %1
  320. %1:
  321. %define ret retf
  322. %ifdef flatmodel
  323. %stacksize flat
  324. %else
  325. %stacksize large
  326. %endif
  327. %assign %$localsize 0
  328. %endmacro
  329. ; Macro to end a C function
  330. %imacro cprocend 0
  331. %pop
  332. %endmacro
  333. ; Macros for entering and exiting C callable functions. Note that we must
  334. ; always save and restore the SI and DI registers for C functions, and for
  335. ; 32 bit C functions we also need to save and restore EBX and clear the
  336. ; direction flag.
  337. %imacro enter_c 0
  338. push _bp
  339. mov _bp,_sp
  340. %ifnidn %$localsize,0
  341. sub _sp,%$localsize
  342. %endif
  343. %ifdef flatmodel
  344. push ebx
  345. %endif
  346. push _si
  347. push _di
  348. %endmacro
  349. %imacro leave_c 0
  350. pop _di
  351. pop _si
  352. %ifdef flatmodel
  353. pop ebx
  354. cld
  355. %endif
  356. %ifnidn %$localsize,0
  357. mov _sp,_bp
  358. %endif
  359. pop _bp
  360. %endmacro
  361. %imacro use_ebx 0
  362. %ifdef flatmodel
  363. push ebx
  364. %endif
  365. %endmacro
  366. %imacro unuse_ebx 0
  367. %ifdef flatmodel
  368. pop ebx
  369. %endif
  370. %endmacro
  371. ; Macros for saving and restoring the value of DS,ES,FS,GS when it is to
  372. ; be used in assembly routines. This evaluates to nothing in the flat memory
  373. ; model, but is saves and restores DS in the large memory model.
  374. %imacro use_ds 0
  375. %ifndef flatmodel
  376. push ds
  377. %endif
  378. %endmacro
  379. %imacro unuse_ds 0
  380. %ifndef flatmodel
  381. pop ds
  382. %endif
  383. %endmacro
  384. %imacro use_es 0
  385. %ifndef flatmodel
  386. push es
  387. %endif
  388. %endmacro
  389. %imacro unuse_es 0
  390. %ifndef flatmodel
  391. pop es
  392. %endif
  393. %endmacro
  394. ; Macros for loading the address of a data pointer into a segment and
  395. ; index register pair. The %imacro explicitly loads DS or ES in the 16 bit
  396. ; memory model, or it simply loads the offset into the register in the flat
  397. ; memory model since DS and ES always point to all addressable memory. You
  398. ; must use the correct _REG (ie: _BX) %imacros for documentation purposes.
  399. %imacro _lds 2
  400. %ifdef flatmodel
  401. mov %1,%2
  402. %else
  403. lds %1,%2
  404. %endif
  405. %endmacro
  406. %imacro _les 2
  407. %ifdef flatmodel
  408. mov %1,%2
  409. %else
  410. les %1,%2
  411. %endif
  412. %endmacro
  413. ; Macros for adding and subtracting a value from registers. Two value are
  414. ; provided, one for 16 bit modes and another for 32 bit modes (the extended
  415. ; register is used in 32 bit modes).
  416. %imacro _add 3
  417. %ifdef flatmodel
  418. add e%1, %3
  419. %else
  420. add %1, %2
  421. %endif
  422. %endmacro
  423. %imacro _sub 3
  424. %ifdef flatmodel
  425. sub e%1, %3
  426. %else
  427. sub %1, %2
  428. %endif
  429. %endmacro
  430. ; Macro to clear the high order word for the 32 bit extended registers.
  431. ; This is used to convert an unsigned 16 bit value to an unsigned 32 bit
  432. ; value, and will evaluate to nothing in 16 bit modes.
  433. %imacro clrhi 1
  434. %ifdef flatmodel
  435. movzx e%1,%1
  436. %endif
  437. %endmacro
  438. %imacro sgnhi 1
  439. %ifdef flatmodel
  440. movsx e%1,%1
  441. %endif
  442. %endmacro
  443. ; Macro to load an extended register with an integer value in either mode
  444. %imacro loadint 2
  445. %ifdef flatmodel
  446. mov e%1,%2
  447. %else
  448. xor e%1,e%1
  449. mov %1,%2
  450. %endif
  451. %endmacro
  452. ; Macros to load and store integer values with string instructions
  453. %imacro LODSINT 0
  454. %ifdef flatmodel
  455. lodsd
  456. %else
  457. lodsw
  458. %endif
  459. %endmacro
  460. %imacro STOSINT 0
  461. %ifdef flatmodel
  462. stosd
  463. %else
  464. stosw
  465. %endif
  466. %endmacro
  467. ; Macros to provide resb, resw, resd compatibility with NASM
  468. %imacro dclb 1
  469. times %1 db 0
  470. %endmacro
  471. %imacro dclw 1
  472. times %1 dw 0
  473. %endmacro
  474. %imacro dcld 1
  475. times %1 dd 0
  476. %endmacro
  477. ; macros to declare assembler function stubs for function structures
  478. %imacro BEGIN_STUBS_DEF 2
  479. begdataseg _STUBS
  480. %ifdef __NOU_VAR__
  481. extern %1
  482. %define STUBS_START %1
  483. %else
  484. extern _%1
  485. %define STUBS_START _%1
  486. %endif
  487. enddataseg _STUBS
  488. begcodeseg _STUBS
  489. %assign off %2
  490. %endmacro
  491. %imacro DECLARE_STUB 1
  492. %ifdef __NOU__
  493. global %1
  494. %1:
  495. %else
  496. global _%1
  497. _%1:
  498. %endif
  499. jmp [DWORD STUBS_START+off]
  500. %assign off off+4
  501. %endmacro
  502. %imacro DECLARE_STDCALL 2
  503. %ifdef STDCALL_MANGLE
  504. global _%1@%2
  505. _%1@%2:
  506. %else
  507. %ifdef __GNUC__
  508. global _%1
  509. _%1:
  510. %else
  511. global %1
  512. %1:
  513. %endif
  514. %endif
  515. jmp [DWORD STUBS_START+off]
  516. %assign off off+4
  517. %endmacro
  518. %imacro END_STUBS_DEF 0
  519. endcodeseg _STUBS
  520. %endmacro
  521. ; macros to declare assembler import stubs for binary loadable drivers
  522. %imacro BEGIN_IMPORTS_DEF 1
  523. BEGIN_STUBS_DEF %1,4
  524. %endmacro
  525. %imacro DECLARE_IMP 1
  526. DECLARE_STUB %1
  527. %endmacro
  528. %imacro END_IMPORTS_DEF 0
  529. END_STUBS_DEF
  530. %endmacro
  531. else ; __NASM_MAJOR__
  532. ;============================================================================
  533. ; Macro package when compiling with TASM.
  534. ;============================================================================
  535. ; Turn off underscores for globals if disabled for all externals
  536. ifdef __NOU__
  537. __NOU_VAR__ = 1
  538. endif
  539. ; Define the __WINDOWS__ symbol if we are compiling for any Windows
  540. ; environment
  541. ifdef __WINDOWS16__
  542. __WINDOWS__ = 1
  543. endif
  544. ifdef __WINDOWS32__
  545. __WINDOWS__ = 1
  546. __WINDOWS32_386__ = 1
  547. endif
  548. ifdef __WIN386__
  549. __WINDOWS__ = 1
  550. __WINDOWS32_386__ = 1
  551. endif
  552. ifdef __VXD__
  553. __WINDOWS__ = 1
  554. __WINDOWS32_386__ = 1
  555. MASM
  556. .386
  557. NO_SEGMENTS = 1
  558. include vmm.inc ; IGNORE DEPEND
  559. include vsegment.inc ; IGNORE DEPEND
  560. IDEAL
  561. endif
  562. ; Macros for accessing 'generic' registers
  563. ifdef __FLAT__
  564. _ax EQU eax ; EAX is used for accumulator
  565. _bx EQU ebx ; EBX is used for accumulator
  566. _cx EQU ecx ; ECX is used for looping
  567. _dx EQU edx ; EDX is used for data register
  568. _si EQU esi ; ESI is the source index register
  569. _di EQU edi ; EDI is the destination index register
  570. _bp EQU ebp ; EBP is used for base pointer register
  571. _sp EQU esp ; ESP is used for stack pointer register
  572. _es EQU ; ES and DS are the same in 32 bit PM
  573. typedef UCHAR BYTE ; Size of a character
  574. typedef USHORT WORD ; Size of a short
  575. typedef UINT DWORD ; Size of an integer
  576. typedef ULONG DWORD ; Size of a long
  577. typedef BOOL DWORD ; Size of a boolean
  578. typedef DPTR DWORD ; Size of a data pointer
  579. typedef FDPTR FWORD ; Size of a far data pointer
  580. typedef NDPTR DWORD ; Size of a near data pointer
  581. typedef CPTR DWORD ; Size of a code pointer
  582. typedef FCPTR FWORD ; Size of a far code pointer
  583. typedef NCPTR DWORD ; Size of a near code pointer
  584. typedef DUINT DWORD ; Declare a integer variable
  585. FPTR EQU NEAR ; Distance for function pointers
  586. intsize = 4 ; Size of an integer
  587. flatmodel = 1 ; This is a flat memory model
  588. P386 ; Turn on 386 code generation
  589. MODEL FLAT ; Set up for 32 bit simplified FLAT model
  590. else
  591. _ax EQU ax ; AX is used for accumulator
  592. _bx EQU bx ; BX is used for accumulator
  593. _cx EQU cx ; CX is used for looping
  594. _dx EQU dx ; DX is used for data register
  595. _si EQU si ; SI is the source index register
  596. _di EQU di ; DI is the destination index register
  597. _bp EQU bp ; BP is used for base pointer register
  598. _sp EQU sp ; SP is used for stack pointer register
  599. _es EQU es: ; ES is used for segment override
  600. typedef UCHAR BYTE ; Size of a character
  601. typedef USHORT WORD ; Size of a short
  602. typedef UINT WORD ; Size of an integer
  603. typedef ULONG DWORD ; Size of a long
  604. typedef BOOL WORD ; Size of a boolean
  605. typedef DPTR DWORD ; Size of a data pointer
  606. typedef FDPTR DWORD ; Size of a far data pointer
  607. typedef NDPTR WORD ; Size of a near data pointer
  608. typedef CPTR DWORD ; Size of a code pointer
  609. typedef FCPTR DWORD ; Size of a far code pointer
  610. typedef NCPTR WORD ; Size of a near code pointer
  611. typedef DUINT WORD ; Declare a integer variable
  612. FPTR EQU FAR ; Distance for function pointers
  613. intsize = 2 ; Size of an integer
  614. P386 ; Turn on 386 code generation
  615. endif
  616. invert EQU not
  617. ; Provide a typedef for real floating point numbers
  618. ifdef DOUBLE
  619. typedef REAL QWORD
  620. typedef DREAL QWORD
  621. else
  622. typedef REAL DWORD
  623. typedef DREAL DWORD
  624. endif
  625. ; Macros to access the floating point stack registers to convert them
  626. ; from NASM style to TASM style
  627. st0 EQU st(0)
  628. st1 EQU st(1)
  629. st2 EQU st(2)
  630. st3 EQU st(3)
  631. st4 EQU st(4)
  632. st5 EQU st(5)
  633. st6 EQU st(6)
  634. st7 EQU st(7)
  635. st8 EQU st(8)
  636. ; Boolean truth values (same as those in debug.h)
  637. ifndef __VXD__
  638. False = 0
  639. True = 1
  640. No = 0
  641. Yes = 1
  642. Yes = 1
  643. endif
  644. ; Macros for the _DATA data segment. This segment contains initialised data.
  645. MACRO begdataseg name
  646. ifdef __VXD__
  647. MASM
  648. VXD_LOCKED_DATA_SEG
  649. IDEAL
  650. else
  651. ifdef flatmodel
  652. DATASEG
  653. else
  654. SEGMENT _DATA DWORD PUBLIC USE16 'DATA'
  655. endif
  656. endif
  657. ENDM
  658. MACRO enddataseg name
  659. ifdef __VXD__
  660. MASM
  661. VXD_LOCKED_DATA_ENDS
  662. IDEAL
  663. else
  664. ifndef flatmodel
  665. ENDS _DATA
  666. endif
  667. endif
  668. ENDM
  669. ; Macro for the main code segment.
  670. MACRO begcodeseg name
  671. ifdef __VXD__
  672. MASM
  673. VXD_LOCKED_CODE_SEG
  674. IDEAL
  675. else
  676. ifdef flatmodel
  677. CODESEG
  678. ASSUME CS:FLAT,DS:FLAT,SS:FLAT
  679. else
  680. SEGMENT &name&_TEXT PARA PUBLIC USE16 'CODE'
  681. ASSUME CS:&name&_TEXT,DS:_DATA
  682. endif
  683. endif
  684. ENDM
  685. ; Macro for a near code segment
  686. MACRO begcodeseg_near
  687. ifdef flatmodel
  688. CODESEG
  689. ASSUME CS:FLAT,DS:FLAT,SS:FLAT
  690. else
  691. SEGMENT _TEXT PARA PUBLIC USE16 'CODE'
  692. ASSUME CS:_TEXT,DS:_DATA
  693. endif
  694. ENDM
  695. MACRO endcodeseg name
  696. ifdef __VXD__
  697. MASM
  698. VXD_LOCKED_CODE_ENDS
  699. IDEAL
  700. else
  701. ifndef flatmodel
  702. ENDS &name&_TEXT
  703. endif
  704. endif
  705. ENDM
  706. MACRO endcodeseg_near
  707. ifndef flatmodel
  708. ENDS _TEXT
  709. endif
  710. ENDM
  711. ; Macro to be invoked at the start of all modules to set up segments for
  712. ; later use.
  713. MACRO header name
  714. begdataseg name
  715. enddataseg name
  716. ENDM
  717. ; Macro for an extern C symbol. If the C compiler requires leading
  718. ; underscores, then the underscores are added to the symbol names, otherwise
  719. ; they are left off. The symbol name is referenced in the assembler code
  720. ; using the non-underscored symbol name.
  721. MACRO cextern name,size
  722. ifdef __NOU_VAR__
  723. EXTRN name:size
  724. else
  725. EXTRN _&name&:size
  726. name EQU _&name&
  727. endif
  728. ENDM
  729. MACRO cexternfunc name,size
  730. ifdef __NOU__
  731. EXTRN name:size
  732. else
  733. EXTRN _&name&:size
  734. name EQU _&name&
  735. endif
  736. ENDM
  737. MACRO stdexternfunc name,args,size
  738. ifdef STDCALL_MANGLE
  739. EXTRN _&name&@&num_args&:size
  740. name EQU _&name&@&num_args
  741. else
  742. EXTRN name:size
  743. endif
  744. ENDM
  745. ; Macro for a public C symbol. If the C compiler requires leading
  746. ; underscores, then the underscores are added to the symbol names, otherwise
  747. ; they are left off. The symbol name is referenced in the assembler code
  748. ; using the non-underscored symbol name.
  749. MACRO cpublic name
  750. ifdef __NOU_VAR__
  751. name:
  752. PUBLIC name
  753. else
  754. _&name&:
  755. PUBLIC _&name&
  756. name EQU _&name&
  757. endif
  758. ENDM
  759. ; Macro for an global C symbol. If the C compiler requires leading
  760. ; underscores, then the underscores are added to the symbol names, otherwise
  761. ; they are left off. The symbol name is referenced in the assembler code
  762. ; using the non-underscored symbol name.
  763. MACRO cglobal name
  764. ifdef __NOU_VAR__
  765. PUBLIC name
  766. else
  767. PUBLIC _&name&
  768. name EQU _&name&
  769. endif
  770. ENDM
  771. ; Macro for an global C function symbol. If the C compiler requires leading
  772. ; underscores, then the underscores are added to the symbol names, otherwise
  773. ; they are left off. The symbol name is referenced in the assembler code
  774. ; using the non-underscored symbol name.
  775. MACRO cglobalfunc name
  776. ifdef __NOU__
  777. PUBLIC name
  778. else
  779. PUBLIC _&name&
  780. name EQU _&name&
  781. endif
  782. ENDM
  783. ; Macro to start a C callable function. This will be a far function for
  784. ; 16-bit code, and a near function for 32-bit code.
  785. MACRO cprocstatic name ; Set up model independant private proc
  786. ifdef flatmodel
  787. PROC name NEAR
  788. else
  789. PROC name FAR
  790. endif
  791. LocalSize = 0
  792. ENDM
  793. MACRO cprocstart name ; Set up model independant proc
  794. ifdef flatmodel
  795. ifdef __NOU__
  796. PROC name NEAR
  797. else
  798. PROC _&name& NEAR
  799. endif
  800. else
  801. ifdef __NOU__
  802. PROC name FAR
  803. else
  804. PROC _&name& FAR
  805. endif
  806. endif
  807. LocalSize = 0
  808. cglobalfunc name
  809. ENDM
  810. MACRO cprocnear name ; Set up near proc
  811. ifdef __NOU__
  812. PROC name NEAR
  813. else
  814. PROC _&name& NEAR
  815. endif
  816. LocalSize = 0
  817. cglobalfunc name
  818. ENDM
  819. MACRO cprocfar name ; Set up far proc
  820. ifdef __NOU__
  821. PROC name FAR
  822. else
  823. PROC _&name& FAR
  824. endif
  825. LocalSize = 0
  826. cglobalfunc name
  827. ENDM
  828. MACRO cprocend ; End procedure macro
  829. ENDP
  830. ENDM
  831. ; This macro sets up a procedure to be exported from a 16 bit DLL. Since the
  832. ; calling conventions are always _far _pascal for 16 bit DLL's, we actually
  833. ; rename this routine with an extra underscore with 'C' calling conventions
  834. ; and a small DLL stub will be provided by the high level code to call the
  835. ; assembler routine.
  836. MACRO cprocstartdll16 name
  837. ifdef __WINDOWS16__
  838. cprocstart _&name&
  839. else
  840. cprocstart name
  841. endif
  842. ENDM
  843. ; Macros for entering and exiting C callable functions. Note that we must
  844. ; always save and restore the SI and DI registers for C functions, and for
  845. ; 32 bit C functions we also need to save and restore EBX and clear the
  846. ; direction flag.
  847. MACRO save_c_regs
  848. ifdef flatmodel
  849. push ebx
  850. endif
  851. push _si
  852. push _di
  853. ENDM
  854. MACRO enter_c
  855. push _bp
  856. mov _bp,_sp
  857. IFDIFI <LocalSize>,<0>
  858. sub _sp,LocalSize
  859. ENDIF
  860. save_c_regs
  861. ENDM
  862. MACRO restore_c_regs
  863. pop _di
  864. pop _si
  865. ifdef flatmodel
  866. pop ebx
  867. endif
  868. ENDM
  869. MACRO leave_c
  870. restore_c_regs
  871. cld
  872. IFDIFI <LocalSize>,<0>
  873. mov _sp,_bp
  874. ENDIF
  875. pop _bp
  876. ENDM
  877. MACRO use_ebx
  878. ifdef flatmodel
  879. push ebx
  880. endif
  881. ENDM
  882. MACRO unuse_ebx
  883. ifdef flatmodel
  884. pop ebx
  885. endif
  886. ENDM
  887. ; Macros for saving and restoring the value of DS,ES,FS,GS when it is to
  888. ; be used in assembly routines. This evaluates to nothing in the flat memory
  889. ; model, but is saves and restores DS in the large memory model.
  890. MACRO use_ds
  891. ifndef flatmodel
  892. push ds
  893. endif
  894. ENDM
  895. MACRO unuse_ds
  896. ifndef flatmodel
  897. pop ds
  898. endif
  899. ENDM
  900. MACRO use_es
  901. ifndef flatmodel
  902. push es
  903. endif
  904. ENDM
  905. MACRO unuse_es
  906. ifndef flatmodel
  907. pop es
  908. endif
  909. ENDM
  910. ; Macros for loading the address of a data pointer into a segment and
  911. ; index register pair. The macro explicitly loads DS or ES in the 16 bit
  912. ; memory model, or it simply loads the offset into the register in the flat
  913. ; memory model since DS and ES always point to all addressable memory. You
  914. ; must use the correct _REG (ie: _BX) macros for documentation purposes.
  915. MACRO _lds reg, addr
  916. ifdef flatmodel
  917. mov reg,addr
  918. else
  919. lds reg,addr
  920. endif
  921. ENDM
  922. MACRO _les reg, addr
  923. ifdef flatmodel
  924. mov reg,addr
  925. else
  926. les reg,addr
  927. endif
  928. ENDM
  929. ; Macros for adding and subtracting a value from registers. Two value are
  930. ; provided, one for 16 bit modes and another for 32 bit modes (the extended
  931. ; register is used in 32 bit modes).
  932. MACRO _add reg, val16, val32
  933. ifdef flatmodel
  934. add e&reg&, val32
  935. else
  936. add reg, val16
  937. endif
  938. ENDM
  939. MACRO _sub reg, val16, val32
  940. ifdef flatmodel
  941. sub e&reg&, val32
  942. else
  943. sub reg, val16
  944. endif
  945. ENDM
  946. ; Macro to clear the high order word for the 32 bit extended registers.
  947. ; This is used to convert an unsigned 16 bit value to an unsigned 32 bit
  948. ; value, and will evaluate to nothing in 16 bit modes.
  949. MACRO clrhi reg
  950. ifdef flatmodel
  951. movzx e&reg&,reg
  952. endif
  953. ENDM
  954. MACRO sgnhi reg
  955. ifdef flatmodel
  956. movsx e&reg&,reg
  957. endif
  958. ENDM
  959. ; Macro to load an extended register with an integer value in either mode
  960. MACRO loadint reg,val
  961. ifdef flatmodel
  962. mov e&reg&,val
  963. else
  964. xor e&reg&,e&reg&
  965. mov reg,val
  966. endif
  967. ENDM
  968. ; Macros to load and store integer values with string instructions
  969. MACRO LODSINT
  970. ifdef flatmodel
  971. lodsd
  972. else
  973. lodsw
  974. endif
  975. ENDM
  976. MACRO STOSINT
  977. ifdef flatmodel
  978. stosd
  979. else
  980. stosw
  981. endif
  982. ENDM
  983. ; Macros to provide resb, resw, resd compatibility with NASM
  984. MACRO dclb count
  985. db count dup (0)
  986. ENDM
  987. MACRO dclw count
  988. dw count dup (0)
  989. ENDM
  990. MACRO dcld count
  991. dd count dup (0)
  992. ENDM
  993. ; Macros to provide resb, resw, resd compatibility with NASM
  994. MACRO resb count
  995. db count dup (?)
  996. ENDM
  997. MACRO resw count
  998. dw count dup (?)
  999. ENDM
  1000. MACRO resd count
  1001. dd count dup (?)
  1002. ENDM
  1003. ; Macros to declare assembler stubs for function structures
  1004. MACRO BEGIN_STUBS_DEF name, firstOffset
  1005. begdataseg _STUBS
  1006. ifdef __NOU_VAR__
  1007. EXTRN name:DWORD
  1008. STUBS_START = name
  1009. else
  1010. EXTRN _&name&:DWORD
  1011. name EQU _&name&
  1012. STUBS_START = _&name
  1013. endif
  1014. enddataseg _STUBS
  1015. begcodeseg _STUBS
  1016. off = firstOffset
  1017. ENDM
  1018. MACRO DECLARE_STUB name
  1019. ifdef __NOU__
  1020. name:
  1021. PUBLIC name
  1022. else
  1023. _&name:
  1024. PUBLIC _&name
  1025. endif
  1026. jmp [DWORD STUBS_START+off]
  1027. off = off + 4
  1028. ENDM
  1029. MACRO DECLARE_STDCALL name,num_args
  1030. ifdef STDCALL_MANGLE
  1031. _&name&@&num_args&:
  1032. PUBLIC _&name&@&num_args&
  1033. else
  1034. name:
  1035. PUBLIC name
  1036. endif
  1037. jmp [DWORD STUBS_START+off]
  1038. off = off + 4
  1039. ENDM
  1040. MACRO END_STUBS_DEF
  1041. endcodeseg _STUBS
  1042. ENDM
  1043. MACRO BEGIN_IMPORTS_DEF name
  1044. BEGIN_STUBS_DEF name,4
  1045. ENDM
  1046. MACRO DECLARE_IMP name
  1047. DECLARE_STUB name
  1048. ENDM
  1049. MACRO END_IMPORTS_DEF
  1050. END_STUBS_DEF
  1051. ENDM
  1052. endif