01

A comprehensive review.

Assembler

What Hack assembly is produced?

  1. push constant 15

  2. How do you get the top item on the stack into the D register?

  3. Write assembly for push local 2.

  4. What registers is the caller responsible for saving? Write pseudocode and explain how the save/restore mechanism works.

  5. What register setup does the function do upon entry?

  6. 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).

  7. 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;
     }
    
  8. 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

  1. Write the resulting token stream.
  2. Write the parse tree. (Variations are possible.)
function int good (int a, String b) {
    var boolean c;
    return a;
}

Code Generation

  1. What is the symbol table produced?
  2. 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
[...]