• Tidak ada hasil yang ditemukan

lecture 07 virtual machine I

N/A
N/A
Protected

Academic year: 2017

Membagikan "lecture 07 virtual machine I"

Copied!
30
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

Virtual

Machine

abstract interface

Software

hierarchy

Assembly

Language

abstract interface

Machine

Language

abstract interface

Hardware

Platform

abstract interface

abstract interface

Human

Thought

Abstract design

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

}

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

(5)

The big picture

. . .

RISC

machine

Intermediate code

other digital platforms, each equipped

with its own 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

(6)

Focus of this lecture

(yellow)

:

VM language

RISC

machine

language

Hack

machine

language

CISC

machine

language

. . .

written in

a high-level

language

VM

implementation

over CISC

platforms

VM imp.

over RISC

platforms

VM imp.

over the Hack

platform

VM

emulator

Some Other

language

Jack

language

Some

compiler

Some Other

compiler

Jack

compiler

. . .

Some

language

. . .

7, 8

(7)

The VM model and language

!

&

(

%

"

$

(

! .

/ 01

#21 .

3/-30

"

((

#

!

((

% '

"

(

!

"

%

'

"

4

5

# #

'

%

% .

% %

!

!

'

(8)

Yet another view

(poetic)

4

(

!

(

%

'

" # 6 !

( !

'

'

"

(

(

#5

7 8 "

(9)

Lecture plan

pop x

(pop into x, which is a variable)

push y

(y being a variable or a constant)

/ :

pop x

(pop into x, which is a variable)

push y

(y being a variable or a constant)

( %

This lecture

Next lecture

;

('

"

"

" !

(10)

Our VM model is

stack-oriented

<

!

!

'

'

"

!

(

'

static

+

% %

(11)

Data types

(

=> "

'

"

!

=> " ).

-32768

+ ### +

32767

:

!

0

-1

+

(

true

false

(12)

Memory access operations

(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

(14)

Evaluation of Boolean expressions

// (x<7) or (y=8)

push x

push 7

lt

push y

push 8

eq

or

// (x<7) or (y=8)

push x

push 7

lt

push y

push 8

eq

or

(15)
(16)

!

"

(

%

!

!

' (

(

%

!

"

-

!

!

"

!

!

"

!

!

"

4 "?

!

"

5 / 4(

5 / 4

5

!

3

!

"

!

"

7

+

+

!

+

!

"

"'

(17)

Memory segments and memory access commands

'

pop

memorySegment index

push

memorySegment index

7

memorySegment

static

+

this

+

local

+

argument

+

that

+

constant

+

pointer

+

temp

index

!

'

pop

memorySegment index

push

memorySegment index

7

memorySegment

static

+

this

+

local

+

argument

+

that

+

constant

+

pointer

+

temp

index

!

2

( +

memorySegment

%

static)

((

(

'

%

"

!

%

% .

"

"

! +

'

% '#

"

@

'

static

+

this

+

local

+

argument

+

that

+

constant

+

pointer

+

temp

(

+

'

"

!

(18)

VM programming

' %

"'

+

"'

$ % ! +

%

"'

###

%

8

+

(

(

.

A

+ % .

%

(

%

function

functionSymbol

// (

label

labelSymbol

// "

if-goto

labelSymbol

//

pop x

// (

x=true

+ ?

(

labelSymbol

//

function

functionSymbol

// (

label

labelSymbol

// "

if-goto

labelSymbol

//

pop x

// (

x=true

+ ?

(

labelSymbol

(19)

VM programming

(example)

function mult (x,y) {

int result, j;

result = 0;

return result;

}

function mult (x,y) {

int result, j;

result = 0;

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

if-goto end

push local 0

(20)

VM programming:

multiple functions

-( =

(

?

.java

(

#

7

%

'

(

+

( =

.vm

(

?

.class

(

# 1

(

%

+

(

(

(

#

1

'

!

(

+

'

(

4

(

5 + %

B

(

%

(

(

4

'5

&

+

main

(

C

%

'

call factorial

+

%

factorial

(

C

(21)

Lecture plan

pop x

(pop into x, which is a variable)

push y

(y being a variable or a constant)

/ :

pop x

(pop into x, which is a variable)

push y

(y being a variable or a constant)

( %

(22)

Implementation

( %

"

# #

!

"

# #

$

$

%

"

8

'

% %

%

"

!

!

"'

C

(23)
(24)

VM implementation on the Hack platform

The stack:

"

+

!

(

(

'#

(

(

%

(

(

static

+

constant

+

temp

+

pointer:

; "

'

+

(

(

local,argument,this,that:

(

! C

(

% +

!

' (

(

(

'

RAM

#

Host

(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

%

+

4

5#

"

(

RAM

LCL

+

ARG

+

THIS

+

THAT

#

i

' (

' (

"'

RAM[segmentBase +

i

]

constant:

'

!

constant

i

"'

(26)

VM implementation on the Hack platform

Statics

3

12

. . .

4 5

14 15 0 1

13 2 THIS THAT SP LCL ARG

TEMP

255

. . .

16 General

purpose

. . .

256

Stack

Host

RAM

2 %

%

%

%

'

RAM

+ %

%

$

8

!

#

(

+

%

$

(

%

push constant 1

pop static 7

(

f

push constant 5

add

pop local 2

eq

(27)

Proposed VM translator implementation:

Parser module

Parser: Handles the parsing of a single

.vm

file, and encapsulates access to the input code. It reads VM commands,

parses them, and provides convenient access to their components. In addition, it removes all white space and comments.

Routine

Arguments

Returns

Function

Constructor

Input file /

stream

--Opens the input file/stream and gets ready to

parse it.

hasMoreCommands

--

boolean

Are there more commands in the input?

advance

--

--Reads the next command from the input and

makes it the current command. Should be called

only if

hasMoreCommands

is true.

Initially there is no current command.

commandType

--C_ARITHMETIC

,

C_PUSH

,

C_POP

,

C_LABEL

,

C_GOTO

,

C_IF

,

C_FUNCTION

,

C_RETURN

,

C_CALL

Returns the type of the current VM command.

C_ARITHMETIC

is returned for all the arithmetic

commands.

arg1

--

string

Returns the first arg. of the current command.

In the case of

C_ARITHMETIC

, the command itself

(

add

,

sub

, etc.) is returned. Should not be called

if the current command is

C_RETURN

.

arg2

--

int

Returns the second argument of the current

command. Should be called only if the current

command is

C_PUSH

,

C_POP

,

C_FUNCTION

, or

(28)

Proposed VM translator implementation:

CodeWriter module

CodeWriter: Translates VM commands into Hack assembly code.

Routine

Arguments

Returns

Function

Constructor

Output file / stream

--

Opens the output file/stream and gets ready to

write into it.

setFileName

fileName (string)

--

Informs the code writer that the translation of a

new VM file is started.

writeArithmetic

command (string)

--

Writes the assembly code that is the translation

of the given arithmetic command.

WritePushPop

command (

C_PUSH

or

C_POP

),

segment (string),

index

(

int

)

--

Writes the assembly code that is the translation

of the given

command

, where

command

is either

(29)

Perspective

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 Other

compiler compiler

. . .

(30)

The big picture

!

!

01

-30

-D

-D

#21 "

"

'

G+ @

F

=B+ ==

=)

:

Referensi

Dokumen terkait

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

Nurul Hidayah Bengkalis..

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

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