XTRAN Example — Analyze HP (Compaq, Digital) MACRO-11 Assembler
If you want to translate a body of assembly code to a higher level language, one of the tasks facing you is to identify all symbol definitions that are shared across separately assembled modules.
There are two common ways such definitions are shared in assembly code:
- The definitions are included in the definition of a macro; that macro is then invoked (expanded) near the beginning of each module that needs its definitions.
- The definitions are made global; the linker then resolves them.
Therefore, in order to find all shared definitions, we must find and report every definition that is either global or defined in the body of macro. For the latter case, we would like to know in which macro each shared name is defined. This is complicated by the fact that in many assemblers, macro definitions can be nested; that is, a macro can define another macro when it is invoked.
The following example shows how XTRAN rules can automate this task. The rules shown are an English paraphrase of the XTRAN rules language ("meta-code") actually used for the demonstration. We use a push-down stack to track the name of each macro we find defined, so that when we exit from a nested macro definition, we know what macro definition we "pop" back up into.
The input to and output from XTRAN are untouched, except that line numbers have been added to the input for reference.
Set macro level to 0 For each MACRO-11 statement: If start of macro definition Increment macro level ("push") Remember name of macro at this level Else if end of macro definition Decrement macro level ("pop") Restore name of macro at now current level Else if local symbol definition If macro level > 0 Output analysis line, including macro name Else if global symbol definition Output analysis line, including macro name if any
Here is a flowchart for this process, in which the elements are color coded:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
- PURPLE for text data files
MACRO-11 code to be analyzed (demanl-a.mac):
1 BITB #1,R0 2 VAR0 == 0 ;GLOBAL, NOT IN MACRO 3 BEQ 10$ 4 .MACRO MAC1 5 MOV R0,R1 6 VAR1 = 1 ;LOCAL, IN "MAC1" MACRO 7 MOV R1,R0 8 VAR2 == 2 ;GLOBAL, IN "MAC1" MACRO 9 .MACRO MAC2 10 VAR3 = 3 ;LOCAL, IN "MAC2" MACRO 11 .ENDM MAC2 12 VAR4 = 4 ;LOCAL, IN "MAC1" MACRO 13 .ENDM MAC1 14 ILLVAR = ILLEGAL ;LOCAL, NOT IN MACRO 15 VAR5 == 5 ;GLOBAL, NOT IN MACRO 16 MOV #1,R0
Output from XTRAN:
File Line Macro Defn G? ------------------------------------------------------------------------- demanl-a.mac 2 VAR0 G 6 MAC1 VAR1 8 MAC1 VAR2 G 10 MAC2 VAR3 12 MAC1 VAR4 15 VAR5 G