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

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

Assembler. Building a Modern Computer From First Principles.

Chapter 6: Assembler

Assembler. Building a Modern Computer From First Principles.

Assembler Human Thought

CMPS 10 Introduction to Computer Science Lecture Notes

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

Brave New World Pseudocode Reference

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

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

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Nachos Project 3. Speaker: Sheng-Wei Cheng 2010/12/16

Programming in Fortran 90 : 2017/2018

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Agenda & Reading. Simple If. Decision-Making Statements. COMPSCI 280 S1C Applications Programming. Programming Fundamentals

Solving two-person zero-sum game by Matlab

Computer models of motion: Iterative calculations

Data Representation in Digital Design, a Single Conversion Equation and a Formal Languages Approach

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

LLVM passes and Intro to Loop Transformation Frameworks

CS1100 Introduction to Programming

Notes on Organizing Java Code: Packages, Visibility, and Scope

such that is accepted of states in , where Finite Automata Lecture 2-1: Regular Languages be an FA. A string is the transition function,

On Some Entertaining Applications of the Concept of Set in Computer Science Course

9. BASIC programming: Control and Repetition

Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

Support Vector Machines

Problem Set 3 Solutions

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

CE 221 Data Structures and Algorithms

An Optimal Algorithm for Prufer Codes *

Priority queues and heaps Professors Clark F. Olson and Carol Zander

Esc101 Lecture 1 st April, 2008 Generating Permutation

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Machine Learning. Topic 6: Clustering

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Mathematics 256 a course in differential equations for engineering students

Conditional Speculative Decimal Addition*

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Overview. CSC 2400: Computer Systems. Pointers in C. Pointers - Variables that hold memory addresses - Using pointers to do call-by-reference in C

4/11/17. Agenda. Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Storage Management.

Today Using Fourier-Motzkin elimination for code generation Using Fourier-Motzkin elimination for determining schedule constraints

The stream cipher MICKEY-128 (version 1) Algorithm specification issue 1.0

Midterms Save the Dates!

VRT012 User s guide V0.1. Address: Žirmūnų g. 27, Vilnius LT-09105, Phone: (370-5) , Fax: (370-5) ,

A Binarization Algorithm specialized on Document Images and Photos

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Compiling Process Networks to Interaction Nets

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

OPL: a modelling language

Load Balancing for Hex-Cell Interconnection Network

Design and Analysis of Algorithms

Outline. Digital Systems. C.2: Gates, Truth Tables and Logic Equations. Truth Tables. Logic Gates 9/8/2011

Machine (Assembly) Language

Lecture 3: Computer Arithmetic: Multiplication and Division

Lecture 5: Multilayer Perceptrons

Functional Testing of Digital Systems

A NOTE ON FUZZY CLOSURE OF A FUZZY SET

3D vector computer graphics

Parallel matrix-vector multiplication

Outline. Discriminative classifiers for image recognition. Where in the World? A nearest neighbor recognition example 4/14/2011. CS 376 Lecture 22 1

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

Machine Learning: Algorithms and Applications

Searching & Sorting. Definitions of Search and Sort. Linear Search in C++ Linear Search. Week 11. index to the item, or -1 if not found.

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Motivation. EE 457 Unit 4. Throughput vs. Latency. Performance Depends on View Point?! Computer System Performance. An individual user wants to:

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

A Taste of Java and Object-Oriented Programming

Sum of Linear and Fractional Multiobjective Programming Problem under Fuzzy Rules Constraints

Outline. CIS 110: Intro to Computer Programming. What Do Our Programs Look Like? The Scanner Object. CIS 110 (11fa) - University of Pennsylvania 1

CAMAS-TR Progress Report **************** ESPRIT III PROJECT NB 6756 **************** CAMAS COMPUTER AIDED MIGRATION OF APPLICATIONS SYSTEM

Smoothing Spline ANOVA for variable screening

Intro. Iterators. 1. Access

Q.1 Q.20 Carry One Mark Each. is differentiable for all real values of x

Algorithm To Convert A Decimal To A Fraction

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

Programming Assignment Six. Semester Calendar. 1D Excel Worksheet Arrays. Review VBA Arrays from Excel. Programming Assignment Six May 2, 2017

Quicksort. Part 1: Understanding Quicksort

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

SENSITIVITY ANALYSIS IN LINEAR PROGRAMMING USING A CALCULATOR

Overview. Basic Setup [9] Motivation and Tasks. Modularization 2008/2/20 IMPROVED COVERAGE CONTROL USING ONLY LOCAL INFORMATION

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

LECTURE NOTES Duality Theory, Sensitivity Analysis, and Parametric Programming

FIBARO WALL PLUG OPERATING MANUAL FGBWHWPE-102/FGBWHWPF-102 CONTENTS

Classification / Regression Support Vector Machines

Fault Detection in Rule-Based Software Systems

TN348: Openlab Module - Colocalization

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Performance Evaluation

Secure and Fast Fingerprint Authentication on Smart Card

Lecture 4: Principal components

A fault tree analysis strategy using binary decision diagrams

UNIT 2 : INEQUALITIES AND CONVEX SETS

ELEC 377 Operating Systems. Week 6 Class 3

ANSYS FLUENT 12.1 in Workbench User s Guide

Transcription:

Harvard Unversty CS 101 Fall 2005, Shmon Schocken Assembler Elements of Computng Systems 1 Assembler (Ch. 6) Why care about assemblers? Because Assemblers employ some nfty trcks Assemblers are the frst rung up the software herarchy ladder An assembler s a translator of a smple language Wrtng an assembler s a good ntroducton for wrtng complers. Elements of Computng Systems 2 Assembler (Ch. 6)

Program translaton Source code Target code Translator The program translaton challenge Parse the source program, usng the syntax rules of the source language Re-express the program s semantcs usng the syntax rules of the target language Assembler smple translator Translates each assembly command nto one or more machne nstructons Handles symbols (, sum, LOOP, END, ). Elements of Computng Systems 3 Assembler (Ch. 6) Symbol resoluton In low level languages, symbols are used to code: Varable names Destnatons of goto commands (labels) Specal memory locatons The assembly process: Frst pass: construct a symbol table Second pass: translate the program, usng the symbol table for symbols resoluton. Elements of Computng Systems 4 Assembler (Ch. 6)

Perspectve Ths example s based on some smplfyng assumptons: Largest possble program s 1024 commands long Each command fts nto one memory locaton Each varable fts nto one memory locaton Every one of these assumptons can be relaxed rather easly. Elements of Computng Systems 5 Assembler (Ch. 6) The Hack assembly language Assembly program (Prog.asm) // Adds 1 +... + 100 M1 // 1 M0 // sum0 (LOOP) @100 DD-A // D-100 D;JGT // f (-100)>0 goto END MD+M // sumsum+ MM+1 // +1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Assembly program a stream of text lnes, each beng one of the followng thngs: Instructon: A-nstructon or C-nstructon Symbol declaraton: (symbol) Comment or whte space: // comment. Elements of Computng Systems 6 Assembler (Ch. 6)

Handlng A-nstructons Symbolc: @value // Where value s ether a non-negatve decmal number // or a symbol referrng to such number. value (v 0 or 1) Bnary: 0 v v v v v v v v v v v v v v v Translaton to bnary: If value s a number: smple If value s a symbol: later. Elements of Computng Systems 7 Assembler (Ch. 6) Handlng C-nstructon Symbolc: destcomp;jump // Ether the dest or jump felds may be empty. // If dest s empty, the "" s ommtted; // If jump s empty, the ";" s omtted. comp dest jump Bnary: 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 Translaton to bnary: smple! Elements of Computng Systems 8 Assembler (Ch. 6)

The overall assembly logc Assembly program (Prog.asm) // Adds 1 +... + 100 M1 // 1 M0 // sum0 (LOOP) @100 DD-A // D-100 D;JGT // f (-100)>0 goto END MD+M // sumsum+ MM+1 // +1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop For each (real) command Parse the command,.e. break t nto ts consttuent felds Replace each symbolc reference (f any) wth the correspondng memory address (a bnary number) For each feld, generate the correspondng bnary code Assemble the bnary codes nto a complete machne nstructon. Elements of Computng Systems 9 Assembler (Ch. 6) Symbols handlng (n the Hack language) Program example // Adds 1 +... + 100 M1 // 1 M0 // sum0 (LOOP) @100 DD-A // D-100 D;JGT // f (-100)>0 goto END MD+M // sumsum+ MM+1 // +1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Predefned symbols: (don t appear n ths example) Label symbols: The pseudo-command (Xxx) declares that the user-defned symbol Xxx should refer to the memory locaton holdng the next command n the program Varable symbols: Any symbol Xxx appearng n an assembly program that s not predefned and s not defned elsewhere usng the (Xxx) pseudo command s treated as a varable Varables are mapped to consecutve memory locatons startng at RAM address 16. Elements of Computng Systems 10 Assembler (Ch. 6)

Example Assembly code (Prog.asm) // Adds 1 +... + 100 M1 // 1 M0 // sum0 (LOOP) Assembler @100 DD-A // D-100 D;JGT // f (-100)>0 goto END MD+M // sumsum+ MM+1 // +1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Bnary code (Prog.hack) (ths lne should be erased) 0000 0000 0001 0000 1110 1111 1100 1000 0000 0000 0001 0001 1110 1010 1000 1000 (ths lne 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 (ths lne should be erased) 0000 0000 0001 0010 1110 1010 1000 0111 Elements of Computng Systems 11 Assembler (Ch. 6) Proposed mplementaton An assembler program can be mplemented (n any language) as follows. Software modules: Parser: Unpacks each command nto ts underlyng felds Code: Translates each feld nto ts correspondng bnary value SymbolTable: Manages the symbol table Man: Intalzes I/O fles and drves the show. Proposed mplementaton steps Stage I: Buld a basc assembler for programs wth no symbols Stage II: Extend the basc assembler wth symbol handlng capabltes. Elements of Computng Systems 12 Assembler (Ch. 6)

Parser module Elements of Computng Systems 13 Assembler (Ch. 6) Parser module (cont.) Elements of Computng Systems 14 Assembler (Ch. 6)

Code module Elements of Computng Systems 15 Assembler (Ch. 6) Buldng the fnal assembler Intalzaton: create the symbol table and ntalze t wth the pre-defned symbols Frst pass: march through the program wthout generatng any code. For each label declaraton (Xxx), add the par < Xxx,n > to the symbol table Second pass: march agan through the program, and translate each lne: If the lne s a C-nstructon, smple If the lne s @Xxx where Xxx s a number, smple If the lne s @Xxx where Xxx s a symbol, look t up n the symbol table If the symbol s found, replace t wth ts numerc meanng and complete the command s translaton If the symbol s not found, then t must represent a new varable: add the par < Xxx,n > to the symbol table, where n s the next avalable RAM address, and complete the command s translaton. (The allocated RAM addresses are runnng, startng at address 16). Elements of Computng Systems 16 Assembler (Ch. 6)

Symbol table Elements of Computng Systems 17 Assembler (Ch. 6) Perspectve Smple machne language, smple assembler Most assemblers are not stand-alone, but rather encapsulated n a translator of a hgher order Low-level programmng (e.g. a C-based real-tme system) may nvolve some assembly programmng (e.g. for optmzaton) Macro assemblers: Elements of Computng Systems 18 Assembler (Ch. 6)

Endnote I: Turng machne (1935) (Drawng by Roger Penrose, The Emperor s New Mnd by) Alan Turng 1912-1954 Informal descrpton: A tape, dvded nto cells, each contanng a symbol A head that can move over the tape left and rght and read and wrte symbols A state regster that stores the machne s state An acton table (transton functon): If the current state s S, and the current symbol s s, then move the tape n postons rght/left, wrte a symbol s, and enter state S. Key conjecture: for any program, n any language, runnng on any computer, there exsts an equvalent TM that acheves the same results (proof?). Elements of Computng Systems 19 Assembler (Ch. 6) The Haltng Problem Program data: a TM program can be wrtten on the tape of another TM, becomng ts nput The haltng detecton program: A program H that, for any gven program p, prnts 1 f p halts on any nput, and 0 otherwse The haltng theorem: H does not exst Theoretcal sgnfcance: If H exsted, t would smple to prove/dsprove many propostons automatcally. Example: // // Goldbach Goldbach conjecture: conjecture: every every even even number number greater greater than than 2 2 s s the the sum sum of of two two prmes. prmes. Functon Functon goldbach() goldbach() 4 4 whle whle true true f f sum sum of of two two prmes prmes + + 2 2 else else prnt( the prnt( the conjecture conjecture s s false. false. Counter Counter example: example:,),) return return } } } } If H exsted, we could apply t to the goldbach() functon, thus provng or dsprove the Goldbach conjecture. Elements of Computng Systems 20 Assembler (Ch. 6)

Hstorcal perspectve Hlbert s challenge (1928): Can we devse a mechancal procedure (algorthm) whch could, n prncple, prove or dsprove any gven mathematcal proposton? Davd Hlbert 1862-1943 Alan Turng (1935): NO. Proof: uncomputablty of the haltng problem Kurt Godel (1931): NO. Proof: Incompleteness theorem (any system contanng the arthmetc of natural numbers s ether ncomplete or nconsstent) Alan Turng 1912-1954 Phlosophcal mplcatons. Kurt Godel 1906-1978 Elements of Computng Systems 21 Assembler (Ch. 6) Endnote II: The Engma Recommended readng: Alan Turng: The Engma, by Andrew Hodges, Walker & Co., 2000. Elements of Computng Systems 22 Assembler (Ch. 6)