Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 40 ยท Practice and Review

Assembly Interview Questions

Prepare for low-level interviews: registers, the stack, calling conventions, flags, endianness, x86 vs ARM, debugging and security, with runnable proofs.

Interviewers rarely ask you to write a long assembly program from memory. They ask short questions that reveal whether you have a working mental model of the machine: what a call does to the stack, why a signed comparison and an unsigned comparison disagree, which registers a function may clobber. This lesson collects the questions that come up most often, grouped by topic, with answers short enough to say aloud in half a minute.

Wherever a claim can be demonstrated, there is a runnable proof: byte layouts with typed arrays, register aliasing with BigInt, flags and recursion on the toy CPU from Meet the Toy CPU. Being able to say "and here is how I would check that" is what separates a memorized answer from an understood one. Questions about the real calling conventions describe the standard Linux, Windows and ARM64 rules; where your platform differs, say so in the interview.

Registers and data

What is a register, and why are registers fast?

A register is a small storage location inside the CPU that instructions can read and write directly, named in the instruction set (rax, x0). It is fast because it sits in the execution core itself: no address has to be computed, no cache or memory bus is involved. A typical program therefore keeps hot values in registers and uses memory for everything that does not fit.

How many general-purpose registers do x86-64 and ARM64 have?

x86-64 has 16 (rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, r8 to r15), one of which (rsp) is the stack pointer in practice. ARM64 has 31 general registers x0 to x30 plus a stack pointer sp and a zero register xzr that reads as zero and ignores writes. More registers means fewer spills to the stack, which is one reason ARM64 compilers can produce tight code. See The x86-64 Register Set and ARM64 (AArch64) Basics.

What happens to the upper bits when you write eax, ax or al?

On x86-64, writing a 32-bit register such as eax zeroes the upper 32 bits of rax. Writing ax (16 bits) or al (8 bits) leaves all other bits unchanged. On ARM64, writing a w register such as w0 also zeroes the upper half of x0. The demonstration below models the three writes.

JS Partial register writes on x86-64

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Only the last line wipes the upper half. This is why xor eax, eax is enough to zero all of rax.

Why do compilers write xor eax, eax instead of mov eax, 0?

The xor form is shorter (2 bytes against 5), and CPUs recognize it as a "zeroing idiom" that needs no real computation. The one difference is that xor changes the flags (ZF becomes 1) while mov never touches them, so a compiler must not put it between a cmp and the jump that reads the flags.

What does lea do, and how is it different from mov?

lea computes an address with the same hardware as a memory operand but never reads memory. mov eax, [rbx+rcx*4] loads a value; lea eax, [rbx+rcx*4] just calculates rbx + rcx*4. Compilers use it for arithmetic such as lea eax, [rdi+rdi*2] (times 3), which also leaves the flags alone. See MOV, LEA and Moving Data.

Can an instruction move memory to memory?

On x86-64 mov cannot have two memory operands, so you load into a register and then store. ARM64 is stricter still: only load and store instructions touch memory at all (a load/store architecture).

What is endianness, and which do x86-64 and ARM64 use?

Endianness is the order of a multi-byte value's bytes in memory. Little-endian stores the least significant byte first. x86-64 is little-endian, and ARM64 as run by Linux, Android, macOS and Windows is too (the architecture can technically be configured big-endian, but you almost never meet it). Network protocols use big-endian ("network byte order"), which is why code calls byte-swap functions at the boundary.

JS The same number, two byte orders

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The third line is the classic bug: bytes written in one order and read in the other give 0x78563412. More on this in Memory, Addresses and Endianness.

The stack and calling conventions

What do call and ret do?

call target pushes the address of the next instruction (the return address) onto the stack and jumps to the target. ret pops that address into the instruction pointer. Nested calls simply stack more return addresses, and the stack grows toward lower addresses on both x86-64 and ARM64. (ARM64's bl stores the return address in the link register x30 instead, so a function that calls others must save x30 itself.)

flowchart TD
  A["caller: call f"] --> B["push return address<br/>jump to f"]
  B --> C["f runs<br/>(may call others)"]
  C --> D["ret: pop address<br/>into rip"]
  D --> E["caller continues<br/>after the call"]

A return address on the stack is what lets every function find its way back.

Which registers carry arguments and results?

Convention Integer arguments, in order Result Notes
System V AMD64 (Linux, macOS, BSD) rdi, rsi, rdx, rcx, r8, r9 rax floats in xmm0 to xmm7; more arguments go on the stack
Windows x64 rcx, rdx, r8, r9 rax caller reserves 32 bytes of "shadow space"
AAPCS64 (ARM64) x0 to x7 x0 floats in v0 to v7

Calling Conventions gives the full details, including variadic functions: on System V the caller sets al to the number of vector registers used before calling something like printf.

What is the difference between caller-saved and callee-saved registers?

A caller-saved (volatile) register may be changed by any function you call, so if you need its value afterwards you must save it yourself. A callee-saved register must have the same value when the function returns as when it was called, so a function that wants to use it must save and restore it. On System V the callee-saved registers are rbx, rbp and r12 to r15 (plus rsp); on ARM64 they are x19 to x28, with the frame pointer x29.

What is a stack frame, a prologue and an epilogue?

A frame is the stack area a function owns: saved registers, local variables and outgoing arguments. The classic x86-64 prologue is push rbp / mov rbp, rsp / sub rsp, N, and the epilogue leave / ret (or mov rsp, rbp / pop rbp / ret). Optimized code often omits the frame pointer and addresses locals relative to rsp. See Stack Frames, Prologues and Epilogues.

What alignment rules does the stack have?

On System V x86-64, rsp must be a multiple of 16 at the moment of the call, so at function entry (after the return address is pushed) it is 8 modulo 16. On ARM64, sp must be 16-byte aligned whenever it is used to access memory. Violating this crashes SIMD code or library calls in surprising places. The System V ABI also gives leaf functions a 128-byte red zone below rsp that signal handlers will not clobber; Windows and kernel code have no red zone.

How do recursion and stack overflow look at the instruction level?

Each recursive call pushes a return address (and often a saved value), so the stack grows with the depth of the recursion. If it never stops, the stack runs into its limit. This toy program sums 1 to 5 recursively and reports how deep the stack went, then calls a function that calls itself forever.

JS Recursion depth and stack overflow

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Six calls and five pushes use eleven 8-byte slots. The runaway program fails when the toy stack reaches its floor, exactly like the segmentation fault a real process gets when it runs off the end of its stack.

Flags and branches

What do ZF, SF, CF and OF mean?

ZF (zero) is set when a result is zero. SF (sign) copies the top bit of the result. CF (carry) is set when an unsigned operation carries out of or borrows into the top bit. OF (overflow) is set when a signed operation produces a result that does not fit. Details are in Flags: ZF, SF, CF, OF.

Explain the difference between CF and OF with an example.

Adding 1 to 0x7FFFFFFF is fine as an unsigned number but overflows as a signed one (it becomes -2147483648), so OF is 1 and CF is 0. Adding 1 to 0xFFFFFFFF wraps an unsigned number back to zero, so CF is 1 and OF is 0. The program shows both, and then the detail that inc leaves CF untouched.

JS CF versus OF, and inc keeping CF

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The trace shows the state before each instruction runs. On the inc line CF is already 1 (the add set it), and on the out line CF is still 1 although rax changed from 0 to 1: inc and dec leave CF alone.

What is the difference between cmp and sub, and between test and and?

cmp a, b performs a - b and keeps only the flags; test a, b performs a & b and keeps only the flags. The two "throwaway" forms exist so that you can ask a question without destroying a register. test rax, rax followed by jz is the idiomatic zero check.

Why do some jumps say "less" and others "below"?

"Less" and "greater" (jl, jg) treat the operands as signed and read SF and OF. "Below" and "above" (jb, ja) treat them as unsigned and read CF. The bits are identical; only your choice of jump decides whether 0xFFFFFFFF is -1 or about 4.3 billion. A compiler picks the family from the C type, and mixing them up is a well-known source of bugs and vulnerabilities. See Compare and Jump.

How do flags work on ARM64?

ARM64 has the flags N, Z, C and V, but ordinary arithmetic leaves them alone. Only the flag-setting forms (adds, subs, ands, plus the aliases cmp and tst) update them. One trap: after a subtraction ARM sets C when there was no borrow, the opposite of x86's CF. Conditions such as b.lt, b.hi and conditional select csel read the flags, which also allows branch-free code. See ARM Load/Store, Conditions and Branches.

x86-64 versus ARM64

What are the main differences?

Topic x86-64 ARM64
Style CISC heritage RISC
Instruction length variable, 1 to 15 bytes fixed, 4 bytes
Memory access many instructions take a memory operand only loads and stores touch memory
General registers 16 31 plus sp and xzr
Flags updated by most arithmetic updated only by s-suffixed instructions
Memory ordering relatively strong (total store order) weaker; needs barriers or acquire/release

The weaker ordering on ARM matters for multithreaded code: a data race that "works" on x86 can break on ARM, so use the language's atomics rather than relying on hardware luck. See x86-64 vs ARM64: Side by Side.

Does unaligned memory access work?

On x86-64, ordinary unaligned loads and stores work (sometimes slower). Modern ARM64 cores also allow unaligned access for normal loads and stores, but atomic and exclusive operations and some vector accesses require alignment. Portable code should keep data naturally aligned, which compilers do by adding padding (Structs, Padding and Alignment).

Debugging

Which gdb commands do you use for instruction-level debugging?

Goal Command
Intel syntax set disassembly-flavor intel
Stop at the first instruction starti
Show code around the PC x/8i $pc, or disassemble
Execute one instruction stepi (si), and nexti to step over calls
Show registers info registers
Dump memory x/8xg $rsp (8 eight-byte words in hex), x/s $rsi (string)
See the instruction every stop display/i $pc
Show the call chain bt

lldb has equivalents: register read, memory read, disassemble and thread step-inst. See Debugging at the Instruction Level.

How do you start analyzing a crash?

Find the faulting instruction (x/i $pc), look at the registers it uses, and ask whether the address in the memory operand is plausible: zero or tiny (null dereference), huge (corrupted pointer), or well-formed but unmapped. Then walk the call chain with bt to see who passed the bad value. Returning to an address that is clearly data, such as 0x4141414141414141, points to a smashed stack.

Security

How does a stack buffer overflow work?

A function's local buffer sits below its saved return address. If the program writes more bytes into the buffer than it holds, the extra bytes overwrite whatever is above it, including the return address, and ret then jumps where the attacker chose. The cause is a missing bounds check, which is why unbounded functions such as gets were removed from the C standard.

What defends against it?

  • Stack canaries: a secret value between the locals and the return address, checked before ret.
  • NX / W^X: data pages (including the stack) are not executable.
  • ASLR and PIE: stack, heap and code load at randomized addresses.
  • Shadow stacks and pointer authentication: Intel CET keeps a protected second copy of return addresses; ARM64 can cryptographically sign them (PAC) and mark valid jump targets (BTI).

None of these replaces correct bounds checks. See Security: Buffer Overflows and Defenses.

What is return-oriented programming (ROP)?

When NX stops an attacker from running injected code, they can chain existing code instead: short snippets already in the program that end in ret, each called by a forged return address. It shows why executable memory is not the only thing worth protecting, and why return-address integrity features exist.

What is constant-time code?

Code whose timing does not depend on secret data, so an observer cannot learn secrets by measuring it. Practically it means no branches and no table lookups indexed by secret values; selections are done with masks or conditional moves such as cmov or csel. Cryptographic libraries work hard to keep compilers from "optimizing" these patterns back into branches.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. After mov eax, 1 executes on x86-64, what is the state of the upper 32 bits of rax?

    A 32-bit register write clears bits 32 to 63. Only 8- and 16-bit writes leave the other bits alone.
  2. 2. Which statement about call and ret is correct?

    That push and pop pair is what lets nested calls return correctly. On ARM64, bl uses the link register instead of pushing.
  3. 3. Adding 1 to 0x7FFFFFFF in 32 bits sets which flags?

    The unsigned result 0x80000000 fits, so there is no carry, but as a signed number the result changed from the largest positive to the most negative value, which is signed overflow.
  4. 4. Which register holds the first integer argument in the System V AMD64 convention, and in Windows x64?

    System V starts with rdi, rsi, rdx, rcx, r8, r9. Windows x64 uses rcx, rdx, r8, r9 and also requires 32 bytes of shadow space.
  5. 5. What is the purpose of a stack canary?

    The canary is a secret value placed between locals and the return address. The epilogue checks it, and a mismatch aborts the program instead of returning to attacker-chosen code.

Summary

  • Know the register basics: 16 registers on x86-64, 31 on ARM64, and the zero-extension rule for 32-bit writes.
  • call pushes a return address and ret pops it; the stack grows downward and must be kept 16-byte aligned.
  • Remember the three argument-register sets (System V, Windows x64, AAPCS64) and the callee-saved registers.
  • CF is unsigned overflow, OF is signed overflow; jl/jg read signed flags, jb/ja read unsigned ones; on ARM only s instructions set flags.
  • x86-64 and ARM64 are little-endian; ARM has a weaker memory model than x86.
  • gdb basics (stepi, info registers, x/, disassemble) cover most instruction-level debugging.
  • Buffer overflows overwrite return addresses; canaries, NX, ASLR, shadow stacks and PAC raise the cost of exploiting them.

Next: keep the essentials on one page in the Assembly Cheat Sheet.