Chapter 12 Quiz: Arrays, Strings, and Data Structures


Question 1. To access int64_t arr[i] where the base address is in RDI and i is in RCX, the correct addressing is:

(A) [rdi + rcx] (B) [rdi + rcx*4] (C) [rdi + rcx*8] (D) [rdi*rcx + 8]


Question 2. The Direction Flag (DF) affects string instructions by:

(A) Determining whether RSI or RDI is used as the source pointer (B) Determining whether the instruction reads or writes memory (C) Controlling whether RSI/RDI are incremented (DF=0) or decremented (DF=1) after each operation (D) Setting the number of bytes processed per iteration


Question 3. REP STOSB with AL = 0x41 and RCX = 10 will:

(A) Copy 10 bytes from [RSI] to [RDI] (B) Fill 10 bytes starting at [RDI] with the value 0x41 (C) Compare 10 bytes at [RSI] with 0x41 (D) Scan [RDI] for the byte 0x41, stopping after at most 10 comparisons


Question 4. After REPNE SCASB completes (found the target byte), where does RDI point?

(A) At the matching byte (B) One byte PAST the matching byte (C) One byte BEFORE the matching byte (D) Back to its original starting position


Question 5. In the strlen implementation using REPNE SCASB, why do we need both NOT RCX and DEC RCX after the instruction?

(A) NOT RCX converts the count to a positive number, DEC RCX removes the null byte from the count (B) NOT RCX makes the count negative, DEC RCX adjusts for the off-by-one (C) Both NOT operations are needed to handle the case where no null is found (D) NOT RCX is not needed; only DEC RCX is required


Question 6. A C struct struct { char a; int64_t b; } has a at offset 0. What is the offset of b?

(A) 1 (immediately after a) (B) 4 (aligned to 4-byte boundary) (C) 8 (aligned to 8-byte boundary, the size of b) (D) 7 (7 bytes of padding after a)


Question 7. REPE CMPSB compares two memory regions and continues while:

(A) The bytes are not equal and RCX > 0 (B) The bytes are equal and RCX > 0 (C) CF = 1 and RCX > 0 (D) SF = 0 and RCX > 0


Question 8. For int32_t matrix[5][4], which expression correctly gives the address of matrix[r][c]?

(A) base + r*4 + c (B) base + (r*4 + c) * 4 (C) base + (r*5 + c) * 4 (D) base + r*5*4 + c*4


Question 9. LODSB does which of the following?

(A) Loads [RDI] into AL and increments RDI (B) Stores AL into [RSI] and increments RSI (C) Loads [RSI] into AL and increments RSI (D) Loads [RSI] into AL and decrements RSI


Question 10. When does REP MOVSB produce INCORRECT results for overlapping regions?

(A) When src and dst are the same pointer (B) When dst > src and the regions overlap (copies forward, overwriting unread source bytes) (C) When src > dst and the regions overlap (D) REP MOVSB always handles overlapping correctly


Question 11. The System V ABI requires that DF (Direction Flag) be:

(A) 1 (set) at function call boundaries (B) 0 (cleared) at function call boundaries (C) Unspecified — the callee must check DF before using string instructions (D) Alternating on each successive call


Question 12. To traverse a singly-linked list where next is at offset 8, the loop body uses:

(A) inc rdi (advance to next node) (B) add rdi, 8 (advance past the value field) (C) mov rdi, [rdi + 8] (follow the next pointer) (D) lea rdi, [rdi + 16] (skip both value and next fields)


Question 13. The REPNE prefix causes the instruction to repeat while:

(A) ZF = 0 AND RCX > 0 (not equal, i.e., mismatch not yet found) (B) ZF = 1 AND RCX > 0 (equal, i.e., match not yet found) (C) CF = 1 AND RCX > 0 (D) Always repeats until RCX = 0


Question 14. (2 points) Write the NASM code to use rep movsq to copy 128 bytes from [rsi] to [rdi]. How many iterations does this take, and how does it compare to using rep movsb for 128 bytes in terms of loop iterations?


Question 15. Which of the following correctly implements a bounds check for arr[i] where the array has n elements?

(A) cmp rcx, n; jg .out_of_bounds (signed greater than) (B) cmp rcx, n; jae .out_of_bounds (unsigned above or equal) (C) test rcx, rcx; jl .out_of_bounds (signed less than zero) (D) Option (B) handles both i < 0 (as unsigned huge) and i >= n in one comparison


Question 16. What is the performance characteristic of rep movsb for a 1000-byte copy on a modern Intel processor (Ice Lake)?

(A) Slower than an explicit loop in all cases (B) Achieves near-memory-bandwidth throughput via the "fast string" microarchitecture feature (C) Limited to 1 byte per clock cycle, slower than rep movsq (D) Only efficient when the source and destination are in different cache levels


Question 17. AoS (Array of Structs) vs. SoA (Struct of Arrays): SoA is preferred for SIMD performance because:

(A) SoA has less memory usage (B) SoA enables loading multiple consecutive values of the same field simultaneously with SIMD (C) AoS has alignment issues that SoA avoids (D) SoA allows smaller array element sizes


Question 18. After repe cmpsb with RCX = 5, comparing "ABCDE" vs "ABXDE" (starting at the beginning of each), what is the final state?

(A) RCX = 0, ZF = 1 (all bytes compared, equal) (B) RCX = 2, ZF = 0 (stopped at 'C' vs 'X' mismatch after 3 comparisons) (C) RCX = 2, ZF = 0 (stopped at the first mismatch: position 2, after comparing positions 0 and 1) (D) RCX = 4, ZF = 0 (stopped at the first mismatch: only 1 byte compared)


Question 19. In the linked list traversal:

.loop:
    mov rdi, [rdi + 8]    ; rdi = node->next
    test rdi, rdi
    jnz .loop

This loop has a bug: it visits the first node but:

(A) It never terminates because test never sets ZF (B) It skips the first node's data (advances before processing) (C) It would work correctly as written for traversal (D) It would dereference the null pointer when the list ends


Question 20. For struct { int8_t a; int8_t b; int8_t c; }, what is the sizeof this struct?

(A) 1 (packed into one byte) (B) 3 (no padding needed, all 1-byte aligned) (C) 8 (padded to 8-byte alignment) (D) 4 (padded to 4-byte alignment)