Assembler Human Abstract Design Software Abstract Interface Thought Chapters 9, 12 Hierarchy H.L

Assembler Human Abstract Design Software Abstract Interface Thought Chapters 9, 12 Hierarchy H.L

Where we are at: Assembler Human Abstract design Software abstract interface Thought Chapters 9, 12 hierarchy H.L. Language Compiler & abstract interface Chapters 10 - 11 Operating Sys. Virtual VM Translator abstract interface Machine Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Computer Building a Modern Computer From First Principles Machine Architecture abstract interface Language Chapters 4 - 5 www.nand2tetris.org Hardware Gate Logic abstract interface Platform Chapters 1 - 3 Electrical Chips & Engineering Hardware Physics hierarchy Logic Gates 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 Target code (a text file of Source code (example) binary Hack code) // Computes 1+...+RAM[0] 0000000000010000 Assemblers are the first rung up the software hierarchy ladder // And stored the sum in RAM[1] 1110111111001000 @i 0000000000010001 M=1 // i = 1 1110101010001000 An assembler is a translator of a simple language @sum 0000000000010000 M=0 // sum = 0 assemble 1111110000010000 execute (LOOP) 0000000000000000 @i // if i>RAM[0] goto WRITE 1111010011010000 Writing an assembler = low-impact practice for writing compilers. D=M 0000000000010010 @R0 1110001100000001 D=D‐M 0000000000010000 @WRITE 1111110000010000 D;JGT 0000000000010001 ... // Etc. ... 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 cross-platform Assembler = simple translator compiling 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 3 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 Assembly program (sum.asm) CPU emulator screen shot Assembly program // Computes 1+...+RAM[0] after running this program // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. // And stores the sum in RAM[1]. Assembly program = @i @i a stream of text lines, each being M=1 // i= 1 M=1 // i= 1 one of the following: @sum @sum M=0 // sum = 0 M=0 // sum = 0 user (LOOP) supplied (LOOP) @i // if i>RAM[0] goto WRITE input @i // if i>RAM[0] goto WRITE D=M D=M @R0 @R0 D=D‐M program D=D‐M generated @WRITE output @WRITE D;JGT D;JGT @i // sum += i @i // sum += i D=M D=M @sum @sum M=D+M M=D+M @i // i++ @i // i++ M=M+1 M=M+1 @LOOP // goto LOOP @LOOP // goto LOOP 0;JMP 0;JMP (WRITE) (WRITE) @sum @sum D=M D=M @R1 @R1 M=D // RAM[1] = the sum M=D // RAM[1] = the sum (END) The CPU emulator allows loading and executing (END) @END @END 0;JMP symbolic Hack code. It resolves all the symbolic 0;JMP symbols to memory locations, and executes the code. 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 Assembly program Assembly program // Computes 1+...+RAM[0] // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. Assembly program = // And stores the sum in RAM[1]. Assembly program = @i a stream of text lines, each being @i a stream of text lines, each being M=1 // i= 1 one of the following: M=1 // i= 1 one of the following: @sum @sum M=0 // sum = 0 M=0 // sum = 0 (LOOP) White space (LOOP) White space @i // if i>RAM[0] goto WRITE Empty lines/indentation @i // if i>RAM[0] goto WRITE Empty lines/indentation D=M D=M @R0 Line comments @R0 Line comments D=D‐M In-line comments D=D‐M In-line comments @WRITE @WRITE D;JGT D;JGT @i // sum += i @i // sum += i Instructions D=M D=M A-instruction @sum @sum M=D+M M=D+M C-instruction @i // i++ @i // i++ M=M+1 M=M+1 @LOOP // goto LOOP @LOOP // goto LOOP 0;JMP 0;JMP (WRITE) (WRITE) @sum @sum D=M D=M @R1 @R1 M=D // RAM[1] = the sum M=D // RAM[1] = the sum (END) (END) @END @END 0;JMP 0;JMP 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 Assembly program Assembly program // Computes 1+...+RAM[0] // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. Assembly program = // And stores the sum in RAM[1]. Assembly program = @i a stream of text lines, each being @16 a stream of text lines, each being M=1 // i= 1 one of the following: M=1 // i= 1 one of the following: @sum @17 M=0 // sum = 0 M=0 // sum = 0 (LOOP) White space White space @i // if i>RAM[0] goto WRITE Empty lines/indentation @16 // if i>RAM[0] goto WRITE Empty lines/indentation D=M D=M @R0 Line comments @0 Line comments D=D‐M In-line comments D=D‐M In-line comments @WRITE @18 D;JGT D;JGT @i // sum += i Instructions @16 // sum += i Instructions D=M A-instruction D=M A-instruction @sum @17 M=D+M C-instruction M=D+M C-instruction @i // i++ @16 // i++ M=M+1 Symbols M=M+1 Symbols @LOOP // goto LOOP references @4 // goto LOOP references 0;JMP 0;JMP (WRITE) Label declaration (XXX) Label declaration (XXX) @sum @17 D=M D=M Assume that there is @R1 @1 M=D // RAM[1] = the sum M=D // RAM[1] = the sum no symbol for now! (END) @END @22 0;JMP 0;JMP 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 White space White space → ignore/remove them Assembly program Assembly program // Computes 1+...+RAM[0] @16 // And stores the sum in RAM[1]. Assembly program = M=1 Assembly program = @16 a stream of text lines, each being @17 a stream of text lines, each being M=1 // i= 1 one of the following: M=0 one of the following: @17 @16 M=0 // sum = 0 D=M White space @0 White space @16 // if i>RAM[0] goto WRITE Empty lines/indentation D=D‐M Empty lines/indentation D=M @18 @0 Line comments D;JGT Line comments D=D‐M In-line comments @16 In-line comments @18 D=M D;JGT @17 @16 // sum += i Instructions M=D+M Instructions D=M A-instruction @16 A-instruction @17 M=M+1 M=D+M C-instruction @4 C-instruction @16 // i++ 0;JMP M=M+1 Symbols @17 Symbols @4 // goto LOOP references D=M references 0;JMP @1 Label declaration (XXX) M=D Label declaration (XXX) @17 @22 D=M 0;JMP @1 M=D // RAM[1] = the sum @22 0;JMP 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 Instructions → binary encoding Translating / assembling A-instructions Assembly program @16 Symbolic: @value // Where value is either a non-negative decimal number M=1 Assembly program = // or a symbol referring to such number. @17 a stream of text lines, each being M=0 one of the following: @16 value (v = 0 or 1) D=M @0 White space D=D‐M Empty lines/indentation Binary: 0 v v v v v v v v v v v v v v v @18 D;JGT Line comments @16 In-line comments D=M @17 M=D+M Instructions @16 A-instruction Translation to binary: M=M+1 @4 C-instruction 0;JMP If value is a non-negative decimal number, simple, e.g. @16 @17 Symbols D=M references If value is a symbol, later. @1 M=D Label declaration (XXX) @22 0;JMP 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 Translating / assembling C-instructions Symbolic: dest=comp;jump // Either the dest or jump fields may be empty. Symbolic: dest=comp;jump // Either the dest or jump fields may be empty. // If dest is empty, the "=" is ommitted; // If dest is empty, the "=" is ommitted; // If jump is empty, the ";" is omitted. // If jump is empty, the ";" is omitted. comp dest jump Example: MD=D+1 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 The overall assembly logic Symbolic: dest=comp;jump // Either the dest or jump fields may be empty.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us