computer science 210 computer organization building an assembler part ii: managing a symbol table

7
Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

Upload: hortense-anthony

Post on 21-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

Computer Science 210Computer Organization

Building an Assembler

Part II: Managing a Symbol Table

Page 2: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

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

Page 3: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

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

Page 4: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

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

Page 5: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

/*Author: Ken LambertFile: symboltable.hInterface 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();

Page 6: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

/*Author: Ken LambertFile: symboltable.cImplementation 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.

Page 7: Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table

// 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"); printSymbolTable(); enterResult = enterSymbol("FIRST", 30); printf("Expect FIRST: %s\n", enterResult); printf("Printing table with two entries:\n"); printSymbolTable(); 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);}