Chapter 10 Quiz: Control Flow
Question 1.
After mov rax, -1; cmp rax, 0, which jump is taken?
(A) ja (jump if above, unsigned)
(B) jg (jump if greater, signed)
(C) jl (jump if less, signed)
(D) jb (jump if below, unsigned)
Question 2. The mnemonic JE and JZ refer to:
(A) Different instructions with different encodings (B) The same instruction with different names; both check ZF = 1 (C) JE checks ZF, JZ checks CF (D) JZ is a pseudoinstruction; only JE is real
Question 3.
What is the assembly idiom for if (x != 0) where x is in RAX, using TEST?
(A) cmp rax, 0; jne body
(B) test rax, rax; jnz body
(C) test rax, 1; jne body
(D) Both (A) and (B) are correct
Question 4. In a jump table, what must always appear before the indirect jump?
(A) A call to a range-check function (B) A bounds check (upper limit comparison) to prevent out-of-bounds table access (C) A multiplication to scale the index (D) A LEA to compute the absolute address
Question 5.
The LOOP instruction decrements RCX and:
(A) Jumps if RCX > 0 (signed) (B) Jumps if RCX != 0 (same as JNZ after DEC) (C) Always jumps (it is an unconditional loop) (D) Jumps if RCX < 0 (signed)
Question 6.
CMOVL rax, rbx performs:
(A) rax = rbx if ZF = 1
(B) rax = rbx if CF = 1
(C) rax = rbx if SF ≠ OF (signed less than)
(D) rax = rbx if SF = OF (signed greater or equal)
Question 7. Which loop structure requires NO initial condition check (the body always executes at least once)?
(A) while loop
(B) for loop
(C) do-while loop
(D) All loops require an initial check
Question 8.
The conditional jump JL checks what condition?
(A) CF = 1 (unsigned less than) (B) ZF = 1 (equal) (C) SF ≠ OF (signed less than) (D) SF = 1 (sign flag set)
Question 9. When is CMOV likely to be SLOWER than a conditional branch?
(A) When the branch is 50% predictable (B) When the branch is 99% always-taken (highly predictable) (C) When the values being selected are 64-bit integers (D) When the code is inside a loop
Question 10. In the "count down" loop optimization, why is counting down often preferred?
(A) DEC is faster than INC (B) The loop condition compares against zero, which is implicit after DEC (no separate CMP needed) (C) Modern CPUs process descending addresses faster (D) Fewer bytes are needed to encode negative numbers
Question 11. To compare two unsigned 64-bit values and jump if the first is greater, use:
(A) cmp rax, rbx; jg label
(B) cmp rax, rbx; ja label
(C) cmp rax, rbx; jge label
(D) cmp rax, rbx; jns label
Question 12.
What does jmp [rax*8 + table_base] do?
(A) Jumps to the address stored at table_base + rax*8
(B) Jumps to the address table_base + rax*8
(C) Jumps to address rax*8 in the data segment
(D) This is an invalid instruction encoding
Question 13. For the code:
cmp rdi, rsi
jl .less
; falls through to "greater or equal" case
If RDI = 0xFFFFFFFFFFFFFFFF and RSI = 1, does the jump to .less execute?
(A) Yes, because 0xFFFF...FFFF < 1 in unsigned comparison (B) No, because 0xFFFF...FFFF = -1 in signed comparison, and -1 < 1 is true — wait, that means JL IS taken (C) No, because 0xFFFF...FFFF > 1 in both signed and unsigned comparison (D) Yes, because JL checks the carry flag
Question 14. (2 points)
Write the most efficient NASM code to compute rax = (rdi >= 0) ? rdi : 0 (clamp to non-negative). Use CMOV. You may use one temporary register.
Question 15. A jump to a label with a short-jump encoding (8-bit offset) can reach at most how far?
(A) ±32 bytes (B) ±127 bytes from the next instruction's address (C) ±256 bytes (D) ±2GB (same as near jump)
Question 16. The do-while loop translates most naturally to which assembly structure?
(A) Test at top, jump to end if condition false, then body, then unconditional jump to test (B) Body first, then test at bottom, conditional jump back to body start (C) Requires two conditional jumps (one at top, one at bottom) (D) Cannot be represented without a LOOP instruction
Question 17.
CMP rax, 0 vs. TEST rax, rax — which is preferred for zero checking, and why?
(A) CMP rax, 0 because it is more readable
(B) TEST rax, rax because it is shorter (2 bytes for the 64-bit form vs. 4) and does the same thing for ZF and SF
(C) They produce different flag results for negative values
(D) CMP rax, 0 is preferred because TEST is deprecated in 64-bit mode
Question 18.
Which of the following represents the correct assembly for if (a > 0 && b > 0)?
(A)
test rdi, rdi
jle .false
test rsi, rsi
jle .false
(B)
test rdi, rdi
test rsi, rsi
jle .false
(C)
and rdi, rsi
jle .false
(D) All of the above are equivalent
Question 19. In a compiler-generated jump table for a switch statement with cases 10, 11, 12, 13, 14, GCC typically:
(A) Creates a 15-entry table and uses the raw value as index (B) Subtracts the minimum case value (10) before using the result as index (C) Creates a hash table for O(1) lookup (D) Falls back to a linear chain of comparisons regardless
Question 20. The branch misprediction penalty on a modern Intel processor is approximately:
(A) 1-2 cycles (B) 5-8 cycles (C) 10-20 cycles (D) 50-100 cycles