VM implementation on the Hack platform - - PowerPoint PPT Presentation

vm implementation on the hack platform
SMART_READER_LITE
LIVE PREVIEW

VM implementation on the Hack platform - - PowerPoint PPT Presentation

VM implementation on the Hack platform ( SP 0


slide-1
SLIDE 1

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

VM implementation on the Hack platform

( "RAM '(C (( ! '+ The stack: RAM[256 ###2047]; RAM SP static: RAM[16 ###255]; (static i (f "' '"f.i "(

'"RAM+(=>%

local,argument,this,that: ! %( 2048 %+45#" (RAM LCL+ARG+THIS+THAT# i '('( "'RAM[segmentBase + i] constant: '! constant i "' 'i. pointer: #

Statics

3 12

. . .

4 5 14 15 1 13 2 THIS THAT SP LCL ARG TEMP 255

. . .

16 General purpose 2047

. . .

256 2048

Stack Heap

. . .

Host RAM

slide-2
SLIDE 2

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

VM implementation on the Hack platform

Statics

3 12

. . .

4 5 14 15 1 13 2 THIS THAT SP LCL ARG TEMP 255

. . .

16 General purpose 2047

. . .

256 2048

Stack Heap

. . .

Host RAM

  • 2%%%%'

RAM+%%$ 8!# (+%$ (%

push constant 1 pop static 7 (f push constant 5 add pop local 2 eq

  • =# ('(

E!$"' !! A=M )# ('('!'%###+ ''R13+R14+R15#

slide-3
SLIDE 3

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-4
SLIDE 4

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-5
SLIDE 5

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-6
SLIDE 6

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

The implementation of the VM’s stack on the host Hack RAM

" RAM " ."" SP "

  • $0
  • ,4
  • (
  • +

""0

  • +"

5

  • #
slide-7
SLIDE 7

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

Implementing the call g nArgs command

#

// In the course of implementing the code of f // (the caller), we arrive to the command call g nArgs. // we assume that nArgs arguments have been pushed // onto the stack. What do we do next? // We generate a symbol, let’s call it returnAddress; // Next, we effect the following logic: push returnAddress // saves the return address push LCL // saves the LCL of f push ARG // saves the ARG of f push THIS // saves the THIS of f push THAT // saves the THAT of f ARG = SP-nArgs-5 // repositions SP for g LCL = SP // repositions LCL for g goto g // transfers control to g returnAddress: // the generated symbol // In the course of implementing the code of f // (the caller), we arrive to the command call g nArgs. // we assume that nArgs arguments have been pushed // onto the stack. What do we do next? // We generate a symbol, let’s call it returnAddress; // Next, we effect the following logic: push returnAddress // saves the return address push LCL // saves the LCL of f push ARG // saves the ARG of f push THIS // saves the THIS of f push THAT // saves the THAT of f ARG = SP-nArgs-5 // repositions SP for g LCL = SP // repositions LCL for g goto g // transfers control to g returnAddress: // the generated symbol call g nArgs call g nArgs

<,### $2

slide-8
SLIDE 8

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

Implementing the function g nVars command

#

function g nVars function g nVars // to implement the command function g nVars, // we effect the following logic: g: repeat nVars times: push 0 // to implement the command function g nVars, // we effect the following logic: g: repeat nVars times: push 0

slide-9
SLIDE 9

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

Implementing the return command

#

// In the course of implementing the code of g, // we arrive to the command return. // We assume that a return value has been pushed // onto the stack. // We effect the following logic: frame = LCL // frame is a temp. variable retAddr = *(frame-5) // retAddr is a temp. variable *ARG = pop // repositions the return value // for the caller SP=ARG+1 // restores the caller’s SP THAT = *(frame-1) // restores the caller’s THAT THIS = *(frame-2) // restores the caller’s THIS ARG = *(frame-3) // restores the caller’s ARG LCL = *(frame-4) // restores the caller’s LCL goto retAddr // goto returnAddress // In the course of implementing the code of g, // we arrive to the command return. // We assume that a return value has been pushed // onto the stack. // We effect the following logic: frame = LCL // frame is a temp. variable retAddr = *(frame-5) // retAddr is a temp. variable *ARG = pop // repositions the return value // for the caller SP=ARG+1 // restores the caller’s SP THAT = *(frame-1) // restores the caller’s THAT THIS = *(frame-2) // restores the caller’s THIS ARG = *(frame-3) // restores the caller’s ARG LCL = *(frame-4) // restores the caller’s LCL goto retAddr // goto returnAddress return return

slide-10
SLIDE 10

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

Bootstrapping

SP = 256 // initialize the stack pointer to 0x0100 call Sys.init // call the function that calls Main.main SP = 256 // initialize the stack pointer to 0x0100 call Sys.init // call the function that calls Main.main $) 2" "# &-" 0Main0 0main# + ,-"0 Main.main

  • $0.vm
  • +.vm "78

),.vm

  • **(0Sys.vm0init.

+Sys.init *(5 0*(0call Main.main

  • +00## 0
slide-11
SLIDE 11

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

  • ?,@
  • +)

VM implementation over the Hack platform