home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / digmid / real / prologue.mac < prev    next >
Text File  |  1992-09-25  |  5KB  |  269 lines

  1. ;; A header macro file that defines a lot of common useful macros when
  2. ;; programming in assembly.  Supports conditional assembly for taking
  3. ;; advantage of 80286 opcodes.
  4.  
  5. IS286    equ    0        ;; True if taking advantage of 80286 opcodes.
  6.  
  7. Macro     SETUPSEGMENT
  8.  
  9. SEGMENT _TEXT    PARA PUBLIC 'CODE'
  10.     ASSUME    CS:_TEXT
  11.  
  12.     Endm
  13.  
  14. ;; This macro replaces the LOOP instruction, because the loop instruction
  15. ;; is too damned slow on a 386/486
  16. macro    CLOOP LABEL
  17.     dec    cx
  18.     jnz    LABEL
  19.     endm
  20.  
  21. macro    CLODSB
  22.     mov    al,[ds:si]
  23.     inc    si
  24.     endm
  25.  
  26. macro    CSTOSB
  27.     mov    [es:di],al
  28.     inc    di
  29.     endm
  30.  
  31. macro    CLODSW
  32.     mov    ax,[ds:si]
  33.     add    si,2
  34.     endm
  35.  
  36. macro    CSTOSW
  37.     mov    [es:di],ax
  38.     add    di,2
  39.     endm
  40.  
  41.  
  42. macro   ShiftR  REG,TIMES
  43. ;; 2 - shift a register right a number of times.
  44. IF      IS286
  45.         shr     REG,TIMES
  46. ELSE
  47.         REPT    TIMES
  48.     shr    REG,1
  49.         ENDM
  50. ENDIF
  51.         endm
  52.  
  53. macro   ShiftL  REG,TIMES
  54. ;; 3 - shift a register left a number of times
  55. IF      IS286
  56.         shl     REG,TIMES
  57. ELSE
  58.         REPT    TIMES
  59.     shl    REG,1
  60.         ENDM
  61. ENDIF
  62.         endm
  63.  
  64. macro    LSMUL
  65. ;; 4 - performs a long signed multiply AX,DX * BX,CX
  66.     LOCAL    @@HOP1,@@HOP2
  67. ;; Long signed multiply
  68. ;; Long #1: AX,DX
  69. ;; Long #2: BX,CX
  70.     push    si
  71.     xchg    si,ax
  72.     xchg    dx,ax
  73.     or    ax,ax
  74.     jz    @@HOP1
  75.     mul    bx
  76. @@HOP1: xchg    cx,ax
  77.     or    ax,ax
  78.     jz    @@HOP2
  79.     mul    si
  80.     add    cx,ax
  81. @@HOP2: xchg    si,ax
  82.     mul    bx
  83.     add    dx,cx
  84.     pop    si
  85.     endm
  86.  
  87. macro    LongShiftL TIMES
  88. ;; 5 - Shift left AX,DX times.
  89.     REPT TIMES
  90.     shl    ax,1
  91.     rcl    dx,1
  92.     ENDM
  93.     endm
  94.  
  95. macro    LongShiftR TIMES
  96. ;; 6 - Shifr right AX,DX times
  97.     REPT TIMES
  98.     sar    dx,1
  99.     rcr    ax,1
  100.     ENDM
  101.     endm
  102.  
  103. macro    ShiftAL REG,TIMES
  104. ;; 7 - shift arithmetic left register, times
  105. IF      IS286
  106.     sal    REG,TIMES
  107. ELSE
  108.         REPT    TIMES
  109.     sal    REG,1
  110.         ENDM
  111. ENDIF
  112.     endm
  113.  
  114. macro    ShiftAR REG,TIMES
  115. ;; 8 - Shifr arithmatic right register, times
  116. IF      IS286
  117.     sar    REG,TIMES
  118. ELSE
  119.         REPT    TIMES
  120.     sar    REG,1
  121.         ENDM
  122. ENDIF
  123.     endm
  124.  
  125. macro   PushI   VALUE
  126. ;; 9 - Push an immediat onto the stack.
  127. ;; Push Immediate
  128. IF      IS286
  129.         push    VALUE
  130. ELSE
  131.         mov     ax,VALUE
  132.         push    ax
  133. ENDIF
  134.         endm
  135.  
  136. macro   PushEA  DATA
  137. ;; 10 - Push an effective address onto the stack.
  138. ;; Push Effective address
  139. IF      IS286
  140.         push    offset DATA
  141. ELSE
  142.         mov     ax,offset DATA
  143.         push    ax
  144. ENDIF
  145.         endm
  146.         
  147. macro   PushFar DATA
  148. ;; 11 - Push far address (relative to DS) onto the stack.
  149.         push    ds              ; push the segment
  150.         PushEA  DATA            ; push the offset
  151.         endm
  152.  
  153. macro   PushAll
  154. ;; 12 - Push ALL registers onto the stack.
  155. ;; Save all registers
  156. IF      IS286
  157.         pusha                   ;; if a 286 machine use the pusha opcode
  158.         push    ds              ;; save segment DS
  159.         push    es              ;; save segment ES
  160. ELSE
  161.         push    ax              ;; if not 286 machine use normal method
  162.         push    bx
  163.         push    cx
  164.         push    dx
  165.         push    si
  166.         push    di
  167.         push    bp
  168.         push    ds
  169.         push    es
  170. ENDIF
  171.         endm
  172.  
  173. macro   PopAll
  174. ;; 13 - Pop all registers off of the stack.
  175. ;;; Restore all registers from a push all
  176. IF      IS286
  177.         pop     es
  178.         pop     ds
  179.         popa
  180. ELSE
  181.         pop     es
  182.         pop     ds
  183.         pop     bp
  184.         pop     di
  185.         pop     si
  186.         pop     dx
  187.         pop     cx
  188.         pop     bx
  189.         pop     ax
  190. ENDIF
  191.         endm
  192.  
  193. macro   DOSTerminate
  194. ;; 14 - Terminate back to DOS
  195.     mov    ah,4Ch
  196.         int     21h
  197.         endm
  198.  
  199. macro   DosTSR  LOC
  200. ;; 15 - Terminate and stay resident back to DOS
  201.         lea     dx,[LOC+100h]   ; (End of program plus PSP)
  202.         int     27h             ; Terminate and stay resident.
  203.         endm
  204.  
  205. macro   Message data
  206. ;; 16 - Print a '$' terminated string to the console
  207.     push    ax
  208.         mov     ah,9            ; Function 9 write string
  209.         lea     dx,[data]       ; Get the address of the message
  210.         int     21h             ; Send the message to the screen.
  211.     pop    ax
  212.         endm
  213.  
  214.  
  215.  
  216. macro   PENTER  STORAGE
  217. ;; 17 - Enter a procedue with storage space
  218. ;; Procedure enter, uses the 286/386 ENTER opcode
  219. IF      IS286
  220.         enter   STORAGE,0       ; nexting level, always zero.
  221. ELSE
  222.         push    bp
  223.         mov     bp,sp
  224.         IF      STORAGE
  225.         sub     sp,STORAGE
  226.         ENDIF
  227. ENDIF
  228.         endm
  229.  
  230. macro   PLEAVE
  231. ;; 18 - Exit a procedure with stack correction.
  232. IF      IS286
  233.         leave
  234. ELSE
  235.         mov     sp,bp
  236.         pop     bp
  237. ENDIF
  238.         endm
  239.  
  240. macro   PushCREGS
  241. ;; 19 - Save registers for C
  242.         push    es
  243.     push    ds   ;The Kernel is responsible for maintaining DS
  244.         push    si
  245.         push    di
  246.         cld
  247.         endm
  248.  
  249. macro   PopCREGS
  250. ;; 20 - Restore registers for C
  251.         pop     di
  252.         pop     si
  253.     pop    ds ;The Kernel is responsible for maintaining DS
  254.         pop     es
  255.         endm
  256.  
  257. ;; Macro used to insert breakpoints in code to invoke the debugger.  Using
  258. ;; the macro allows for easier searches to be done on debug invokations.
  259. Macro    DoDebug
  260.     int    3
  261.     endm
  262.  
  263. Macro    SwapSegs
  264.     push    es
  265.     push    ds
  266.     pop    es
  267.     pop    ds
  268.     endm
  269.