Chapter 20 Quiz: Calling C from Assembly and Assembly from C
Select the best answer for each question.
1. In NASM, which directive makes an assembly symbol visible to other object files (including C)?
a) extern
b) global
c) public
d) export
2. When calling printf from assembly with two integer arguments and no floating-point arguments, what must you set in RAX (AL)?
a) RAX = 2 (number of integer arguments) b) RAX = 0 (number of XMM register arguments used) c) RAX = 1 (the format string is counted) d) RAX can be any value — it doesn't matter for integer-only calls
3. After calling malloc in assembly, which register holds the returned pointer?
a) RDI b) RSI c) RAX d) RBX
4. Why must you save malloc's return value into a callee-saved register before calling free?
a) free requires the pointer to be in a callee-saved register
b) A subsequent function call (including free) will clobber RAX and other caller-saved registers
c) malloc returns different values depending on which register is used
d) The C runtime automatically saves RAX on function calls
5. Which of the following is NOT a callee-saved register in System V AMD64 ABI?
a) RBX b) R12 c) RDI d) RBP
6. The "red zone" is:
a) The first 128 bytes of the stack above RSP, reserved for the OS b) A 128-byte area below RSP that leaf functions can use without adjusting RSP c) The memory region between the stack and heap d) The 16-byte alignment padding added before function calls
7. Which function prototype requires the caller to pass a hidden pointer as the first argument?
a) int compute(int a, int b) — returns int
b) double compute(double a) — returns double
c) struct Large compute(void) — returns a 100-byte struct
d) char *compute(void) — returns a pointer
8. To declare an assembly function fast_sort callable from C++ without name mangling issues, the C++ header should contain:
a) extern fast_sort;
b) extern void fast_sort(int*, int);
c) extern "C" void fast_sort(int* arr, int n);
d) #pragma asm_extern fast_sort
9. When calling printf from assembly with the format "%f" and one double value in XMM0, AL must be set to:
a) 0 b) 1 c) 2 d) 8 (number of XMM registers available)
10. A struct {int64_t a; int64_t b;} (16 bytes) is passed as the first argument to a function. How is it passed per System V AMD64 ABI?
a) As a pointer (too large for registers)
b) a in RDI, b in RSI (two 64-bit halves in two argument registers)
c) Both values packed into a single 128-bit register
d) Pushed onto the stack before the call
11. The instruction mov rax, [rel global_var] differs from mov rax, global_var in that:
a) They are identical
b) [rel global_var] loads the VALUE at the address; global_var loads the ADDRESS
c) [rel global_var] uses absolute addressing; global_var uses RIP-relative
d) mov rax, global_var is not valid NASM syntax
12. When must a function NOT use the red zone for local variable storage?
a) When the function uses floating-point operations b) When the function calls other functions (non-leaf function) c) When the function uses callee-saved registers d) When the function has more than 2 arguments
13. How many bytes of stack alignment padding (beyond the return address) are needed after push rbp; mov rbp, rsp if RSP must be 16-byte aligned before calling another function and you have no local variables?
a) 0 bytes (push rbp already restores 16-byte alignment) b) 8 bytes c) 16 bytes d) 32 bytes
14. xor eax, eax at function entry before a call to printf (with no FP args) achieves what?
a) Initializes the return value to 0 b) Sets AL = 0, indicating no XMM registers are used for FP arguments (variadic convention) c) Clears the carry flag before the call d) Resets the format string argument
15. To build a program that mixes main.c (C) and helper.asm (NASM), which command sequence is correct?
a) nasm helper.asm -o helper.o && ld main.c helper.o -o program
b) nasm -f elf64 helper.asm -o helper.o && gcc main.c helper.o -o program
c) gcc -S main.c -o main.s && nasm main.s helper.asm -o program
d) gcc main.c && nasm helper.asm && ld main.o helper.o -o program
16. In System V AMD64 ABI, which registers hold the first three integer/pointer function arguments?
a) RAX, RBX, RCX b) RDI, RSI, RDX c) RCX, RDX, R8 d) R8, R9, R10
17. A leaf function needs to store 200 bytes of local data. Since this exceeds the red zone (128 bytes), it must:
a) Use dynamic allocation (call malloc) for the extra 72 bytes b) Adjust RSP (sub rsp, 208) to allocate stack space c) Use callee-saved registers to hold the data instead d) Split into two functions to stay within the red zone
18. What assembly instruction implements the standard function prologue alongside push rbp?
a) push rsp
b) mov rbp, rsp
c) enter 0, 0
d) sub rsp, 16
19. After the call call malloc in assembly, which of the following registers is guaranteed to be unchanged from before the call?
a) RAX b) RCX c) RBX d) RDI
20. When writing an assembly function that C code calls as size_t my_strlen(const char *s), where does the return value need to go?
a) On the stack at [rbp-8] b) In RDI (the input argument register) c) In RAX d) In RCX