Assembler Human Thought

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

Chapter 6: Assembler

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

Machine (Assembly) Language

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

Assembler. Building a Modern Computer From First Principles.

Machine (Assembly) Language Human Thought

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

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

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

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

Introduction: From Nand to Tetris

Computer Architecture

Introduction: Hello, World Below

Chapter 11: Compiler II: Code Generation

Chapter 8: Virtual Machine II: Program Control

Compiler II: Code Generation Human Thought

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

Chapter 10: Compiler I: Syntax Analysis

Compiler I: Syntax Analysis

Compiler I: Sytnax Analysis

STEVEN R. BAGLEY THE ASSEMBLER

Computer Architecture

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 (

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

Boolean Arithmetic. Building a Modern Computer From First Principles.

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

Virtual Machine (Part II)

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

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

Arithmetic Logic Unit (ALU)

EP1200 Introduction to Computing Systems Engineering. Computer Architecture

Chapter 9: High Level Language

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

Sequential Logic. Building a Modern Computer From First Principles.

7. The Virtual Machine II: Flow Control 1

Computer Architecture

Real instruction set architectures. Part 2: a representative sample

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

10. The Compiler II: Code Generation 1

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

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

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Admin. ! Assignment 3. ! due Monday at 11:59pm! one small error in 5b (fast division) that s been fixed. ! Midterm next Thursday in-class (10/1)

CS311 Lecture: The Architecture of a Simple Computer

Topic Notes: MIPS Instruction Set Architecture

1 Little Man Computer

Computational Optimization ISE 407. Lecture1. Dr. Ted Ralphs

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

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

4. Computer Architecture 1

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

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Virtual Machine (Part II)

CS 6353 Compiler Construction Project Assignments

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

CS 6353 Compiler Construction Project Assignments

Admin CS41B MACHINE. Midterm topics. Admin 2/11/16. Midterm next Thursday in-class (2/18) SML. recursion. math. David Kauchak CS 52 Spring 2016

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

SOEE1160: Computers and Programming in Geosciences Semester /08. Dr. Sebastian Rost

Memory Management. Goals of Memory Management. Mechanism. Policies

Digital Forensics Lecture 3 - Reverse Engineering

Introduction to MiniSim A Simple von Neumann Machine

CSE 582 Autumn 2002 Exam 11/26/02

Compiler Code Generation COMP360

Intermediate Code Generation

Building a Virtual Computer

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

ECS 142 Project: Code generation hints

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Lecture 4: MIPS Instruction Set

Computer Architecture

CSC180: Lecture 2. Wael Aboulsaadat.

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

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

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

Project 6: Assembler

COMPILER DESIGN. For COMPUTER SCIENCE

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CSE 141L Computer Architecture Lab Fall Lecture 3

Lecture 2: The Instruction Set Architecture

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

2.2 THE MARIE Instruction Set Architecture

Intermediate Representations

CSE 4/521 Introduction to Operating Systems. Lecture 14 Main Memory III (Paging, Structure of Page Table) Summer 2018

Computer Architecture

Lecture 03 Bits, Bytes and Data Types

The SURE Architecture

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

LECTURE 3. Compiler Phases

Boolean logic. Boolean Algebra. Introduction to Computer Yung-Yu Chuang NOT AND NOT

Dr. Chuck Cartledge. 15 July 2015

Transcription:

Where we are at: Assembler Human Thought Abstract design Chapters 9, 12 H.L. Language & Operating Sys. Compiler Chapters 10-11 Virtual Machine Software hierarchy VM Translator Chapters 7-8 Assembly Language Assembler Chapter 6 Building a Modern Computer From First Principles www.nand2tetris.org Machine Language Computer Architecture Chapters 4-5 Hardware hierarchy Hardware Platform Gate Logic Chapters 1-3 Chips & Logic Gates Electrical Engineering Physics Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 1 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 2 Why care about assemblers? Assembly example Assemblers employ 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 = low-impact practice for writing compilers. // if i>ram[0] goto WRITE // Etc. assemble Target code (a text file of binary Hack code) 1110111111001000 1110101010001000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 execute cross-platform compiling Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 3 The program translation challenge Extract the program s semantics from the source program, using the syntax rules of the source language Re-express the program s semantics in the target language, using the syntax rules of the target language Assembler = simple translator Translates each assembly command into one or more binary machine instructions Handles symbols (e.g. i, sum, LOOP, ). Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 4

Revisiting Hack low-level programming: an example The assembler s view of an assembly program (sum.asm) // if i>ram[0] goto WRITE // sum += i // i++ CPU emulator screen shot after running this program user supplied input program generated output The CPU emulator allows loading and executing symbolic Hack code. It resolves all the symbolic symbols to memory locations, and executes the code. // if i>ram[0] goto WRITE // sum += i // i++ = Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 5 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 6 The assembler s view of an assembly program The assembler s view of an assembly program // if i>ram[0] goto WRITE // sum += i // i++ = Empty lines/indentation Line comments In-line comments // if i>ram[0] goto WRITE // sum += i // i++ = Empty lines/indentation Line comments In-line comments A-instruction C-instruction Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 7 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 8

The assembler s view of an assembly program The assembler s view of an assembly program // if i>ram[0] goto WRITE // sum += i // i++ = Empty lines/indentation Line comments In-line comments A-instruction C-instruction references Label declaration (XXX) // if i>ram[0] goto WRITE 8 // sum += i // i++ @4 // goto LOOP @22 = Empty lines/indentation Line comments In-line comments A-instruction C-instruction references Label declaration (XXX) Assume that there is no symbol for now! Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 9 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 10 ignore/remove them // if i>ram[0] goto WRITE 8 // sum += i // i++ @4 // goto LOOP = Empty lines/indentation Line comments In-line comments A-instruction C-instruction references Label declaration (XXX) M=1 M=0 8 @4 @22 = Empty lines/indentation Line comments In-line comments A-instruction C-instruction references Label declaration (XXX) @22 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 11 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 12

binary encoding M=1 M=0 8 @4 @22 = Empty lines/indentation Line comments In-line comments A-instruction C-instruction references Label declaration (XXX) Translating / assembling A-instructions Symbolic: Binary: Translation to binary: @value // Where value is either a non-negative decimal number // or a symbol referring to such number. value (v = 0 or 1) 0 v v v v v v v v v v v v v v v If value is a non-negative decimal number, simple, e.g. If value is a symbol, later. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 13 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 14 Translating / assembling C-instructions 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 Translating / assembling C-instructions Symbolic: Example: MD=D+1 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 Binary: 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 15 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 16

Translating / assembling C-instructions Symbolic: Example: D; JGT Binary: 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 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 The overall assembly logic // if i>ram[0] goto WRITE // sum += i // i++ For each (real) command Parse the command, i.e. break it into its underlying fields A-instruction: replace the symbolic reference (if any) with the corresponding memory address, which is a number (how to do it, later) C-instruction: for each field in the instruction, generate the corresponding binary code Assemble the translated binary codes into a complete 16-bit machine instruction Write the 16-bit instruction to the output file. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 17 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 18 Handling symbols (aka symbol resolution) s typically have many symbols: Labels that mark destinations of goto commands Labels that mark special memory locations Variables These symbols fall into two categories: User defined symbols (created by programmers) Variables Labels (forward reference could be a problem) Pre-defined symbols (used by the Hack platform). Typical symbolic Hack assembly code: D;JLE @SCREEN D=A A=M M= 1 @32 D=D+A M 1 @LOOP Handling symbols: user-defined symbols Label symbols: Used to label destinations of goto commands. Declared by the pseudo-command (XXX). This directive defines the symbol XXX to refer to the instruction memory location holding the next command in the program. (the assembler needs to maintain instrctr) Variable symbols: Any user-defined symbol xxx appearing in an assembly program that is not defined elsewhere using the (xxx) directive is treated as a variable, and is automatically assigned a unique RAM address, starting at RAM address 16 (the assembler needs to maintain nextaddr) (why start at 16? Later.) By convention, Hack programmers use lower-case and upper-case to represent variable and label names, respectively Typical symbolic Hack assembly code: D;JLE @SCREEN D=A A=M M= 1 @32 D=D+A M 1 @LOOP Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 19 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 20

Handling symbols: pre-defined symbols Virtual registers: The symbols R0,, R15 are automatically predefined to refer to RAM addresses 0,,15 I/O pointers: The symbols SCREEN and KBD are automatically predefined to refer to RAM addresses 16384 and 24576, respectively (base addresses of the screen and keyboard memory maps) VM control pointers: the symbols SP, LCL, ARG, THIS, and THAT (that don t appear in the code example on the right) are automatically predefined to refer to RAM addresses 0 to 4, respectively (The VM control pointers, which overlap R0,, R4 will come to play in the virtual machine implementation, covered in the next lecture) Typical symbolic Hack assembly code: D;JLE @SCREEN D=A A=M M= 1 @32 D=D+A M 1 @LOOP Handling symbols: symbol table // if i>ram[0] goto WRITE // sum += i // i++ LOOP 4 WRITE 18 END 22 i 16 sum 17 This symbol table is generated by the assembler, and used to translate the symbolic code into binary code. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 21 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 22 Handling symbols: constructing the symbol table Handling symbols: constructing the symbol table // if i>ram[0] goto WRITE // sum += i // i++ Initialization: create an empty symbol table and populate it with all the pre-defined symbols // if i>ram[0] goto WRITE // sum += i // i++ LOOP 4 WRITE 18 END 22 Initialization: create an empty symbol table and populate it with all the pre-defined symbols First pass: go through the entire source code, and add all the user-defined label symbols to the symbol table (without generating any code) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 23 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 24

Handling symbols: constructing the symbol table Handling symbols: constructing the symbol table (one-pass solution?) // if i>ram[0] goto WRITE // sum += i // i++ LOOP 4 WRITE 18 END 22 i 16 sum 17 Initialization: create an empty symbol table and populate it with all the pre-defined symbols First pass: go through the entire source code, and add all the user-defined label symbols to the symbol table (without generating any code) Second pass: go again through the source code, and use the symbol table to translate all the commands. In the process, handle all the userdefined variable symbols. // if i>ram[0] goto WRITE // sum += i // i++ Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 25 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 26 The assembly process (detailed) Initialization: create the symbol table and initialize it with the predefined symbols First pass: march through the source code without generating any code. For each label declaration (LABEL) that appears in the source code, add the pair <LABEL, n > to the symbol table The assembly process (detailed) Second pass: march again through the source, and process each line: If the line is a C-instruction, simple If the line is xx where xxx is a number, simple If the line is xx and xxx is a symbol, look it up in the symbol table and proceed as follows: If the symbol is found, replace it with its numeric value 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. (Platform design decision: the allocated RAM addresses are running, starting at address 16). Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 27 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 28

The result // if i>ram[0] goto WRITE // sum += i // i++ assemble Target code 1110111111001000 1110101010001000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 1111000010001000 1111110111001000 0000000000000100 1110101010000111 0000000000000001 1110001100001000 0000000000010110 1110101010000111 Note that comment lines and pseudo-commands (label declarations) generate no code. Proposed assembler implementation An assembler program can be written in any high-level language. (and could be run in the other platforms, cross-platform compiling) The book proposes a language-independent design, as follows. Software modules: Parser: Unpacks each command into its underlying fields Code: Translates each field into its corresponding binary value, and assembles the resulting values SymbolTable: Manages the symbol table Main: Initializes I/O files and drives the show. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 29 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 30 Perspective Simple machine language, simple assembler Most assemblers are not stand-alone, but rather encapsulated in a translator of a higher order C programmers that understand the code generated by a C compiler can improve their code considerably C programming (e.g. for real-time systems) may involve re-writing critical segments in assembly, for optimization Writing an assembler is an excellent practice for writing more challenging translators, e.g. a VM Translator and a compiler, as we will do in the next lectures. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 31