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

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

Assembler. Building a Modern Computer From First Principles.

Chapter 6: Assembler

Assembler. Building a Modern Computer From First Principles.

Assembler Human Thought

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

CMPS 10 Introduction to Computer Science Lecture Notes

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

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

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

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

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

Solving two-person zero-sum game by Matlab

Computer models of motion: Iterative calculations

Brave New World Pseudocode Reference

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

Programming in Fortran 90 : 2017/2018

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

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

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

Machine (Assembly) Language

CS1100 Introduction to Programming

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

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

An Optimal Algorithm for Prufer Codes *

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

A Taste of Java and Object-Oriented Programming

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

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

Mathematics 256 a course in differential equations for engineering students

Conditional Speculative Decimal Addition*

Support Vector Machines

Problem Set 3 Solutions

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

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

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

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

LLVM passes and Intro to Loop Transformation Frameworks

9. BASIC programming: Control and Repetition

CE 221 Data Structures and Algorithms

Parallel matrix-vector multiplication

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

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

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

Esc101 Lecture 1 st April, 2008 Generating Permutation

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

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

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Lecture 5: Multilayer Perceptrons

Lecture 3: Computer Arithmetic: Multiplication and Division

AADL : about scheduling analysis

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

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

Design and Analysis of Algorithms

Machine Learning. Topic 6: Clustering

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

Machine (Assembly) Language Human Thought

CSE 326: Data Structures Quicksort Comparison Sorting Bound

A Binarization Algorithm specialized on Document Images and Photos

OPL: a modelling language

Specifications in 2001

3D vector computer graphics

PHYSICS-ENHANCED L-SYSTEMS

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

AP PHYSICS B 2008 SCORING GUIDELINES

ELEC 377 Operating Systems. Week 6 Class 3

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

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

Smoothing Spline ANOVA for variable screening

Compiling Process Networks to Interaction Nets

Virtual Machine Migration based on Trust Measurement of Computer Node

Machine Learning: Algorithms and Applications

Classification / Regression Support Vector Machines

CSc 520 Principles of Programming Languages. Example Language Translators. What s a Language Translator??? 2: Translators

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Functional Testing of Digital Systems

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

Load Balancing for Hex-Cell Interconnection Network

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

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

A RECONFIGURABLE ARCHITECTURE FOR MULTI-GIGABIT SPEED CONTENT-BASED ROUTING. James Moscola, Young H. Cho, John W. Lockwood

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

Storage Binding in RTL synthesis

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

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

NUMERICAL SOLVING OPTIMAL CONTROL PROBLEMS BY THE METHOD OF VARIATIONS

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

Memory and I/O Organization

Review of approximation techniques

A mathematical programming approach to the analysis, design and scheduling of offshore oilfields

Lecture 4: Principal components

Secure and Fast Fingerprint Authentication on Smart Card

Computer Architecture ELEC3441

Algorithm To Convert A Decimal To A Fraction

Midterms Save the Dates!

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

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.

Quicksort. Part 1: Understanding Quicksort

Transcription:

IDC Herzlya Shmon Schocken Assembler Shmon Schocken Sprng 2005 Elements of Computng Systems 1 Assembler (Ch. 6) Where we are at: Human Thought Abstract desgn Chapters 9, 12 abstract nterface H.L. Language & Operatng Sys. Compler Chapters 10-11 abstract nterface Vrtual Machne Software herarchy VM Translator Chapters 7-8 abstract nterface Assembly Language Assembler Chapter 6 abstract nterface Machne Language Computer Archtecture Chapters 4-5 Hardware herarchy abstract nterface Hardware Platform Gate Logc Chapters 1-3 abstract nterface Chps & Logc Gates Electrcal Engneerng Physcs Elements of Computng Systems 2 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 compler for a smple language Wrtng an assembler s a good ntroducton for wrtng a compler. Elements of Computng Systems 3 Assembler (Ch. 6) Program translaton 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 lang. Assembler = smple translator Translates each assembly command nto one or more machne nstructons Handles symbols (, sum, loop, end, ). Elements of Computng Systems 4 Assembler (Ch. 6)

Symbol resoluton In low level languages, symbols are normally used to represent: Varables Destnatons of goto commands 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 5 Assembler (Ch. 6) Perspectve Smplfyng assumptons: Largest possble program s 1024 commands long Each command fts nto one memory locaton Each varable fts nto one memory locaton These assumptons can be relaxed rather easly, requrng more sophstcated assemblers. Elements of Computng Systems 6 Assembler (Ch. 6)

The Hack assembly language Assembly program (Prog.asm) // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+1 // =+1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Assembly program = a stream of text lnes, each beng: An nstructon: A-nstructon or C-nstructon A symbol declaraton: (symbol) A comment / whte space: // comment. Elements of Computng Systems 7 Assembler (Ch. 6) A-nstructon 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 8 Assembler (Ch. 6)

C-nstructon Symbolc: dest=comp;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 9 Assembler (Ch. 6) The overall assembly logc Assembly program (Prog.asm) // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+1 // =+1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop For each symbolc command Parse nto the underlyng symbolc 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 10 Assembler (Ch. 6)

Symbols handlng (n the Hack language) Program example // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+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 11 Assembler (Ch. 6) Example Assembly code (Prog.asm) // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) Assembler @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+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 12 Assembler (Ch. 6)

Proposed mplementaton An assembler program can be mplemented (n any language) based on the followng 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 fles and drves the show. Elements of Computng Systems 13 Assembler (Ch. 6) Parser module Elements of Computng Systems 14 Assembler (Ch. 6)

Parser module (cont.) Elements of Computng Systems 15 Assembler (Ch. 6) Code module Elements of Computng Systems 16 Assembler (Ch. 6)

Proposed mplementaton plan 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 17 Assembler (Ch. 6) Symbol table Elements of Computng Systems 18 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 and buld the symbol table, wthout generatng any code 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 19 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 Typcally, low-level C programmng (e.g. for real-tme systems) nvolves some assembly programmng (e.g. for optmzaton) Macro assemblers: // // R1 R1 = 1+2+...+100 sum=0 n=1 n=1 loop: f f n=101 goto goto end end sum=sum+n n=n+1 goto goto loop loop end: end: R1=sum Elements of Computng Systems 20 Assembler (Ch. 6)

Endnote I: Turng machne (1935) 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. Important conjecture: for any program runnng on any computer there s an equvalent TM that does the same thng. Elements of Computng Systems 21 Assembler (Ch. 6) The Haltng Problem Program = data: a TM program can be wrtten on the tape on another TM, becomng ts nput The haltng problem: 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 mply that we can prove theorems 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 22 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 23 Assembler (Ch. 6) Endnote II: The Engma Great book: Alan Turng: The Engma, by Andrew Hodges Elements of Computng Systems 24 Assembler (Ch. 6)