PassOne calls isDirective(), isComment(), isLabel()
and isOpcode() to verify value of tokens encountered. When valid labels are encountered, both
the name of the symbol(token) and its address are stored in a symbol table. Location counter is increased
by calling addByte(), addWord() and addStr() and treated as the address
of the symbol when a symbol is encountered.
searchST() is called to check symbol duplication before the symbol is inserted into symbol table. If the symbol is first time encountered, makeNode() is called to create a new SymbolNode that contains this symbol name and address, and insertSymbol() is used to add this new node into the Symbol table.
#include <stdio.h> - Standard C header file #include <string.h> #include <stdlib.h> #include "pass.h" - contains static records of directives, operators, special symbols, Table structure, and all associated typedefs #include "table.h" - contains function declarations for functions used to process tables
Parameter: char *str Compare str with MIPS AL directive by using strcmp if str is a directive then return an integer to indicates what kind of directive it is else return 0
Parameter: char *str if str[0] = "#" then return true else return false
Parameter: char *str
ErrMes *err
if str is in opcode list
then write error message into err
return false
if there are special characters in str
then write error message into err
return false
if str is in symbol table list
then write error message into err
return false
return true
Parameter: char *str
ErrMes *err
if str is in opcode list
then return an integer to indicate what kind of opcode it is
else write error into err
return 0
Parameter: int lc, n return lc + n
Parameter: int lc, n return lc + n * 4
Parameter: int lc
char *str
return lc + strlen(str) + 1
Parameter: char *name
int address
SymbolTable newNode
allocate memory space for newNode and newNode->name
strcpy(newNode->name, name)
newNode->address = address
newNode->next = NULL
return newNode
SymbolTable sbt = NULL return sbt
Parameter: SymbolTabel *sbt
char *name
int address
call makeNode(name, address) to get new SymbolTabel newNode
call searchST(sbt, name) to check duplication
if name is not in sbt
then insert newNode into the front of sbt
parameter SymbolTabel sbt
char *name
search name in sbt by using strcmp
if name is not in sbt
then return false
else return true
Parameter FILE *fp
read first input line and get toklist
initialize two location counters(one for data and one for instructions)
while not EOF and first token is not ".data" or ".text" do
if the first token is a comment
then read next input line
else set error flag
if not EOF and directive = ".data"
then if the token follow ".data" is not comment or NULL
then set error flag
else read next input line and get the toklist
while not EOF and the token is not ".text" do
if this is a legel label
then save it and location counter in symbol table
if next directive is ".word" or ".equ"
then add location counter by using addWord()
else if it is ".byte"
then add location counter by using addByte()
else if it is ".asciiz"
then add location counter by using addStr()
else set error flag
read next input line and get toklist
if not EOF and directive = ".text"
then if the token follow ".text" is not comment or NULL
then set error flag
else read next input line and get the toklist
while not EOF and the line does not begin with "__start" do
if the line is not a comment
then set error flag
read next input line and get toklist
while not EOF
if there is a legel label
then save it and location counter into symbol tabel
else set error flag
if there is an opcode
then add location counter by 4