Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 Computer Organization

Similar presentations


Presentation on theme: "Computer Science 210 Computer Organization"— Presentation transcript:

1 Computer Science 210 Computer 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 CharacterIO – handles files, text
Text file Line stream 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 int inputLineNumber; static int totalErrors; 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; }

9 Using a Symbol Table: Pass 1
On the first pass, the assembler Enters each distinct label into a symbol table If a duplicate label is encountered at the beginning of an instruction, ERROR! The instruction’s address # is also entered with the label

10 Using a Symbol Table: Pass 2
On the second pass, the assembler Looks up a label when it’s encountered as an operand If the label is not found in the symbol table, ERROR! Otherwise, the label’s address is used to complete the binary code for the instruction

11 The Symbol Table Manager
Consists of two files, symboltable.h and symboltable.c The header specifies the interface (the function headers) The implementation file defines the data structure types, declares the variable for the table, and implements the functions

12 /* Author: Ken Lambert File: symboltable.h Interface for the symbol table module. The application can have only one symbol table. */ // Creates an empty table. void initTable(); // Attempts to enter a symbol and its address. // If the symbol is already in the table, simply returns NULL. // Otherwise, enters the symbol and its address and returns the // symbol. char* enterSymbol(char* symbol, int address); // Looks up the symbol in the table. // If the symbol is in the table, returns its address. // Otherwise, returns -1. int findSymbol(char* symbol); // Prints the contents of the table in two columns. void printSymbolTable();

13 /* Author: Ken Lambert File: symboltable.c Implementation for the symbol table module. */ // Includes of libraries go here. // Type definitions for the symbolEntry and node types go here. // The single symbol table. static nodePtr symbolTable; // The function implementations go here. // They include all of the functions in the interface, as well as // a getNode function.

14 // The test driver for a symbol table.
#include <string.h> #include <stdio.h> #include "symboltable.h" int main(){ initSymbolTable(); printf("Printing empty table:\n"); printSymbolTable(); char* enterResult = enterSymbol("SECOND", 45); printf("Expect SECOND: %s\n", enterResult); printf("Printing table with one entry:\n"); enterResult = enterSymbol("FIRST", 30); printf("Expect FIRST: %s\n", enterResult); printf("Printing table with two entries:\n"); int findResult = findSymbol("SECOND"); printf("Expect 45: %d\n", findResult); findResult = findSymbol("THIRD"); printf("Expect -1: %d\n", findResult); enterResult = enterSymbol("SECOND", 56); printf("Expect 0: %p\n", enterResult); }

15 The First Pass of the Assembler
For Friday The First Pass of the Assembler


Download ppt "Computer Science 210 Computer Organization"

Similar presentations


Ads by Google