Chapter 6 Quiz: The NASM Assembler

16 questions covering NASM syntax, directives, data declarations, macros, and output formats.


Multiple Choice

1. In NASM Intel syntax, the instruction mov rax, rbx means:

A) rbx ← rax (copy rax to rbx) B) rax ← rbx (copy rbx to rax) C) Exchange the values of rax and rbx D) Add rax and rbx, store result in rax


2. What does the NASM directive db 'A', 0x42, 66 emit?

A) Three separate declarations that cannot be combined B) Three bytes: 0x41, 0x42, 0x42 (since 'A'=65=0x41, and 66=0x42) C) Three bytes: 0x41, 0x42, 0x42 (correct—both 0x42 and 66 are the same value) D) A syntax error; you cannot mix character literals and hex in one declaration


3. The expression $ - msg` in `msglen equ $ - msg evaluates to:

A) The runtime address of the current instruction minus the address of msg B) The compile-time size of the data defined since msg, computed by NASM C) The distance between two labels (runtime, computed each execution) D) Always zero, because $ and msg are the same symbol


4. What is the difference between section .data and section .bss?

A) .data stores code; .bss stores data B) .data is read-only; .bss is read-write C) .data content is stored in the ELF file; .bss content is not (it's zero-initialized at load time) D) .data and .bss are synonymous in modern ELF


5. A label beginning with . (such as .loop) is:

A) A global label visible to the linker B) A local label, scoped to the enclosing non-dot label, allowing reuse in different functions C) A special label used only for data declarations D) A label that the assembler ignores


6. The NASM directive times 10 db 0 in .data:

A) Reserves 10 bytes of zero-initialized space (equivalent to resb 10 in .bss) B) Emits exactly 10 bytes, all zero, into the current section C) Defines a compile-time loop that runs 10 times D) Creates an array of 10 NULL pointer values


7. In NASM, %define MAX 1024 and MAX equ 1024 both make MAX usable as 1024 in code. What is the key difference?

A) %define is for strings; equ is for numbers B) %define is a preprocessor text substitution; equ creates an assembler numeric symbol that can reference positions ($) and labels C) %define only works in .data; equ only works in .text D) There is no difference; they are completely equivalent


8. The %%loop syntax within a %macro / %endmacro block:

A) Causes the macro body to loop B) Creates a label that is local to each expansion of the macro, preventing duplicate label errors C) Is invalid syntax; macros cannot contain labels D) Creates a global label with a numeric suffix


9. To assemble NASM source into a flat binary (no ELF headers), useful for bootloaders, you use:

A) nasm -f raw file.asm -o file.bin B) nasm -f bin file.asm -o file.bin C) nasm --binary file.asm -o file.bin D) nasm file.asm | strip > file.bin


10. The ORG 0x7C00 NASM directive tells the assembler:

A) Load the binary into memory at address 0x7C00 (the linker does this) B) Assume that the first byte of the output will be loaded at address 0x7C00; resolve all addresses accordingly C) Reserve 0x7C00 bytes at the start of the output D) Only valid for 16-bit code; invalid in 64-bit mode


11. What does resq 64 in .bss declare?

A) 64 bytes of reserved space B) 64 × 8 = 512 bytes of reserved quadword space C) 64 quadwords, all initialized to zero (stored in ELF) D) 64 quad-precision floating-point values


12. In NASM's %macro print_msg 2 declaration, what do %1 and %2 refer to?

A) The first and second bytes of the macro body B) The first and second arguments passed to the macro when it is used C) The first and second lines of the macro body D) Optional arguments; they default to 0 if not provided


True/False

13. In NASM Intel syntax, mov [rdi], 42 is a valid instruction that stores the immediate value 42 into the memory at the address in RDI.

True / False (If False, explain why and how to fix it)


14. The %include directive in NASM works identically to extern in C — it causes the contents of the included file to be compiled separately and linked in.

True / False


15. BITS 16 at the beginning of a NASM source file causes NASM to generate 16-bit machine code instructions, which is necessary for bootloaders that run in the CPU's initial real mode.

True / False


Short Answer

16. Explain the difference between the following two NASM approaches for defining an array of 8 zeroed quadwords:

Approach A (in .data):

section .data
    my_array  times 8 dq 0

Approach B (in .bss):

section .bss
    my_array  resq 8

Both approaches result in 8 zeroed 64-bit values at runtime. What is different about: a) The ELF file size b) The initial value guarantee c) When the memory is zeroed d) Whether you can write to the memory at runtime