home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
276_01
/
a16.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-03
|
10KB
|
407 lines
/*
HEADER: CUG276;
TITLE: PIC1650 Cross-Assembler (Portable);
FILENAME: A16.C;
VERSION: 0.0;
DATE: 06/15/1989;
SEE-ALSO: A16.H;
AUTHORS: William C. Colley III;
*/
/*
PIC1650 Cross-Assembler in Portable C
Copyright (c) 1985,1987,1989 William C. Colley, III
Revision History:
Ver Date Description
0.0 JUNE 1989 Derived from version 0.2 of my portable 8085 cross-
assembler. WCC3.
This file contains the main program and line assembly routines for the
assembler. The main program parses the command line, feeds the source lines
to the line assembly routine, and sends the results to the listing and object
file output routines. It also coordinates the activities of everything. The
line assembly routines uses the expression analyzer and the lexical analyzer
to parse the source line and convert it into the object bytes that it
represents.
*/
/* Get global goodies: */
#include "a16.h"
/* Define global mailboxes for all modules: */
char errcode, line[MAXLINE + 1], title[MAXLINE];
int pass = 0;
int eject, filesp, forwd, listhex;
unsigned address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
FILE *filestk[FILES], *source;
TOKEN token;
/* Mainline routine. This routine parses the command line, sets up */
/* the assembler at the beginning of each pass, feeds the source text */
/* to the line assembler, feeds the result to the listing and hex file */
/* drivers, and cleans everything up at the end of the run. */
static int done, ifsp, off;
void main(argc,argv)
int argc;
char **argv;
{
SCRATCH unsigned *o;
int newline();
void asm_line();
void lclose(), lopen(), lputs();
void hclose(), hopen(), hputc();
void error(), fatal_error(), warning();
printf("PIC1650 Cross-Assembler (Portable) Ver 0.0\n");
printf("Copyright (c) 1985,1987,1989 William C. Colley, III\n\n");
while (--argc > 0) {
if (**++argv == '-') {
switch (toupper(*++*argv)) {
case 'L': if (!*++*argv) {
if (!--argc) { warning(NOLST); break; }
else ++argv;
}
lopen(*argv);
break;
case 'O': if (!*++*argv) {
if (!--argc) { warning(NOHEX); break; }
else ++argv;
}
hopen(*argv);
break;
default: warning(BADOPT);
}
}
else if (filestk[0]) warning(TWOASM);
else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
}
if (!filestk[0]) fatal_error(NOASM);
while (++pass < 3) {
fseek(source = filestk[0],0L,0); done = off = FALSE;
errors = filesp = ifsp = pagelen = pc = 0; title[0] = '\0';
while (!done) {
errcode = ' ';
if (newline()) {
error('*');
strcpy(line,"\tEND\n");
done = eject = TRUE; listhex = FALSE;
bytes = 0;
}
else asm_line();
pc = word(pc + bytes);
if (pass == 2) {
lputs();
for (o = obj; bytes--; hputc(*o++));
}
}
}
fclose(filestk[0]); lclose(); hclose();
if (errors) printf("%d Error(s)\n",errors);
else printf("No Errors\n");
exit(errors);
}
/* Line assembly routine. This routine gets expressions and tokens */
/* from the source file using the expression evaluator and lexical */
/* analyzer, respectively. It fills a buffer with the machine code */
/* bytes and returns nothing. */
static char label[MAXLINE];
static int ifstack[IFDEPTH] = { ON };
static OPCODE *opcod;
void asm_line()
{
SCRATCH char *p;
SCRATCH int i;
int isalph(), popc();
OPCODE *find_code(), *find_operator();
void do_label(), flush(), normal_op(), pseudo_op();
void error(), pops(), pushc(), trash();
address = pc; bytes = 0; eject = forwd = listhex = FALSE;
for (i = 0; i < BIGINST; obj[i++] = NOP);
label[0] = '\0';
if ((i = popc()) != ' ' && i != '\n') {
if (isalph(i)) {
pushc(i); pops(label);
for (p = label; *p; ++p);
if (*--p == ':') *p = '\0';
if (find_operator(label)) { label[0] = '\0'; error('L'); }
}
else {
error('L');
while ((i = popc()) != ' ' && i != '\n');
}
}
trash(); opcod = NULL;
if ((i = popc()) != '\n') {
if (!isalph(i)) error('S');
else {
pushc(i); pops(token.sval);
if (!(opcod = find_code(token.sval))) error('O');
}
if (!opcod) { listhex = TRUE; bytes = BIGINST; }
}
if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
else if (off) { listhex = FALSE; flush(); return; }
if (!opcod) { do_label(); flush(); }
else {
listhex = TRUE;
if (opcod -> attr & PSEUDO) pseudo_op();
else normal_op();
while ((i = popc()) != '\n') if (i != ' ') error('T');
}
source = filestk[filesp];
return;
}
static void flush()
{
while (popc() != '\n');
}
static void do_label()
{
SCRATCH SYMBOL *l;
SYMBOL *find_symbol(), *new_symbol();
void error();
if (label[0]) {
listhex = TRUE;
if (pass == 1) {
if (!((l = new_symbol(label)) -> attr)) {
l -> attr = FORWD + VAL;
l -> valu = pc;
}
}
else {
if (l = find_symbol(label)) {
l -> attr = VAL;
if (l -> valu != pc) error('M');
}
else error('P');
}
}
}
static void normal_op()
{
SCRATCH unsigned a, *o, u;
unsigned expr();
TOKEN *lex();
void do_label(), error(), unlex();
do_label(); bytes = (a = opcod -> attr) & BYTES;
if (pass < 2) return;
*(o = obj) = opcod -> valu;
if (bytes == 2) *++o = 0xa00;
if (a & ARG1) {
u = expr();
switch (a & ARG1) {
case ADDR_9: if (u > 0x1ff) { u = 0; error('A'); }
break;
case ADDR_8: if (u > 0x0ff) { u = 0; error('A'); }
break;
case IMMED_8: if (u > 0xff && u < 0xff80) {
u = 0; error('V');
}
else u &= 0xff;
break;
case REG_5: if (u > 0x1f) { u = 0; error('R'); }
break;
case TRIS_REG: if (u > 7 || u < 5) { u = 5; error('R'); }
break;
}
*o += u;
}
if (a & ARG2) {
if ((lex() -> attr & TYPE) != SEP) { unlex(); error('S'); }
else {
u = expr();
switch (a & ARG2) {
case BIT_3: if (u > 7) { u = 0; error('B'); }
break;
case DIR: if (u > 1) { u = 0; error('W'); }
}
*o += u << 5;
}
}
return;
}
static void pseudo_op()
{
SCRATCH unsigned *o, u;
SCRATCH SYMBOL *l;
int popc();
unsigned expr();
SYMBOL *find_symbol(), *new_symbol();
TOKEN *lex();
void do_label(), error(), fatal_error(), hseek();
void pushc(), trash(), unlex();
o = obj;
switch (opcod -> valu) {
case DS: do_label();
u = word(pc + expr());
if (forwd) error('P');
else {
pc = u;
if (pass == 2) hseek(pc);
}
break;
case DW: do_label();
do {
lex(); unlex(); ++bytes;
u = ((token.attr & TYPE) == SEP) ? 0 : expr();
if (u > 0x0fff && u < 0xf800) {
*o++ = 0; error('V');
}
else *o++ = u;
} while ((lex() -> attr & TYPE) == SEP);
break;
case ELSE: listhex = FALSE;
if (ifsp) off = (ifstack[ifsp] = -ifstack[ifsp]) != ON;
else error('I');
break;
case END: do_label();
if (filesp) { listhex = FALSE; error('*'); }
else {
done = eject = TRUE;
if (pass == 2) {
if ((lex() -> attr & TYPE) != EOL) {
unlex(); hseek(address = expr());
}
}
if (ifsp) error('I');
}
break;
case ENDIF: listhex = FALSE;
if (ifsp) off = ifstack[--ifsp] != ON;
else error('I');
break;
case EQU: if (label[0]) {
if (pass == 1) {
if (!((l = new_symbol(label)) -> attr)) {
l -> attr = FORWD + VAL;
address = expr();
if (!forwd) l -> valu = address;
}
}
else {
if (l = find_symbol(label)) {
l -> attr = VAL;
address = expr();
if (forwd) error('P');
if (l -> valu != address) error('M');
}
else error('P');
}
}
else error('L');
break;