01
Assembler
What Hack assembly is produced?
-
push constant 15
-
How do you get the top item on the stack into the D register?
-
Write assembly for
push local 2
. -
What registers is the caller responsible for saving? Write pseudocode and explain how the save/restore mechanism works.
-
What register setup does the function do upon entry?
-
Who is responsible for restoring the registers when a function call ends? How does the answer get returned to the caller? Explain how that works, including pseudo-code.
Examples of pseudo-code:
(pop THAT)
(set! SP ARG)
. -
How does a function give an answer back to the caller?
function int f(int n) { var int answer; let answer = 2 * n; return answer; }
-
What code is generated for this call to a two argument function? Assume both arguments are already pushed on the stack. You do not need to repeat the details of your code from question 3 (“how do you push onto the stack?). Use pseudocode to emphasize the main ideas.
//push constant 10 //push constant 15 call My.Work 2
Parser
- Write the resulting token stream.
- Write the parse tree. (Variations are possible.)
function int good (int a, String b) {
var boolean c;
return a;
}
Code Generation
- What is the symbol table produced?
- Write the VM code.
function int work (int a) {
var int b, c;
let c = Math.random(256); // [0,255]
return a+c;
}
Jack
Print the pattern below so that it fills the screen.
A
AB
ABA
ABABB
ABABBA
ABABBABBB
ABABBABBBA
[...]