home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / pdp8 / pdp8e-em.lzh / pal.c < prev    next >
C/C++ Source or Header  |  1994-11-09  |  26KB  |  1,193 lines

  1. /* file:    pal.c
  2.  
  3.    purpose: a 2 pass PDP-8 pal-like assembler
  4.  
  5.    author:  Douglas Jones <jones@cs.uiowa.edu> -- built basic bits
  6.             Rich Coon <coon@convexw.convex.com> -- added enough handle OS/278
  7.  
  8.    disclaimer:  This assembler accepts a subset of the PAL 8 assembly language.
  9.     It was written as a quick hack in order to download software into my
  10.     bare-bones PDP-8 system, and no effort has been made to apply sound
  11.     software engineering techniques in its design.  See the symbol table
  12.     and code for the set of pseudo-ops supported.  See DEC's "Programming
  13.     Languages (for the PDP/8)" for complete documentation of pseudo-ops,
  14.     or see DEC's "Introduction to Programming (for the PDP/8)" or a
  15.     lower level introduction to the assembly language.
  16.  
  17.    use: After assembly on a UNIX system, this program should be stored as
  18.     the pal command.  This program uses the following file name extensions
  19.  
  20.         .pal    source code (input)
  21.         .lst    assembly listing (output)
  22.         .bin    assembly output in DEC's bin format (output)
  23.         .rim    assembly output in DEC's rim format (output)
  24.  
  25.     This program takes the following command line switches
  26.  
  27.         -d    dump the symbol table at end of assembly
  28.         -r    produce output in rim format (default is bin format)
  29.  
  30.    known bugs:  Only a minimal effort has been made to keep the listing
  31.     format anything like the PAL-8 listing format.  It screams too loud
  32.     for off-page addresses (a quote mark would suffice), and it doesn't
  33.     detect off-page addresses it can't fix (it should scream about them).
  34.  
  35.    warrantee:  If you don't like it the way it works or if it doesn't work,
  36.     that's tough.  You're welcome to fix it yourself.  That's what you
  37.     get for using free software.
  38.  
  39.    copyright notice:  I wrote this and gave it away for free; if you want to
  40.     make changes and give away the result for free, that's fine with me.
  41.     If you can fix it enough that you can sell it, ok, but don't put any
  42.     limits on the purchaser's right to do the same.  All the above aside,
  43.     if you improve it or fix any bugs, it would be nice if you told me
  44.     and offered me a copy of the new version.
  45. */
  46.  
  47. #include <stdio.h>
  48. #include <ctype.h>
  49. #define isend(c) ((c=='\0')||(c=='\n'))
  50. #define isdone(c) ( (c == '/') || (isend(c)) || (c == ';') )
  51.  
  52. /* connections to command line */
  53. #define NAMELEN 128
  54. FILE *in;        /* input file */
  55. char *filename = NULL;    /* input file's name */
  56. FILE *obj = NULL;    /* object file */
  57. FILE *objsave = NULL;    /* alternate object file (supports ENPUNCH/NOPUNCH) */
  58. char objname[NAMELEN];    /* object file's name */
  59. FILE *lst = NULL;    /* listing file */
  60. FILE *lstsave = NULL;    /* alternate listing file */
  61. char lstname[NAMELEN];    /* listing file's name */
  62. int dumpflag = 0;    /* dump symtab if 1 (defaults to no dump) */
  63. int rimflag = 0;    /* generate rim format if 1 (defaults bin) */
  64. int cksum = 0;        /* checksum generated for .bin files */
  65. int errors = 0;        /* number of errors found so far */
  66.  
  67. /* return c as upper-case if it is lower-case; else no change */
  68. int c2upper(c)
  69. int c;
  70. {
  71.     return ((c >= 'a' && c <= 'z') ? (c - ('a'-'A')) : c);
  72. }
  73.  
  74. /* command line argument processing */
  75. getargs(argc, argv)
  76. int argc;
  77. char *argv[];
  78. {
  79.     int i,j;
  80.  
  81.         for (i=1; i < argc; i++) {
  82.         if (argv[i][0] == '-') { /* a flag */
  83.             for (j=1; argv[i][j] != 0; j++) {
  84.                 if (argv[i][1] == 'd') {
  85.                     dumpflag = 1;
  86.                 } else if (argv[i][1] == 'r') {
  87.                     rimflag = 1;
  88.                 } else {
  89.                     fprintf( stderr,
  90.                          "%s: unknown flag: %s\n",
  91.                          argv[0], argv[i] );
  92.                     fprintf( stderr,
  93.                          " -d -- dump symtab\n" );
  94.                     fprintf( stderr,
  95.                          " -r -- output rim file\n" );
  96.                     exit(-1);
  97.                 }
  98.             }
  99.         } else { /* no - at front of argument, must be file name */
  100.             if (filename != NULL) {
  101.                 fprintf( stderr, "%s: too many input files\n",
  102.                      argv[0] );
  103.                 exit(-1);
  104.             }
  105.             filename = &argv[i][0];
  106.         } /* end if */
  107.         } /* end for loop */
  108.  
  109.         if (filename == NULL) { /* no input file specified */
  110.         fprintf( stderr, "%s: no input file specified\n", argv[0] );
  111.         exit(-1);
  112.     }
  113.  
  114.     /* now, fix up the file names */
  115.     {
  116.         int dot; /* location of last dot in filename */
  117.         dot = 0;
  118.         for (j = 0; filename[j] != 0; j++) {
  119.             if (j > (NAMELEN - 5)) {
  120.                 fprintf( stderr,
  121.                      "%s: file name too long %s\n",
  122.                      argv[0], filename );
  123.                 exit(-1);
  124.             }
  125.             lstname[j] = objname[j] = filename[j];
  126.             if (filename[j]=='.') {
  127.                 dot = j;
  128.             } else if (filename[j]=='/') {
  129.                 dot = 0;
  130.             }
  131.         }
  132.         if (dot == 0) {
  133.             dot = j;
  134.             lstname[dot] = objname[dot] = '.';
  135.         }
  136.         lstname[dot + 1] = 'l';
  137.         lstname[dot + 2] = 's';
  138.         lstname[dot + 3] = 't';
  139.         lstname[dot + 4] = '\0';
  140.  
  141.         if (rimflag == 1) {
  142.             objname[dot + 1] = 'r';
  143.             objname[dot + 2] = 'i';
  144.             objname[dot + 3] = 'm';
  145.         } else {
  146.             objname[dot + 1] = 'b';
  147.             objname[dot + 2] = 'i';
  148.             objname[dot + 3] = 'n';
  149.         }
  150.         objname[dot + 4] = '\0';
  151.         }
  152.  
  153.     /* now, open up the input file */
  154.     if ((in = fopen(filename, "r")) == NULL) {
  155.         fprintf( stderr, "%s: cannot open %s\n", argv[0], filename );
  156.         exit(-1);
  157.     }
  158. }
  159.  
  160. /* symbol table */
  161. #define SYMBOLS  1024
  162. #define SYMLEN 7
  163. struct symbol {
  164.     char sym[SYMLEN]; /* the textual name of the symbol, zero filled */
  165.     short int val;      /* the value associated with the symbol */
  166. } symtab[SYMBOLS] = {  /* values = 01xxxx indicate MRI */
  167.                /* values = 04xxxx indicate pseudo-ops */
  168.  
  169.     "DECIMAL", 040000, /* read literal constants in base 10 */
  170.     "OCTAL"  , 040001, /* read literal constants in base 8 */
  171.     "ZBLOCK" , 040002, /* zero a block of memory */
  172.     "PAGE"   , 040003, /* advance origin to next page or page x (0..37) */
  173.     "TEXT"   , 040004, /* pack 6 bit trimmed ASCII into memory */
  174.     "EJECT"  , 040005, /* eject a page in the listing */
  175.     "FIELD"  , 040006, /* set origin to memory field */
  176.     "NOPUNCH", 040007, /* turn off object code generation */
  177.     "ENPUNCH", 040010, /* turn on object code generation */
  178.     "XLIST"  , 040011, /* toggle listing generation */
  179.     "IFZERO" , 040012, /* unsupported */
  180.     "IFNZRO" , 040013, /* unsupported */
  181.     "IFDEF"  , 040014, /* unsupported */
  182.     "IFNDEF" , 040015, /* unsupported */
  183.     "RELOC"  , 040016, /* assemble for execution at a different address */
  184.     "SEGMNT" , 040017, /* like page, but with page size = 1K words */
  185.     "BANK"   , 040020, /* like field, select a different 32K out of 128K */
  186.     "FIXMRI" , 040021, /* like =, but creates mem ref instruction */
  187.  
  188.     "AND", 010000, /* mainline instructions */
  189.     "TAD", 011000,
  190.     "ISZ", 012000,
  191.     "DCA", 013000,
  192.     "JMS", 014000,
  193.     "JMP", 015000,
  194.     "I",   010400,
  195.     "Z",   010000,
  196.  
  197.     "NOP", 007000, /* group 1 */
  198.     "CLA", 007200,
  199.     "CIA", 007041,
  200.     "CLL", 007100,
  201.     "CMA", 007040,
  202.     "CML", 007020,
  203.     "IAC", 007001,
  204.     "BSW", 007002,
  205.     "RAR", 007010,
  206.     "RAL", 007004,
  207.     "RTR", 007012,
  208.     "RTL", 007006,
  209.     "STA", 007240,
  210.     "STL", 007120,
  211.     "GLK", 007204,
  212.     "LAS", 007604,
  213.     
  214.  
  215.         "SMA", 007500, /* group 2 */
  216.     "SZA", 007440,
  217.     "SNL", 007420,
  218.     "SKP", 007410,
  219.     "SPA", 007510,
  220.     "SNA", 007450,
  221.     "SZL", 007430,
  222.     "OSR", 007404,
  223.     "HLT", 007402,
  224.  
  225.     "KCC", 006032, /* actually iots, but haven't fixed iots yet */
  226.     "KSF", 006031,
  227.     "KRS", 006034,
  228.     "KRB", 006036,
  229.     "IOT", 006000,
  230.     "ION", 006001,
  231.     "IOF", 006002,
  232.     "CDF", 006201,
  233.     "CIF", 006202,
  234.     "RDF", 006214,
  235.     "RIF", 006224,
  236.     "RIB", 006234,
  237.     "RMF", 006244,
  238.     "TSF", 006041,
  239.     "TCF", 006042,
  240.     "TPC", 006044,
  241.     "TLS", 006046,
  242.     "RSF", 006011,
  243.     "RRB", 006012,
  244.     "RFC", 006014,
  245.     "PSF", 006021,
  246.     "PCF", 006022,
  247.     "PPC", 006024,
  248.     "PLS", 006026
  249. };
  250.  
  251. /* the following define is based on a careful count of the above entries */
  252. #define firstsym 75
  253.  
  254. /* dump symbol table */
  255. dump()
  256. {
  257.     int i;
  258.     fprintf( lst, "\n" );
  259.     for (i = firstsym; symtab[i].sym[0] != '\0'; i++) {
  260.         fprintf( lst, "%s    %4.4o\n",
  261.              symtab[i].sym, symtab[i].val );
  262.     }
  263. }
  264.  
  265. /* define symbol */
  266. define( sym, val )
  267. char sym[SYMLEN];
  268. short int val;
  269. {
  270.     int i,j;
  271.     for (i = 0; symtab[i].sym[0] != '\0'; i++) {
  272.         for (j = 0; j < SYMLEN; j++) {
  273.             if (symtab[i].sym[j] != sym[j]) {
  274.                 goto mismatch;
  275.             }
  276.         }
  277.         goto match;
  278.         mismatch:;
  279.     }
  280.     /* if it ever gets here, a new symbol must be defined */
  281.     if (i < (SYMBOLS - 1)) {
  282.         for (j = 0; j < SYMLEN; j++) {
  283.             symtab[i].sym[j] = sym[j];
  284.         }
  285.     }
  286.     match:;
  287.     symtab[i].val = val;
  288. }
  289.  
  290. short int lookup( sym )
  291. char sym[SYMLEN];
  292. {
  293.     int i,j;
  294.     for (i = 0; symtab[i].sym[0] != '\0'; i++) {
  295.         for (j = 0; j < SYMLEN; j++) {
  296.             if (symtab[i].sym[j] != sym[j]) {
  297.                 goto mismatch;
  298.             }
  299.         }
  300.         goto match;
  301.         mismatch:;
  302.     }
  303.     /* if it ever gets here, the symbol is undefined */
  304.     return -1;
  305.  
  306.     match:;
  307.     return symtab[i].val;
  308. }
  309.  
  310. /* single pass of the assembler */
  311.  
  312. #define LINELEN 96
  313. char line[LINELEN];
  314. int pos;    /* position on line */
  315. int listed; /* has line been listed to listing yet (0 = no, 1 = yes) */
  316. int lineno; /* line number of current line */
  317.  
  318.  
  319. listline()
  320. /* generate a line of listing if not already done! */
  321. {
  322.     if ((lst != NULL) && (listed == 0)) {
  323.         fprintf( lst, "%4d            ", lineno );
  324.         fputs( line, lst );
  325.     }
  326.     listed = 1;
  327. }
  328.  
  329. error( msg )
  330. char *msg;
  331. /* generate a line of listing with embedded error messages */
  332. {
  333.     FILE *tlst;
  334.     if (lst == NULL) { /* force error messages to print despite XLIST */
  335.         tlst = lstsave;
  336.     } else {
  337.         tlst = lst;
  338.     }
  339.     if (tlst != NULL) {
  340.         listline();
  341.         fprintf( tlst, "%-15.15s ", msg );
  342.         {
  343.             int i;
  344.             for (i = 0; i < (pos-1); i++) {
  345.                 if (line[i] == '\t') {
  346.                     putc('\t', tlst );
  347.                 } else {
  348.                     putc( ' ', tlst );
  349.                 }
  350.             }
  351.         }
  352.         fputs( "^\n", tlst );
  353.         fprintf( stderr, "%4d  %s\n", lineno, msg );
  354.     }
  355.     listed = 1;
  356.     errors++;
  357. }
  358.  
  359. readline()
  360. /* read one input line, setting things up for lexical analysis */
  361. {
  362.     listline(); /* if it hasn't been listed yet, list it! */
  363.     lineno = lineno + 1;
  364.     listed = 0;
  365.     pos = 0;
  366.     if (fgets( line, LINELEN-1, in ) == NULL) {
  367.         line[0] = '$';
  368.         line[1] = '\n';
  369.         line[2] = '\000';
  370.         error( "end of file" );
  371.     }
  372. }
  373.  
  374. putleader()
  375. /* generate 2 feet of leader on the object file, as per DEC documentation */
  376. {
  377.     if (obj != NULL) {
  378.         int i;
  379.         for (i = 1; i < 240; i++) {
  380.              fputc( 0200, obj );
  381.         }
  382.     }
  383. }
  384.  
  385. puto(c)
  386. int c;
  387. /* put one character to obj file and include it in checksum */
  388. {
  389.     c &= 0377;
  390.     fputc(c, obj);
  391.     cksum += c;
  392. }
  393.  
  394. int field; /* the current field */
  395.  
  396. putorg( loc )
  397. short int loc;
  398. {
  399.     puto( ((loc >> 6) & 0077) | 0100 );
  400.     puto( loc & 0077 );
  401. }
  402.  
  403. putout( loc, val )
  404. short int loc;
  405. short int val;
  406. /* put out a word of object code */
  407. {
  408.     if (lst != NULL) {
  409.         if (listed == 0) {
  410.             fprintf( lst, "%4d %1.1o%4.4o %4.4o ",
  411.                  lineno, field, loc, val);
  412.             fputs( line, lst );
  413.         } else {
  414.             fprintf( lst, "     %1.1o%4.4o %4.4o ",
  415.                  field, loc, val);
  416.             putc( '\n', lst );
  417.         }
  418.     }
  419.     if (obj != NULL) {
  420.         if (rimflag == 1) { /* put out origin in rim mode */
  421.             putorg( loc );
  422.         }
  423.         puto( (val >> 6) & 0077 );
  424.         puto( val & 0077 );
  425.     }
  426.     listed = 1;
  427. }
  428.  
  429.  
  430. char lexstart; /* index of start of the current lexeme on line */
  431. char lexterm;  /* index of character after the current lexeme on line */
  432.  
  433. #define isblank(c) ((c==' ')||(c=='\t')||(c=='\f')||(c=='>'))
  434.  
  435. nextlex()
  436. /* get the next lexeme into lex */
  437. {
  438.     while (isblank(line[pos])) {
  439.         pos++;
  440.     }
  441.  
  442.     lexstart = pos;
  443.  
  444.     if (isalpha(line[pos])) { /* identifier */
  445.         while (isalnum(line[pos])) {
  446.             pos++;
  447.         }
  448.     } else if (isdigit(line[pos])) { /* number */
  449.         while (isdigit(line[pos])) {
  450.             pos++;
  451.         }
  452.     } else if (line[pos] == '"') { /* quoted letter */
  453.         pos ++;
  454.         pos ++;
  455.     } else if (isend(line[pos])) { /* end of line */
  456.         /* don't advance pos! */
  457.     } else if (line[pos] == '/') { /* comment */
  458.         /* don't advance pos! */
  459.     } else { /* all punctuation is handled here! */
  460.         pos++;
  461.     }
  462.  
  463.     lexterm = pos;
  464. }
  465.  
  466. deflex( start, term, val )
  467. int start; /* start of lexeme to be defined */
  468. int term; /* character after end of lexeme to be defined */
  469. int val; /* value of lexeme to be defined */
  470. {
  471.     char sym[SYMLEN];
  472.     int from, to;
  473.  
  474.     from = start;
  475.     to = 0;
  476.     while ((from < term) && (to < SYMLEN)) {
  477.         sym[to++] = c2upper(line[from++]);
  478.     }
  479.     while (to < SYMLEN) {
  480.         sym[to++] = '\000';
  481.     }
  482.  
  483.     define( sym, val );
  484. }
  485.  
  486.  
  487. condtrue()
  488. /* called when a true conditional has been evaluated */
  489. /* lex should be the opening <; skip it and setup for normal assembly */
  490. {
  491.     if (line[lexstart] == '<') {
  492.         nextlex(); /* skip the opening bracket */
  493.     } else {
  494.         error( "< expected" );
  495.     }
  496. }
  497.  
  498. condfalse()
  499. /* called when a false conditional has been evaluated */
  500. /* lex should be the opening <; ignore all text until the closing > */
  501. {
  502.     if (line[lexstart] == '<') {
  503.         int level = 1;
  504.         /* invariant: line[pos] is the next unexamined character */
  505.         while (level > 0) {
  506.             if (isend(line[pos])) { /* need to get a new line */
  507.                 readline();
  508.             } else if (line[pos] == '>') {
  509.                 level --;
  510.                 pos++;
  511.             } else if (line[pos] == '<') {
  512.                 level ++;
  513.                 pos++;
  514.             } else if (line[pos] == '$') {
  515.                 level = 0;
  516.                 pos++;
  517.             } else {
  518.                 pos++;
  519.             }
  520.         }
  521.         nextlex();
  522.     } else {
  523.         error( "< expected" );
  524.     }
  525. }
  526.  
  527. int lc; /* the location counter */
  528. int reloc; /* the relocation distance (see RELOC) */
  529. int pzlc; /* the page zero location counter for page zero constants */
  530. int cplc; /* the current page location counter for current page constants */
  531. int radix; /* the default number radix */
  532.  
  533. int pz[0200]; /* storehouse for page zero constants */
  534. int cp[0200]; /* storehouse for current page constants */
  535.  
  536. putpz()
  537. /* put out page zero data */
  538. {
  539.     int loc;
  540.     if (pzlc < 00177) {
  541.         if (rimflag != 1) { /* put out origin if not in rim mode */
  542.             if (obj != NULL) {
  543.                 putorg( pzlc+1 );
  544.             }
  545.         }
  546.         for (loc = pzlc+1; loc <= 00177; loc ++) {
  547.             putout( loc, pz[loc] );
  548.         }
  549.     }
  550.     pzlc = 00177;
  551. }
  552.  
  553. putcp()
  554. /* put out current page data */
  555. {
  556.     int loc;
  557.     if (cplc < 00177) {
  558.         if (lc > (cplc + (lc & 07600))) { /* overrun constant pool */
  559.             error( "overrun" );
  560.         }
  561.         if (rimflag != 1) { /* put out origin if not in rim mode */
  562.             if (obj != NULL) {
  563.                 putorg( cplc+1 + (lc & 07600) );
  564.             }
  565.         }
  566.         for (loc = cplc+1; loc <= 00177; loc ++) {
  567.             putout( loc + (lc & 07600), cp[loc] );
  568.         }
  569.     }
  570.     cplc = 00177;
  571. }
  572.  
  573. int getexprs(); /* forward declaration */
  574.  
  575. int evalsym()
  576. /* get the value of the current identifier lexeme; don't advance lexeme */
  577. {
  578.     char sym[SYMLEN];
  579.     int from = lexstart;
  580.     int to = 0;
  581.  
  582.     /* assert isalpha(line[lexstart]) */
  583.  
  584.     /* copy the symbol */
  585.     while ((from < lexterm) && (to < SYMLEN)) {
  586.         sym[to++] = c2upper(line[from++]);
  587.     }
  588.     while (to < SYMLEN) {
  589.         sym[to++] = '\000';
  590.     }
  591.  
  592.     return (lookup( sym ));
  593. }
  594.  
  595. int delimiter; /* the character immediately after this eval'd term */
  596. nextlexblank()
  597. /* used only within eval, getexpr, this prevents illegal blanks */
  598. {
  599.     nextlex();
  600.     if (isblank(delimiter)) {
  601.         error("illegal blank");
  602.     }
  603.     delimiter = line[lexterm];
  604. }
  605.  
  606. int eval()
  607. /* get the value of the current lexeme, set delimiter and advance */
  608. {
  609.     int val;
  610.  
  611.     delimiter = line[lexterm];
  612.     if (isalpha(line[lexstart])) {
  613.         val = evalsym();
  614.  
  615.         if (val == -1) {
  616.             error( "undefined" );
  617.             nextlex();
  618.             return 0;
  619.         } else {
  620.             nextlex();
  621.             return val;
  622.         }
  623.  
  624.     } else if (isdigit(line[lexstart])) {
  625.         int from = lexstart;
  626.  
  627.         val = 0;
  628.         while (from < lexterm) {
  629.             int digit = (int)line[from++] - (int)'0';
  630.             if (digit < radix) {
  631.                 val = (val * radix) + digit;
  632.             } else {
  633.                 error("d > radix");
  634.             }
  635.         }
  636.         nextlex();
  637.         return val;
  638.  
  639.     } else if (line[lexstart] == '"') {
  640.         val = line[lexstart+1] | 0200;
  641.         delimiter = line[lexstart+2];
  642.         pos = lexstart+2;
  643.         nextlex();
  644.         return val;
  645.  
  646.     } else if (line[lexstart] == '.') {
  647.         val = (lc + reloc) & 07777;
  648.         nextlex();
  649.         return val;
  650.  
  651.     } else if (line[lexstart] == '[') {
  652.         int loc;
  653.  
  654.         nextlexblank(); /* skip bracket */
  655.         val = getexprs() & 07777;
  656.         if (line[lexstart] == ']') {
  657.             nextlexblank(); /* skip end bracket */
  658.         } else {
  659.             /* error("parens") */;
  660.         }
  661.  
  662.         loc = 00177;
  663.         while ((loc > pzlc) && (pz[loc] != val)) {
  664.             loc--;
  665.         }
  666.         if (loc == pzlc) {
  667.             pz[pzlc] = val;
  668.             pzlc--;
  669.         }
  670.         return loc;
  671.  
  672.     } else if (line[lexstart] == '(') {
  673.         int loc;
  674.  
  675.         if ((lc & 07600) == 0) {
  676.             error("page zero");
  677.         }
  678.         nextlexblank(); /* skip paren */
  679.         val = getexprs() & 07777;
  680.         if (line[lexstart] == ')') {
  681.             nextlexblank(); /* skip end paren */
  682.         } else { /*
  683.             error("parens") */ ;
  684.         }
  685.  
  686.         loc = 00177;
  687.         while ((loc > cplc) && (cp[loc] != val)) {
  688.             loc--;
  689.         }
  690.         if (loc == cplc) {
  691.             cp[cplc] = val;
  692.             cplc--;
  693.         }
  694.         return loc + ((lc + reloc) & 07600);
  695.  
  696.     }
  697.     error("value");
  698. }
  699.  
  700. int getexpr()
  701. /* get an expression, from the current lexeme onward, leave the current
  702.    lexeme as the one after the expression!
  703.  
  704.    Expressions contain terminal symbols (identifiers) separated by operators
  705. */
  706. {
  707.     int value;
  708.  
  709.     delimiter = line[lexterm];
  710.     if (line[lexstart] == '-') {
  711.         nextlexblank();
  712.         value = -eval();
  713.     } else {
  714.         value = eval();
  715.     }
  716.  
  717. more:    /* here, we assume the current lexeme is the operator
  718.            separating the previous operand from the next, if any */
  719.  
  720.     if (isblank(delimiter)) {
  721.         return value;
  722.     }
  723.  
  724.     /* assert line[lexstart] == delimiter */
  725.  
  726.     if (line[lexstart] == '+') { /* add */
  727.  
  728.         nextlexblank(); /* skip over the operator */
  729.         value = value + eval();
  730.         goto more;
  731.  
  732.     }
  733.     if (line[lexstart] == '-') { /* subtract */
  734.  
  735.         nextlexblank(); /* skip over the operator */
  736.         value = value - eval();
  737.         goto more;
  738.  
  739.     }
  740.     if (line[lexstart] == '^') { /* multiply */
  741.  
  742.         nextlexblank(); /* skip over the operator */
  743.         value = value * eval();
  744.         goto more;
  745.  
  746.     }
  747.     if (line[lexstart] == '%') { /* divide */
  748.  
  749.         nextlexblank(); /* skip over the operator */
  750.         value = value / eval();
  751.         goto more;
  752.  
  753.     }
  754.     if (line[lexstart] == '&') { /* and */
  755.  
  756.         nextlexblank(); /* skip over the operator */
  757.         value = value & eval();
  758.         goto more;
  759.  
  760.     }
  761.     if (line[lexstart] == '!') { /* or */
  762.  
  763.         nextlexblank(); /* skip over the operator */
  764.         value = value | eval();
  765.         /* OPTIONAL PATCH 2 -- change to (value << 6) ! eval() */
  766.         goto more;
  767.  
  768.     }
  769.  
  770.     if (isend(line[lexstart])) { 
  771.         return value;
  772.     }
  773.     if (line[lexstart] == '/') { 
  774.         return value;
  775.     }
  776.     if (line[lexstart] == ';') { 
  777.         return value;
  778.     }
  779.     if (line[lexstart] == ')') { 
  780.         return value;
  781.     }
  782.     if (line[lexstart] == ']') { 
  783.         return value;
  784.     }
  785.     if (line[lexstart] == '<') { 
  786.         return value;
  787.     }
  788.  
  789.     error("expression");
  790.     return 0;
  791. }
  792.  
  793. int getexprs()
  794. /* or together a list of blank-separated expressions, from the current
  795.    lexeme onward, leave the current lexeme as the one after the last in
  796.    the list!
  797. */
  798. {
  799.     int value;
  800.     value = getexpr();
  801.  
  802. more:    /* here, we check if we are done */
  803.  
  804.     if (isdone(line[lexstart])) { /* no operand */
  805.         return value;
  806.     }
  807.     if (line[lexstart] == ')') { /* end of list */
  808.         return value;
  809.     }
  810.     if (line[lexstart] == ']') { /* end of list */
  811.         return value;
  812.     }
  813.  
  814.     { /* interpret space as logical or */
  815.         int temp;
  816.  
  817.         temp = getexpr();
  818.  
  819.         if (value <= 07777) { /* normal 12 bit value */
  820.             value = value | temp;
  821.         } else if (temp > 07777) { /* or together MRI opcodes */
  822.             value = value | temp;
  823.         } else if (temp < 0200) { /* page zero MRI */
  824.             value = value | temp;
  825.         } else if (   (((lc + reloc) & 07600) <= temp)
  826.                && (temp <= ((lc + reloc) | 00177))
  827.               ) {
  828.             /* current page MRI */
  829.             value = value | 00200 | (temp & 00177);
  830.         } else {
  831.             /* off page MRI */
  832.             int loc;
  833.             error("off page");
  834.  
  835.             /* having complained, fix it up */
  836.             loc = 00177;
  837.             while ((loc > cplc) && (cp[loc] != temp)) {
  838.                 loc--;
  839.             }
  840.             if (loc == cplc) {
  841.                 cp[cplc] = temp;
  842.                 cplc--;
  843.             }
  844.             value = value | 00600 | loc;
  845.         }
  846.         goto more;
  847.     }
  848. }
  849.  
  850. onepass()
  851. /* do one assembly pass */
  852. {
  853.     lc = 0;
  854.     reloc = 0;
  855.     field = 0;
  856.     cplc = 00177; /* points to end of page for [] operands */
  857.     pzlc = 00177; /* points to end of page for () operands */
  858.     radix = 8;
  859.     listed = 1;
  860.     lineno = 0;
  861.  
  862. getline:
  863.     readline();
  864.     nextlex();
  865.  
  866. restart:
  867.     if (line[lexstart] == '/') {
  868.         goto getline;
  869.     }
  870.     if (isend(line[lexstart])) {
  871.         goto getline;
  872.     }
  873.     if (line[lexstart] == ';') {
  874.         nextlex();
  875.         goto restart;
  876.     }
  877.     if (line[lexstart] == '$') {
  878.         putcp();
  879.         putpz(); /* points to end of page for () operands */
  880.         listline(); /* if it hasn't been listed yet, list it! */
  881.         return;
  882.     }
  883.     if (line[lexstart] == '*') {
  884.         int newlc;
  885.         nextlex(); /* skip * (set origin symbol) */
  886.         newlc = getexpr() & 07777;
  887.         if ((newlc & 07600) != (lc & 7600)) { /* we changed pages */
  888.             putcp();
  889.         }
  890.         lc = newlc - reloc;
  891.         if (rimflag != 1) { /* put out origin if not in rim mode */
  892.             if (obj != NULL) {
  893.                 putorg( lc );
  894.             }
  895.         }
  896.         goto restart;
  897.     }
  898.     if (line[lexterm] == ',') {
  899.         if (isalpha(line[lexstart])) {
  900.             deflex( lexstart, lexterm, (lc + reloc) & 07777 );
  901.         } else {
  902.             error("label");
  903.         }
  904.         nextlex(); /* skip label */
  905.         nextlex(); /* skip comma */
  906.         goto restart;
  907.     }
  908.     if (line[lexterm] == '=') {
  909.         if (isalpha(line[lexstart])) {
  910.             int start = lexstart;
  911.             int term = lexterm;
  912.             nextlex(); /* skip symbol */
  913.             nextlex(); /* skip trailing = */
  914.             deflex( start, term, getexprs() );
  915.         } else {
  916.             error("symbol");
  917.             nextlex(); /* skip symbol */
  918.             nextlex(); /* skip trailing = */
  919.             (void) getexprs(); /* skip expression */
  920.         }
  921.         goto restart;
  922.     }
  923.     if (isalpha(line[lexstart])) {
  924.         int val;
  925.         val = evalsym();
  926.         if (val > 037777) { /* pseudo op */
  927.             nextlex(); /* skip symbol */
  928.             val = val & 07777;
  929.             switch (val) {
  930.             case 0: /* DECIMAL */
  931.                 radix = 10;
  932.                 break;
  933.             case 1: /* OCTAL */
  934.                 radix = 8;
  935.                 break;
  936.             case 2: /* ZBLOCK */
  937.                 val = getexpr();
  938.                 if (val < 0) {
  939.                     error("too small");
  940.                 } else if (val+lc-1 > 07777) {
  941.                     error("too big");
  942.                 } else {
  943.                     for ( ;val > 0; val--) {
  944.                         putout( lc, 0 );
  945.                         lc++;
  946.                     }
  947.                 }
  948.                 break;
  949.             case 3: /* PAGE */
  950.                 putcp();
  951.                 if (isdone(line[lexstart])) { /* no arg */
  952.                     lc = (lc & 07600) + 00200;
  953.                                 } else {
  954.                                         val = getexpr();
  955.                     lc = (val & 037) << 7;
  956.                                 }
  957.                 if (rimflag != 1) {
  958.                     if (obj != NULL) {
  959.                         putorg( lc );
  960.                     }
  961.                 }
  962.                 break;
  963.             case 4: /* TEXT */
  964.                 {
  965.                     char delim = line[lexstart];
  966.                     int pack = 0;
  967.                     int count = 0;
  968.                     int index = lexstart + 1;
  969.                     while ((line[index] != delim) &&
  970.                            !isend(line[index])       ) {
  971.                         pack = (pack << 6)
  972.                              | (line[index] & 077);
  973.                         count++;
  974.                         if (count > 1) {
  975.                             putout( lc, pack );
  976.                             lc++;
  977.                             count = 0;
  978.                             pack = 0;
  979.                         }
  980.                         index++;
  981.                     }
  982.                     if (count != 0) {
  983.                         putout( lc, pack << 6 );
  984.                         lc++;
  985.                     } else { /* OPTIONAL PATCH 4 */
  986.                         putout( lc, 0 );
  987.                         lc++;
  988.                     }
  989.                     if (isend(line[index])) {
  990.                         lexterm = index;
  991.                         pos = index;
  992.                         error("parens");
  993.                         nextlex();
  994.                     } else {
  995.                         lexterm = index + 1;
  996.                         pos = index + 1;
  997.                         nextlex();
  998.                     }
  999.                 }
  1000.                 break;
  1001.             case 5: /* EJECT */
  1002.                 if (lst != NULL) {
  1003.                     /* this will do for now */
  1004.                     fprintf( lst, "\n . . . \n\n" );
  1005.                     goto getline;
  1006.                 }
  1007.                 break;
  1008.             case 6: /* FIELD */
  1009.                 putcp();
  1010.                 putpz();
  1011.                 if (isdone(line[lexstart])) {
  1012.                     /* blank FIELD directive */
  1013.                     val = field + 1;
  1014.                 } else {
  1015.                     /* FIELD with an argument */
  1016.                     val = getexpr();
  1017.                 }
  1018.                 if (rimflag == 1) { /* can't change fields */
  1019.                     error("rim mode");
  1020.                 } else if ((val > 7) || (val < 0)) {
  1021.                     error("field");
  1022.                 } else if (obj != NULL) { /* change field */
  1023.                     puto( ((val & 0007)<<3) | 0300 );
  1024.                     field = val;
  1025.                 }
  1026.                 lc = 0;
  1027.                 /* OPTIONAL PATCH 4 -- delete next line */
  1028.                 lc = 0200;
  1029.                 if (rimflag != 1) {
  1030.                     if (obj != NULL) {
  1031.                         putorg( lc );
  1032.                     }
  1033.                 }
  1034.                 break;
  1035.             case 7: /* NOPUNCH */
  1036.                 obj = NULL;
  1037.                 break;
  1038.             case 010: /* ENPUNCH */
  1039.                 obj = objsave;
  1040.                 break;
  1041.             case 011: /* XLIST */
  1042.                 if (isdone(line[lexstart])) {
  1043.                     /* blank XLIST directive */
  1044.                     FILE *temp = lst;
  1045.                     lst = lstsave;
  1046.                     lstsave = temp;
  1047.                 } else {
  1048.                     /* XLIST with an argument */
  1049.                     if (getexpr() == 0) { /* ON */
  1050.                         if (lst == NULL) {
  1051.                             lst = lstsave;
  1052.                             lstsave = NULL;
  1053.                         }
  1054.                     } else { /* OFF */
  1055.                         if (lst != NULL) {
  1056.                             lstsave = lst;
  1057.                             lst = NULL;
  1058.                         }
  1059.                     }
  1060.                 }
  1061.                 break;
  1062.             case 012: /* IFZERO */
  1063.                 if (getexpr() == 0) {
  1064.                     condtrue();
  1065.                 } else {
  1066.                     condfalse();
  1067.                 }
  1068.                 break;
  1069.             case 013: /* IFNZRO */
  1070.                 if (getexpr() == 0) {
  1071.                     condfalse();
  1072.                 } else {
  1073.                     condtrue();
  1074.                 }
  1075.                 break;
  1076.             case 014: /* IFDEF */
  1077.                 if (isalpha(line[lexstart])) {
  1078.                     int val = evalsym();
  1079.                     nextlex();
  1080.                     if (val >= 0) {
  1081.                         condtrue();
  1082.                     } else {
  1083.                         condfalse();
  1084.                     }
  1085.                 } else {
  1086.                     error( "identifier" );
  1087.                 }
  1088.                 break;
  1089.             case 015: /* IFNDEF */
  1090.                 if (isalpha(line[lexstart])) {
  1091.                     int val = evalsym();
  1092.                     nextlex();
  1093.                     if (val >= 0) {
  1094.                         condfalse();
  1095.                     } else {
  1096.                         condtrue();
  1097.                     }
  1098.                 } else {
  1099.                     error( "identifier" );
  1100.                 }
  1101.                 break;
  1102.             case 016: /* RELOC */
  1103.                 if (isdone(line[lexstart])) {
  1104.                     /* blank RELOC directive */
  1105.                     reloc = 0;
  1106.                 } else {
  1107.                     /* RELOC with an argument */
  1108.                     val = getexpr();
  1109.                     reloc = val - (lc + reloc);
  1110.                 }
  1111.                 break;
  1112.             case 017: /* SEGMNT */
  1113.                 putcp();
  1114.                 if (isdone(line[lexstart])) { /* no arg */
  1115.                     lc = (lc & 06000) + 02000;
  1116.                                 } else {
  1117.                                         val = getexpr();
  1118.                     lc = (val & 003) << 10;
  1119.                                 }
  1120.                 if (rimflag != 1) {
  1121.                     if (obj != NULL) {
  1122.                         putorg( lc );
  1123.                     }
  1124.                 }
  1125.                 break;
  1126.             case 020: /* BANK   */
  1127.                 error("unsupported");
  1128.                 /* should select a different 32K out of 128K */
  1129.                 break;
  1130.             case 021: /* FIXMRI */
  1131.                 if ((line[lexterm] == '=')
  1132.                 && (isalpha(line[lexstart]))) {
  1133.                     int start = lexstart;
  1134.                     int term = lexterm;
  1135.                     nextlex(); /* skip symbol */
  1136.                     nextlex(); /* skip trailing = */
  1137.                     deflex( start, term, getexprs() );
  1138.                 } else {
  1139.                     error("symbol");
  1140.                     nextlex(); /* skip symbol */
  1141.                     nextlex(); /* skip trailing = */
  1142.                     (void) getexprs(); /* skip expr */
  1143.                 }
  1144.                 break;
  1145.             } /* end switch */
  1146.             goto restart;
  1147.         } /* else */
  1148.             /* identifier is not pseudo op */
  1149.         /* } end if */
  1150.         /* fall through here if ident is not pseudo-op */
  1151.     }
  1152.     { /* default -- interpret line as load value */
  1153.         putout( lc, getexprs() & 07777); /* interpret line load value */
  1154.         lc++;
  1155.         goto restart;
  1156.     }
  1157. }
  1158.  
  1159.  
  1160. /* main program */
  1161. main(argc, argv)
  1162. int argc;
  1163. char *argv[];
  1164. {
  1165.     getargs(argc, argv);
  1166.  
  1167.     onepass();
  1168.  
  1169.     rewind(in);
  1170.     obj = fopen(objname, "w");
  1171.     objsave = obj;
  1172.     lst = fopen(lstname, "w");
  1173.     lstsave = NULL;
  1174.     putleader();
  1175.     errors = 0;
  1176.     cksum = 0;
  1177.  
  1178.     onepass();
  1179.  
  1180.     if (lst == NULL) { /* undo effects of XLIST for any following dump */
  1181.         lst = lstsave;
  1182.     }
  1183.     if (dumpflag != 0) dump();
  1184.     obj = objsave; /* undo effects of NOPUNCH for any following checksum */
  1185.     if ((rimflag == 0) && (obj != NULL)) { /* checksum */
  1186.         /* for now, put out the wrong value */
  1187.         fputc( (cksum >> 6) & 0077, obj );
  1188.         fputc( cksum & 0077, obj );
  1189.     }
  1190.     putleader();
  1191.     exit (errors != 0);
  1192. }
  1193.