computer science 210 computer organization n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Computer Science 210 Computer Organization PowerPoint Presentation
Download Presentation
Computer Science 210 Computer Organization

Loading in 2 Seconds...

play fullscreen
1 / 9

Computer Science 210 Computer Organization - PowerPoint PPT Presentation


  • 101 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Computer Science 210 Computer Organization' - amable


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
computer science 210 computer organization

Computer Science 210Computer Organization

Building an Assembler

Part I: Character I/O

designing an assembler
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
Example: An Assembler

Line stream

Text file

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

static inttotalErrors;

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

}

return NULL;

}

create a library of functions
Create a Library of Functions
  • Place the function prototypes for a module in a header file (.h extension)
  • Place the completed function definitions for that module in an implementation file (.c extension)
  • Compile the c. file, link it to a test driver, test it, and store it away
  • Example: a mylib library with factorial and gcd