Master the fundamental concepts of x86 assembly (intel syntax) through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksCalling conventions are the contract between separately compiled functions. On x86-64 Linux, the System V AMD64 ABI passes the first six integer/pointer args in rdi, rsi, rdx, rcx, r8, r9 and returns scalars in rax. Python ctypes, Rust extern "C", and JNI all assume these slots.
nasmLoading…
Before call, rsp must be 16-byte aligned. The call itself pushes 8 bytes, which is why prologues often subtract another 8 bytes of stack space.
Seventh and later arguments spill to the stack in order.
For this exercise, you will implement add_numbers and recursive factorial obeying these rules. This task asks you to read arguments from rdi/rsi and return in rax, because GDB backtraces and FFI bugs are unreadable without this map.
Keep the relevant man page, ABI doc, or Rust reference chapter open while you work. When your output disagrees with the reference implementation on the same machine, the mismatch is usually an alignment rule, an off-by-one terminator, or a register slot you misread in GDB. Skim the official documentation for the tool or ABI named in the exercise; the prose changes, but register roles, syscall numbers, and ownership rules stay stable across releases.
Write two functions in x86-64 NASM that follow the System V AMD64 calling convention, the one every C compiler on Linux uses:
nasmLoading…
add returns a + b.factorial must be recursive: it calls itself for n - 1. 0! and 1! are 1.call: on entry rsp is 8 below a multiple of 16, because call pushed the return address. One push before the recursive call fixes it.The harness reads one command, calls your function, and prints the result.
One line, either add A B or fact N. A and B are signed 64-bit integers, and 0 <= N <= 20.
The result on one line: 8 for add 5 3, 120 for fact 5.