Drill 5 Solutions

  1. Does the current compiler ever put the address of a memory location on the heap onto the stack?
  1. Does the current compiler ever put the address of a memory location on the heap into a register?
  1. How many 8-byte heap "cells" will be used when each of the following programs are compiled and executed? (Remember that each pair takes up two cells).

    • (+ (left (pair 1 2)) (right (pair 3 4)))
      
      • 4
    • (let ((x false)) 
          (pair 1 (if x (pair 2 (pair 3 (pair 4 false))) 
                          (pair 2 (pair 3 false))
                  )
          )
      )
      
        1. Remember that only one branch of the conditional will actually be executed: in this case, the else branch! We can't count heap usage just by counting occurences of pair.
  2. For each expression below, will it result in an error or a valid result when run in the interpreter we have built in class?

    program error valid result
    (+ 1 3 4) x
    (+ 1 (let ((x 2)) x)) x
    (+ 1 (let ((x 2)) false)) x
    (left (pair 1 false)) x
    (left (left (pair 1 false))) x
    (** 2 3) x
    (if true (* 2 3) (** 2 3)) x
  3. For each expression below, will it result in a compile-time error, a run-time error, or a valid result when run in the compiler we have built in class? (Assume that we have added error handling for all supported operations).

    program compile error runtime error valid result
    (+ 1 3 4) x
    (+ 1 (let ((x 2)) x)) x
    (+ 1 (let ((x 2)) false)) x
    (left (pair 1 false)) x
    (left (left (pair 1 false))) x
    (** 2 3) x
    (if true (* 2 3) (** 2 3)) x
  4. Which of the following sequences of assembly instructions is equivalent to the single instruction "call read_num"?

    • ( ) jmp read_num; sub rsp, 8; mov [rsp + 0], rip
    • (X) sub rsp, 8; mov [rsp + 0], rip; jmp read_num
    • ( ) mov [rsp + 0], rip; sub rsp, 8; jmp read_num
    • ( ) jmp read_num; mov [rsp + 0], rip; sub rsp, 8
  5. Does the current compiler ever put the address of a memory location on the heap into a register?

    • Yes. For example, (pair 1 2) will store 1 and 2 on the heap, and put the memory address of 1 into rax.
  6. Does the current compiler ever put the address of a memory location on the heap onto the stack?

    • Yes. For example, (let ((x (pair 1 2))) x) will store 1 and 2 on the heap, and put the memory address of 1 on the stack.