Motivation $ - - PowerPoint PPT Presentation

motivation
SMART_READER_LITE
LIVE PREVIEW

Motivation $ - - PowerPoint PPT Presentation

Motivation $ 0000000000010000 0000000000010000 class Main { class Main { 1110111111001000


slide-1
SLIDE 1

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 3

Motivation

class Main { static int x; function void main() { // Inputs and multiplies two numbers var int a, b, x; let a = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); let x = mult(a,b); return; } } // Multiplies two numbers. function int mult(int x, int y) { var int result, j; let result = 0; let j = y; while ~(j = 0) { let result = result + x; let j = j – 1; } return result; } } class Main { static int x; function void main() { // Inputs and multiplies two numbers var int a, b, x; let a = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); let x = mult(a,b); return; } } // Multiplies two numbers. function int mult(int x, int y) { var int result, j; let result = 0; let j = y; while ~(j = 0) { let result = result + x; let j = j – 1; } return result; } }

  • !
  • "#

Compiler

0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 ... 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 ...

$

slide-2
SLIDE 2

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 4

Compilation models

. . .

requires n m translators

hardware platform 2 hardware platform 1 hardware platform m

. . .

language 1 language 2 language n

direct compilation:

.

. . .

hardware platform 2 hardware platform 1 hardware platform m

. . .

language 1 language 2 language n intermediate language

requires n + m translators

2-tier compilation:

%

&

'( '(#

slide-3
SLIDE 3

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 5

The big picture

. . .

RISC machine Intermediate code

  • ther digital platforms, each equipped

with its own VM implementation RISC machine language

Hack computer

Hack machine language CISC machine language CISC machine

. . .

written in a high-level language Any computer

. . .

VM implementation

  • ver CISC

platforms VM imp.

  • ver RISC

platforms

VM imp.

  • ver the Hack

platform

VM emulator Some Other language

Jack language

Some compiler Some Other compiler

Jack compiler

. . .

Some language . . .

  • ("%

)

"(('

' * !+

,

  • "

(" !

  • "

!((%'#

slide-4
SLIDE 4

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 10

Our VM model is stack-oriented

  • <!!'
  • '"!
  • ('static+%%

"'(%

slide-5
SLIDE 5

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 11

Data types

( => "'"

!=> ").-32768+###+32767 :!0 -1+(true false '

slide-6
SLIDE 6

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 12

Memory access operations

  • 3&

1%( !%/(%#

  • "(
  • (
slide-7
SLIDE 7

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 13

Evaluation of arithmetic expressions

// z=(2-x)-(y+5) push 2 push x sub push y push 5 add sub pop z // z=(2-x)-(y+5) push 2 push x sub push y push 5 add sub pop z

  • x (static 0+

y (static 1+ z (static 2

slide-8
SLIDE 8

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 14

Evaluation of Boolean expressions

// (x<7) or (y=8) push x push 7 lt push y push 8 eq

  • r

// (x<7) or (y=8) push x push 7 lt push y push 8 eq

  • r
  • x (static 0+

y (static 1

'true false 0 -1+ !'

slide-9
SLIDE 9

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 15

Arithmetic and Boolean commands in the VM language (wrap-up)

slide-10
SLIDE 10

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 16

!"( % ! !'((%!"

  • !
  • !" !!"
  • !!"4"?!"5 /4(5 /45

!

  • 3!"
  • !"

7+ +!+!""' ('static+this+local+argument +('+%%" that+constant+pointer+temp.

The VM’s Memory segments

slide-11
SLIDE 11

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 17

Memory segments and memory access commands

'

pop memorySegment index push memorySegment index

7memorySegment static+this+local+argument+that+constant+pointer+ temp index ! '

pop memorySegment index push memorySegment index

7memorySegment static+this+local+argument+that+constant+pointer+ temp index ! 2 (+memorySegment %static) ((('%"!%%. " "!+'%'# "@'

static+this+local+argument+that+constant+pointer+temp

(+'"! '+(%'

slide-12
SLIDE 12

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 18

VM programming

'%"'+"' $%!+%"'### %8+(( .A +%.%( %

  • function functionSymbol //(
  • label labelSymbol //"
  • if-goto labelSymbol // pop x

//(x=true+?( labelSymbol //

&+((if (x > n) goto loop+%(%

push x push n gt if-goto loop //2x+n+!%!(#

  • function functionSymbol //(
  • label labelSymbol //"
  • if-goto labelSymbol // pop x

//(x=true+?( labelSymbol //

&+((if (x > n) goto loop+%(%

push x push n gt if-goto loop //2x+n+!%!(#

slide-13
SLIDE 13

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 19

VM programming (example)

function mult (x,y) { int result, j; result = 0; j = y; while ~(j = 0) { result = result + x; j = j - 1; } return result; } function mult (x,y) { int result, j; result = 0; j = y; while ~(j = 0) { result = result + x; j = j - 1; } return result; }

$ !

function mult(x,y) push 0 pop result push y pop j label loop push j push 0 eq if-goto end push result push x add pop result push j push 1 sub pop j goto loop label end push result return function mult(x,y) push 0 pop result push y pop j label loop push j push 0 eq if-goto end push result push x add pop result push j push 1 sub pop j goto loop label end push result return

(#

function mult 2 push constant 0 pop local 0 push argument 1 pop local 1 label loop push local 1 push constant 0 eq if-goto end push local 0 push argument 0 add pop local 0 push local 1 push constant 1 sub pop local 1 goto loop label end push local 0 return function mult 2 push constant 0 pop local 0 push argument 1 pop local 1 label loop push local 1 push constant 0 eq if-goto end push local 0 push argument 0 add pop local 0 push local 1 push constant 1 sub pop local 1 goto loop label end push local 0 return

slide-14
SLIDE 14

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 20

VM programming: multiple functions

  • (=(?.java (#

7%'(+(=

.vm (?.class (#1 (%

+(((#

1

'!(+'(4

(5+%B(%(( 4'5

&+main (C%'

call factorial+%factorial (C %'call mult+%mult ( +%"main factorial %( The stack: "+! ' (('## main factorial# ((%((##mult#

slide-15
SLIDE 15

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 9

Program flow commands in the VM language

!/

(

  • 0 +
  • !"

!/23#

label c %% goto c %%2 %% if-goto c %%"4 %%502 %% label c %% goto c %%2 %% if-goto c %%"4 %%502 %% ,

function mult 1 push constant 0 pop local 0 label loop push argument 0 push constant 0 eq if-goto end push argument 0 push 1 sub pop argument 0 push argument 1 push local 0 add pop local 0 goto loop label end push local 0 return function mult 1 push constant 0 pop local 0 label loop push argument 0 push constant 0 eq if-goto end push argument 0 push 1 sub pop argument 0 push argument 1 push local 0 add pop local 0 goto loop label end push local 0 return

slide-16
SLIDE 16

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 11

Subroutines

(62

  • & ,)

" % % ###

  • )

"))

  • +,

)

  • +0##
  • 7$)",0

,"8 ( "0!+."

// Compute x = (-b + sqrt(b^2 -4*a*c)) / 2*a if (~(a = 0)) x = (-b + sqrt(b * b – 4 * a * c)) / (2 * a) else x = - c / b // Compute x = (-b + sqrt(b^2 -4*a*c)) / 2*a if (~(a = 0)) x = (-b + sqrt(b * b – 4 * a * c)) / (2 * a) else x = - c / b

slide-17
SLIDE 17

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 12

Subroutines in the VM language

+

  • ,

+

%

  • +%

"0 ",#

function mult 1 push constant 0 pop local 0 // result (local 0) = 0 label loop push argument 0 push constant 0 eq if-goto end // if arg0 == 0, jump to end push argument 0 push 1 sub pop argument 0 // arg0-- push argument 1 push local 0 add pop local 0 // result += arg1 goto loop label end push local 0 // push result return function mult 1 push constant 0 pop local 0 // result (local 0) = 0 label loop push argument 0 push constant 0 eq if-goto end // if arg0 == 0, jump to end push argument 0 push 1 sub pop argument 0 // arg0-- push argument 1 push local 0 add pop local 0 // result += arg1 goto loop label end push local 0 // push result return 0"78 , ... // computes (7 + 2) * 3 - 5 push constant 7 push constant 2 add push constant 3 call mult push constant 5 sub ... ... // computes (7 + 2) * 3 - 5 push constant 7 push constant 2 add push constant 3 call mult push constant 5 sub ... ,

  • ))
slide-18
SLIDE 18

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 13

Function commands in the VM language

9 .,/ $ &# function g nVars %%g0 %%nVars call g nArgs %% "g 4 %%nArgs " return %%, function g nVars %%g0 %%nVars call g nArgs %% "g 4 %%nArgs " return %%,

slide-19
SLIDE 19

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 14

Function call-and-return conventions

function mult 1 push constant 0 pop local 0 // result (local 0) = 0 label loop ... // rest of code ommitted label end push local 0 // push result return function mult 1 push constant 0 pop local 0 // result (local 0) = 0 label loop ... // rest of code ommitted label end push local 0 // push result return

"78 ,

function demo 3 ... push constant 7 push constant 2 add push constant 3 call mult ... function demo 3 ... push constant 7 push constant 2 add push constant 3 call mult ...

  • ))
  • +00
  • &0
  • $00)0

,2call

  • 2"

&

  • :)2
  • ( 7"8
  • 0 %")0(#

$ ,0 ; local, argument, this, that, pointer) +, #

slide-20
SLIDE 20

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 15

The function-call-and-return protocol

+

.,0 argument 5 local 55 +static static 0"" &,0 "return# .,0 argument 5 local 55 +static static 0"" &,0 "return# &g0" g <,0 "call g nArgs $g + "0 , " $local, argument, this, that, pointer) # &g0" g <,0 "call g nArgs $g + "0 , " $local, argument, this, that, pointer) #

+g =

&6

  • &"6",0

+0

  • 7"8 #

function g nVars call g nArgs return function g nVars call g nArgs return

slide-21
SLIDE 21

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 16

.f g0

  • ( f =

2call

  • ( f
  • $05>0 g
  • (local argument g
  • +g#

.g f0

  • g 2""
  • : f
  • +"f

2 # 9 !""7"8/ $ ." #

The function-call-and-return protocol: the VM implementation view

function g nVars call g nArgs return function g nVars call g nArgs return

slide-22
SLIDE 22

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II slide 24

Perspective

&

  • 1
  • A)
  • (
  • ?
  • ,

<

  • . . .

VM language

RISC machine language

Hack

CISC machine language

. . .

written in a high-level language

. . .

VM implementation

  • ver CISC

platforms VM imp.

  • ver RISC

platforms

Translator

VM emulator Some Other language

Jack

Some compiler Some Other compiler

compiler

. . .

Some language . . .

&

( $0,"0B $) ?#

  • #