Writing Your First Program
Assembly Language · Assembly & OS track
5 min read
In this module
Your First Assembly Program
Let's write a small program that adds two numbers and exits with the sum as its exit code. About as simple as a useful x86-64 Linux program gets:
section .textglobal _start
_start: mov rax, 7 ; load the first number (7) into rax mov rbx, 3 ; load the second number (3) into rbx add rax, rbx ; add rbx to rax - rax now holds 10
mov rdi, rax ; move the result into rdi (exit code argument) mov rax, 60 ; system call number for exit (60 on Linux) syscall ; invoke the kernel - program ends hereRun it and check the exit code. You'll get 10, exactly what you'd expect from 7 + 3.
Walking Through Each Instruction
section .text tells the assembler that what follows belongs in the text (code) segment of memory.
global _start makes the _start label visible to the linker, so the linker knows where to start execution.
_start: is the entry-point label. Execution begins on the next line.
mov rax, 7 copies the immediate value 7 into register rax. Read it as rax = 7.
mov rbx, 3 does the same thing with 3 and rbx.
add rax, rbx adds the value in rbx to the value in rax, leaving the sum back in rax. After this line runs, rax holds 10.
Assembly programs are simply queues of instructions, fed to the CPU one at a time. Every instruction does exactly one small thing — load a value, add two registers, fire a system call. No abstractions, no hidden machinery.
Making the System Call
The final three lines hand the program over to the kernel:
mov rdi, rax— Linux looks inrdifor the exit code, so we copy our sum there.mov rax, 60— number 60 is theexitsystem call on Linux x86-64.syscall— punches a hole into the kernel, which tears the process down.
How It Maps to Machine Code
The assembler turns every line into bytes. By the time the program runs, the text you typed is gone. Only the encoded bytes remain. Run objdump -d -M intel add on the finished program and you'll see them laid out next to each instruction. Built with NASM, it looks like this:
401000: b8 07 00 00 00 mov eax, 0x7401005: bb 03 00 00 00 mov ebx, 0x340100a: 48 01 d8 add rax, rbx40100d: 48 89 c7 mov rdi, rax401010: b8 3c 00 00 00 mov eax, 0x3c401015: 0f 05 syscallTwo surprises in there. First, recent versions of NASM quietly rewrite mov rax, 7 as mov eax, 7 (check your own objdump output; older or differently configured assemblers may not). On x86-64, writing to a 32-bit register like eax also clears the top half of rax, so the two do the same job, and the eax form is two bytes shorter. Assemble the same code with GNU as and you get the longer 48 c7 c0 07 00 00 00 instead; both are correct.
Second, look at the 48 at the start of the add and the second mov. That's a REX prefix, the byte x86-64 added to say "this instruction works on 64-bit registers". Drop it and 01 d8 means add eax, ebx. And 60 shows up as 3c, its value in hex. The whole program is 23 bytes.
Building and Running
On a Linux box, the build pipeline looks like this:
nasm -f elf64 add.asm -o add.o # assemble to object fileld add.o -o add # link into executable./add # run itecho $? # prints 10NASM turns your text into an object file. ld, the linker, glues that object into a final executable. The same two-stage shuffle happens invisibly every time you compile a C program.
Predict: change the numbers to 200 and 100. What does echo $? print?
44. rax correctly holds 300, and that's what gets passed to exit. But a process's exit status only keeps the lowest 8 bits, so the shell sees 300 − 256 = 44. It's the same wrap-around you met in Binary Arithmetic, turning up somewhere unexpected.
The simulator below runs the same program on our simpler teaching CPU: load two numbers, add them, stop. Step through it and watch R0 reach 10, just as rax did.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0Line for line, the two programs match up like this:
| Teaching CPU | x86-64 | Note |
|---|---|---|
MOV R0, #7 | mov rax, 7 | # marks an immediate value |
MOV R1, #3 | mov rbx, 3 | |
ADD R0, R1 | add rax, rbx | destination first in both |
HALT | mov rdi, rax / mov rax, 60 / syscall | the teaching CPU just stops; a real process has to ask the kernel to end it |
The last row is the honest difference. Our teaching CPU has no operating system underneath, so stopping is a single instruction. On Linux, your program can't simply stop: it has to hand its result and the exit call number to the kernel.