• Tidak ada hasil yang ditemukan

lecture 08 virtual machine II

N/A
N/A
Protected

Academic year: 2017

Membagikan "lecture 08 virtual machine II"

Copied!
24
0
0

Teks penuh

(1)

www.nand2tetris.org

(2)

Where we are at:

Assembler

Chapter 6 H.L. Language

&

Operating Sys.

abstract interface

Compiler

Chapters 10 - 11

VM Translator

Chapters 7 - 8

Computer Architecture

Chapters 4 - 5

Gate Logic

Chapters 1 - 3 Electrical

Engineering

Physics

Virtual Machine

abstract interface

Software hierarchy

Assembly Language

abstract interface

Hardware hierarchy Machine

Language

abstract interface

Hardware Platform

abstract interface

Chips & Logic Gates

abstract interface Human

Thought

Abstract design

(3)

The big picture

. . .

RISC machine

VM language

other digital platforms, each equipped with its VM implementation RISC

over CISC platforms

VM imp. over RISC

platforms

VM imp. over the Hack

platform VM

emulator Some Other

language

Jack language

Some

compiler Some Othercompiler

Jack compiler

. . .

Some

language

. . .

Chapters 1-6

Chapters 7-8

Chapters 9-13

A Java-based emulator is included in the course software suite

(4)

The VM langauge

! "

#

$

% &

add

sub

neg

eq

gt

lt

and

or

not

pop x (pop into x, which is a variable)

push y (y being a variable or a constant)

$

% &

add

sub

neg

eq

gt

lt

and

or

not

pop x (pop into x, which is a variable)

push y (y being a variable or a constant)

label

goto

if-goto

'

function

call

return

label

goto

if-goto

'

function

call

return

previous lecture

(5)

The compilation challenge

class Main { static int x;

function void main() {

// Inputs and multiplies two numbers var int a, b, c;

let a = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); let c = Keyboard.readInt(“Enter a number”); let x = solve(a,b,c);

return; }

}

// Solves a quadearic equation (sort of) function int solve(int a, int b, int c) {

var int x;

class Main { static int x;

function void main() {

// Inputs and multiplies two numbers var int a, b, c;

let a = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); let c = Keyboard.readInt(“Enter a number”); let x = solve(a,b,c);

return; }

}

// Solves a quadearic equation (sort of) function int solve(int a, int b, int c) {

(6)

The compilation challenge / two-tier setting

if (~(a = 0))

x = (-b+sqrt(b*b–4*a*c))/(2*a) else

x = -c/b if (~(a = 0))

x = (-b+sqrt(b*b–4*a*c))/(2*a) else

if-goto elseLabel push b

neg push b push b call mult push 4 push a call mult push c call mult call sqrt add

push 2 push a call mult div

pop x

goto contLable elseLabel:

push c neg push b call div pop x contLable:

push a push 0 eq

if-goto elseLabel push b

neg push b push b call mult push 4 push a call mult push c call mult call sqrt add

push 2 push a call mult div

pop x

goto contLable elseLabel:

push c neg push b call div pop x

VM translator

.

.

+

))

(7)

// Computes 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

// Computes 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 +

The compilation challenge

, ,

!

)

/

)

0

"

)

1

")

! ")- "

0

)

"

%

, %

")

0 # #

(8)

Lecture plan

$

% &

add

sub

neg

eq

gt

lt

and

or

not

pop x (pop into x, which is a variable)

push y (y being a variable or a constant)

$

% &

add

sub

neg

eq

gt

lt

and

or

not

pop x (pop into x, which is a variable)

push y (y being a variable or a constant)

label

goto

if-goto

'

function

call

return

label

goto

if-goto

'

function

call

return

(9)

Program flow commands in the

VM

language

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

(10)

Lecture plan

$

% &

add

sub

neg

eq

gt

lt

and

or

not

pop x (pop into x, which is a variable)

push y (y being a variable or a constant)

$

% &

add

sub

neg

eq

gt

lt

and

or

not

pop x (pop into x, which is a variable)

push y (y being a variable or a constant)

label

goto

if-goto

'

function

call

return

label

goto

if-goto

'

function

call

return

(11)

Subroutines

(

6

2

&

,

)

"

%

%

###

)

")

)

+

,

)

+

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

(12)

Subroutines in the

VM

language

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 " 7 8 ,

...

// 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

...

,

(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

"

(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

" 7 8 ,

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

...

local, argument, this, that, pointer)

+ ,

(15)

The function-call-and-return protocol

+

. , 0 argument 5

local 5 5

+ static static

0 " "

& , 0 " return#

. , 0 argument 5

local 5 5

+ 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

(16)

.

f

g

0

(

f

=

2

call

(

f

$

0

5

>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

(17)

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

"

RAM

"

. "

"

SP "

$

0

,

4

(

+

"

"0

+

"

5

(18)

Implementing the

call

g nArgs

command

0

#

// 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

< , ###

(19)

Implementing the

function

g nVars

command

0

#

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:

(20)

Implementing the

return

command

0

#

// 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

(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 "

"

#

&

- "

0

Main

0

0

main

#

+

,

- "

0

Main.main

$

0

.vm

+

.vm

" 7

8

) ,

.vm

*

*(

0

Sys.vm

0

init

.

+

Sys.init

*(

5

0

*( 0

call Main.main

(22)

?, @

+ )

(23)
(24)

Perspective

&

1

A

)

(

?

,

<

. . .

VM language

RISC machine

language Hack

CISC machine

language

. . .

written in a high-level

language

. . .

VM implementation

over CISC platforms

VM imp. over RISC

platforms Translator

VM emulator Some Other

language Jack

Some

compiler Some Othercompiler compiler

. . .

Some

language

. . .

&

(

$

0

,

"

0 B

$

)

? #

Referensi

Dokumen terkait

Sesuai hasil evaluasi tersebut di atas, maka didapatkan penawaran yang memenuhi persyaratan administrasi, teknis dan harga sebagai calon pemenang lelang yaitu :c.

PERSENTASE BIAYA PENDIDIKAN MENURUT SUMBER DAN SATUAN BIAYA TINGKAT SD PROVINSI GORONTALO.. TAHUN

In addition, this research is aimed at finding out whether the English textbooks used in one elementary school in Cimahi is appropriate for young learners and to

pengalaman mengajar,subjek yang di ajar tahun semasa,tugas khas guru dalam e operasi dan melantik pegawai pendaftar dalam modul keberadaan guru dalam e operasi....

Sistem distribusi tiga fasa dengan empat kawat , Pada tipe ini, sisi sekunder (output) trafo distribusi terhubung star,dimana saluran netral diambil dari titik

bagian – bagian otot mana saja yang mengalami gangguan nyeri atau keluhan dari. tingkat rendah (tidak ada keluhan/cedera) sampai dengan keluhan

namun bukan menajdi keharusan bagi masyarakat Melayu untuk melakukan suatu upacara adat Melayu, karena dengan mengikuti upacara adat yang dialkukan oleh elite Melayu sudah menjadi

Gangguan kualitas tidur pada pasien post operasi akibat nyeri dapat diatasi dengan terapi farmakolofis dan nonfarmakologi, untuk terapi farmakologi atau terapi komplementer