1 / 9

Computer Science 210 Computer Organization

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

amable
Download Presentation

Computer Science 210 Computer Organization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 210Computer Organization Building an Assembler Part I: Character I/O

  2. 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)

  3. 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

  4. 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

  5. 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();

  6. 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

  7. 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

  8. 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; }

  9. 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

More Related