What is assembly language

What assembly is

Assembly language is the human-readable form of machine code. Each instruction moves data, does arithmetic, or changes the flow of control, and each one maps almost one-to-one to a CPU instruction.

A program is a list of these instructions plus data, assembled by an assembler into an object file, then linked into an executable. There is no runtime, no garbage collector, and no hidden work.

Where it runs

Assembly runs everywhere a program runs: Linux, Windows, macOS, and the embedded devices around you. This course targets x86-64 on Linux, the architecture of most servers and desktops.

The classic first program writes one line to the screen and exits, using two Linux system calls:

section .data msg db "Hello from assembly", 10 len equ $ - msg section .text global _start _start: mov rax, 1 ; sys_write mov rdi, 1 ; stdout mov rsi, msg mov rdx, len syscall mov rax, 60 ; sys_exit xor rdi, rdi ; status 0 syscall

Save it as hello.asm, assemble it, link it, and run it:

nasm -f elf64 -o hello.o hello.asm
ld -o hello hello.o
./hello
echo $?

Hello from assembly
0

Why learn it

Assembly teaches what every other language hides: how the stack works, how a function call really happens, and why a cache miss costs hundreds of cycles. After assembly, profilers and debuggers make sense.

You will not write a web server in assembly, but you will read it when a compiler emits a surprising instruction, when you patch a binary, or when you write a boot sector.

Why NASM

NASM (Netwide Assembler) is the assembler this course uses. Its syntax is clean, its documentation is free, and it targets every x86 system in existence. GAS uses a different syntax (AT&T), and MASM belongs to Windows.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does an assembler produce from a source file?

    Answer

    An object file

    NASM turns the source into an object file, and a linker turns that object into an executable.

  2. Which two Linux system calls does the first program on this page use?

    Answer

    write and exit

    Call 1 is write, which prints the bytes, and call 60 is exit, which ends the process.

  3. Just before the syscall instruction, what does rax hold?

    Answer

    The system call number

    Linux takes the call number in rax and the arguments in rdi, rsi, rdx, r10, r8 and r9.

  4. Which label does the linker use as the entry point of the program?

    Answer

    _start

    A program with no C library starts at _start, which global _start makes visible to the linker.

  5. What does this program write to standard output? section .data msg db "AB", 10 section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 3 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    AB

    The buffer holds A, B and a newline, and rdx asks write for three bytes, so the output is AB and a newline.

  6. What exit status does this program end with? section .text global _start _start: mov rax, 60 mov rdi, 42 syscall

    Answer

    42

    Call 60 is exit, and its first argument, rdi = 42, becomes the exit status.

  7. Which instruction transfers control from a 64-bit Linux program to the kernel?

    Answer

    syscall

    The syscall instruction enters the kernel on x86-64; int 0x80 is the older 32-bit entry point.

  8. Where does the kernel put the result of a system call?

    Answer

    rax

    The result, such as the number of bytes written, comes back in rax.

  9. What does this program print? section .data msg db "Hi there", 10 section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 2 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    Hi

    write prints only rdx bytes, and rdx is 2, so the reader sees Hi and not the rest of the buffer.

  10. Which program links an object file into an executable on Linux?

    Answer

    ld

    ld is the GNU linker; nasm assembles, and objdump and readelf only inspect a file.

The code on this page was assembled with NASM 3.01 and linked with GNU ld 2.45.1, and the program was run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev