HACK Virtual Machine: Stack, Control, Implementation Flashcards

Week 2.2, 2.3 (36 cards)

1
Q

where does a virtual machine reside in computer strucutre

A

high level language & OS -> VM code -> machine language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the 2 compilation paradigms

A
  1. one-tier
  2. two-tier
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is one-tier complimation

A

source code -> compiler -> machine level instructions
- C/C++

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

pros of one-tier complimation

A
  1. generates efficient code
  2. can take full advantage of hardware
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

cons of one-tier compilation

A
  1. requires one compiler per target platform
  2. porting & maintaining software is time consuming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is two-tier compilation

A

source code -> compiler -> intermediate code -> VM translator -> executable code
- python, java, C#

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pros of two-tier compilation

A
  1. combile once, run everywher
  2. maximum portability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

cons of two-tier compilation

A
  1. requires VM translator per target platform
  2. generates less efficient machine code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

4 uses of a stack-based VM

A
  1. data movement
  2. arithmetic operations
  3. logical operations
  4. function calls
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

why is a stack-based VM a simpler paradigm

A
  • no registers
  • no direct RAM access
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

stack-based VM operations

A
  1. push
  2. pop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

9 stack-based VM commands

A
  1. add - x + y
  2. sub x - y
  3. neg - -y
  4. gt - x > y
  5. lt - x < y
  6. and - x AND y
  7. or - x OR y
  8. not - NOT y
  9. eq - x = y
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what are the 8 default segements of the HACK VM

A
  1. constant
  2. static
  3. local
  4. argument
  5. this
  6. that
  7. pointer
  8. temp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the constant segment

A

push constants to stack
- no pop allowed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the static segment

A

static variables seen by a function - not used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is the local segment

A

a functions local variables - used for function calls

17
Q

what is the argument segement

A

a functions arguments - used for function calls

18
Q

what is the this segment

A

variable segment, starting at pointer[0] - used for objects/arrays

19
Q

what is the that segment

A

variable segment, starting at pointer[0] - used for objects/arrays

20
Q

what is the pointer segment

A

start of segments this & that

21
Q

what is the temp segment

A

segment containing eight temporary variables

22
Q

what are the 3 branching commands

A
  1. label
  2. goto
  3. if-goto
23
Q

what does the label command do

A

defines the destenation for goto and if-goto commands

24
Q

what does the goto command do

A

jump to execute the command just after <label></label>

25
what does the if-goto command do
pop condition from stack - if condition is true, jump to command just after
26
what are the 3 function commands
1. function 2. call 3. return
27
what does the function command do
function - function declaration starts here
28
what does the call function do
call - call function , informimg it that arguments have been pushed into the stack - caller MUST push values onto the stack first
29
what does the return command do
return - return replaces arguments in the caller stack. control is transferred back to the caller. execution resumes from after the call command. - callee must push return value onto the stack before returning
30
syntax for argument, constant & local memory
- push constant - pop argument - push local
31
how is static memory assembled
assembler will map variable @var to the static segment & numbered within their file
32
how is temp memory segment implemented
numbered within the whole program
33
how is pointer memory implemented
- pop pointer i - push pointer i
34
how is the call command implemented
1. push arguments onto the stack 2. save the return address 3. save the caller's segement pointers 4. repostion ARG 5. repsoition LCL 6. go to execute the callee's code
35
how is the function command implemented
1. push 0s to initialise local segment 2. execute code 3. push result onto stack 4. call return
36
how is the return command implemented
1. copy return value to caller's stack 2. recycle memory used by callee 3. reinstate caller's segment pointers 4. jump to return address