Last active
November 15, 2022 08:47
-
-
Save johnhmj/a390110b90535a8cf2cc05cc97b516d5 to your computer and use it in GitHub Desktop.
sic code for visual studio, sic1 codes for sic1 project, sic2 codes for sic2 project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COPY START 1000 | |
FIRST STL RETADR | |
CLOOP JSUB RDREC | |
LDA LENGTH | |
COMP ZERO | |
JEQ ENDFIL | |
JSUB WRREC | |
J CLOOP | |
ENDFIL LDA EOF | |
STA BUFFER | |
LDA THREE | |
STA LENGTH | |
JSUB WRREC | |
LDL RETADR | |
RSUB | |
EOF BYTE C'EOF' | |
THREE WORD 3 | |
ZERO WORD 0 | |
RETADR RESW 1 | |
LENGTH RESW 1 | |
BUFFER RESB 4096 | |
. | |
. COMMENT | |
. | |
RDREC LDX ZERO | |
LDA ZERO | |
RLOOP TD INPUT | |
JEQ RLOOP | |
RD INPUT | |
COMP ZERO | |
JEQ EXIT | |
STCH BUFFER,X | |
TIX MAXLEN | |
JLT RLOOP | |
EXIT STX LENGTH | |
RSUB | |
INPUT BYTE X'F1' | |
MAXLEN WORD 4096 | |
. | |
. COMMENT | |
. | |
WRREC LDX ZERO | |
WLOOP TD OUTPUT | |
JEQ WLOOP | |
LDCH BUFFER,X | |
WD OUTPUT | |
TIX LENGTH | |
JLT WLOOP | |
RSUB | |
OUTPUT BYTE X'05' | |
END FIRST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Built in Dec 10, 2005*/ | |
/*Fixed in May 10, 2007*/ | |
/*Fixed in May 1, 2009*/ | |
/*Fixed in May 7, 2009*/ | |
/*Update in Nov 15, 2022*/ | |
#include"sic1def.h" | |
/*main function*/ | |
int main(int argc, char *argv[]) { | |
FILE *fSource, *fIntermediate, *fSymTable; | |
unsigned int sic_length = 0; | |
char name[1][128]; | |
char pathIntermediate[256], pathSymbolTable[256]; | |
char cwdBuffer[_MAX_PATH]; | |
/*Fixed in May 1, 2009*/ | |
printf("(Dec 10, 2005) sic1.c Released!!\n"); | |
printf("(May 10, 2007) sic1.c Fixed!!\n"); | |
printf("(May 1, 2009) sic2.c Fixed!!\n"); | |
printf("Author's Website: http://tw.myblog.yahoo.com/mjshya/\n"); | |
printf("Author's Website: http://johnmhsheep.pixnet.net/\n\n"); | |
if (argc != 2) { | |
printf("Input SIC source file name\n(e.g. 123.txt): "); | |
scanf("%s", &name[0]); | |
argv[1] = name[0]; | |
} | |
fSource = fopen(argv[1], "r"); | |
if (fSource == NULL) { | |
printf("Open Source File Failure!!\n"); | |
printf("Check Files Please!!\n"); | |
system("pause"); | |
exit(0); | |
} | |
getcwd(cwdBuffer, _MAX_PATH); | |
strcpy(pathIntermediate, cwdBuffer); | |
strcat(pathIntermediate, "\\intermed\\interMed.txt"); | |
strcpy(pathSymbolTable, cwdBuffer); | |
strcat(pathSymbolTable, "\\intermed\\symTable.txt"); | |
mkdir("intermed"); | |
fIntermediate = fopen(pathIntermediate, "w"); | |
fSymTable = fopen(pathSymbolTable, "w"); | |
if (fSource != NULL || fIntermediate != NULL || fSymTable != NULL) { | |
if ((sic_length = funPassFirst(fSource, fIntermediate, fSymTable)) != -1) { | |
fclose(fSource); | |
fclose(fSymTable); | |
fclose(fIntermediate); | |
printf("...Address Completed!!\n"); | |
system("pause"); | |
} | |
else { | |
fclose(fSource); | |
fclose(fIntermediate); | |
fclose(fSymTable); | |
system("pause"); | |
exit(0); | |
} | |
} | |
else { | |
printf("Open Source, Destination and Symbol_Table Files Failure!!\n"); | |
printf("Check Files Please!!\n"); | |
system("pause"); | |
} | |
return 0; | |
}/*main function*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include"sic1def.h" | |
/*OP code Table*/ | |
OpTable arOpTable[NUMOP] = { "ADD",0x18,3,"ADDF",0x58,3,"ADDR",0x90,2,"AND",0x40,3, | |
"CLEAR",0xB4,2,"COMP",0x28,3,"COMPF",0x88,3,"COMPR",0xA0,2, | |
"DIV",0x24,3,"DIVF",0x64,3,"DIVR",0x9C,2,"FIX", 0xC4,1, | |
"FLOAT",0xC0,1,"HIO",0xF4,1,"J",0x3C,3,"JEQ",0x30,3, | |
"JGT",0x34,3,"JLT",0x38,3,"JSUB",0x48,3,"LDA",0x00,3, | |
"LDB",0x68,3,"LDCH",0x50,3,"LDF",0x70,3,"LDL",0x08,3, | |
"LDS",0x6C,3,"LDT",0x74,3,"LDX",0x04,3,"LPS",0xD0,3, | |
"MUL",0x20,3,"MULF",0x60,3,"MULR",0x98,2,"NORM",0xC8,1, | |
"OR",0x44,3,"RD",0xD8,3,"RMO",0xAC,2,"RSUB",0x4C,3, | |
"SHIFTL",0xA4,2,"SHIFTR",0xA8,2,"SIO",0xF0,1,"SSK",0xEC,3, | |
"STA",0x0C,3,"STB",0x78,3,"STCH",0x54,3,"STF",0x80,3, | |
"STI",0xD4,3,"STL",0x14,3,"STS",0x7C,3,"STSW",0xE8,3, | |
"STT",0x84,3,"STX",0x10,3,"SUB",0x1C,3,"SUBF",0x5C,3, | |
"SUBR",0x94,2,"SVC",0xB0,2,"TD",0xE0,3,"TIO",0xF8,1, | |
"TIX",0x2C,3,"TIXR",0xB8,2,"WD",0xDC,3 }; | |
/*Address Table*/ | |
AddTable arAddTable[NUMAD] = { "START","END","BYTE","WORD","RESB","RESW" }; | |
/*Symbol Table*/ | |
SymTable arSymTable[MAXSYM]; | |
/*Covert HEX strings to integer*/ | |
unsigned int hextoint(char *S) { | |
char Hex[17] = "0123456789ABCDEF"; | |
unsigned int retInteger = 0, nTemp, i, j; | |
strupr(S); | |
for (i = 0; i<strlen(S); i++) { | |
for (j = 0, nTemp = 1; j<strlen(Hex); j++) { | |
if (*(S + i) == *(Hex + j)) { | |
nTemp *= j; | |
break; | |
} | |
} | |
retInteger += (nTemp *= (int)pow(16, strlen(S) - i - 1)); | |
} | |
return retInteger; | |
} | |
/*Comment*/ | |
unsigned int find_comment(char *S) { | |
if (S[0] != '.') { | |
return -1; | |
} | |
else { | |
return 1; | |
} | |
} | |
/*find Op Table*/ | |
int find_optable(char *S) { | |
int i, nRet = -1; | |
for (i = 0; i<NUMOP; i++) { | |
if (stricmp(arOpTable[i].name, S) == 0) { | |
nRet = i; | |
break; | |
} | |
} | |
return nRet; | |
} | |
/*find Address Table*/ | |
int find_addtable(char *S) { | |
int i, nRet = -1; | |
for (i = 0; i<NUMAD; i++) { | |
if (stricmp(arAddTable[i].name, S) == 0) { | |
nRet = i; | |
break; | |
} | |
} | |
return nRet; | |
} | |
/*Count BYTE length*/ | |
unsigned int length_Byte(char *S) { | |
unsigned int nRet = 0; | |
char chSep[] = "\'", Temp[64]; | |
strtok(S, chSep); | |
strcpy(Temp, strtok(NULL, chSep)); | |
if (S[0] == 'C' || S[0] == 'c') { | |
nRet = (unsigned int)strlen(Temp); | |
} | |
else if (S[0] == 'X' || S[0] == 'x') { | |
nRet = (unsigned int)(strlen(Temp) / 2); | |
} | |
return nRet; | |
} | |
/*find Address Table*/ | |
unsigned int setup_symtable(char *S, unsigned int MAX) { | |
unsigned int i, nRet = 0; | |
for (i = 0; i<MAX; i++) { | |
if (stricmp(arSymTable[i].name, S) == 0) { | |
nRet++; | |
} | |
} | |
return ((nRet>0) ? (nRet - 1) : nRet); | |
} | |
/*Pass First*/ | |
unsigned int funPassFirst(FILE *SOURCE, FILE *INTERMEDIATE, FILE *SYMTABLE) { | |
unsigned int nSymCounter = 0;/*symbol table counter*/ | |
unsigned int Starting_Address = 0;/*start address*/ | |
unsigned int PC = 0;/*program counter*/ | |
unsigned int hextoint(char *S); | |
unsigned int i, nOpRet = 0, LOCCTR = 0, length_Source = 0; | |
/*Fixed in May 7, 2009*/ | |
/*funRet change to INT from UNSIGNED INT*/ | |
int funRet = 0; | |
char chLine[128], Temp[128] = { ' ' }; | |
char LABEL[32], OPCODE[32], OPERAND[32], Temp32[32] = { ' ' }; | |
char *strToken, chSep[] = " \n"; | |
/*read first input line*/ | |
fgets(chLine, 128, SOURCE); | |
strcpy(Temp, chLine); | |
strcpy(LABEL, strtok(Temp, chSep)); | |
strcpy(OPCODE, strtok(NULL, chSep)); | |
strcpy(OPERAND, strtok(NULL, chSep)); | |
if (stricmp(OPCODE, "START") == 0) {/*OPCODE='START'*/ | |
/*save #[OPERAND] as starting address*/ | |
Starting_Address = hextoint(OPERAND); | |
/*initialize LOCCTR to starting address*/ | |
PC = Starting_Address; | |
/*write line to intermediate file*/ | |
strcpy(Temp, strupr(itoa(PC, Temp32, 16))); | |
strcat(Temp, " "); | |
strcat(Temp, chLine); | |
fputs(Temp, INTERMEDIATE); | |
/*read next input line*/ | |
fgets(chLine, 128, SOURCE); | |
strcpy(Temp, chLine); | |
strcpy(LABEL, strtok(Temp, chSep)); | |
strcpy(OPCODE, strtok(NULL, chSep)); | |
strcpy(OPERAND, strtok(NULL, chSep)); | |
} | |
else { | |
PC = 0; | |
}/*if START*/ | |
while (stricmp(OPCODE, "END") != 0) { | |
if (find_comment(chLine) != 1) { | |
if (LABEL[0] != ' ') { | |
/*search SYMTAB for other LABEL*/ | |
strcpy(arSymTable[nSymCounter].name, LABEL); | |
if (setup_symtable(LABEL, nSymCounter) != 0) {/*found*/ | |
/*set error flag (duplicate symbol)*/ | |
funRet = -1; | |
printf("Error: duplicate symbol.\n"); | |
break; | |
} | |
else { | |
/*insert (LABEL,LOCCTR) into SYMTAB*/ | |
arSymTable[nSymCounter].locctr = PC; | |
nSymCounter++; | |
} | |
}/*if symbol*/ | |
nOpRet = find_optable(OPCODE); | |
if (nOpRet != -1) {/*found OPCODE*/ | |
LOCCTR = PC; | |
PC += 3; | |
} | |
else if (find_addtable(OPCODE) == 3) {/*WORD*/ | |
LOCCTR = PC; | |
PC += 3; | |
} | |
else if (find_addtable(OPCODE) == 5) {/*RESW*/ | |
LOCCTR = PC; | |
PC += 3 * atoi(OPERAND); | |
} | |
else if (find_addtable(OPCODE) == 4) {/*RESB*/ | |
LOCCTR = PC; | |
PC += atoi(OPERAND); | |
} | |
else if (find_addtable(OPCODE) == 2) {/*BYTE*/ | |
LOCCTR = PC; | |
PC += length_Byte(OPERAND); | |
} | |
else { | |
/*set error flag(invalid operation code)*/ | |
funRet = -1; | |
printf("Error: invalid operation code.\n"); | |
break; | |
} | |
}/*if not a comment*/ | |
/*write line to intermediate file*/ | |
if (find_comment(chLine) != 1) { | |
strcpy(Temp, strupr(itoa(LOCCTR, Temp32, 16))); | |
strcat(Temp, " "); | |
chLine[strlen(chLine) - 1] = '\0'; | |
strcat(Temp, chLine); | |
strcat(Temp, "\n"); | |
} | |
else { | |
strcpy(Temp, chLine); | |
} | |
fputs(Temp, INTERMEDIATE); | |
/*read next input line*/ | |
fgets(chLine, 128, SOURCE); | |
if (find_comment(chLine) != 1) { | |
strcpy(Temp, chLine); | |
if (Temp[0] == ' ') { | |
strcpy(LABEL, " "); | |
strcpy(OPCODE, strtok(Temp, chSep)); | |
strToken = strtok(NULL, chSep); | |
if (strToken == NULL) { | |
strcpy(OPERAND, " "); | |
} | |
else { | |
strcpy(OPERAND, strToken); | |
} | |
} | |
else { | |
strcpy(LABEL, strtok(Temp, chSep)); | |
strcpy(OPCODE, strtok(NULL, chSep)); | |
strcpy(OPERAND, strtok(NULL, chSep)); | |
} | |
} | |
}/*while not END*/ | |
if (funRet == -1) { | |
return funRet; | |
} | |
/*write last line to intermediate file*/ | |
if (chLine[strlen(chLine) - 1] != '\n') { | |
strcat(chLine, "\n"); | |
} | |
strcpy(Temp, chLine); | |
fputs(Temp, INTERMEDIATE); | |
/*save (LOCCTR-starting address) as program length*/ | |
length_Source = PC - Starting_Address; | |
if (funRet != -1) { | |
funRet = length_Source; | |
} | |
strcpy(Temp, "\n! Length_of_the_SIC_Source= "); | |
strcat(Temp, strupr(itoa(length_Source, Temp32, 16))); | |
strcat(Temp, "\n"); | |
fputs(Temp, INTERMEDIATE); | |
for (i = 0; i<nSymCounter; i++) { | |
strcpy(Temp, arSymTable[i].name); | |
strcat(Temp, " "); | |
strcat(Temp, strupr(itoa(arSymTable[i].locctr, Temp32, 16))); | |
strcat(Temp, "\n"); | |
fputs(Temp, SYMTABLE); | |
} | |
return funRet; | |
}/*Pass First*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<direct.h> | |
#include<math.h> | |
#define NUMOP 59 | |
#define NUMAD 6 | |
#define NUMREG 9 | |
#define MAXSYM 50 | |
/*OP code Table*/ | |
typedef struct optable { | |
char name[8]; | |
unsigned int opcode; | |
unsigned int format; | |
}OpTable; | |
/*Address Table*/ | |
typedef struct addtable { | |
char name[8]; | |
}AddTable; | |
/*Symbol Table*/ | |
typedef struct symtable { | |
char name[8]; | |
unsigned int locctr; | |
}SymTable; | |
/*Covert HEX strings to integer*/ | |
unsigned int hextoint(char *S); | |
/*Comment*/ | |
unsigned int find_comment(char *S); | |
/*find Op Table*/ | |
int find_optable(char *S); | |
/*find Address Table*/ | |
int find_addtable(char *S); | |
/*Count BYTE length*/ | |
unsigned int length_Byte(char *S); | |
/*find Address Table*/ | |
unsigned int setup_symtable(char *S, unsigned int MAX); | |
/*Pass First*/ | |
unsigned int funPassFirst(FILE *SOURCE, FILE *INTERMEDIATE, FILE *SYMTABLE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Built in Dec 10, 2005*/ | |
/*Fixed in May 10, 2007*/ | |
/*Fixed in May 1, 2009*/ | |
/*Fixed in May 7, 2009*/ | |
/*Update in Nov 15, 2022*/ | |
#include"sic2def.h" | |
/*main function*/ | |
int main(int argc, char *argv[]) { | |
FILE *fIntermediate, *fSymTable, *fList, *fObj; | |
unsigned int sic_length = 0; | |
char cwdBuffer[_MAX_PATH]; | |
char pathIntermediate[256], pathSymbolTable[256], pathList[256], pathObj[256]; | |
/*Fixed in May 1, 2009*/ | |
printf("(Dec 10, 2005) sic1.c Released!!\n"); | |
printf("(May 10, 2007) sic1.c Fixed!!\n"); | |
printf("(May 1, 2009) sic2.c Fixed!!\n"); | |
printf("Author's Website: http://tw.myblog.yahoo.com/mjshya/\n"); | |
printf("Author's Website: http://johnmhsheep.pixnet.net/\n\n"); | |
getcwd(cwdBuffer, _MAX_PATH); | |
strcpy(pathIntermediate, cwdBuffer); | |
strcat(pathIntermediate, "\\intermed\\interMed.txt"); | |
strcpy(pathSymbolTable, cwdBuffer); | |
strcat(pathSymbolTable, "\\intermed\\symTable.txt"); | |
strcpy(pathList, cwdBuffer); | |
strcat(pathList, "\\intermed\\list.txt"); | |
strcpy(pathObj, cwdBuffer); | |
strcat(pathObj, "\\intermed\\obj.txt"); | |
fIntermediate = fopen(pathIntermediate, "r"); | |
fSymTable = fopen(pathSymbolTable, "r"); | |
fList = fopen(pathList, "w"); | |
fObj = fopen(pathObj, "w"); | |
if (fIntermediate != NULL || fSymTable != NULL || fList != NULL) { | |
if (funPassSecond(fIntermediate, fSymTable, fList, fObj) != -1) { | |
fclose(fIntermediate); | |
fclose(fSymTable); | |
fclose(fList); | |
fclose(fObj); | |
printf("...Compile Completed!!\n"); | |
system("pause"); | |
} | |
else { | |
fclose(fIntermediate); | |
fclose(fSymTable); | |
fclose(fList); | |
fclose(fObj); | |
system("pause"); | |
exit(0); | |
} | |
} | |
else { | |
printf("Open Intermediate, Symbol_Table and List Files Failure!!\n"); | |
printf("Check Files Please!!\n"); | |
system("pause"); | |
} | |
return 0; | |
}/*main function*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include"sic2def.h" | |
/*OP code Table*/ | |
OpTable arOpTable[NUMOP] = { "ADD",0x18,3,"ADDF",0x58,3,"ADDR",0x90,2,"AND",0x40,3, | |
"CLEAR",0xB4,2,"COMP",0x28,3,"COMPF",0x88,3,"COMPR",0xA0,2, | |
"DIV",0x24,3,"DIVF",0x64,3,"DIVR",0x9C,2,"FIX", 0xC4,1, | |
"FLOAT",0xC0,1,"HIO",0xF4,1,"J",0x3C,3,"JEQ",0x30,3, | |
"JGT",0x34,3,"JLT",0x38,3,"JSUB",0x48,3,"LDA",0x00,3, | |
"LDB",0x68,3,"LDCH",0x50,3,"LDF",0x70,3,"LDL",0x08,3, | |
"LDS",0x6C,3,"LDT",0x74,3,"LDX",0x04,3,"LPS",0xD0,3, | |
"MUL",0x20,3,"MULF",0x60,3,"MULR",0x98,2,"NORM",0xC8,1, | |
"OR",0x44,3,"RD",0xD8,3,"RMO",0xAC,2,"RSUB",0x4C,3, | |
"SHIFTL",0xA4,2,"SHIFTR",0xA8,2,"SIO",0xF0,1,"SSK",0xEC,3, | |
"STA",0x0C,3,"STB",0x78,3,"STCH",0x54,3,"STF",0x80,3, | |
"STI",0xD4,3,"STL",0x14,3,"STS",0x7C,3,"STSW",0xE8,3, | |
"STT",0x84,3,"STX",0x10,3,"SUB",0x1C,3,"SUBF",0x5C,3, | |
"SUBR",0x94,2,"SVC",0xB0,2,"TD",0xE0,3,"TIO",0xF8,1, | |
"TIX",0x2C,3,"TIXR",0xB8,2,"WD",0xDC,3 }; | |
/*Address Table*/ | |
AddTable arAddTable[NUMAD] = { "START","END","BYTE","WORD","RESB","RESW" }; | |
/*Covert HEX strings to integer*/ | |
unsigned int hextoint(char *S) { | |
char Hex[17] = "0123456789ABCDEF"; | |
unsigned int retInteger = 0, nTemp, i, j; | |
strupr(S); | |
for (i = 0; i<strlen(S); i++) { | |
for (j = 0, nTemp = 1; j<strlen(Hex); j++) { | |
if (*(S + i) == *(Hex + j)) { | |
nTemp *= j; | |
break; | |
} | |
} | |
retInteger += (nTemp *= (int)pow(16, strlen(S) - i - 1)); | |
} | |
return retInteger; | |
} | |
/*Comment*/ | |
unsigned int find_comment(char *S) { | |
if (S[0] != '.') { | |
return 0; | |
} | |
else { | |
return 1; | |
} | |
} | |
/*find Op Table*/ | |
int find_optable(const char *S) { | |
int i, nRet = -1; | |
for (i = 0; i<NUMOP; i++) { | |
if (stricmp(arOpTable[i].name, S) == 0) { | |
nRet = i; | |
break; | |
} | |
} | |
return nRet; | |
} | |
/*find Address Table*/ | |
int find_addtable(const char *S) { | |
int i, nRet = -1; | |
for (i = 0; i<NUMAD; i++) { | |
if (stricmp(arAddTable[i].name, S) == 0) { | |
nRet = i; | |
break; | |
} | |
} | |
return nRet; | |
} | |
/*==========Pass Second functions==========*/ | |
int p2_find_symtable(FILE *SYMTABLE, const char *S) { | |
unsigned int hextoint(char *S); | |
int nRet = -1; | |
char line[64], name[32], address[32], SEP[] = " \n"; | |
rewind(SYMTABLE); | |
while (!feof(SYMTABLE)) { | |
fgets(line, 64, SYMTABLE); | |
strcpy(name, strtok(line, SEP)); | |
strcpy(address, strtok(NULL, SEP)); | |
if (stricmp(name, S) == 0) { | |
nRet = hextoint(address); | |
break; | |
} | |
} | |
return nRet; | |
} | |
char *stringtoascii(const char *S1, char *S2) { | |
int i, j, k; | |
char temp[32], tempS1[32], tokenS1[16], SEP[] = "\'"; | |
if (S1[0] == 'C' || S1[0] == 'c') { | |
strcpy(tempS1, S1); | |
strcpy(tokenS1, strtok(tempS1, SEP)); | |
strcpy(tokenS1, strtok(NULL, SEP)); | |
j = (int)strlen(tokenS1); | |
for (i = 0; i<j; i++) { | |
k = toascii(tokenS1[i]); | |
itoa(k, temp, 16); | |
if (i == 0) { | |
strupr(strcpy(S2, temp)); | |
} | |
else { | |
strupr(strcat(S2, temp)); | |
} | |
} | |
} | |
else if (S1[0] == 'X' || S1[0] == 'x') { | |
strcpy(tempS1, S1); | |
strcpy(tokenS1, strtok(tempS1, SEP)); | |
strcpy(tokenS1, strtok(NULL, SEP)); | |
strupr(strcpy(S2, tokenS1)); | |
} | |
else { | |
strcpy(tempS1, strupr(itoa(atoi(S1), temp, 16))); | |
j = 6 - (int)strlen(tempS1); | |
if (j>0) { | |
strcpy(S2, "0"); | |
for (i = 1; i<j; i++) { | |
strcat(S2, "0"); | |
} | |
strcat(S2, tempS1); | |
} | |
} | |
return S2; | |
} | |
unsigned int funPassSecond(FILE *INTERMEDIATE, FILE *SYMTABLE, FILE *LISTFILE, FILE *OBJ) { | |
unsigned int retOPCODE = 0, obj_counter = 0, obj_length = 0, sic_length = 0; | |
/*Fixed in May 7, 2009*/ | |
/*retfunSEC change to INT from UNSIGNED INT*/ | |
int retfunSEC = 0; | |
unsigned int sic_start = 0, nSymCounter = 0, Starting_Address = 0, PC = 0, flagRES = 0, objLineRESET = 0; | |
/*Fixed in May 1, 2009*/ | |
/*retSYMTABLE and regX has been INT from UNSIGNED INT*/ | |
int retSYMTABLE = 0, regX = 0; | |
char *ptrToken; | |
char objCODE[32], objOPCODE[16], objOPERAND[16]; | |
char LINE[128], tempLINE[128], SEP[] = " \n"; | |
char ADDRESS[32], LABEL[32], OPCODE[32], OPERAND[32], X_OPERAND[32]; | |
char objLINE[128], objTEXT[128], temp_32[32]; | |
/*=====find sic length value=====*/ | |
while (!feof(INTERMEDIATE)) { | |
fgets(LINE, 128, INTERMEDIATE); | |
if (LINE[0] == '!') { | |
break; | |
} | |
} | |
strcpy(tempLINE, strtok(LINE, " \n"));/*space*/ | |
strcpy(tempLINE, strtok(NULL, " \n")); | |
strcpy(tempLINE, strtok(NULL, " \n")); | |
sic_length = hextoint(tempLINE); | |
rewind(INTERMEDIATE); | |
/*read first input line(from intermediate file)*/ | |
fgets(LINE, 128, INTERMEDIATE); | |
strcpy(tempLINE, LINE); | |
strcpy(ADDRESS, strtok(tempLINE, SEP)); | |
sic_start = hextoint(ADDRESS); | |
strcpy(LABEL, strtok(NULL, SEP)); | |
strcpy(OPCODE, strtok(NULL, SEP)); | |
strcpy(OPERAND, strtok(NULL, SEP)); | |
if (stricmp(OPCODE, "START") == 0) {/*OPCODE='START'*/ | |
/*write listing line*/ | |
fputs(LINE, LISTFILE); | |
/*read next input line*/ | |
fgets(LINE, 128, INTERMEDIATE); | |
strcpy(tempLINE, LINE); | |
}/*if START*/ | |
/*write Header record to object program*/ | |
strcpy(objLINE, "H"); | |
strcat(objLINE, LABEL); | |
strcat(objLINE, " ");/*2 space*/ | |
strcat(objLINE, "00");/*2 zero*/ | |
strcat(objLINE, ADDRESS); | |
strcat(objLINE, "00");/*2 zero*/ | |
strcat(objLINE, strupr(itoa(sic_length, temp_32, 16))); | |
strcat(objLINE, "\n"); | |
fputs(objLINE, OBJ); | |
/*=====read next input line continue=====*/ | |
strcpy(ADDRESS, strtok(tempLINE, SEP)); | |
strcpy(LABEL, strtok(NULL, SEP)); | |
strcpy(OPCODE, strtok(NULL, SEP)); | |
strcpy(OPERAND, strtok(NULL, SEP)); | |
/*initialize first Text record*/ | |
strcpy(objLINE, "T00"); | |
strcat(objLINE, ADDRESS); | |
Starting_Address = hextoint(ADDRESS); | |
while (stricmp(OPCODE, "END") != 0) {/*OPCODE!='END'*/ | |
if (find_comment(LINE) != 1) {/*this is not a comment line*/ | |
/*search OPTAB for OPCODE*/ | |
if (stricmp(OPCODE, "LDX") == 0) { | |
regX = 1; | |
} | |
retOPCODE = find_optable(OPCODE); | |
if (retOPCODE != -1) {/*found*/ | |
strcpy(objOPCODE, strupr(itoa(arOpTable[retOPCODE].opcode, temp_32, 16))); | |
if (hextoint(objOPCODE)<16) { | |
strcpy(temp_32, "0"); | |
strcat(temp_32, objOPCODE); | |
strcpy(objOPCODE, temp_32); | |
} | |
if (stricmp(OPERAND, " ") != 0) {/*there is a symbol in OPERAND field*/ | |
/*search SYMTAB for OPERAND*/ | |
if (strstr(OPERAND, ",X") != NULL) { | |
strcpy(X_OPERAND, OPERAND); | |
ptrToken = strtok(X_OPERAND, ","); | |
retSYMTABLE = p2_find_symtable(SYMTABLE, ptrToken); | |
if (regX == 1) { | |
regX = retSYMTABLE; | |
retSYMTABLE = 32768 | regX; | |
regX = 0; | |
} | |
} | |
else { | |
retSYMTABLE = p2_find_symtable(SYMTABLE, OPERAND); | |
} | |
if (retSYMTABLE != -1) {/*found*/ | |
/*store symbol value as operand address*/ | |
strcpy(objOPERAND, strupr(itoa(retSYMTABLE, temp_32, 16))); | |
} | |
else { | |
/*store 0 as operand address*/ | |
strcpy(objOPERAND, "000000"); | |
/*set error flag(undefined symbol)*/ | |
retfunSEC = -1; | |
} | |
} | |
else { | |
/*store 0 as operand address*/ | |
strcpy(objOPERAND, "0000"); | |
} | |
/*assemble the object code instruction*/ | |
strcpy(objCODE, objOPCODE); | |
strcat(objCODE, objOPERAND); | |
} | |
else if (find_addtable(OPCODE) == 2 || find_addtable(OPCODE) == 3) {/*OPCODE='BYTE'or'WORD'*/ | |
/*covert constant to object code*/ | |
stringtoascii(OPERAND, objCODE); | |
} | |
if (find_addtable(OPCODE) == 4 || find_addtable(OPCODE) == 5) { | |
flagRES++; | |
objLineRESET = 1; | |
} | |
else { | |
flagRES = 0; | |
} | |
if (obj_counter == 10 || flagRES == 1) {/*object code will not fit into the current Text record*/ | |
/*write Text record to object program*/ | |
PC = hextoint(ADDRESS); | |
obj_length = PC - Starting_Address; | |
if (obj_length<16) { | |
strcat(objLINE, "0"); | |
} | |
strcat(objLINE, strupr(itoa(obj_length, temp_32, 16))); | |
strcat(objLINE, objTEXT); | |
strcat(objLINE, "\n"); | |
fputs(objLINE, OBJ); | |
/*initialize new Text record*/ | |
obj_counter = 0; | |
strcpy(objLINE, "T00"); | |
strcat(objLINE, ADDRESS); | |
Starting_Address = hextoint(ADDRESS); | |
} | |
if (flagRES == 0 && objLineRESET == 1) { | |
strcpy(objLINE, "T00"); | |
strcat(objLINE, ADDRESS); | |
Starting_Address = hextoint(ADDRESS); | |
objLineRESET = 0; | |
} | |
/*add object code to Text record*/ | |
if (find_addtable(OPCODE) == 4 || find_addtable(OPCODE) == 5) { | |
/*nothing*/ | |
} | |
else { | |
if (obj_counter == 0) { | |
strcpy(objTEXT, objCODE); | |
} | |
else { | |
strcat(objTEXT, objCODE); | |
} | |
obj_counter++; | |
} | |
}/*if not comment*/ | |
/*write listing line*/ | |
if (find_addtable(OPCODE) == 4 || find_addtable(OPCODE) == 5) { | |
/*nothing*/ | |
} | |
else { | |
if (find_comment(LINE) != 1) { | |
LINE[strlen(LINE) - 1] = '\0'; | |
if (stricmp(objCODE, " ") != 0) { | |
if (stricmp(OPERAND, " ") == 0) { | |
strcat(LINE, " "); | |
} | |
else if (strstr(OPERAND, ",X") != NULL) { | |
strcat(LINE, " "); | |
} | |
else { | |
strcat(LINE, " "); | |
} | |
strcat(LINE, objCODE); | |
strcat(LINE, "\n"); | |
} | |
} | |
} | |
fputs(LINE, LISTFILE); | |
/*read next input line*/ | |
fgets(LINE, 128, INTERMEDIATE); | |
strcpy(tempLINE, LINE); | |
if (find_comment(LINE) != 1) { | |
strcpy(ADDRESS, strtok(tempLINE, SEP)); | |
if (stricmp(ADDRESS, "END") == 0) { | |
strcpy(OPCODE, ADDRESS); | |
strcpy(OPERAND, strtok(NULL, SEP)); | |
strcpy(ADDRESS, " "); | |
strcpy(LABEL, " "); | |
} | |
else { | |
strcpy(LABEL, strtok(NULL, SEP)); | |
ptrToken = strtok(NULL, SEP); | |
if (ptrToken != NULL) { | |
strcpy(OPCODE, ptrToken); | |
} | |
else { | |
strcpy(OPCODE, " "); | |
} | |
if (find_optable(LABEL) != -1) { | |
strcpy(OPERAND, OPCODE); | |
strcpy(OPCODE, LABEL); | |
strcpy(LABEL, " "); | |
} | |
else { | |
strcpy(OPERAND, strtok(NULL, SEP)); | |
} | |
} | |
} | |
}/*while not END*/ | |
/*write last Text record to object program*/ | |
if (stricmp(OPCODE, "END") == 0) { | |
PC = sic_start + sic_length; | |
obj_length = PC - Starting_Address; | |
if (obj_length<16) { | |
strcat(objLINE, "0"); | |
} | |
strcat(objLINE, strupr(itoa(obj_length, temp_32, 16))); | |
strcat(objLINE, objTEXT); | |
strcat(objLINE, "\n"); | |
fputs(objLINE, OBJ); | |
} | |
/*write End record to object program*/ | |
strcpy(objLINE, "E"); | |
strcat(objLINE, "00"); | |
strcat(objLINE, strupr(itoa(sic_start, temp_32, 16))); | |
strcat(objLINE, "\n"); | |
fputs(objLINE, OBJ); | |
/*write last listing line*/ | |
fputs(LINE, LISTFILE); | |
return retfunSEC; | |
}/*Pass Second*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<ctype.h> | |
#include<direct.h> | |
#include<math.h> | |
#define NUMOP 59 | |
#define NUMAD 6 | |
#define NUMREG 9 | |
#define MAXSYM 50 | |
/*OP code Table*/ | |
typedef struct optable { | |
char name[8]; | |
unsigned int opcode; | |
unsigned int format; | |
}OpTable; | |
/*Address Table*/ | |
typedef struct addtable { | |
char name[8]; | |
}AddTable; | |
/*Covert HEX strings to integer*/ | |
unsigned int hextoint(char *S); | |
/*Comment*/ | |
unsigned int find_comment(char *S); | |
/*find Op Table*/ | |
int find_optable(const char *S); | |
/*find Address Table*/ | |
int find_addtable(const char *S); | |
/*==========Pass Second functions==========*/ | |
int p2_find_symtable(FILE *SYMTABLE, const char *S); | |
char *stringtoascii(const char *S1, char *S2); | |
unsigned int funPassSecond(FILE *INTERMEDIATE, FILE *SYMTABLE, FILE *LISTFILE, FILE *OBJ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment