0% found this document useful (0 votes)
5 views

EOC-2- VirtualMachine-The Stack (1)

Uploaded by

D. Jivites
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

EOC-2- VirtualMachine-The Stack (1)

Uploaded by

D. Jivites
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

22AIE113

Elements of Computing Systems-II


Virtual Machine –Stack Arithmetic and
Implementation
Dr. Lekshmi C. R. , Assistant Professor

Center for Computational Engineering and Networking (CEN)


Amrita School of Artificial Intelligence, Coimbatore

Acknowledgment: Prof. Noam Nisan and Prof. Shimon Schocken


Stack

top

Basic operations
push: adds an element at the stack’s top
pop: removes the top element

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Stack

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Stack arithmetic

Applying a function f (that has n arguments)


• pops n values (arguments) from the stack,
• Computes f on the values,
• Pushes the resulting value onto the stack.

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Arithmetic operations

VM pseudocode (example)
// d = (2 – x) + (y + 9)
push 2
push x
sub
push y
push 9
add
add
21
pop d

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Arithmetic / Logical VM operations
Command Return value Return value
add x+y integer
sub x–y integer x
neg –y integer y
eq x == y boolean
gt x>y boolean
lt x<y boolean Each command pops as many
operands as it needs from the
and x And y boolean
stack, computes the specified
or x Or y boolean operation, and pushes the
not Not x boolean result onto the stack.

The big picture: Compilation


VM code is typically written by compilers;
Every high-level arithmetic-logical expression can be translated into
a sequence of operations on a stack, using these VM commands.

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 9
Logical operations

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

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Logical operations

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

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 11
The VM language (abstraction)

Push / pop commands Branching commands


• push segment i • label label

• pop segment i • goto label


• if-goto label

Arithmetic / Logical commands Function commands

• add, sub , neg • Function functionName nVars

• eq , gt , lt • Call functionName nArgs

• and, or , not • return

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 12
The big picture: Compilation
Source code (Jack) Compiled VM code
class Foo { ...
static int s1, s2; ...
function int bar (int x, int y) { ...
var int a, b, c; ...
push static 0
...
push argument 1
let c = s1 + y;
compiler add
...
pop local 2
}
} each variable has a kind: ...
local, argument, static, ...

a x s1 The compiler represents each symbolic variable


b y s2 as an entry in an abstract memory segment
c
...
The compiler translates each high-level
statement into a sequence of VM commands.
Abstract memory segments

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Memory segments
stack memory
segments
push
00
local Our VM model features 8
pop 11 argument abstract memory segments;
22 static
... constant The role of each segment:
this Later.
that
pointer
... temp

All segments are accessed the same way:

push / pop segment i

where i is a non-negative integer and segment is local, argument, static, ..., temp

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 14
The VM language: Abstraction

Push / pop commands


• push segment i Question
• pop segment i How can we make these commands “work”?

Arithmetic / Logical commands


• add, sub , neg
• eq , gt , lt
• and, or , not

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 15
The VM language: Implementation

Push / pop commands


• push segment i Question
• pop segment i How can we make these commands “work”?

Answer
Arithmetic / Logical commands We’ll develop a VM translator:
• add, sub , neg A program that translates each VM command
• eq , gt , lt into machine language instructions
• and, or , not

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 16
Implementing push constant i

Abstraction push constant 17

(example
values)

Implementation
push constant 17
The stack is stored in
the RAM, from
address 256 onward
The stack pointer is
stored in RAM[0]

(before) (after)

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 17
Implementing push constant i

Abstraction push constant 17

(example
values)

Pseudocode
// push constant 17
RAM[SP] = 17
SP++
Implementation
Hack assembly
The stack is stored in // D = 17
the RAM, from @17
D=A
address 256 onward
// RAM[SP]= D
The stack pointer is @SP
stored in RAM[0] A=M
M=D
// SP++
(before) @SP (after)
M=M+1

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 18
Implementing push constant i
Abstraction Implementation
VM code Assembly code
push constant i VM translator // D = i
@i
D=A
// RAM[SP]= D
@SP
Notes A=M
M=D
1. constant is not a “real segment”
// SP++
(used for VM syntax consistency) @SP
M=M+1
2. There is no pop constant i command.

Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 19
Thank you

You might also like