I don't know if I posted the full functions yet or not so here we go:
pass1.c
Code:
/* Pre-Processor Directives */
#include <string.h>
#ifndef SYMBSIZE
#define SYMBSIZE 10
#endif
typedef struct tablemem
{
char symbol[SYMBSIZE];
int value;
int casenmbr;
int otherinfo;
} tabletype;
struct toklist
{
char *lbl;
char *opc;
char *opr1;
char *opr2;
int *ni;
int *xbpe;
} components;
extern char sourcelist[][81];
extern int sourcecnt;
extern int errors[];
extern tabletype symbtab[];
extern int symbtabsize;
extern tabletype codetable[];
extern int codetabsize;
int pass1()
{
int Scnt = 0;
int Locctr = 0;
int ENDval = 0;
int Errorflag = 0;
int returnVal = 0;
char label[81], opcode[81], opr1[81], opr2[81];
int ni, xbpe;
components.lbl = label;
components.opc = opcode;
components.opr1 = opr1;
components.opr2 = opr2;
components.ni = ∋
components.xbpe = &xbpe;
struct tablemem newentry;
while(sourcelist[Scnt] == ".")
Scnt++;
breakup(sourcelist[Scnt], components);
if(components.opc == "START")
{
Locctr = (int)strtol(components.opr1, 0, 16);
ENDval = (int)strtol(components.opr1, 0, 16);
if(strcmp(components.lbl, "\0") != 0)
{
strcpy(newentry.symbol, components.lbl);
newentry.value = Locctr;
newentry.casenmbr = 0;
newentry.otherinfo = 0;
insert(newentry, symbtab, &symbtabsize);
}
Scnt++;
breakup(sourcelist[Scnt], components);
}
while(components.opc != "END")
{
if(sourcelist[Scnt] != ".")
{
if(components.lbl != '\0')
{
int index = findfirst(symbtab, symbtabsize, components.lbl);
if(index == -1)
{
strcpy(newentry.symbol, components.lbl);
newentry.value = Locctr;
newentry.casenmbr = 0;
newentry.otherinfo = 0;
insert(newentry, symbtab, &symbtabsize);
}
else
{
errors[Scnt] = 1;
Errorflag = 1;
}
}
int opcodeIndex = findfirst(codetable, codetabsize, components.opc);
if(opcodeIndex != -1)
{
int opcodecase = 0;
if(components.opc == "RESW" || components.opc == "RESB")
opcodecase = 1;
else if(components.opc == "WORD" || components.opc == "BYTE")
opcodecase = 2;
switch(opcodecase)
{
case 1:
{
Locctr += storageincr(opcodeIndex, components.opr1);
break;
}
case 2:
{
Locctr += storageincr(opcodeIndex, components.opr1);
break;
}
default:
{
Locctr += opcodeincr(opcodeIndex, components.ni,
components.xbpe);
break;
}
}
}
else
{
/* directives such as BASE handled here or error flags */
Errorflag = 1;
}
}
Scnt++;
breakup(sourcelist[Scnt], components);
}
if(strcmp(components.lbl, "\0") != 0)
{
int index = findfirst(symbtab, symbtabsize, components.lbl);
if(index == -1)
{
strcpy(newentry.symbol, components.lbl);
newentry.value = Locctr;
newentry.casenmbr = 0;
newentry.otherinfo = 0;
insert(newentry, symbtab, &symbtabsize);
}
else
{
errors[Scnt] = 1;
Errorflag = 1;
}
}
if(strcmp(components.opr1, "\0") != 0)
{
int index = findfirst(symbtab, symbtabsize, components.opr1);
if(index != -1)
ENDval = symbtab[index].value;
}
if(ENDval != 0)
returnVal = ENDval;
else
{
// Set returnVal to START value
}
if(Errorflag == 1)
returnVal = -1;
return returnVal;
}
breakup.c
Code:
/* Pre-Processor Directives */
#include <string.h>
struct toklist
{
char *lbl;
char *opc;
char *opr1;
char *opr2;
int *ni;
int *xbpe;
} ;
void breakup(char sourceline[], struct toklist components)
{
char workingstring[81], temp[81], *ptr;
char label[81], opcode[81], operand1[81], operand2[81];
int ni = 3, bp = 0;
int xbpe = *components.xbpe;
/* Remove x and e bits */
if(xbpe > 7)
xbpe -= 8;
if(xbpe > 3)
{
bp += 4;
xbpe -= 4;
}
if(xbpe > 1)
{
bp += 2;
xbpe -= 2;
}
xbpe = 0;
strcpy(workingstring, sourceline);
strtok(workingstring, "\n");
label[0] = '\0';
opcode[0] = '\0';
operand1[0] = '\0';
operand2[0] = '\0';
/* Check for a label */
if(workingstring[0] == ' ' || workingstring[0] == '\t')
{/* No label, tokenize opcode */
if((ptr = strtok(workingstring, " \t")) != NULL)
{
if(ptr[0] == '*')
{
ptr = ptr + 1;
ni = 0;
}
if(ptr[0] == '+')
{
ptr = ptr + 1;
xbpe += 1;
}
strcpy(opcode, ptr);
}
}
else
{/* Tokenize label and opcode */
if((ptr = strtok(workingstring, " \t")) != NULL)
strcpy(label, ptr);
if((ptr = strtok(NULL, " \t")) != NULL)
{
if(ptr[0] == '*')
{
ptr = ptr + 1;
ni = 0;
}
if(ptr[0] == '+')
{
ptr = ptr + 1;
xbpe += 1;
}
strcpy(opcode, ptr);
}
}
/* Remove White Space */
if((ptr = strtok(NULL, "")) != NULL)
{
while(ptr[0] == ' ' || ptr[0] == '\t')
ptr = ptr + 1;
strcpy(workingstring, ptr);
}
/* Set n bit for indirect addressing */
if(workingstring[0] == '@')
{
strcpy(workingstring, workingstring + 1);
ni = 2;
}
/* Set i bit for immediate addressing */
if(workingstring[0] == '#')
{
strcpy(workingstring, workingstring + 1);
ni = 1;
}
/* Check for single quote */
if(workingstring[1] == '\'' || workingstring[2] == '\'')
{
ptr = strtok(workingstring, "\'");
strcpy(temp, ptr);
strcat(temp, "\'");
ptr = strtok(NULL, "\'");
strcat(temp, ptr);
strcat(temp, "\'");
strcpy(operand1, temp);
}
else
/* Tokenize operand1 */
if((ptr = strtok(workingstring, ", \t")) != NULL)
strcpy(operand1, ptr);
/* Tokenize operand2 */
if((ptr = strtok(NULL, ", \t")) != NULL)
strcpy(operand2, ptr);
/* If there is an operand2 set x bit */
if(strncmp(operand2, "\0", 1) != 0)
xbpe += 8;
/* Set values in the components */
strcpy(components.lbl, label);
strcpy(components.opc, opcode);
strcpy(components.opr1, operand1);
strcpy(components.opr2, operand2);
*components.ni = ni;
*components.xbpe = xbpe + bp;
}