home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
276_01
/
a684util.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-03
|
13KB
|
478 lines
/*
HEADER: CUG276;
TITLE: 6804 Cross-Assembler (Portable);
FILENAME: A684UTIL.C;
VERSION: 0.1;
DATE: 08/27/1988;
DESCRIPTION: "This program lets you use your computer to assemble
code for the Motorola 6804 family microprocessors.
The program is written in portable C rather than BDS
C. All assembler features are supported except
relocation linkage, and macros.";
KEYWORDS: Software Development, Assemblers, Cross-Assemblers,
Motorola, MC6804;
SYSTEM: CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
COMPILERS: Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
Lattice C, Microsoft C, QNIX C;
WARNINGS: "This program should compile on any full-featured C
compiler. Subset compilers such as Toolworks C and
BDS C will present substantial difficulties."
AUTHORS: William C. Colley III;
*/
/*
6804 Cross-Assembler in Portable C
Copyright (c) 1985, 1988 William C. Colley, III
Revision History:
Ver Date Description
0.0 MAR 1988 Adapted from version 0.2 of the portable 6805 cross-
assembler which was adapted from version 3.2 of the
portable 6801 cross-assembler. WCC3.
0.1 AUG 1988 Fixed a bug in the command line parser that puts it
into a VERY long loop if the user types a command line
like "A684 FILE.ASM -L". WCC3 per Alex Cameron.
This module contains the following utility packages:
1) symbol table building and searching
2) opcode and operator table searching
3) listing file output
4) hex file output
5) error flagging
*/
/* Get global goodies: */
#include "a684.h"
/* Make sure that MSDOS compilers using the large memory model know */
/* that calloc() returns pointer to char as an MSDOS far pointer is */
/* NOT compatible with the int type as is usually the case. */
char *calloc();
/* Get access to global mailboxes defined in A684.C: */
extern char errcode, line[], title[];
extern int eject, listhex;
extern unsigned address, bytes, errors, listleft, obj[], pagelen;
/* The symbol table is a binary tree of variable-length blocks drawn */
/* from the heap with the calloc() function. The root pointer lives */
/* here: */
static SYMBOL *sroot = NULL;
/* Add new symbol to symbol table. Returns pointer to symbol even if */
/* the symbol already exists. If there's not enough memory to store */
/* the new symbol, a fatal error occurs. */
SYMBOL *new_symbol(nam)
char *nam;
{
SCRATCH int i;
SCRATCH SYMBOL **p, *q;
void fatal_error();
for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
p = i < 0 ? &(q -> left) : &(q -> right);
if (!q) {
if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
fatal_error(SYMBOLS);
strcpy(q -> sname,nam);
}
return q;
}
/* Look up symbol in symbol table. Returns pointer to symbol or NULL */
/* if symbol not found. */
SYMBOL *find_symbol(nam)
char *nam;
{
SCRATCH int i;
SCRATCH SYMBOL *p;
for (p = sroot; p && (i = strcmp(nam,p -> sname));
p = i < 0 ? p -> left : p -> right);
return p;
}
/* Opcode table search routine. This routine pats down the opcode */
/* table for a given opcode and returns either a pointer to it or */
/* NULL if the opcode doesn't exist. */
OPCODE *find_code(nam)
char *nam;
{
OPCODE *bsearch();
static OPCODE opctbl[] = {
{ IMMEDOK + ARITHOP, 0xe2, "ADD" },
{ IMMEDOK + ARITHOP, 0xe5, "AND" },
{ AREG + IMPLIED, 0xfa, "ASLA" },
{ FLAGJMP, 0x40, "BCC" },
{ BITOP, 0xd0, "BCLR" },
{ FLAGJMP, 0x60, "BCS" },
{ FLAGJMP, 0x20, "BEQ" },
{ FLAGJMP, 0x40, "BHS" },
{ FLAGJMP, 0x60, "BLO" },
{ FLAGJMP, 0x00, "BNE" },
{ BITJMP, 0xc0, "BRCLR" },
{ BITJMP, 0xc8, "BRSET" },
{ BITOP, 0xd8, "BSET" },
{ AREG + IMPLIED, 0xfb, "CLRA" },
{ ZERO + XREG + IMPLIED, 0xb0, "CLRX" },
{ ZERO + YREG + IMPLIED, 0xb0, "CLRY" },
{ IMMEDOK + ARITHOP, 0xe4, "CMP" },
{ IMPLIED, 0xb4, "COMA" },
{ SHORTOK + ARITHOP, 0xe7, "DEC" },
{ AREG + IMPLIED, 0xff, "DECA" },
{ IMPLIED, 0xe7, "DECX" },
{ IMPLIED, 0xf7, "DECY" },
{ PSEUDO + ISIF, ELSE, "ELSE" },
{ PSEUDO, END, "END" },
{ PSEUDO + ISIF, ENDIF, "ENDIF" },
{ PSEUDO, EQU, "EQU" },
{ PSEUDO, FCB, "FCB" },
{ PSEUDO, FCC, "FCC" },
{ PSEUDO, FDB, "FDB" },
{ PSEUDO + ISIF, IF, "IF" },
{ SHORTOK + ARITHOP, 0xe6, "INC" },
{ AREG + IMPLIED, 0xfe, "INCA" },
{ PSEUDO, INCL, "INCL" },
{ IMPLIED, 0xe6, "INCX" },
{ IMPLIED, 0xf6, "INCY" },
{ LONGJMP, 0x90, "JMP" },
{ LONGJMP, 0x80, "JSR" },
{ SHORTOK + IMMEDOK + ARITHOP, 0xe0, "LDA" },
{ XREG + IMMED, 0xb0, "LDXI" },
{ YREG + IMMED, 0xb0, "LDYI" },
{ DIR_IMMED, 0xb0, "MVI" },
{ IMPLIED, 0x20, "NOP" },
{ PSEUDO, ORG, "ORG" },
{ PSEUDO, PAGE, "PAGE" },
{ PSEUDO, RMB, "RMB" },
{ IMPLIED, 0xb5, "ROLA" },
{ IMPLIED, 0xb2, "RTI" },
{ IMPLIED, 0xb3, "RTS" },
{ PSEUDO, SET, "SET" },
{ SHORTOK + ARITHOP, 0xe1, "STA" },
{ IMPLIED, 0xb6, "STOP" },
{ IMMEDOK + ARITHOP, 0xe3, "SUB" },
{ IMPLIED, 0xbc, "TAX" },
{ IMPLIED, 0xbd, "TAY" },
{ PSEUDO, TITLE, "TITLE" },
{ IMPLIED, 0xac, "TXA" },
{ IMPLIED, 0xad, "TYA" },
{ IMPLIED, 0xb7, "WAIT" }
};
return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
}
/* Operator table search routine. This routine pats down the */
/* operator table for a given operator and returns either a pointer */
/* to it or NULL if the opcode doesn't exist. */
OPCODE *find_operator(nam)
char *nam;
{
OPCODE *bsearch();
static OPCODE oprtbl[] = {
{ BINARY + LOG1 + OPR, AND, "AND" },
{ BINARY + RELAT + OPR, '=', "EQ" },
{ BINARY + RELAT + OPR, GE, "GE" },
{ BINARY + RELAT + OPR, '>', "GT" },
{ UNARY + UOP3 + OPR, HIGH, "HIGH" },
{ BINARY + RELAT + OPR, LE, "LE" },
{ UNARY + UOP3 + OPR, LOW, "LOW" },
{ BINARY + RELAT + OPR, '<', "LT" },
{ BINARY + MULT + OPR, MOD, "MOD" },
{ BINARY + RELAT + OPR, NE, "NE" },
{ UNARY + UOP2 + OPR, NOT, "NOT" },
{ BINARY + LOG2 + OPR, OR, "OR" },
{ BINARY + MULT + OPR, SHL, "SHL" },
{ BINARY + MULT + OPR, SHR, "SHR" },
{ REG, 'X', "X" },
{ BINARY + LOG2 + OPR, XOR, "XOR" },
{ REG, 'Y', "Y" }
};
return bsearch(oprtbl,oprtbl + (sizeof(oprtbl) / sizeof(OPCODE)),nam);
}
static OPCODE *bsearch(lo,hi,nam)
OPCODE *lo, *hi;
char *nam;
{
SCRATCH int i;
SCRATCH OPCODE *chk;
for (;;) {
chk = lo + (hi - lo) / 2;
if (!(i = ustrcmp(chk -> oname,nam))) return chk;
if (chk == lo) return NULL;
if (i < 0) lo = chk;
else hi = chk;
}
}
static int ustrcmp(s,t)
char *s, *t;
{
SCRATCH int i;
while (!(i = toupper(*s++) - toupper(*t)) && *t++);
return i;
}
/* Buffer storage for line listing routine. This allows the listing */
/* output routines to do all operations without the main routine */
/* having to fool with it. */
static FILE *list = NULL;
/* Listing file open routine. If a listing file is already open, a */
/* warning occurs. If the listing file doesn't open correctly, a */
/* fatal error occurs. If no listing file is open, all calls to */
/* lputs() and lclose() have no effect. */
void lopen(nam)
char *nam;
{
FILE *fopen();
void fatal_error(), warning();
if (list) warning(TWOLST);
else if (!(list = fopen(nam,"w"))) fatal_error(LSTOPEN);
return;
}
/* Listing file line output routine. This routine processes the */
/* source line saved by popc() and the output of the line assembler in */
/* buffer obj into a line of the listing. If the disk fills up, a */
/* fatal error occurs. */
void lputs()
{
SCRATCH int i, j;
SCRATCH unsigned *o;
void check_page(), fatal_error();
if (list) {
i = bytes; o = obj;
do {
fprintf(list,"%c ",errcode);
if (listh