Chapter 21 Quiz: Understanding Compiler Output

Select the best answer for each question.


1. In AT&T syntax, movq %rbx, %rax means:

a) rbx = rax b) rax = rbx c) Move the address of rbx to rax d) Swap rbx and rax


2. What does the q suffix in movq indicate?

a) The operation is "quick" (single cycle) b) The operand size is 64-bit (quadword) c) The instruction uses the REX prefix d) The operand is a queue pointer


3. In AT&T syntax, -8(%rbp) is equivalent to which Intel (NASM) memory operand?

a) [rbp + 8] b) [-8 + rbp] c) [rbp - 8] d) rbp[-8]


4. What GCC command-line flag produces assembly output in a file?

a) gcc -asm program.c b) gcc -S program.c c) gcc -assembly program.c d) gcc -o program.s program.c


5. GCC -O0 stores function arguments immediately to the stack. GCC -O2 often doesn't. Why?

a) -O2 uses different calling conventions than -O0 b) -O2 can keep values in registers throughout a function without spilling to memory c) -O2 changes the ABI to use fewer arguments d) -O2 is buggy and loses argument values


6. What GCC optimization converts if (x < 0) return -x; else return x; into a CMOVNS instruction?

a) Dead code elimination b) Loop invariant code motion c) Branchless code generation (conditional move) d) Constant folding


7. For a switch statement with cases 0-11 (all consecutive), GCC -O2 is likely to generate:

a) 12 separate if-else comparisons b) A jump table with an indirect jmp instruction c) A binary search through the cases d) A call to a hash function


8. The leaq (%rdi,%rdi,8), %rax instruction computes:

a) rax = rdi * 8 b) rax = rdi + rdi * 8 = rdi * 9 c) rax = address of rdi + 8 d) rax = (rdi + rdi8)


9. When GCC -O2 replaces y / 7 with a multiply-high + shift sequence, this is called:

a) Constant folding b) Strength reduction (replacing division with cheaper multiply) c) Loop unrolling d) Dead code elimination


10. What does gcc -S -fverbose-asm program.c add to the output?

a) Verbose error messages for syntax errors b) Comments linking assembly instructions to C source variables c) Full function documentation in the output file d) Detailed instruction timing information


11. In at&T syntax, cltq does what?

a) Clears the top quadword register b) Sign-extends EAX to RAX (Convert Long To Quad) c) Converts floating-point to long d) Clears the carry flag in the top bits


12. Compiler Explorer (godbolt.org) is primarily useful for:

a) Compiling and running programs online b) Visually comparing assembly output across compilers, optimization levels, and architectures c) Debugging C programs with breakpoints d) Static analysis and finding memory leaks


13. GCC "tail call optimization" on a recursive function:

a) Adds extra tail bytes to function calls for debugging b) Converts a tail-recursive call into a loop (avoiding stack frame accumulation) c) Moves the function to the end of the executable d) Adds tail padding to align the function


14. What GCC flag switches assembly output to Intel (NASM-like) syntax instead of AT&T?

a) -mintel b) -masm=intel c) -fasm-syntax=intel d) -intel-asm


15. At GCC -O0, local variables are typically stored:

a) Only in registers b) At fixed offsets from RBP in the stack frame c) In the BSS section d) In the red zone below RSP


16. What does the magic number in movl $1717986919, %edx (used for integer division) represent?

a) The divisor b) A precomputed multiplication constant that approximates dividing by a specific constant c) The result of the division d) A memory address


17. When does GCC use an imulq instruction for division?

a) Never — GCC always uses idiv for division b) When dividing by a variable (unknown at compile time) c) When dividing by a known constant, as part of the multiply-high trick d) When the quotient exceeds 32 bits


18. Loop invariant code motion moves computation:

a) Into the loop for better cache locality b) Out of the loop when the result doesn't change between iterations c) To a different loop that executes first d) To compile time (constant folding)


19. Which optimization level is typically used for production software releases?

a) -O0 (debug) b) -O1 (basic) c) -O2 (standard) d) -O3 (aggressive)


20. In GCC's jump table for a switch statement, the instruction jmp *.Ljumptable(,%rdi,8) computes the jump target as:

a) Ljumptable + rdi b) *(Ljumptable + rdi * 8) — loads a 64-bit address from the table c) Ljumptable * rdi * 8 d) rdi (the switch variable directly)