home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / C_DISK2.ZIP / MACROS.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-06-25  |  7.7 KB  |  463 lines

  1. ;_ macros.asm   Wed Feb  3 1988   Modified by: Walter Bright */
  2. ; Copyright (C) 1985-1988 by Northwest Software
  3. ; All Rights Reserved
  4. ; Written by Walter Bright
  5.  
  6. ; Determine which memory model we are assembling for. For .COM files,
  7. ; force S model.
  8.     ifdef I8086T
  9. I8086S equ    1
  10.     else
  11.     ifndef I8086S
  12.     ifndef I8086M
  13.     ifndef I8086C
  14.     ifndef I8086L        ;if none of the memory models are defined
  15. I8086S equ    1        ;default to S model
  16.     endif
  17.     endif
  18.     endif
  19.     endif
  20.     endif
  21.  
  22. ;Decide if SI and DI are saved across function calls
  23. SAVESIDI equ    1        ;1 means SI and DI are saved across functions
  24.  
  25. ;Decide if we want to use Microsoft C calling conventions
  26. ;or Lattice C calling conventions
  27. MSC    equ    1        ;ifdef means use Microsoft C calling conventions
  28.                 ;ifndef means use Lattice
  29.  
  30. ; Macros to bracket data segment stuff.
  31.  
  32. begdata    macro
  33.     ifdef MSC
  34. _DATA    segment word public 'DATA'
  35. _DATA    ends
  36. CONST    segment word public 'CONST'
  37. CONST    ends
  38. _BSS    segment word public 'BSS'
  39. _BSS    ends
  40. DGROUP    group    _DATA,CONST,_BSS
  41. _DATA    segment
  42.     else
  43. DGROUP    group    data
  44. data    segment    word public 'data'
  45.     endif
  46.     assume ds:DGROUP
  47.     endm
  48.  
  49. enddata    macro
  50.     ifdef MSC
  51. _DATA    ends
  52.     else
  53. data    ends
  54.     endif
  55.     endm
  56.  
  57. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  58. ; Macros specific to each memory model in an attempt to make it easier
  59. ; to write memory model independent code.
  60. ;    begcode,endcode        Use to bracket code sections
  61. ;    P            Offset on BP to first argument on stack
  62. ;                (excluding any local variables)
  63. ;    SPTR            1 if small data model
  64. ;    LPTR            1 if large pointers (large data)
  65. ;    LCODE            1 if large code model
  66. ;    ESeqDS            1 if ES == DS at all times
  67. ;    SIZEPTR            # of bytes in a pointer
  68. ;    func            Declare a function as NEAR or FAR
  69. ;    callm            Call function as NEAR or FAR
  70.  
  71. ;;;;;;;;;;;;;; SMALL MEMORY MODEL ;;;;;;;;;;;;;;;;;
  72.  
  73. ifdef I8086S
  74.     ifdef MSC
  75. begcode    macro    module
  76. _TEXT    segment    word public 'CODE'
  77.     assume    cs:_TEXT
  78.     endm
  79.  
  80. endcode    macro    module
  81. _TEXT    ENDS
  82.     endm
  83.     else
  84. begcode    macro    module
  85. pgroup    group    prog
  86. prog    segment    byte public 'prog'
  87.     assume    cs:pgroup
  88.     endm
  89.  
  90. endcode    macro    module
  91. prog    ends
  92.     endm
  93.     endif
  94.  
  95. P    equ    4    ; Offset of start of parameters on the stack frame
  96. SPTR    equ    1
  97. LPTR    equ    0
  98. LCODE    equ    0
  99. ESeqDS    equ    0
  100. SIZEPTR    equ    2    ; Size of a pointer
  101.  
  102.     ifdef MSC
  103. func    macro    name
  104. _&name    proc    near
  105.     ifndef name
  106. name    equ    _&name
  107.     endif
  108.     endm
  109.  
  110. callm    macro    name
  111.     call    near ptr _&name
  112.     endm
  113.     else
  114. func    macro    name
  115. name    proc    near
  116.     endm
  117.  
  118. callm    macro    name
  119.     call    near ptr name
  120.     endm
  121.     endif
  122. endif
  123.  
  124. ;;;;;;;;;;;;;;;;; MEDIUM MEMORY MODEL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  125.  
  126. ifdef I8086M
  127.     ifdef MSC
  128. begcode    macro    module
  129. module&_TEXT    segment word public 'CODE'
  130.     assume    cs:module&_TEXT
  131.     endm
  132.  
  133. endcode    macro    module
  134. module&_TEXT    ends
  135.     endm
  136.     else
  137. begcode    macro    module
  138. module&_code    segment    byte public 'code'
  139.     assume    cs:module&_code
  140.     endm
  141.  
  142. endcode    macro    module
  143. module&_code    ends
  144.     endm
  145.     endif
  146.  
  147. P    equ    6    ; Offset of start of parameters on the stack frame
  148. SPTR    equ    1
  149. LPTR    equ    0
  150. LCODE    equ    1
  151. ESeqDS    equ    0
  152. SIZEPTR    equ    2
  153.  
  154.     ifdef MSC
  155. func    macro    name
  156. _&name    proc    far
  157.     ifndef name
  158. name    equ    _&name
  159.     endif
  160.     endm
  161.  
  162. callm    macro    name
  163.     call    far ptr _&name
  164.     endm
  165.     else
  166. func    macro    name
  167. name    proc    far
  168.     endm
  169.  
  170. callm    macro    name
  171.     call    far ptr name
  172.     endm
  173.     endif
  174. endif
  175.  
  176. ;;;;;;;;;;;;;;;;; COMPACT MEMORY MODEL ;;;;;;;;;;;;;;
  177.  
  178. ifdef I8086C
  179.     ifdef MSC
  180. begcode    macro    module
  181. _TEXT    segment word public 'CODE'
  182.     assume    cs:_TEXT
  183.     endm
  184.  
  185. endcode    macro    module
  186. _TEXT    ends
  187.     endm
  188.     else
  189. begcode    macro    module
  190. cgroup    group    code
  191. code    segment    byte public 'code'
  192.     assume    cs:cgroup
  193.     endm
  194.  
  195. endcode    macro    module
  196. code    ends
  197.     endm
  198.     endif
  199.  
  200. P    equ    4    ; Offset of start of parameters on the stack frame
  201. SPTR    equ    0
  202. LPTR    equ    1
  203. LCODE    equ    0
  204. ESeqDS    equ    0
  205. SIZEPTR    equ    4
  206.  
  207.     ifdef MSC
  208. func    macro    name
  209. _&name    proc    near
  210.     ifndef name
  211. name    equ    _&name
  212.     endif
  213.     endm
  214.  
  215. callm    macro    name
  216.     call    near ptr _&name
  217.     endm
  218.     else
  219. func    macro    name
  220. name    proc    near
  221.     endm
  222.  
  223. callm    macro    name
  224.     call    near ptr name
  225.     endm
  226.     endif
  227. endif
  228.  
  229. ;;;;;;;;;;;;;;;; LARGE MEMORY MODEL ;;;;;;;;;;;;;;;;;;;
  230.  
  231. ifdef I8086L
  232.     ifdef MSC
  233. begcode    macro    module
  234. module&_TEXT    segment    word 'CODE'
  235.     assume    cs:module&_TEXT
  236.     endm
  237.  
  238. endcode    macro    module
  239. module&_TEXT    ends
  240.     endm
  241.     else
  242. begcode    macro    module
  243. module&_prog    segment    byte 'prog'
  244.     assume    cs:module&_prog
  245.     endm
  246.  
  247. endcode    macro    module
  248. module&_prog    ends
  249.     endm
  250.     endif
  251.  
  252.  
  253. P    equ    6    ; Offset of start of parameters on the stack frame
  254. SPTR    equ    0
  255. LPTR    equ    1
  256. LCODE    equ    1
  257. ESeqDS    equ    0
  258. SIZEPTR    equ    4
  259.  
  260.     ifdef MSC
  261. func    macro    name
  262. _&name    proc    far
  263.     ifndef name
  264. name    equ    _&name
  265.     endif
  266.     endm
  267.  
  268. callm    macro    name
  269.     call    far ptr _&name
  270.     endm
  271.     else
  272. func    macro    name
  273. name    proc    far
  274.     endm
  275.  
  276. callm    macro    name
  277.     call    far ptr name
  278.     endm
  279.     endif
  280. endif
  281.  
  282. ;Macros to replace public, extrn, and endp for C-callable assembly routines,
  283. ; and to define labels: c_label defines labels,
  284. ; c_public replaces public, c_extrn replaces extrn, and c_endp replaces endp
  285.  
  286.     ifdef MSC
  287.  
  288. c_name    macro    name
  289.     name equ _&name
  290.     endm
  291.  
  292. c_label    macro    name
  293. _&name:
  294.     endm
  295.  
  296. c_public macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  297.     ifnb <a>            ;;Check for blank argument
  298.     public    _&a
  299.     a equ _&a
  300.       ifnb <b>
  301.     c_public b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  302.       endif
  303.     endif
  304.     endm
  305.  
  306. c_extrn    macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  307.     ifnb <a>            ;;Check for blank argument
  308.     extrn    _&a:b
  309.     a equ _&a
  310.       ifnb <c>
  311.     c_extrn    c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  312.       endif
  313.     endif
  314.     endm
  315.  
  316. c_endp    macro    name
  317. _&name    ENDP
  318.     endm
  319.  
  320.     else
  321.  
  322. c_name    macro    name
  323.     endm
  324.  
  325. c_label    macro    name
  326. name:
  327.     endm
  328.  
  329. c_public macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  330.     ifnb <a>            ;;Check for blank argument
  331.     public    a
  332.       ifnb <b>
  333.     c_public b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  334.       endif
  335.     endif
  336.     endm
  337.  
  338. c_extrn    macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  339.     ifnb    <a>            ;;Check for blank argument
  340.     extrn    a:b
  341.       ifnb <c>
  342.     c_extrn    c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  343.       endif
  344.     endif
  345.     endm
  346.  
  347. c_endp    macro    name
  348. name    endp
  349.     endm
  350.  
  351.     endif
  352.  
  353. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  354. ; Other more or less useful macros
  355.  
  356. setESeqDS macro        ;set ES == DS, if not already true
  357.     ife ESeqDS
  358.     push    DS
  359.     pop    ES
  360.     endif
  361.     endm
  362.  
  363. .push    macro    list
  364.     irp    arg,<list>
  365.      push    arg
  366.     endm
  367.     endm
  368.  
  369. .pop    macro    list
  370.     irp    arg,<list>
  371.      pop    arg
  372.     endm
  373.     endm
  374.  
  375. ; Macros to save and restore regs destroyed by a function
  376.  
  377. .save    macro    list
  378.     if SAVESIDI
  379.     irp    arg,<list>
  380.      push    arg
  381.     endm
  382.     endif
  383.     endm
  384.  
  385. .restore macro    list
  386.     if SAVESIDI
  387.     irp    arg,<list>
  388.      pop    arg
  389.     endm
  390.     endif
  391.     endm
  392.  
  393. ; Macros to save and restore ES, but only if ESeqDS is 1.
  394. pushES    macro
  395.     if    ESeqDS
  396.     push    ES
  397.     endif
  398.     endm
  399.  
  400. popES    macro
  401.     if    ESeqDS
  402.     pop    ES
  403.     endif
  404.     endm
  405.  
  406. clr    macro    list        ;clear a register
  407.     irp    reg,<list>
  408.      xor    reg,reg
  409.     endm
  410.     endm
  411.  
  412. tst    macro    reg
  413.     or    reg,reg
  414.     endm
  415.  
  416. jmps    macro    lbl
  417.     jmp    short    lbl
  418.     endm
  419.  
  420. .if    macro    arg1,cond,arg2,lbl
  421.     cmp    arg1,arg2
  422.     j&cond    lbl
  423.     endm
  424.  
  425. ;sob    macro    arg,lbl
  426. ;    ifidn    <arg>,<CX>
  427. ;     loop    lbl
  428. ;    else
  429. ;     dec    arg
  430. ;     jnz    lbl
  431. ;    endif
  432. ;    endm
  433.  
  434. ifndef nobdos
  435. bdos    macro    func
  436.     ifnb    <func>
  437.      mov    AH,func
  438.     endif
  439.     int    21h
  440.     endm
  441. endif
  442.  
  443. .retf    macro    val        ;force assembler to build a far return
  444.     ifnb    <val>
  445.      db    0CAh
  446.      dw    val
  447.     else
  448.      db    0CBh
  449.     endif
  450.     endm
  451.  
  452. ; Sometimes MASM ignores my segment overrides.
  453. segES    macro
  454.     db    26h
  455.     endm
  456.  
  457. ; 32 bit negate
  458. neg32    macro    reg1,reg2
  459.      neg    reg1
  460.      neg    reg2
  461.      sbb    reg1,0
  462.     endm
  463.