Drill 3 Solutions

  1. Which of the following are valid expressions in our source language, as it stands at the end of Wednesday's lecture?

    • (add1 4)
    • (add1 (num? 4))
    • (num? (add1 4))
    • (num? (not 4))
    • (zero? 4)
    • (zero? not)
    • (if (num? 4) (add1 4) (sub1 4))
    • (if (add1 4) (num? 4) (sub1 4))
    • (if num? 4 4)
  2. Remember that the runtime type-tag for numbers is 0b00; the type-tag for booleans is 0b0011111. Later in the semester we'll add lists to our language. Given the current type tags, which of the following could we use as the tag for list values?

    • 0b11111111
    • 0b10011111
    • 0b0011100
    • 0b100
    • 0b0011101
  3. The ocaml code below implements a bool? operation in the interpreter. (bool? e) should evaluate to true if e evaluates to a boolean, and false otherwise. What should BLANK be replaced with?

    | Lst [Sym "bool?"; arg] -> 
    (match interp_exp arg with BLANK _ -> Boolean true | _ -> Boolean false)
    
    • Boolean
  4. The ocaml code below implements a bool? operation in the compiler. (bool? e) should evaluate to true if e evaluates to a boolean, and false otherwise.

    | Lst [Sym "bool?"; arg] ->
    compile_exp arg 
    @ [And (Reg Rax, Imm binary_number_1); Cmp (Reg Rax, Imm binary_number_2)]
    @ zf_to_bool
    

    What should binary_number_1 be replaced with? Your answer should be a number in binary format (i.e., "0b" followed by some 0s and 1s).

    • 0b1111111

    What should binary_number_2 be replaced with? Your answer should be a number in binary format (i.e., "0b" followed by some 0s and 1s).

    • 0b0011111
  5. Our zf_to_bool snippet is a list of assembly instructions that convert the ZF flag into a boolean and store it in rax--i.e., after the snippet is executed rax should store the runtime representation of true if ZF was set to 1 and false otherwise. Here's another way we could do the same conversion, using jumps instead of the setz instruction.

        jz label_1
        mov rax, binary_number_1
        jmp label_2 
    label_1:
        mov rax, binary_number_2 
    label_2:
    

    What should binary_number_1 be replaced with? Your answer should be a number in binary format (i.e., "0b" followed by some 0s and 1s).

    • 0b00011111

    What should binary_number_2 be replaced with? Your answer should be a number in binary format (i.e., "0b" followed by some 0s and 1s).

    • 0b10011111