home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 149_01 / a685util.c < prev    next >
Text File  |  1989-01-13  |  15KB  |  529 lines

  1. /*
  2.     HEADER:        CUG149;
  3.     TITLE:        6805 Cross-Assembler (Portable);
  4.     FILENAME:    A685UTIL.C;
  5.     VERSION:    0.3;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Motorola 6805 family microprocessors.
  10.             The program is written in portable C rather than BDS
  11.             C.  All    assembler features are supported except
  12.             relocation linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Motorola, MC6805;
  16.  
  17.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  18.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  19.             Lattice C, Microsoft C,    QNIX C;
  20.  
  21.     WARNINGS:    "This program has compiled successfully on 2 UNIX
  22.             compilers, 5 MSDOS compilers, and 2 CP/M compilers.
  23.             A port to BDS C would be extremely difficult, but see
  24.             volume CUG113.  A port to Toolworks C is untried."
  25.  
  26.     AUTHORS:    William C. Colley III;
  27. */
  28.  
  29. /*
  30.               6805 Cross-Assembler in Portable C
  31.  
  32.            Copyright (c) 1985 William C. Colley, III
  33.  
  34. Revision History:
  35.  
  36. Ver    Date        Description
  37.  
  38. 0.0    SEP 1985    Adapted from version 3.2 of the portable 6801 cross-
  39.             assembler.  WCC3.
  40.  
  41. 0.1    JUL 1986    Added compilation instructions and tweaks for CI-C86,
  42.             Eco-C88, and Lattice C.  WCC3.
  43.  
  44. 0.2    JAN 1987    Fixed bug that made "FCB 0," legal syntax.  WCC3.
  45.  
  46. 0.3    AUG 1988    Fixed a bug in the command line parser that puts it
  47.             into a VERY long loop if the user types a command line
  48.             like "A685 FILE.ASM -L".  WCC3 per Alex Cameron.
  49.  
  50. This module contains the following utility packages:
  51.  
  52.     1)  symbol table building and searching
  53.  
  54.     2)  opcode and operator table searching
  55.  
  56.     3)  listing file output
  57.  
  58.     4)  hex file output
  59.  
  60.     5)  error flagging
  61. */
  62.  
  63. /*  Get global goodies:  */
  64.  
  65. #include "a685.h"
  66.  
  67. /*  Make sure that MSDOS compilers using the large memory model know    */
  68. /*  that calloc() returns pointer to char as an MSDOS far pointer is    */
  69. /*  NOT compatible with the int type as is usually the case.        */
  70.  
  71. char *calloc();
  72.  
  73. /*  Get access to global mailboxes defined in A68.C:            */
  74.  
  75. extern char errcode, line[], title[];
  76. extern int eject, listhex;
  77. extern unsigned address, bytes, errors, listleft, obj[], pagelen;
  78.  
  79. /*  The symbol table is a binary tree of variable-length blocks drawn    */
  80. /*  from the heap with the calloc() function.  The root pointer lives    */
  81. /*  here:                                */
  82.  
  83. static SYMBOL *sroot = NULL;
  84.  
  85. /*  Add new symbol to symbol table.  Returns pointer to symbol even if    */
  86. /*  the symbol already exists.  If there's not enough memory to store    */
  87. /*  the new symbol, a fatal error occurs.                */
  88.  
  89. SYMBOL *new_symbol(nam)
  90. char *nam;
  91. {
  92.     SCRATCH int i;
  93.     SCRATCH SYMBOL **p, *q;
  94.     void fatal_error();
  95.  
  96.     for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
  97.     p = i < 0 ? &(q -> left) : &(q -> right);
  98.     if (!q) {
  99.     if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
  100.         fatal_error(SYMBOLS);
  101.     strcpy(q -> sname,nam);
  102.     }
  103.     return q;
  104. }
  105.  
  106. /*  Look up symbol in symbol table.  Returns pointer to symbol or NULL    */
  107. /*  if symbol not found.                        */
  108.  
  109. SYMBOL *find_symbol(nam)
  110. char *nam;
  111. {
  112.     SCRATCH int i;
  113.     SCRATCH SYMBOL *p;
  114.  
  115.     for (p = sroot; p && (i = strcmp(nam,p -> sname));
  116.     p = i < 0 ? p -> left : p -> right);
  117.     return p;
  118. }
  119.  
  120. /*  Opcode table search routine.  This routine pats down the opcode    */
  121. /*  table for a given opcode and returns either a pointer to it or    */
  122. /*  NULL if the opcode doesn't exist.                    */
  123.  
  124. OPCODE *find_code(nam)
  125. char *nam;
  126. {
  127.     OPCODE *bsearch();
  128.  
  129.     static OPCODE opctbl[] = {
  130.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb9,    "ADC"    },
  131.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xbb,    "ADD"    },
  132.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb4,    "AND"    },
  133.     { DIR + IDX + 2,                0x38,    "ASL"    },
  134.     { NULL + 1,                    0x48,    "ASLA"    },
  135.     { NULL + 1,                    0x58,    "ASLX"    },
  136.     { DIR + IDX + 2,                0x37,    "ASR"    },
  137.     { NULL + 1,                    0x47,    "ASRA"    },
  138.     { NULL + 1,                    0x57,    "ASRX"    },
  139.     { REL + 2,                    0x24,    "BCC"    },
  140.     { BIT + DIR + 2,                0x11,    "BCLR"    },
  141.     { REL + 2,                    0x25,    "BCS"    },
  142.     { REL + 2,                    0x27,    "BEQ"    },
  143.     { REL + 2,                    0x28,    "BHCC"    },
  144.     { REL + 2,                    0x29,    "BHCS"    },
  145.     { REL + 2,                    0x22,    "BHI"    },
  146.     { REL + 2,                    0x24,    "BHS"    },
  147.     { REL + 2,                    0x2f,    "BIH"    },
  148.     { REL + 2,                    0x2e,    "BIL"    },
  149.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb5,    "BIT"    },
  150.     { REL + 2,                    0x25,    "BLO"    },
  151.     { REL + 2,                    0x23,    "BLS"    },
  152.     { REL + 2,                    0x2c,    "BMC"    },
  153.     { REL + 2,                    0x2b,    "BMI"    },
  154.     { REL + 2,                    0x2d,    "BMS"    },
  155.     { REL + 2,                    0x26,    "BNE"    },
  156.     { REL + 2,                    0x2a,    "BPL"    },
  157.     { REL + 2,                    0x20,    "BRA"    },
  158.     { BIT + REL + DIR + 3,                0x01,    "BRCLR"    },
  159.     { REL + 2,                    0x21,    "BRN"    },
  160.     { BIT + REL + DIR + 3,                0x00,    "BRSET"    },
  161.     { BIT + DIR + 2,                0x10,    "BSET"    },
  162.     { REL + 2,                    0xad,    "BSR"    },
  163.     { NULL + 1,                    0x98,    "CLC"    },
  164.     { NULL + 1,                    0x9a,    "CLI"    },
  165.     { DIR + IDX + 2,                0x3f,    "CLR"    },
  166.     { NULL + 1,                    0x4f,    "CLRA"    },
  167.     { NULL + 1,                    0x5f,    "CLRX"    },
  168.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb1,    "CMP"    },
  169.     { DIR + IDX + 2,                0x33,    "COM"    },
  170.     { NULL + 1,                    0x43,    "COMA"    },
  171.     { NULL + 1,                    0x53,    "COMX"    },
  172.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb3,    "CPX"    },
  173.     { DIR + IDX + 2,                0x3a,    "DEC"    },
  174.     { NULL + 1,                    0x4a,    "DECA"    },
  175.     { NULL + 1,                    0x5a,    "DECX"    },
  176.     { NULL + 1,                    0x5a,    "DEX"    },
  177.     { PSEUDO + ISIF,                ELSE,    "ELSE"    },
  178.     { PSEUDO,                    END,    "END"    },
  179.     { PSEUDO + ISIF,                ENDIF,    "ENDIF"    },
  180.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb8,    "EOR"    },
  181.     { PSEUDO,                    EQU,    "EQU"    },
  182.     { PSEUDO,                    FCB,    "FCB"    },
  183.     { PSEUDO,                    FCC,    "FCC"    },
  184.     { PSEUDO,                    FDB,    "FDB"    },
  185.     { PSEUDO + ISIF,                IF,    "IF"    },
  186.     { DIR  + IDX + 2,                0x3c,    "INC"    },
  187.     { NULL + 1,                    0x4c,    "INCA"    },
  188.     { PSEUDO,                    INCL,    "INCL"    },
  189.     { NULL + 1,                    0x5c,    "INCX"    },
  190.     { NULL + 1,                    0x5c,    "INX"    },
  191.     { DIR + EXT + IDX + EXT_IDX + 3,        0xbc,    "JMP"    },
  192.     { DIR + EXT + IDX + EXT_IDX + 3,        0xbd,    "JSR"    },
  193.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb6,    "LDA"    },
  194.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xbe,    "LDX"    },
  195.     { DIR + IDX + 2,                0x38,    "LSL"    },
  196.     { NULL + 1,                    0x48,    "LSLA"    },
  197.     { NULL + 1,                    0x58,    "LSLX"    },
  198.     { DIR + IDX + 2,                0x34,    "LSR"    },
  199.     { NULL + 1,                    0x44,    "LSRA"    },
  200.     { NULL + 1,                    0x54,    "LSRX"    },
  201.     { NULL + 1,                    0x42,    "MUL"    },
  202.     { DIR + IDX + 2,                0x30,    "NEG"    },
  203.     { NULL + 1,                    0x40,    "NEGA"    },
  204.     { NULL + 1,                    0x50,    "NEGX"    },
  205.     { NULL + 1,                    0x9d,    "NOP"    },
  206.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xba,    "ORA"    },
  207.     { PSEUDO,                    ORG,    "ORG"    },
  208.     { PSEUDO,                    PAGE,    "PAGE"    },
  209.     { PSEUDO,                    RMB,    "RMB"    },
  210.     { DIR + IDX + 2,                0x39,    "ROL"    },
  211.     { NULL + 1,                    0x49,    "ROLA"    },
  212.     { NULL + 1,                    0x59,    "ROLX"    },
  213.     { DIR + IDX + 2,                0x36,    "ROR"    },
  214.     { NULL + 1,                    0x46,    "RORA"    },
  215.     { NULL + 1,                    0x56,    "RORX"    },
  216.     { NULL + 1,                    0x9c,    "RSP"    },
  217.     { NULL + 1,                    0x80,    "RTI"    },
  218.     { NULL + 1,                    0x81,    "RTS"    },
  219.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb2,    "SBC"    },
  220.     { NULL + 1,                    0x99,    "SEC"    },
  221.     { NULL + 1,                    0x9b,    "SEI"    },
  222.     { PSEUDO,                    SET,    "SET"    },
  223.     { DIR + EXT + IDX + EXT_IDX + 3,        0xb7,    "STA"    },
  224.     { NULL + 1,                    0x8e,    "STOP"    },
  225.     { DIR + EXT + IDX + EXT_IDX + 3,        0xbf,    "STX"    },
  226.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb0,    "SUB"    },
  227.     { NULL + 1,                    0x83,    "SWI"    },
  228.     { NULL + 1,                    0x97,    "TAX"    },
  229.     { PSEUDO,                    TITLE,    "TITLE"    },
  230.     { DIR + IDX + 2,                0x3d,    "TST"    },
  231.     { NULL + 1,                    0x4d,    "TSTA"    },
  232.     { NULL + 1,                    0x5d,    "TSTX"    },
  233.     { NULL + 1,                    0x9f,    "TXA"    },
  234.     { NULL + 1,                    0x8f,    "WAIT"    }
  235.     };
  236.  
  237.     return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
  238. }
  239.  
  240. /*  Operator table search routine.  This routine pats down the        */
  241. /*  operator table for a given operator and returns either a pointer    */
  242. /*  to it or NULL if the opcode doesn't exist.                */
  243.  
  244. OPCODE *find_operator(nam)
  245. char *nam;
  246. {
  247.     OPCODE *bsearch();
  248.  
  249.     static OPCODE oprtbl[] = {
  250.     { BINARY + LOG1  + OPR,        AND,        "AND"    },
  251.     { BINARY + RELAT + OPR,        '=',        "EQ"    },
  252.     { BINARY + RELAT + OPR,        GE,        "GE"    },
  253.     { BINARY + RELAT + OPR,        '>',        "GT"    },
  254.     { UNARY  + UOP3  + OPR,        HIGH,        "HIGH"    },
  255.     { BINARY + RELAT + OPR,        LE,        "LE"    },
  256.     { UNARY  + UOP3  + OPR,        LOW,