90 likes | 215 Views
Computer Science 210 Computer Organization. Building an Assembler Part I: Character I/O. Designing an Assembler. Will need several component modules Character I/O : handles text files, messages Scanner : extracts symbols from a line of text
E N D
Computer Science 210Computer Organization Building an Assembler Part I: Character I/O
Designing an Assembler • Will need several component modules • Character I/O: handles text files, messages • Scanner: extracts symbols from a line of text • Parser: performs the two passes (fixing label addresses and generating code for instructions)
Example: An Assembler Line stream Text file CharacterIO Scanner Tools Token stream Symbol table Parser Opcode table Source program listing, error messages (file and/or terminal) Sym file Object file CharacterIO – handles files, text Scanner – recognizes numbers, symbols, assembler directives Parser – handles syntax checking, code generation
Responsibilities of chario • Tracks the total number of errors • Prints error messages • Prints a program listing (with line numbers) • Returns the next line of code (skipping leading blank lines and program comments) • Detects and returns the end of input file
The Interface for chario // File: chario.h void initChario(FILE* infile, FILE* outfile); // Returns NULL if the end of file has been reached. // Otherwise, returns the next line of code, after skipping any // leading blank lines or comments. char* nextCodeLine(); int getTotalErrors(); void putError(char* errorMessage); void reportErrors();
Testing chario void processFile(FILE *infile, FILE *outfile){ initChario(infile, outfile); int lineNumber = 0; char* line; while (line = nextCodeLine()){ lineNumber++; printf("%d %s", lineNumber, line); } } Use the main function from the example in the file lecture
Implementing chario // File: chario.c #include <stdio.h> #include <string.h> #include "chario.h" #define MAX_COLUMNS 81 #define FIELD_WIDTH 4 #define TRUE 1 static char inputLine[MAX_COLUMNS]; static intinputLineNumber; static inttotalErrors; static FILE* infile; static FILE* outfile; void initChario(FILE* inf, FILE* outf){ inputLineNumber = 0; totalErrors = 0; infile = inf; outfile = outf; } static variables remain in use for the lifetime of the program, so storage is allocated at load time The scope of these variables is global, so they can be seen in all the functions
Implementing nextCodeLine // Returns NULL if the end of file has been reached // Otherwise, returns the next line of code, after skipping any // leading blank lines or comments. char* nextCodeLine(){ while (TRUE){ char* result = fgets(inputLine, MAX_COLUMNS, infile); if (result == NULL) return NULL; inputLineNumber++; fprintf(outfile, "%*d> ", FIELD_WIDTH, inputLineNumber); fputs(inputLine, outfile); int index = skipBlanks(inputLine); if (inputLine[index] != ';' && inputLine[index] != '\n' && inputLine[index] != 0) return inputLine; } return NULL; }
Create a Library of Functions • Place the function prototypes for a module in a header file (.h extension) • Place the completed function definitions for that module in an implementation file (.c extension) • Compile the c. file, link it to a test driver, test it, and store it away • Example: a mylib library with factorial and gcd