Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Chapter 11 Implementing an Assembler and a Linker Using C++ and Java.
Intermediate Code Generation
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Lecture 2 Introduction to C Programming
Chapter 10.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
Computer Science 210 Computer Organization Introduction to C.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
A Simple Two-Pass Assembler
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Computer Science 210 Computer Organization More on Assembler.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Computer Science 210 Computer Organization
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Text Input/Output Objectives
ECE Application Programming
Chapter 7 Text Input/Output Objectives
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Text Input/Output Objectives
Software Development Handing Errors and Creating Documentation
Overview of Compilation The Compiler BACK End
-by Nisarg Vasavada (Compiled*)
Computer Science 210 Computer Organization
CS 536 / Fall 2017 Introduction to programming languages and compilers
Programmazione I a.a. 2017/2018.
Computer Science 210 Computer Organization
Stack Data Structure, Reverse Polish Notation, Homework 7
Assembler Design Options
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 LC-2 Assembly Language.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Computer Science 210 Computer Organization
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Overview of Compilation The Compiler BACK End
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 LC-2 Assembly Language.
Compiler 薛智文 TH 6 7 8, DTH Spring.
A Simple Two-Pass Assembler
Line at a time I/O with fgets() and fputs()
Exercise Arrays.
Characters and Strings Functions
Compiler 薛智文 M 2 3 4, DTH Spring.
Classes and Objects Systems Programming.
Presentation transcript:

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 Parser: performs the two passes (fixing label addresses and generating code for instructions)

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

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

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

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

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

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

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

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

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

/* 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();

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

// 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); }

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