Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Machine
Language
From Nand to Tetris
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 1
Where we are at:
Human Abstract design Software
abstract interface
Thought hierarchy
Chapters 9, 12
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
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 2
High-Level vs. Low-Level
Programming Languages
• Goal of Low-Level Language:
– Code low-level programs as a series of machine
instruc:ons
• Command processor to do logic/arithme:c
• Fetch and store values from/to memory
• Move values between registers
• Test boolean condi:ons
• Etc.
– Basically: direct execu:on + total control of hardware
– Fine line between hardware and soFware meet
• Goal of High-Level:
– Generality and power of expression
– Everything else you’ve learned about Java/C++
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 3
Machine language
Abstrac:on – implementa:on duality:
• The hardware plaOorm can be viewed as a physical means for realizing the
machine language abstrac:on Another duality:
punch cards)
string of cards
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 5
Punched Cards
• Needles went through where there were holes
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 6
Charles Babbage
• “Father of Compu:ng”
• 2 major contribu:ons
– Difference Engine (1822)
• First Mechanical Computer
• Never Finished
• un:l 2002: 8000 parts, 2 tons, 11 feet
– Analy:cal Engine
• First programmed computer
– Could handle condi:onal jumps
– Used Jacquards “Punch Card” idea
• Powered by 6 steam engines
• Never completed
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 7
Ada Lovelace
• Educated in Math
Analy:cal Engine
• First Programmer
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 8
Binary and symbolic notation
1010 0001 0010 1011 ADD R1, R2, R3
Evolu:on:
• Physical coding
• Symbolic documenta:on
• Symbolic coding
• Requires a translator.
Jacquard loom Augusta Ada King,
(1801) Countess of Lovelace
(1815-1852)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 9
Lecture plan
– Symbolic version
– Binary version
• Perspec:ve
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 10
Typical machine language commands
(a small sample)
ADD R1,R2,R3 // R1 ß R2 + R3
NOP // Do nothing
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 11
The Hack computer
A 16-bit machine consis:ng of the following elements:
Control: The ROM is loaded with a sequence of 16-bit instruc:ons, one per memory
loca:on, beginning at address 0. Fetch-execute cycle: later
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 12
The A-instruction
@value // A ß value
Coding example:
Used for:
• Entering a constant value @17 // A = 17
( A = value) D = A // D = 17
Later
n Selecting a ROM location @17 // A = 17
( PC = A ) JMP // fetch the instruction
// stored in ROM[17]
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 13
The C-instruction dest = x + y
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 14
The C-instruction (first approximation)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 15
The C-instruction (first approximation)
dest = x + y
Exercise: Implement the following tasks
dest = x - y using Hack commands:
dest = x
1) sum = 0
dest = 0
dest = 1 2) j = j + 1
D register D
a-bit
ALU
A
A register
A/M
Mux
address RAM M
input
(selected
register)
In the Hack architecture:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 17
Coding examples Hack commands:
(practice)
q False is represented by 0
q 0000 0000 0000 0000
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 19
WHILE logic – Hack style
High level: Hack:
q False is represented by 0
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 20
Side note (focus on the yellow chip parts only)
D register D
a-bit
ALU
A
A register
A/M
Mux
address RAM M
input
(selected
register)
In the Hack architecture, the A register
addresses both the RAM and the ROM,
simultaneously. Therefore:
n Command pairs like @addr followed by
address ROM D=M;someJumpDirective make no sense
input Instruction
PC n Best practice: in well-written Hack
(selected
register)
programs, a C-instruction should contain
q either a reference to M, or
q a jump directive,
n but not both.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 21
Complete program example
C language code: Hack assembly code:
// Adds 1+...+100. // Adds 1+...+100.
int i = 1; @i // i refers to some RAM location
int sum = 0; M=1 // i=1
while (i <= 100){ @sum // sum refers to some RAM location
M=0 // sum=0
sum += i;
(LOOP)
i++;
@i
} D=M // D = i
@100
D=D-A // D = i - 100
Hack assembly convention: @END
D;JGT // If (i-100) > 0 goto END
q Variables: lower-case @i
D=M // D = i
q Labels: upper-case @sum
M=D+M // sum += i
q Commands: upper-case @i
M=M+1 // i++
@LOOP
0;JMP // Got LOOP
Demo
CPU emulator (END)
@END
0;JMP // Infinite loop
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 22
Symbols in Hack assembly // Typical symbolic
programs
// Hack code, meaning
// not important
@R0
Symbols created by Hack programmers and code generators: D=M
@INFINITE_LOOP
• Label symbols: Used to label des:na:ons of goto commands. Declared by the
D;JLE
pseudo command (XXX). This direc:ve defines the symbol XXX to refer to the
@counter
instruc:on memory loca:on holding the next command in the program (within the
M=D
program, XXX is called “label”)
@SCREEN
• Variable symbols: Any user-defined symbol xxx appearing in an assembly program D=A
that is not defined elsewhere using the (xxx) direc:ve is treated as a variable, @addr
and is “automa:cally” assigned a unique RAM address, star:ng at RAM address 16 M=D
• By conven:on, Hack programmers use lower-case and upper-case leVers for (LOOP)
@addr
variable names and labels, respec:vely.
A=M
Predefined symbols: M=-1
• I/O pointers: The symbols SCREEN and KBD are “automa:cally” predefined to refer @addr
to RAM addresses 16384 and 24576, respec:vely (base addresses of the Hack D=M
plaOorm’s screen and keyboard memory maps) @32
D=D+A
• Virtual registers: covered in future lectures. @addr
• VM control registers: covered in future lectures. M=D
@counter
Q: Who does all the “automatic” assignments of symbols to RAM addresses?
MD=M-1
A: The assembler, which is the program that translates symbolic Hack @LOOP
D;JGT
programs into binary Hack program. As part of the translation process,
(INFINITE_LOOP)
the symbols are resolved to RAM addresses. (more about this in future lectures)
@INFINITE_LOOP
0;JMP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 23
SCREEN + KBD
• SCREEN is a pointer to memory
16,384 to 24,575 (inclusive)
= 8,192 registers at 16 bits each
= 131,072 bits, each represents a pixel
0 = white
1 = black
= 512 columns x 256 rows = screen resolu:on
• KBD is a pointer to keyboard register
– 24,576 stores current value of key pressed
• if nothing pressed, value is 0
• else value is key pressed
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 24
Perspective
• Hack is a simple machine language
• Hack is a “½-address machine”: any opera:on that needs to operate on the RAM
must be specified using two commands: an A-command to address the RAM, and a
subsequent C-command to operate on it
• A Hack assembler is needed and will be discusses and developed later in the
course.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 25