程序代写代做代考 Introduction to Computer Systems :: Virtual Machine Language

Introduction to Computer Systems :: Virtual Machine Language
Worksheet 1:
Write the Hack instructions that implement each of the following VM commands:
1. sub
// pop y, pop x, compute x-y, push result
2. neg
// pop x, negate it, push result
1 @SP
2 AM=M-1 // decrement stack pointer and dereference it
3 D=M
4 A=A-1
5 M=M-D
// pop the value off the stack
// look at the next value on the stack
// and replace that value with the result of the subtraction
1 @SP
2 A=M-1 // dereference SP and point to top stack item 3 M=-M // don¡¯t change SP, just make value negative
ýBillingsley 2012-20

Introduction to Computer Systems :: Virtual Machine Language
3. gt
1 2 3 4 5 6 7 8 9
10
11
12
13
14
15
16
17
4. not
// pop y, pop x, if x>y push -1, else push 0
@SP
AM=M-1 //decrement SP and dereference
D=M //D holds y
A=A-1 //A points to x
D=M-D //D holds x-y; if >0 then true, else false
@FALSE
D;JLE
@SP // true, put -1 on the stack; SP points to TOS after operation A=M-1 //A points to where x was; put -1 there
M=-1
@CONTINUE
0;JMP
(FALSE)
@SP // false, put 0 on stack; SP points to TOS after operation A=M-1 // A points to where x was; put 0 there
M=0
(CONTINUE)
// pop x, push !x
1 @SP
2 A=M-1 //dereference pointer, find actual TOS 3 M=!M // invert that value
ýBillingsley 2012-20

Introduction to Computer Systems :: Virtual Machine Language
Worksheet 2:
Write the Hack instructions that implement each of the following VM commands:
1. push constant 20 // put 20 onto the stack
1 @20
2 D=A
3 @SP
4 A=M
5 M=D
6 @SP
7 M=M+1
2. push local 3
// put constant 20 into D
// dereference the SP
// put contents of D (constant 20) on stack
// increment SP
// put contents of RAM location local 3 onto the stack
@LCL
D=M
@3
A=D+A // A holds address of local 3
D=M // D holds contents of local 3
@SP
A=M // dereference SP
M=D // put D (contents of local 3) on stack
@SP
M=M+1 // increment SP
3. pop argument 2 // put top stack item into RAM location argument 2
@ARG
D=M
@2
D=D+A // D holds address of RAM location argument 2 @R13
M=D // put address of argument 2 into a temporary memory location @SP
AM=M-1 // decrement SP and dereference
D=M // D holds the value of the top item on the stack
@R13
A=M // A holds the address of RAM location argument 2 (dereference ptr) M=D // RAM location argument 2 holds what was on the top of the stack
ýBillingsley 2012-20