Chapter 6: Assembler

Similar documents
Assembler. Building a Modern Computer From First Principles.

Assembler Human Thought

IWKS 3300: NAND to Tetris Spring John K. Bennett. Assembler

Chapter 6: The Assembler The Assembler Hack Assembly-to-Binary Translation Specification

Chapter 11: Compiler II: Code Generation

Chapter 10: Compiler I: Syntax Analysis

Compiler I: Sytnax Analysis

Machine (Assembly) Language

Introduction: Hello, World Below

Chapter 8: Virtual Machine II: Program Control

Machine (Assembly) Language Human Thought

Assembler. Building a Modern Computer From First Principles.

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

IWKS 2300/5300 Fall John K. Bennett. Machine Language

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Elements of Computing Systems, Nisan & Schocken, MIT Press. Boolean Arithmetic

Chapter 9: High Level Language

Computer Architecture

STEVEN R. BAGLEY THE ASSEMBLER

Introduction: From Nand to Tetris

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Compiler I: Syntax Analysis

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Compiler II: Code Generation Human Thought

Boolean Arithmetic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 2

Sequential Logic. Building a Modern Computer From First Principles.

Computer Architecture

Virtual Machine (Part II)

Motivation. Compiler. Our ultimate goal: Hack code. Jack code (example) Translate high-level programs into executable code. return; } } return

EP1200 Introduktion till datorsystemteknik Tentamen tisdagen den 3 juni 2014, till 18.00

Project 6: Assembler

10. The Compiler II: Code Generation 1

4. Computer Architecture 1

Boolean Arithmetic. Building a Modern Computer From First Principles.

Compiler Code Generation COMP360

CSE 582 Autumn 2002 Exam 11/26/02

Computer and Communications Fundamentals. How Computers Work. The Art of (low level) Programming. How Computers Work: Lecture 2 1

CS E1: Section 1: Counting in Binary

//converts a string to a non-negative number string2int(string s){

Computer Architecture

EP1200 Introduction to Computing Systems Engineering. Computer Architecture

Virtual Machine (Part II)

Chapter 0: The grand Tour The Grand Tour 1. The true voyage of discovery consists not of going to new places, but of having a new pair of eyes.

Boolean Logic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 1

Computer Architecture

4 Programing Paradigms in 45 Minutes

4 Programing Paradigms in 45 Minutes

CSE P 501 Exam 8/5/04

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

7. The Virtual Machine II: Flow Control 1

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

CS311 Lecture: The Architecture of a Simple Computer

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

Computational Optimization ISE 407. Lecture1. Dr. Ted Ralphs

Theory and Compiling COMP360

Arithmetic Logic Unit (ALU)

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Printing this Slide Show

CS 6353 Compiler Construction Project Assignments

CSE 582 Autumn 2002 Exam Sample Solution

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Intermediate Code Generation

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS 6353 Compiler Construction Project Assignments

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Internal Data Representation

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compiling Techniques

Recap A Search Optimality of A. Search: A. CPSC 322 Search 5. Textbook 3.6. Search: A CPSC 322 Search 5, Slide 1

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

VHDL simulation and synthesis

CMSC 350: COMPILER DESIGN

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Building a Virtual Computer

Compilers and Code Optimization EDOARDO FUSELLA

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

A Simple Syntax-Directed Translator

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMPILER DESIGN. For COMPUTER SCIENCE

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Principles of Compiler Design

Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats. CS61C Negative Numbers and Logical Operations R I J.

Real instruction set architectures. Part 2: a representative sample

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Appendix A: Hardware Description Language (HDL) 1

Lecture 03 Bits, Bytes and Data Types

Computing Inside The Parser Syntax-Directed Translation. Comp 412

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Software Engineering using Formal Methods

Basic Assembly SYSC-3006

Overview of Compiler. A. Introduction

Basic Concepts COE 205. Computer Organization and Assembly Language Dr. Aiman El-Maleh

Undergraduate Compilers in a Day

MODEL ANSWERS COMP36512, May 2016

Transcription:

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 www.idc.ac.il/tecs Chapter 6: Assembler Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation contains lecture materials that accompany the textbook The Elements of Computing Systems by Noam Nisan & Shimon Schocken, MIT Press, 2005. The book web site, www.idc.ac.il/tecs, features 13 such presentations, one for each book chapter. Each presentation is designed to support about 3 hours of classroom or self-study instruction. You are welcome to use or edit this presentation for instructional and non-commercial purposes. If you use our materials, we will appreciate it if you will include in them a reference to the book s web site. And, if you have any comments, you can reach us at tecs.ta@gmail.com Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 1

Where we are at: Human Thought Abstract design Chapters 9, 12 abstract interface H.L. Language & Operating Sys. Compiler Chapters 10-11 abstract interface Virtual Machine Software hierarchy VM Translator Chapters 7-8 abstract interface Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture Chapters 4-5 Hardware hierarchy abstract interface Hardware Platform Gate Logic Chapters 1-3 abstract interface Chips & Logic Gates Electrical Engineering Physics Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 2

Why care about assemblers? Because Assemblers employ some nifty programming tricks Assemblers are the first rung up the software hierarchy ladder An assembler is a translator of a simple language Writing an assembler is a good introduction for writing compilers. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 3

Program translation Source code Target code Translator The program translation challenge Parse the source program, using the syntax rules of the source language Re-express the program s semantics using the syntax rules of the target language Assembler = simple translator Translates each assembly command into one or more machine instructions Handles symbols (i, sum, LOOP, END, ). Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 4

Symbol resolution In low level languages, symbols are used to code: Variable names Destinations of goto commands (labels) Special memory locations The assembly process: First pass: construct a symbol table Second pass: translate the program, using the symbol table for symbols resolution. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 5

Perspective This example is based on some simplifying assumptions: Largest possible program is 1024 commands long Each command fits into one memory location Each variable fits into one memory location Every one of these assumptions can be relaxed easily. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 6

The Hack assembly language Assembly program (Prog.asm) // Adds 1 +... + 100 M=1 // i=1 @sum M=0 // sum=0 (LOOP) D=M // D=i @100 D=D-A // D=i-100 @END D;JGT // if (i-100)>0 goto END D=M // D=i @sum M=D+M // sum=sum+i M=M+1 // i=i+1 @LOOP 0;JMP // goto LOOP (END) @END 0;JMP // infinite loop Assembly program = a stream of text lines, each being one of the following things: Instruction: A-instruction or C-instruction Symbol declaration: (symbol) Comment or white space: // comment. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 7

Handling A-instructions Symbolic: @value // Where value is either a non-negative decimal number // or a symbol referring to such number. value (v = 0 or 1) Binary: 0 v v v v v v v v v v v v v v v Translation to binary: If value is a number: simple If value is a symbol: later. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 8

Handling C-instruction Symbolic: dest=comp;jump // Either the dest or jump fields may be empty. // If dest is empty, the "=" is ommitted; // If jump is empty, the ";" is omitted. comp dest jump Binary: 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 Translation to binary: simple! Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 9

The overall assembly logic Assembly program (Prog.asm) // Adds 1 +... + 100 M=1 // i=1 @sum M=0 // sum=0 (LOOP) D=M // D=i @100 D=D-A // D=i-100 @END D;JGT // if (i-100)>0 goto END D=M // D=i @sum M=D+M // sum=sum+i M=M+1 // i=i+1 @LOOP 0;JMP // goto LOOP (END) @END 0;JMP // infinite loop For each (real) command Parse the command, i.e. break it into its constituent fields Replace each symbolic reference (if any) with the corresponding memory address (a binary number) For each field, generate the corresponding binary code Assemble the binary codes into a complete machine instruction. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 10

Symbols handling (in the Hack language) Program example Predefined symbols: (don t appear in this example) // Adds 1 +... + 100 M=1 // i=1 @sum M=0 // sum=0 (LOOP) D=M // D=i @100 D=D-A // D=i-100 @END D;JGT // if (i-100)>0 goto END D=M // D=i @sum M=D+M // sum=sum+i M=M+1 // i=i+1 @LOOP 0;JMP // goto LOOP (END) @END 0;JMP // infinite loop Label symbols: The pseudo-command (Xxx) declares that the user-defined symbol Xxx should refer to the memory location holding the next command in the program Variable symbols: Any symbol Xxx appearing in an assembly program that is not predefined and is not defined elsewhere using the (Xxx) pseudo command is treated as a variable Design decision: variables are mapped to consecutive memory locations starting at RAM address 16. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 11

Example Assembly code (Prog.asm) // Adds 1 +... + 100 M=1 // i=1 @sum M=0 // sum=0 (LOOP) D=M // D=i Assembler @100 D=D-A // D=i-100 @END D;JGT // if (i-100)>0 goto END D=M // D=i @sum M=D+M // sum=sum+i M=M+1 // i=i+1 @LOOP 0;JMP // goto LOOP (END) @END 0;JMP // infinite loop Binary code (Prog.hack) (this line should be erased) 0000 0000 0001 0000 1110 1111 1100 1000 0000 0000 0001 0001 1110 1010 1000 1000 (this line should be erased) 0000 0000 0001 0000 1111 1100 0001 0000 0000 0000 0110 0100 1110 0100 1101 0000 0000 0000 0001 0010 1110 0011 0000 0001 0000 0000 0001 0000 1111 1100 0001 0000 0000 0000 0001 0001 1111 0000 1000 1000 0000 0000 0001 0000 1111 1101 1100 1000 0000 0000 0000 0100 1110 1010 1000 0111 (this line should be erased) 0000 0000 0001 0010 1110 1010 1000 0111 Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 12

Proposed implementation An assembler program can be implemented as follows. Software modules: Parser: Unpacks each command into its underlying fields Code: Translates each field into its corresponding binary value SymbolTable: Manages the symbol table Main: Initializes I/O files and drives the show. Proposed implementation steps Stage I: Build a basic assembler for programs with no symbols Stage II: Extend the basic assembler with symbol handling capabilities. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 13

Parser module Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 14

Parser module (cont.) Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 15

Code module Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 16

Building the final assembler Initialization: create the symbol table and initialize it with the pre-defined symbols First pass: march through the program without generating any code. For each label declaration (Xxx), add the pair < Xxx,n > to the symbol table Second pass: march again through the program, and translate each line: If the line is a C-instruction, simple If the line is @Xxx where Xxx is a number, simple If the line is @Xxx where Xxx is a symbol, look it up in the symbol table If the symbol is found, replace it with its numeric meaning and complete the command s translation If the symbol is not found, then it must represent a new variable: add the pair < Xxx,n > to the symbol table, where n is the next available RAM address, and complete the command s translation. (The allocated RAM addresses are running, starting at address 16). Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 17

Symbol table Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 18

Perspective Simple machine language, simple assembler Most assemblers are not stand-alone, but rather encapsulated in a translator of a higher order Low-level C programming (e.g. for real-time systems) may involve some assembly programming (e.g. for optimization) Macro assemblers: Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs, Chapter 6: Assembler slide 19