Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************
- Title: Phase 3
- Author: Oscar Alvarez III
- Course: 3334 Systems Programming
- *************************************************/
- /////////////////////////////////////////
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define WHITESPACE " \t\r\n\v\f"
- //Function that parses the string recieved
- int prse(char in[], char *a, int size, int i);
- //Function that checks for a valid alphanumeric value
- int alphanum(int x);
- //Function that check for a space
- int space(int y);
- void assemble(char str[]);
- void pass1(char str[]);
- void parse2(char line[], char lab[], char mnem[],char op[]);
- void insertSym(char label[],int LOCCTR);
- int findLabel(char *label);
- int searchOp(char *mnemonic);
- int getHex(char *mnemonic);
- void pass2();
- void parse3(char line[], char label[], char mnemonic[], char operand[]);
- void parse4(char line[], char hex[]);
- void getloca();
- int count = 0;
- typedef struct {
- char mn[5];
- char op[5];
- } OPCODE;
- OPCODE Ops[] = {
- { "ADD","0x18"}, {"AND","0x58"}, { "COMP","0x28"}, { "DIV","0x24"},{ "J","0x3C"},
- { "JEQ","0x30"}, { "JGT","0x34"},{ "JLT","0x38"},{ "JSUB","0x48"}, { "LDA","0x00"},
- { "LDCH","0x50"}, {"LDL","0x08"}, { "LDX","0x04"}, { "MUL","0x20"},{ "OR","0x44"},
- { "RD","0xD8"}, { "RSUB","0x4C"}, { "STA","0x0C"}, { "STCH","0x54"},{ "STL","0x14"},
- { "STX","0x10"}, { "SUB","0x1C"},{ "TD","0xE0"}, { "TIX","0x2C"}, { "WD","0xDC"}
- };
- struct symtab{
- char lab[7];
- int loca;
- }
- symtab1[500];
- int global = 0;
- int main()
- {
- char str[100]; //input buffer
- char a[20]; //Commmand buffer
- char b[20]; //1st parameter buffer
- char c[20]; //2nd parameter buffer
- int len; //length of input string
- int i = 0;
- printf("Welcome to my command interpreter! Please enter a command.\n");
- while(1){
- i = 0;
- memset(a, '\0', 20);
- memset(b, '\0', 20);
- memset(c, '\0', 20);
- fgets(str,100,stdin);
- len = strlen(str) - 1;
- if(str[len] == '\n'){
- str[len] = '\0';
- }
- i = prse(str, a, len, i);
- //checks for help command and outputs menu
- if (a[0] == 'h' && a[1] == 'e' && a[2] == 'l' && a[3] == 'p'){
- printf("\n");
- printf("load 'filename': Load the specified file.\n");
- printf("execute: Execute previous loaded program. \n");
- printf("debug: Execute in debug mode.\n");
- printf("dump 'start' 'end': Execute dump with start/end parameters in hex.\n");
- printf("help: Displays list of available commands.\n");
- printf("assemble 'filename': Assemble an SIC asl program into a load module.\n");
- printf("directory: List the files stored in the current directory.\n");
- printf("exit: Exit the command interpreter.\n");
- printf("clear: Clears the screen.\n");
- printf("\n");
- }
- //checks for load command and filename
- else if (a[0] == 'l' && a[1] == 'o' && a[2] == 'a' && a[3] == 'd'){
- if (alphanum(str[i])){
- i = prse(str, b, len, i);
- printf("You entered filename: %s\n", b);
- printf("This function is not yet implemented!\n");
- }
- else{
- printf("Please include the name of the file.\n");
- }
- }
- //checks for execute command
- else if (a[0] == 'e' && a[1] == 'x' && a[2] == 'e' && a[3] == 'c' && a[4] == 'u' && a[5] == 't' && a[6] == 'e'){
- printf("This function is not yet implemented!\n");
- }
- //checks for debug command
- else if (a[0] == 'd' && a[1] == 'e' && a[2] == 'b' && a[3] == 'u' && a[4] == 'g'){
- printf("This function is not yet implemented!\n");
- }
- //checks for dump command and start/end parameters
- else if (a[0] == 'd' && a[1] == 'u' && a[2] == 'm' && a[3] == 'p'){
- if (alphanum(str[i])){
- i = prse(str, b, len, i);
- printf("You entered start value: %s\n", b);
- if (alphanum(str[i])){
- i = prse(str, c, len, i);
- printf("You entered end value: %s\n", c);
- printf("This function is not yet implemented!\n");
- }
- else{
- printf("Please include an end parameter in hexadecimal.\n");
- }
- }
- else{
- printf("Please include a start/end parameter in hexadecimal.\n");
- }
- }
- //checks for assemble command and filename
- else if (a[0] == 'a' && a[1] == 's' && a[2] == 's' && a[3] == 'e' && a[4] == 'm' && a[5] == 'b' && a[6] == 'l' && a[7] == 'e'){
- if (alphanum(str[i])){
- i = prse(str, b, len, i);
- if( access( b, F_OK ) != -1 ) {
- printf("You entered filename: %s\n", b);
- assemble(b);
- }
- else {
- printf("File '%s' does not exist! Please re-enter your command with an existing file.\n", b);
- }
- }
- else{
- printf("Please include the name of the file.\n");
- }
- }
- //checks for directory command and displays items in current directory
- else if (a[0] == 'd' && a[1] == 'i' && a[2] == 'r' && a[3] == 'e' && a[4] == 'c' && a[5] == 't' && a[6] == 'o' && a[7] == 'r' && a[8] == 'y'){
- system("ls");
- }
- //checks for exit command and exits command interpreter
- else if (a[0] == 'e' && a[1] == 'x' && a[2] == 'i' && a[3] == 't'){
- printf("Goodbye!\n");
- exit(EXIT_SUCCESS);
- }
- //checks for clear command and clears screen
- else if (a[0] == 'c' && a[1] == 'l' && a[2] == 'e' && a[3] == 'a' && a[4] == 'r'){
- system("clear");
- }
- //if no valid input, returns error message
- else{
- printf("Invalid command! Please type 'help' for list of commands.\n");
- }
- }
- return 0;
- }
- int prse(char in[], char *a, int size, int i){
- int j = 0;
- for(i; i < size; i++){
- a[j] = in[i];
- if(space(in[i])){
- a[j] = '\0';
- while(space(in[i])){
- i++;
- }
- return i;
- }
- j++;
- }
- }
- int space(int y){
- if (y == 32){
- return 1;
- }
- else{
- return 0;
- }
- }
- int alphanum(int x){
- if (x <= 122 && x >= 48){
- return 1;
- }
- else{
- return 0;
- }
- }
- void assemble(char str[]){
- pass1(str);
- pass2();
- }
- void pass1(char str[]){
- int c;
- FILE *outfile;
- FILE *infile;
- char line[1000];
- char *backup = NULL;
- size_t len= 0;
- char read;
- char label[15];
- char mnemonic[15];
- char operand[15];
- int holder;
- int flag;
- unsigned LOCCTR = 0x00;
- unsigned startingAddress;
- infile = fopen(str, "r");
- outfile = fopen("intermediate.txt", "w");
- if(fgets (line, 1000, infile) != NULL){
- backup = (char*)malloc(strlen(line)+1);
- memset(backup, 0, strlen(line) + 1);
- strncpy(backup, line, strlen(line));
- parse2(line, label, mnemonic, operand);
- if(strncmp(mnemonic, "START", 15) == 0 && strlen(mnemonic) == 5){
- startingAddress = strtoul(operand, NULL, 16);
- LOCCTR = startingAddress;
- fprintf(outfile, "// %s", backup);
- fprintf(outfile, "%s ", mnemonic);
- fprintf(outfile, "%04X", LOCCTR);
- /////////////////////////////////////////////
- flag = getHex(mnemonic);
- if (flag >= 0){
- fprintf(outfile, " %s", Ops[flag].op);
- }
- else
- {
- fprintf(outfile, " XXXX");
- }
- /////////////////////////////////////////////
- if(strncmp(mnemonic, "RSUB", 15) == 0 && strlen(mnemonic) == 4){
- fprintf(outfile, " XXXX\n");
- }
- else{
- fprintf(outfile, " %s\n", operand);
- }
- }
- else{
- startingAddress = 0;
- LOCCTR = startingAddress;
- }
- if(backup){
- free(backup);
- }
- fgets (line, 1000, infile);
- backup = (char*)malloc(strlen(line)+1);
- memset(backup, 0, strlen(line) + 1);
- strncpy(backup, line, strlen(line));
- parse2(line, label, mnemonic, operand);
- while (strncmp(mnemonic, "END", 15) != 0){
- if(line[0] != '.'){
- if(isalpha(label[0])){
- holder = findLabel(label);
- if(holder == 0){
- insertSym(label, LOCCTR);
- global++;
- }
- else{
- //set error flag(duplicate symbol)
- }
- }
- if(searchOp(mnemonic) == 1){
- LOCCTR = LOCCTR + 3;
- }
- else if(strncmp(mnemonic, "WORD", 15) == 0 && strlen(mnemonic) == 4){
- LOCCTR = LOCCTR + 3;
- }
- else if(strncmp(mnemonic, "RESW", 15) == 0 && strlen(mnemonic) == 4){
- LOCCTR = LOCCTR + (3 * strtoul(operand, NULL, 16));
- }
- else if(strncmp(mnemonic, "RESB", 15) == 0 && strlen(mnemonic) == 4){
- LOCCTR = LOCCTR + strtoul(operand, NULL, 10);
- }
- else if(strncmp(mnemonic, "BYTE", 15) == 0 && strlen(mnemonic) == 4){
- if(operand[0] == 'C'){
- LOCCTR = LOCCTR + (strlen(operand)-3);
- }
- else if(operand[0] == 'X'){
- LOCCTR = LOCCTR + (strlen(operand)-3)/2;
- }
- }
- else{
- //set error flag
- }
- }
- fprintf(outfile, "// %s", backup);
- fprintf(outfile, "%s ", mnemonic);
- fprintf(outfile, "%04X", LOCCTR);
- //////////////////////////////////////////////////
- flag = getHex(mnemonic);
- if (flag >= 0){
- fprintf(outfile, " %s", Ops[flag].op);
- }
- else
- {
- fprintf(outfile, " XXXX");
- }
- //////////////////////////////////////////////////
- if(strncmp(mnemonic, "RSUB", 15) == 0 && strlen(mnemonic) == 4){
- fprintf(outfile, " XXXX\n");
- }
- else{
- fprintf(outfile, " %s\n", operand);
- }
- if(backup){
- free(backup);
- }
- fgets (line, 1000, infile);
- backup = (char*)malloc(strlen(line)+1);
- memset(backup, 0, strlen(line) + 1);
- strncpy(backup, line, strlen(line));
- parse2(line, label, mnemonic, operand);
- }
- fprintf(outfile, "// %s\n", backup);
- fprintf(outfile, "%s ", mnemonic);
- fprintf(outfile, "%04X", LOCCTR);
- ////////////////////////////////////////////////
- flag = getHex(mnemonic);
- if (flag >= 0){
- fprintf(outfile, " %s", Ops[flag].op);
- }
- else
- {
- fprintf(outfile, " XXXX");
- }
- ////////////////////////////////////////////////
- if(strncmp(mnemonic, "RSUB", 15) == 0 && strlen(mnemonic) == 4){
- fprintf(outfile, " XXXX\n");
- }
- else{
- fprintf(outfile, " %s\n", operand);
- }
- fprintf(outfile, "%s", backup);
- if(backup){
- free(backup);
- }
- //save (LOCCTR - starting address_ as program length)
- }
- fclose(infile);
- fclose(outfile);
- int i = 0;
- while(symtab1[i].loca != 0){
- printf("%s", symtab1[i].lab);
- printf(" %X\n", symtab1[i].loca);
- i++;
- }
- }
- void parse2(char line[], char lab[], char mnem[],char op[]){
- char * pch;
- char * ilab;
- char * imnem;
- char * iop;
- memset(lab, 0, 15);
- memset(mnem, 0, 15);
- memset(op, 0, 15);
- //not a comment
- if(line[0] != '.'){
- //3 relevant items
- if(!isspace(line[0])){
- ilab = strtok (line, WHITESPACE);
- strncpy(lab, ilab, 14);
- imnem = strtok (NULL, WHITESPACE);
- strncpy(mnem, imnem, 14);
- iop = strtok (NULL, WHITESPACE);
- strncpy(op, iop, 14);
- }
- //2 relevant items
- else{
- imnem = strtok (line, WHITESPACE);
- strncpy(mnem, imnem, 14);
- iop = strtok (NULL, WHITESPACE);
- strncpy(op, iop, 14);
- }
- }
- }
- //parses comment line in intermediate line
- void parse3(char line[], char label[], char mnemonic[], char operand[]){
- char * ilab;
- char * imnem;
- char * iop;
- memset(label, 0, 15);
- memset(mnemonic, 0, 15);
- memset(operand, 0, 15);
- imnem = strtok (line, WHITESPACE);
- ilab = strtok (NULL, WHITESPACE);
- imnem = strtok (NULL, WHITESPACE);
- iop = strtok (NULL, WHITESPACE);
- strncpy(mnemonic, imnem, 14);
- }
- //parses everyother line that isn't commented
- void parse4(char line[], char hex[]){
- char * ihex;
- char x[2];
- memset(hex, 0, 15);
- ihex = strtok (line, WHITESPACE);
- ihex = strtok (NULL, WHITESPACE);
- x[0] = ihex[2];
- x[1] = ihex[3];
- puts(ihex);
- puts(x);
- strncpy(hex, x, 14);
- }
- int findLabel(char *label){
- int i = 0;
- for(i = 0; i < global; i++){
- if(strcmp(symtab1[i].lab, label) == 0){
- return 1;
- }
- return 0;
- }
- }
- void insertSym(char label[],int LOCCTR){
- //puts(label);
- strncpy(symtab1[count].lab, label,14);
- symtab1[count].loca = LOCCTR;
- count++;
- }
- int searchOp(char *mnemonic){
- int i = 0;
- while(i < sizeof(Ops)){
- if(strcmp(Ops[i].mn, mnemonic) == 0){
- return 1;
- }
- i++;
- }
- return 0;
- }
- int getHex(char *mnemonic){
- int i = 0;
- while(i < sizeof(Ops)){
- if(strcmp(Ops[i].mn, mnemonic) == 0){
- return i;
- }
- i++;
- }
- return -1;
- }
- void getloca(){
- }
- void pass2(){
- FILE *infile;
- FILE *outlist;
- FILE *outobj;
- char line[1000];
- char label[15];
- char mnemonic[15];
- char operand[15];
- char hex[15];
- infile = fopen("intermediate.txt", "r");
- outlist = fopen("listing.txt", "w");
- outobj = fopen("object.txt", "w");
- int i = 0;
- //read first input line from int file
- if(fgets (line, 1000, infile) != NULL){
- parse3(line, label, mnemonic, operand);
- }
- fgets (line, 1000, infile);
- parse4(line, hex);
- if(strncmp(mnemonic, "START", 15) == 0 && strlen(mnemonic) == 5){
- //write listing line
- fprintf(outlist, "%s1000", hex);
- //read next input line
- }
- //write header record to object program
- //initialize first Text record
- /*while(OPCODE != 'END'){
- if (not a comment line){
- //seatch OPTAB for OPCODE
- if(found){
- if(there is a symbol in OPERAND field){
- //search SYMTAB for OPERAND
- if(found){
- //store symbol value as operand address
- }
- else{
- //store 0 as operand address
- //set error flag (undefined symbol)
- }
- }
- else{
- //store 0 as operand address
- }
- //assemble the object code instruction
- }
- else if{OPCODE = 'BYTE' or 'WORD'}{
- //convert constant to object code
- }
- if(//object code will not fit into the current Text record){
- //write Text record to object program
- //initialize new Text record
- }
- //add object code to Text record
- }
- //write listing line
- //read next input line
- }
- //write last Text record to object program
- //write End record to object program
- //write last listing line
- */ fclose(infile);
- fclose(outlist);
- fclose(outobj);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement