home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / asm / bmac.arc / BMAC.MAC next >
Text File  |  1988-07-27  |  8KB  |  245 lines

  1. ; bmac.mac  27 Oct 83  Craig Milo Rogers at USC/ISI
  2. ;    Added BEXTRN to declare external routines.
  3. ; bmac.mac  27 Oct 83  Craig Milo Rogers at USC/ISI
  4. ;    Added NOSAVE and PRIVATE parameters to the BENTRY macro.
  5. ; bmac.mac  17 Oct 83  Craig Milo Rogers at USC/ISI
  6. ;    This version is designed to interface with the Lattice C
  7. ; environment.  For example, subroutine agruments are stored with
  8. ; the first argument occupying the lowest address on the stack.
  9. ; The BP register is not handled quite correctly, however, so there
  10. ; may be incompatibilities with C library routines which expect a
  11. ; varying number of arguments.
  12. ;
  13. ; BMAC - Macros for 8088 Assembly Language
  14. ; Ron Burk, June 1983
  15. ; Dr. Dobbs Journal, Number 84, October 1983, pp. 98-106.
  16. ;
  17. ;    Stack format (lowest address at bottom):
  18. ;
  19. ;    |    Previous data    | <===    BP on call from C routines.
  20. ;    +-----------------------+
  21. ;    |    Last Arg    |
  22. ;    |    .        |
  23. ;    |    .        |
  24. ;    |    First Arg    |
  25. ;    +-----------------------+
  26. ;    |  (Optional Saved CS)    |
  27. ;    |    Saved IP    | <===    SP at start of BENTRY macro.
  28. ;    +-----------------------+
  29. ;    |    Prev BP        | <===    SP and BP after BENTRY macro.
  30. ;    +-----------------------+
  31. ;    |    First Local    |
  32. ;    |    .        |
  33. ;    |    .        |
  34. ;    |    Last Local    | <===    SP after AUTO macro.
  35. ;    +-----------------------+
  36. ;    |    First Reg    |
  37. ;    |    .        |
  38. ;    |    .        |
  39. ;    |    Last Reg    | <===    SP after SAVE macro.
  40. ;    +-----------------------+
  41. ;    
  42.  
  43. @ARGDEF    MACRO    ARGNAMES    ;; Internal macro for defining args.
  44.   ifnb <ARGNAMES>        ;; If there were any arguments:
  45.     if @nosave            ;;   Is BP being left untouched?
  46.       @err <No arguments allowed in BENTRY when NOSAVE is specified.>
  47.     endif
  48.     if LPROG            ;;   Large program model:
  49.       @nargs=4            ;;     Allow 4 bytes for return address.
  50.     else            ;;   Small program model:
  51.       @nargs=2            ;;     Allow 2 bytes for return address.
  52.     endif
  53.     irp arg,<ARGNAMES>        ;;   For each argument:
  54.       @nargs=@nargs+2        ;;     Point to argument (skip saved BP).
  55.       arg&&@=@nargs        ;;     Define a redefinable offset.
  56.       ifndef arg        ;;     If name not yet defined:
  57.     arg equ [BP + arg&&@]    ;;       Define name itself.
  58.       endif
  59.     endm
  60.   endif
  61. ENDM                ;; End of @ARGDEF macro.
  62.  
  63.  
  64. @ERR    MACRO    MSG        ;; Print error message if in pass 1.
  65.   if1
  66.     %out MSG
  67.   endif
  68. ENDM                ;; End of @ERR macro.
  69.  
  70.  
  71. @CHECK    MACRO    EXTRA,MACNAME    ;; Checks for bad invocation.
  72.   ifnb <EXTRA>
  73.     @err <You forgot angle brackets in MACNAME macro.>
  74.   endif
  75. ENDM                ;; End of @CHECK macro.
  76.  
  77.  
  78. BEXTRN    MACRO    FUNCS,MISTAKE    ;; Declare external procedures:
  79.   ifb <FUNCS>            ;; Check for missing argument.
  80.     @err <Missing function names on BEXTRN macro.>
  81.   endif
  82.   @check MISTAKE,BEXTRN        ;; Check for missing brackets.
  83.   if LPROG            ;; Large or small programs?
  84.                 ;; Large model:
  85.     irp func,<FUNCS>        ;; Process list of functions.
  86.     EXTRN    func:FAR    ;; Declare an external routine.
  87.     endm
  88.   else
  89.                 ;; Small model:
  90.     PSEG            ;; Programs are in this public segment.
  91.     irp func,<FUNCS>        ;; Process list of functions.
  92.     EXTRN    func:NEAR    ;; Declare an external routine.
  93.     endm
  94.     ENDPS            ;; Return to former segment.
  95.   endif
  96. ENDM
  97.  
  98. BENTRY    MACRO    FUNC,ARGNAMES,OPTIONAL,MISTAKE    ;; Defines procedure:
  99.   ifb <FUNC>            ;; Check for missing function name.
  100.     @err <Missing function name on BENTRY macro.>
  101.   endif
  102.   @check MISTAKE,BENTRY        ;; Check for missing brackets.
  103.   @nlv=0            ;; Number of bytes of local variables so far.
  104.   @savefl=0            ;; Set nonzero when SAVE macro has been used.
  105.   @nosave=0            ;; Set nonzero when BP is not to be saved.
  106.   @private=0            ;; Set nonzere when FUNC is not global.
  107.   irp flag,<@AX,@BX,@CX,@DX,@SI,@DI,@DS,@SS,@ES,@flags>
  108.     flag=0            ;; Initialize register flags.
  109.   endm
  110.   ifnb <OPTIONAL>        ;; Any optional arguments?
  111.     irp keyword,<OPTIONAL>    ;; Process list of optional keywords.
  112.       ifidn <keyword>,<NOSAVE>    ;; Request to not save BP?
  113.     @nosave=1        ;;  Yes, do not save BP.
  114.       else
  115.     ifidn <keyword>,<PRIVATE>
  116.       @private=1        ;; This is a local function.
  117.     else
  118.       @err <Unrecognized key word KEYWORD in BENTRY FUNC.>
  119.     endif
  120.       endif
  121.     endm
  122.   endif
  123.   @argdef <ARGNAMES>        ;; Define argument offsets.
  124.   ife @private            ;; Is this a global function?
  125.     PUBLIC    FUNC        ;; Publicly available procedure.
  126.   endif
  127.   if LPROG
  128. FUNC    PROC    FAR        ;; Long procedure pointers.
  129.   else
  130. FUNC    PROC    NEAR        ;; Short procedure pointers.
  131.   endif
  132.   ife @nosave            ;; Don't save BP when NOSAVE is present.
  133.     PUSH    BP        ;; Save previous BP.
  134.     MOV    BP,SP        ;; Save pointer to arguments.
  135.   endif
  136. ENDM                ;; End of BENTRY macro.
  137.  
  138.  
  139. AUTO    MACRO    VARS,MISTAKE    ;; Defines local variables.
  140.   if @savefl            ;; Must not use save before auto!
  141.     @err <***ERROR*** Must not SAVE before AUTO.>
  142.   endif
  143.   if @nosave            ;; Does BP point to current stack loc?
  144.     @err <No auto variables allowed when NOSAVE is specified.>
  145.   endif
  146.   @check MISTAKE,AUTO        ;; Check for missing brackets.
  147.   irp var,<VARS>
  148.     @nlv=@nlv+2            ;; Two more bytes of local variable.
  149.     var&&@=-@nlv        ;; Offset of variable (skip saved BP).
  150.     ifndef var            ;; If not defined,
  151.       var equ [BP + var&&@]    ;;   build permanent definition.
  152.     endif
  153.   endm
  154.     SUB    SP,@nlv        ;; Bump SP past local vars.
  155. ENDM                ;; End of AUTO macro.
  156.  
  157.  
  158. @POP    MACRO    REG        ;; Internal macro to pop a register.
  159.   if @®            ;; If we saved this reg:
  160.     ifidn <reg>,<flags>
  161.     POPF            ;; Use special instruction for flags.
  162.     else
  163.     POP    REG        ;; Use ordinary pop.
  164.     endif
  165.   endif
  166. ENDM                ;; End of @POP macro.
  167.  
  168.  
  169. BEND    MACRO    FUNC        ;; Define end of procedure.
  170.   ifb <FUNC>
  171.     @err <Missing name on BEND macro.>
  172.   endif
  173.   irp reg,<flags,ES,SS,DS,DI,SI,DX,CX,BX,AX>
  174.     @pop reg            ;; Restore any saved registers.
  175.   endm
  176.   if @nlv            ;; If any local variables,
  177.     ADD    SP,@nlv        ;;   release stack space.
  178.   endif
  179.   ife @nosave            ;; Don't restore BP if we didn't save it.
  180.     POP    BP        ;; Restore old BP.
  181.   endif
  182.     RET            ;; Return to caller.
  183. FUNC    ENDP            ;; Define end of procedure.
  184. ENDM                ;; End of BEND macro.
  185.  
  186.  
  187. @SAVE    MACRO    R1,R2        ;; Famulus of SAVE macro.
  188.   ifidn <R1>,<R2>        ;; If they are the same:
  189.     @&R1=1            ;;   Set flag to show register saved.
  190.     @found=1            ;;   Tell caller we found it.
  191.   endif
  192. ENDM                ;; End of @SAVE macro.
  193.  
  194.  
  195. SAVE    MACRO    REGS,MISTAKE    ;; Macro for saving registers.
  196.   if @savefl
  197.     @err <***ERROR*** Must not SAVE more than once.>
  198.   endif
  199.   @savefl=1            ;; Enforce ordering constraints.
  200.   @check MISTAKE,SAVE        ;; Check for missing brackets.
  201.   irp reg,<REGS>        ;; Check if all registers are known:
  202.     @found=0            ;;   Haven't found register yet.
  203.     irp regname,<AX,BX,CX,DX,SI,DI,DS,SS,ES,flags>
  204.       @save regname,reg        ;;   Set corresponding register flag.
  205.     endm
  206.     ife @found            ;;   If we don't know a register:
  207.       @err <***ERROR*** 'reg': Unknown register name in SAVE.>
  208.     endif
  209.   endm
  210.   irp reg,<AX,BX,CX,DX,SI,DI,DS,SS,ES,flags>
  211.     if @&®            ;; Are we supposed to save this register?
  212.       ifidn <flags>,<reg>
  213.     PUSHF            ;; Special instruction to save flags.
  214.       else
  215.     PUSH    reg        ;; Save the register.
  216.       endif
  217.     endif
  218.   endm
  219. ENDM                ;; End of SAVE macro.
  220.  
  221. BCALL    MACRO    FUNC,ARGS,MISTAKE    ;; Macro for calling procedures.
  222.   @check MISTAKE,FUNC        ;; Check for missing brackets.
  223.   @nrgs=0            ;; Will get number of arguments.
  224.   ifnb <ARGS>            ;; Any arguments to process?
  225.     irp arg,<ARGS>
  226.       @nrgs=@nrgs+1        ;; Count number of arguments.
  227.     endm
  228.   endif
  229.   @thsrg=@nrgs            ;; Scan through all arguments,
  230.   rept @nrgs            ;; right-to-left:
  231.     @i=@thsrg            ;;   Next, scan through arguments
  232.     irp arg,<ARGS>        ;;   left-to-right:
  233.       @i=@i-1            ;;     Have we finally reached the
  234.       ife @i            ;;     desired argument?
  235.     PUSH    arg        ;;       Yes, store it on the stack.
  236.       endif
  237.     endm            ;;   End of left-to-right scan.
  238.     @thsrg=@thsrg-1        ;;   Set to look for previous argument.
  239.   endm                ;; End of right-to-left scan.
  240.     CALL    FUNC        ;; Call the target procedure.
  241.   if @nrgs            ;; Were there any arguments?
  242.     ADD    SP,@nrgs*2    ;; Remove any arguments from stack.
  243.   endif
  244. ENDM                ;; End of BCALL macro.
  245.