Skip to main content
Be Bitwise

Writing Your First Program

Assembly Language · Assembly & OS track

5 min read

Part ofSystems Programming Companion, week 3

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:

asm
section .text
global _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 here

Run 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.

Key Concept

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 in rdi for the exit code, so we copy our sum there.
  • mov rax, 60 — number 60 is the exit system 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:

text
401000: b8 07 00 00 00 mov eax, 0x7
401005: bb 03 00 00 00 mov ebx, 0x3
40100a: 48 01 d8 add rax, rbx
40100d: 48 89 c7 mov rdi, rax
401010: b8 3c 00 00 00 mov eax, 0x3c
401015: 0f 05 syscall

Two 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:

text
nasm -f elf64 add.asm -o add.o # assemble to object file
ld add.o -o add # link into executable
./add # run it
echo $? # prints 10

NASM 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

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded

Line for line, the two programs match up like this:

Teaching CPUx86-64Note
MOV R0, #7mov rax, 7# marks an immediate value
MOV R1, #3mov rbx, 3
ADD R0, R1add rax, rbxdestination first in both
HALTmov rdi, rax / mov rax, 60 / syscallthe 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.