Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Chapter 11 Implementing an Assembler and a Linker Using C++ and Java.
Advertisements

Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Chapter 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
Chapter 7 Assembly Language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 7-2 Human-Readable Machine Language.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language.
Chapter 7 Assembly Language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 7-2 Human-Readable Machine Language.
The assembler is the system program that translate source code written in assembly language to object code( Machine Language) and other information for.
Structure of a C program
Computer Science 210 Computer Organization Building an Assembler Part II: Managing a Symbol Table.
Computer Science 210 Computer Organization Building an Assembler Part III: The First Pass and Scope Analysis.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
A Simple Two-Pass Assembler
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Attribute Grammar Examples and Symbol Tables Compiler Design Lecture (02/23/98) Computer Science Rensselaer Polytechnic.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computer Science 101 Introduction to Programming.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Computer Science 210 Computer Organization Building an Assembler Part IV: The Second Pass, Syntax Analysis, and Code Generation.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
©SoftMoore ConsultingSlide 1 Lexical Analysis (a.k.a. Scanning)
CS501 Advanced Computer Architecture Lecture 29 Dr.Noor Muhammad Sheikh.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Lecture 9 Symbol Table and Attributed Grammars
C++ LANGUAGE MULTIPLE CHOICE QUESTION SET-3
Computer Science 210 Computer Organization
Lexical Analysis (a.k.a. Scanning)
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Computer Science 210 Computer Organization
Chapter 7 & 9.2 Assembly Language
Computer Science 210 Computer Organization
A Simple Syntax-Directed Translator
Constructing Precedence Table
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Computer Science 210 Computer Organization
Introduction to C Programming
Computer Science 210 Computer Organization
Chapter 7 LC-2 Assembly Language.
Computer Science 210 Computer Organization
Starting JavaProgramming
Assembler CASE Tool.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Govt. Polytechnic,Dhangar
Chapter 7 LC-2 Assembly Language.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chapter 7 Assembly Language
A Simple Two-Pass Assembler
Chapter 7 Assembly Language
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Assemblers CSCI/CMPE 3334 David Egle.
Functions continued.
CSC 221: Introduction to Programming Fall 2018
Location Counter (LC) = 0 while line of code <> END if ORG
CMPE 152: Compiler Design September 3 Class Meeting
Chapter 7 Assembly Language
Presentation transcript:

Computer Science 210 Computer Organization Building an Assembler Part III: The First Pass and Scope Analysis

The First Pass Text file Line stream Token stream Tools Opcode table Text file Line stream CharacterIO Scanner Token stream Symbol table First Pass Source program listing, error messages (file and/or terminal) Sym file

What the First Pass Does 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

Complications The default start address is x3000, but .ORIG can override this .BLKW and .STRINGz might specify an array of many cells, so we can’t just increment the address counter after each line of code Must treat both as special cases during the first pass

The Token Set The token set of a language is its vocabulary, or set of words and their types Includes keywords, opcode symbols, labels, register names, integer literals, commas, directive symbols, etc.

LC-3 Tokens in token.h int TC_ADD = 0; int TC_AND = 1; int TC_BR = 2; int TC_NOT = 3; etc. Define a bunch of symbols to be returned by the scanner Will examine these to make choices during scope analysis in pass one and syntax analysis in pass two

LC-3 Tokens in token.h typedef enum { TC_ADD, TC_AND, TC_BLKW, TC_BR, TC_BRZ, TC_BRN, TC_BRP, TC_BRNZ, TC_BRZP, TC_BRNZP, TC_BRNP, TC_COMMA, TC_END, TC_ERROR, TC_FILL, TC_GETC, TC_HALT, TC_IN, TC_INT, TC_JMP, TC_JSR, TC_JSRR, TC_LABEL, TC_LD, TC_LDI, TC_LDR, TC_LEA, TC_NEWLINE, TC_NOT, TC_ORIG, TC_OUT, TC_PUTS, TC_REG, TC_RET, TC_RTI, TC_ST, TC_STI, TC_STR, TC_STRING_LIT, TC_STRINGZ, TC_TRAP } tokenType; // A token has a type, a source string (in uppercase), // a binary string, and an int value. typedef struct token{ tokenType type; char* source; char* binary; int intValue; } token; Use a C enum type to introduce a set of symbolic values of a new type

The scanner Interface void initScanner(FILE* infile, FILE* outfile); // Must be run to get the first instruction, and after a token // is a newline. // Can be run after the first token is scanned to advance to // the next instruction on the assembler's first pass. // Returns 0 if the end of file has been reached. int nextInstruction(); // Advances to the next token, if there is one, in the // instruction, and returns the token just scanned. // Returns the end of line token when the instruction has // been completely scanned. // Ignores program comments. token nextToken();

Setting Up the Scanner static token opcodeTable[MAX_OPCODES]; static token registerTable[MAX_REGISTERS]; static token directiveTable[MAX_DIRECTIVES]; static char* codeLine; static int codeLineNumber; static int codeLineColumn; void initScanner(FILE* infile, FILE* outfile){ initOpcodeTable(); initRegisterTable(); initDirectiveTable(); codeLineNumber = 0; codeLineColumn = 0; initChario(infile, outfile); } Maintains lookup tables of opcodes, registers, and directives

The Interface for Token Tables // Initializes token, inserts it at index, and increments index. // Assumes that index < size of table array. void initToken(tokenType t, char* src, char* bin, int intValue, int *index, token table[]); // Returns the index of the token associated with src or -1 // if it is not there. int findToken(char* src, token table[], int length); // Prints the table in three columns void printTokenTable(token table[], int length); Unlike the symbol tables, these tables are of fixed size

Initializing a Token Table // The test driver for a symbol table. void initRegisterTable(){ int index = 0; initToken(TC_REG, "R0", "000", 0, &index, registerTable); initToken(TC_REG, "R1", "001", 0, &index, registerTable); initToken(TC_REG, "R2", "010", 0, &index, registerTable); initToken(TC_REG, "R3", "011", 0, &index, registerTable); initToken(TC_REG, "R4", "100", 0, &index, registerTable); initToken(TC_REG, "R5", "101", 0, &index, registerTable); initToken(TC_REG, "R6", "110", 0, &index, registerTable); initToken(TC_REG, "R7", "111", 0, &index, registerTable); } Other modules can access a register’s 3-bit address in its token object

First Pass Strategy Handle a possible .ORIG first Then get instructions, look at the first token, and if it’s a label, take some action Look ahead for a .BLKW and handle that case Repeat the last two steps until .END is reached or there are no more instructions

Trying Out the First Pass When you have completed and thoroughly tested your symbol table module, copy it to the firstpass directory and run make Then, run ./testfirstpass with your favorite assembly language program Be sure to try it with a program that has scope errors as well!

Grammar and Syntax Analysis For Monday Grammar and Syntax Analysis