home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 149_01 / a685.c < prev    next >
Text File  |  1989-01-13  |  12KB  |  476 lines

  1. /*
  2.     HEADER:        CUG149;
  3.     TITLE:        6805 Cross-Assembler (Portable);
  4.     FILENAME:    A685.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 file contains the main program and line assembly routines for the
  51. assembler.  The main program parses the command line, feeds the source lines
  52. to the line assembly routine, and sends the results to the listing and object
  53. file output routines.  It also coordinates the activities of everything.  The
  54. line assembly routines uses the expression analyzer and the lexical analyzer
  55. to parse the source line and convert it into the object bytes that it
  56. represents.
  57. */
  58.  
  59. /*  Get global goodies:  */
  60.  
  61. #include "a685.h"
  62.  
  63. /*  Define global mailboxes for all modules:                */
  64.  
  65. char errcode, line[MAXLINE + 1], title[MAXLINE];
  66. int pass = 0;
  67. int eject, filesp, forwd, listhex;
  68. unsigned  address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
  69. FILE *filestk[FILES], *source;
  70. TOKEN token;
  71.  
  72. /*  Mainline routine.  This routine parses the command line, sets up    */
  73. /*  the assembler at the beginning of each pass, feeds the source text    */
  74. /*  to the line assembler, feeds the result to the listing and hex file    */
  75. /*  drivers, and cleans everything up at the end of the run.        */
  76.  
  77. static int done, ifsp, off;
  78.  
  79. void main(argc,argv)
  80. int argc;
  81. char **argv;
  82. {
  83.     SCRATCH unsigned *o;
  84.     int newline();
  85.     void asm_line();
  86.     void lclose(), lopen(), lputs();
  87.     void hclose(), hopen(), hputc();
  88.     void error(), fatal_error(), warning();
  89.  
  90.     printf("6805 Cross-Assembler (Portable) Ver 0.3\n");
  91.     printf("Copyright (c) 1985 William C. Colley, III\n\n");
  92.  
  93.     while (--argc > 0) {
  94.     if (**++argv == '-') {
  95.         switch (toupper(*++*argv)) {
  96.         case 'L':   if (!*++*argv) {
  97.                 if (!--argc) { warning(NOLST);  break; }
  98.                 else ++argv;
  99.                 }
  100.                 lopen(*argv);
  101.                 break;
  102.  
  103.         case 'O':   if (!*++*argv) {
  104.                 if (!--argc) { warning(NOHEX);  break; }
  105.                 else ++argv;
  106.                 }
  107.                 hopen(*argv);
  108.                 break;
  109.  
  110.         default:    warning(BADOPT);
  111.         }
  112.     }
  113.     else if (filestk[0]) warning(TWOASM);
  114.     else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
  115.     }
  116.     if (!filestk[0]) fatal_error(NOASM);
  117.  
  118.     while (++pass < 3) {
  119.     fseek(source = filestk[0],0L,0);  done = off = FALSE;
  120.     errors = filesp = ifsp = pagelen = pc = 0;  title[0] = '\0';
  121.     while (!done) {
  122.         errcode = ' ';
  123.         if (newline()) {
  124.         error('*');
  125.         strcpy(line,"\tEND\n");
  126.         done = eject = TRUE;  listhex = FALSE;
  127.         bytes = 0;
  128.         }
  129.         else asm_line();
  130.         pc = word(pc + bytes);
  131.         if (pass == 2) {
  132.         lputs();
  133.         for (o = obj; bytes--; hputc(*o++));
  134.         }
  135.     }
  136.     }
  137.  
  138.     fclose(filestk[0]);  lclose();  hclose();
  139.  
  140.     if (errors) printf("%d Error(s)\n",errors);
  141.     else printf("No Errors\n");
  142.  
  143.     exit(errors);
  144. }
  145.  
  146. /*  Line assembly routine.  This routine gets expressions and tokens    */
  147. /*  from the source file using the expression evaluator and lexical    */
  148. /*  analyzer, respectively.  It fills a buffer with the machine code    */
  149. /*  bytes and returns nothing.                        */
  150.  
  151. static char label[MAXLINE];
  152. static int ifstack[IFDEPTH] = { ON };
  153.  
  154. static OPCODE *opcod;
  155.  
  156. void asm_line()
  157. {
  158.     SCRATCH int i;
  159.     int isalph(), popc();
  160.     OPCODE *find_code(), *find_operator();
  161.     void do_label(), flush(), normal_op(), pseudo_op();
  162.     void error(), pops(), pushc(), trash();
  163.  
  164.     address = pc;  bytes = 0;  eject = forwd = listhex = FALSE;
  165.     for (i = 0; i < BIGINST; obj[i++] = NOP);
  166.  
  167.     label[0] = '\0';
  168.     if ((i = popc()) != ' ' && i != '\n') {
  169.     if (isalph(i)) {
  170.         pushc(i);  pops(label);
  171.         if (find_operator(label)) { label[0] = '\0';  error('L'); }
  172.     }
  173.     else {
  174.         error('L');
  175.         while ((i = popc()) != ' ' && i != '\n');
  176.     }
  177.     }
  178.  
  179.     trash(); opcod = NULL;
  180.     if ((i = popc()) != '\n') {
  181.     if (!isalph(i)) error('S');
  182.     else {
  183.         pushc(i);  pops(token.sval);
  184.         if (!(opcod = find_code(token.sval))) error('O');
  185.     }
  186.     if (!opcod) { listhex = TRUE;  bytes = BIGINST; }
  187.     }
  188.  
  189.     if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
  190.     else if (off) { listhex = FALSE;  flush();  return; }
  191.  
  192.     if (!opcod) { do_label();  flush(); }
  193.     else {
  194.     listhex = TRUE;
  195.     if (opcod -> attr & PSEUDO) pseudo_op();
  196.     else normal_op();
  197.     while ((i = popc()) != '\n') if (i != ' ') error('T');
  198.     }
  199.     source = filestk[filesp];
  200.     return;
  201. }
  202.  
  203. static void flush()
  204. {
  205.     while (popc() != '\n');
  206. }
  207.  
  208. static void do_label()
  209. {
  210.     SCRATCH SYMBOL *l;
  211.     SYMBOL *find_symbol(), *new_symbol();
  212.     void error();
  213.  
  214.     if (label[0]) {
  215.     listhex = TRUE;
  216.     if (pass == 1) {
  217.         if (!((l = new_symbol(label)) -> attr)) {
  218.         l -> attr = FORWD + VAL;
  219.         l -> valu = pc;
  220.         }
  221.     }
  222.     else {
  223.         if (l = find_symbol(label)) {
  224.         l -> attr = VAL;
  225.         if (l -> valu != pc) error('M');
  226.         }
  227.         else error('P');
  228.     }
  229.     }
  230. }
  231.  
  232. #define    NONUM        0
  233. #define    NEEDBYTE    1
  234. #define    HAVENUM        2
  235.  
  236. static void normal_op()
  237. {
  238.     SCRATCH unsigned attrib, operand;
  239.     unsigned expr();
  240.     TOKEN *lex();
  241.     void do_label(), error(), unlex();
  242.  
  243.     do_label();
  244.  
  245.     bytes = (attrib = opcod -> attr) & MAXBYTES;
  246.     obj[0] = opcod -> valu;  operand = obj[1] = obj[2] = 0;
  247.  
  248.     if (!(attrib & ~MAXBYTES)) return;
  249.     if (attrib & BIT) {
  250.     if ((operand = expr()) > 7) error('V');
  251.     else obj[0] += operand << 1;
  252.     operand = 0;
  253.     }
  254.     if ((lex() -> attr & TYPE) != SEP) {
  255.     if ((token.attr & TYPE) == IMM) {
  256.         if (attrib & IMMED) {
  257.         bytes = 2;  obj[0] -= 0x10;
  258.         if ((operand = expr()) > 0xff && operand < 0xff80) error('V');
  259.         else obj[1] = low(operand);
  260.         }
  261.         else error('A');
  262.         return;
  263.     }
  264.     unlex();  operand = expr();
  265.     if ((attrib & (DIR + REL)) == (DIR + REL)) {
  266.         attrib &= ~DIR;
  267.         if (operand > 255) error('V');
  268.         else obj[1] = operand;
  269.         operand = expr();
  270.     }
  271.     if (attrib & REL) {
  272.         if ((operand = word(operand - (pc + bytes))) > 0x7f &&
  273.         operand < 0xff80) {
  274.         error('B');  operand = -bytes;
  275.         }
  276.         obj[bytes - 1] = low(operand);
  277.         return;
  278.     }
  279.     if ((token.attr & TYPE) == EOL) {
  280.         if ((attrib & EXT) && (forwd || operand > 0xff)) {
  281.         obj[0] += 0x10;
  282.         obj[1] = high(operand);  obj[2] = low(operand);
  283.         }
  284.         else {
  285.         bytes = 2;
  286.         if (operand > 0xff) error('V');
  287.         else obj[1] = operand;
  288.         }
  289.         return;
  290.     }
  291.     }
  292.     if ((lex() -> attr & TYPE) != REG) error('S');
  293.     else {
  294.     if (!(attrib & IDX)) error('A');
  295.     else if ((attrib & EXT_IDX) && (forwd || operand > 0xff)) {
  296.         obj[0] += 0x20;  obj[1] = high(operand);  obj[2] = low(operand);
  297.     }
  298.     else if (forwd || operand) {
  299.         obj[0] += 0x30;  bytes = 2;
  300.         if (operand > 0xff) error('V');
  301.         else obj[1] = low(operand);
  302.     }
  303.     else { obj[0] += 0x40;  bytes = 1; }
  304.     }
  305.     return;
  306. }
  307.  
  308. static void pseudo_op()
  309. {
  310.     SCRATCH char *s;
  311.     SCRATCH unsigned *o, u;
  312.     SCRATCH SYMBOL *l;
  313.     unsigned expr();
  314.     SYMBOL *find_symbol(), *new_symbol();
  315.     TOKEN *lex();
  316.     void do_label(), error(), fatal_error(), hseek(), unlex();
  317.  
  318.     o = obj;
  319.     switch (opcod -> valu) {
  320.     case ELSE:  listhex = FALSE;
  321.             if (ifsp) off = (ifstack[ifsp] = -ifstack[ifsp]) != ON;
  322.             else error('I');
  323.             break;
  324.  
  325.     case END:   do_label();
  326.             if (filesp) { listhex = FALSE;  error('*'); }
  327.             else {
  328.             done = eject = TRUE;
  329.             if (pass == 2 && (lex() -> attr & TYPE) != EOL) {
  330.                 unlex();  hseek(address = expr());
  331.             }
  332.             if (ifsp) error('I');
  333.             }
  334.             break;
  335.  
  336.     case ENDIF: listhex = FALSE;
  337.             if (ifsp) off = ifstack[--ifsp] != ON;
  338.             else error('I');
  339.             break;
  340.  
  341.     case EQU:   if (label[0]) {
  342.             if (pass == 1) {
  343.                 if (!((l = new_symbol(label)) -> attr)) {
  344.                 l -> attr = FORWD + VAL;
  345.                 address = expr();
  346.                 if (!forwd) l -> valu = address;
  347.                 }
  348.             }
  349.             else {
  350.                 if (l = find_symbol(label)) {
  351.                 l -> attr = VAL;
  352.                 address = expr();
  353.                 if (forwd) error('P');
  354.                 if (l -> valu != address) error('M');
  355.                 }
  356.                 else error('P');
  357.             }
  358.             }
  359.             else error('L');
  360.             break;
  361.  
  362.     case FCB:   do_label();
  363.             do {
  364.             if ((lex() -> attr & TYPE) == SEP) u = 0;
  365.             else {
  366.                 unlex();
  367.                 if ((u = expr()) > 0xff && u < 0xff80) {
  368.                 u = 0;  error('V');
  369.                 }
  370.             }
  371.             *o++ = low(u);  ++bytes;
  372.             } while ((token.attr & TYPE) == SEP);
  373.             break;
  374.  
  375.     case FCC:   do_label();
  376.             while ((lex() -> attr & TYPE) != EOL) {
  377.             if ((token.attr & TYPE) == STR) {
  378.                 for (s = token.sval; *s; *o++ = *s++)
  379.                 ++bytes;
  380.                 if ((lex() -> attr & TYPE) != SEP) unlex();
  381.             }
  382.             else error('S');
  383.             }
  384.             break;
  385.  
  386.     case FDB:   do_label();
  387.             do {
  388.             if ((lex() -> attr & TYPE) == SEP) u = 0;
  389.             else { unlex();  u = expr(); }
  390.             *o++ = high(u);  *o++ = low(u);
  391.             bytes += 2;
  392.             } while ((token.attr & TYPE) == SEP);
  393.             break;
  394.  
  395.     case IF:    if (++ifsp == IFDEPTH) fatal_error(IFOFLOW);
  396.             address = expr();
  397.             if (forwd) { error('P');  address = TRUE; }
  398.             if (off) { listhex = FALSE;  ifstack[ifsp] = NULL; }
  399.             else {
  400.             ifstack[ifsp] = address ? ON : OFF;
  401.             if (!address) off = TRUE;
  402.             }
  403.             break;
  404.  
  405.     case INCL:  listhex = FALSE;  do_label();
  406.             if ((lex() -> attr & TYPE) == STR) {
  407.             if (++filesp == FILES) fatal_error(FLOFLOW);
  408.             if (!(filestk[filesp] = fopen(token.sval,"r"))) {
  409.                 --filesp;  error('V');
  410.             }
  411.             }
  412.             else error('S');
  413.             break;
  414.  
  415.     case ORG:   u = expr();
  416.             if (forwd) error('P');
  417.             else {
  418.             pc = address = u;
  419.             if (pass == 2) hseek(pc);
  420.             }
  421.             do_label();
  422.             break;
  423.  
  424.     case PAGE:  listhex = FALSE;  do_label();
  425.             if ((lex() -> attr & TYPE) != EOL) {
  426.             unlex();  pagelen = expr();
  427.             if (pagelen > 0 && pagelen < 3) {
  428.                 pagelen = 0;  error('V');
  429.             }
  430.             }
  431.             eject = TRUE;
  432.             break;
  433.  
  434.     case RMB:   do_label();
  435.             u = word(pc + expr());
  436.             if (forwd) error('P');
  437.             else {
  438.             pc = u;
  439.             if (pass == 2) hseek(pc);
  440.             }
  441.             break;
  442.  
  443.     case SET:   if (label[0]) {
  444.             if (pass == 1) {
  445.                 if (!((l = new_symbol(label)) -> attr)
  446.                 || (l -> attr & SOFT)) {
  447.                 l -> attr = FORWD + SOFT + VAL;
  448.                 address = expr();
  449.                 if (!forwd) l -> valu = address;
  450.                 }
  451.             }
  452.             else {
  453.                 if (l = find_symbol(label)) {
  454.                 address = expr();
  455.                 if (forwd) error('P');
  456.                 else if (l -> attr & SOFT) {
  457.                     l -> attr = SOFT + VAL;
  458.                     l -> valu = address;
  459.                 }
  460.                 else error('M');
  461.                 }
  462.                 else error('P');
  463.             }
  464.             }
  465.             else error('L');
  466.             break;
  467.  
  468.     case TITLE: listhex = FALSE;  do_label();
  469.             if ((lex() -> attr & TYPE) == EOL) title[0] = '\0';
  470.             else if ((token.attr & TYPE) != STR) error('S');
  471.             else strcpy(title,token.sval);
  472.             break;
  473.     }
  474.     return;
  475. }
  476.