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 >
Wrap
Text File
|
1989-01-13
|
15KB
|
529 lines
/*
HEADER: CUG149;
TITLE: 6805 Cross-Assembler (Portable);
FILENAME: A685UTIL.C;
VERSION: 0.3;
DATE: 08/27/1988;
DESCRIPTION: "This program lets you use your computer to assemble
code for the Motorola 6805 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, MC6805;
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 has compiled successfully on 2 UNIX
compilers, 5 MSDOS compilers, and 2 CP/M compilers.
A port to BDS C would be extremely difficult, but see
volume CUG113. A port to Toolworks C is untried."
AUTHORS: William C. Colley III;
*/
/*
6805 Cross-Assembler in Portable C
Copyright (c) 1985 William C. Colley, III
Revision History:
Ver Date Description
0.0 SEP 1985 Adapted from version 3.2 of the portable 6801 cross-
assembler. WCC3.
0.1 JUL 1986 Added compilation instructions and tweaks for CI-C86,
Eco-C88, and Lattice C. WCC3.
0.2 JAN 1987 Fixed bug that made "FCB 0," legal syntax. WCC3.
0.3 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 "A685 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 "a685.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 A68.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[] = {
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb9, "ADC" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xbb, "ADD" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb4, "AND" },
{ DIR + IDX + 2, 0x38, "ASL" },
{ NULL + 1, 0x48, "ASLA" },
{ NULL + 1, 0x58, "ASLX" },
{ DIR + IDX + 2, 0x37, "ASR" },
{ NULL + 1, 0x47, "ASRA" },
{ NULL + 1, 0x57, "ASRX" },
{ REL + 2, 0x24, "BCC" },
{ BIT + DIR + 2, 0x11, "BCLR" },
{ REL + 2, 0x25, "BCS" },
{ REL + 2, 0x27, "BEQ" },
{ REL + 2, 0x28, "BHCC" },
{ REL + 2, 0x29, "BHCS" },
{ REL + 2, 0x22, "BHI" },
{ REL + 2, 0x24, "BHS" },
{ REL + 2, 0x2f, "BIH" },
{ REL + 2, 0x2e, "BIL" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb5, "BIT" },
{ REL + 2, 0x25, "BLO" },
{ REL + 2, 0x23, "BLS" },
{ REL + 2, 0x2c, "BMC" },
{ REL + 2, 0x2b, "BMI" },
{ REL + 2, 0x2d, "BMS" },
{ REL + 2, 0x26, "BNE" },
{ REL + 2, 0x2a, "BPL" },
{ REL + 2, 0x20, "BRA" },
{ BIT + REL + DIR + 3, 0x01, "BRCLR" },
{ REL + 2, 0x21, "BRN" },
{ BIT + REL + DIR + 3, 0x00, "BRSET" },
{ BIT + DIR + 2, 0x10, "BSET" },
{ REL + 2, 0xad, "BSR" },
{ NULL + 1, 0x98, "CLC" },
{ NULL + 1, 0x9a, "CLI" },
{ DIR + IDX + 2, 0x3f, "CLR" },
{ NULL + 1, 0x4f, "CLRA" },
{ NULL + 1, 0x5f, "CLRX" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb1, "CMP" },
{ DIR + IDX + 2, 0x33, "COM" },
{ NULL + 1, 0x43, "COMA" },
{ NULL + 1, 0x53, "COMX" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb3, "CPX" },
{ DIR + IDX + 2, 0x3a, "DEC" },
{ NULL + 1, 0x4a, "DECA" },
{ NULL + 1, 0x5a, "DECX" },
{ NULL + 1, 0x5a, "DEX" },
{ PSEUDO + ISIF, ELSE, "ELSE" },
{ PSEUDO, END, "END" },
{ PSEUDO + ISIF, ENDIF, "ENDIF" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb8, "EOR" },
{ PSEUDO, EQU, "EQU" },
{ PSEUDO, FCB, "FCB" },
{ PSEUDO, FCC, "FCC" },
{ PSEUDO, FDB, "FDB" },
{ PSEUDO + ISIF, IF, "IF" },
{ DIR + IDX + 2, 0x3c, "INC" },
{ NULL + 1, 0x4c, "INCA" },
{ PSEUDO, INCL, "INCL" },
{ NULL + 1, 0x5c, "INCX" },
{ NULL + 1, 0x5c, "INX" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xbc, "JMP" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xbd, "JSR" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb6, "LDA" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xbe, "LDX" },
{ DIR + IDX + 2, 0x38, "LSL" },
{ NULL + 1, 0x48, "LSLA" },
{ NULL + 1, 0x58, "LSLX" },
{ DIR + IDX + 2, 0x34, "LSR" },
{ NULL + 1, 0x44, "LSRA" },
{ NULL + 1, 0x54, "LSRX" },
{ NULL + 1, 0x42, "MUL" },
{ DIR + IDX + 2, 0x30, "NEG" },
{ NULL + 1, 0x40, "NEGA" },
{ NULL + 1, 0x50, "NEGX" },
{ NULL + 1, 0x9d, "NOP" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xba, "ORA" },
{ PSEUDO, ORG, "ORG" },
{ PSEUDO, PAGE, "PAGE" },
{ PSEUDO, RMB, "RMB" },
{ DIR + IDX + 2, 0x39, "ROL" },
{ NULL + 1, 0x49, "ROLA" },
{ NULL + 1, 0x59, "ROLX" },
{ DIR + IDX + 2, 0x36, "ROR" },
{ NULL + 1, 0x46, "RORA" },
{ NULL + 1, 0x56, "RORX" },
{ NULL + 1, 0x9c, "RSP" },
{ NULL + 1, 0x80, "RTI" },
{ NULL + 1, 0x81, "RTS" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb2, "SBC" },
{ NULL + 1, 0x99, "SEC" },
{ NULL + 1, 0x9b, "SEI" },
{ PSEUDO, SET, "SET" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xb7, "STA" },
{ NULL + 1, 0x8e, "STOP" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xbf, "STX" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb0, "SUB" },
{ NULL + 1, 0x83, "SWI" },
{ NULL + 1, 0x97, "TAX" },
{ PSEUDO, TITLE, "TITLE" },
{ DIR + IDX + 2, 0x3d, "TST" },
{ NULL + 1, 0x4d, "TSTA" },
{ NULL + 1, 0x5d, "TSTX" },
{ NULL + 1, 0x9f, "TXA" },
{ NULL + 1, 0x8f, "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,