Assembly for Beginners – Module 1
■ Learning Goals
- Understand what Assembly is.
- See how Assembly relates to machine code and high-level languages.
- Set up the environment to run Assembly programs.
■ What is Assembly?
Assembly is a low-level programming language. It talks directly to the CPU using instructions that
are very close to machine code (binary). Each line of Assembly corresponds to one CPU
instruction.
Example:
C code:
x = x + 1;
Assembly (NASM, x86):
mov eax, x
add eax, 1
mov x, eax
■ Why Learn Assembly?
- To understand how computers really work.
- Useful for cybersecurity & reverse engineering.
- Essential in embedded systems & IoT.
- Performance optimization in critical programs.
■■ How Does Code Reach the CPU?
1. High-level language (C, Python) → Easy to read for humans.
2. Compiler → Translates into Assembly / Machine Code.
3. Assembly → Human-readable version of machine code.
4. Machine Code (binary) → What the CPU executes.
■■ Tools You Need
Depending on OS, we use different assemblers:
- Linux: NASM (Netwide Assembler)
Install:
sudo apt update
sudo apt install nasm
- Windows: MASM or TASM (Microsoft / Turbo Assembler).
- Mac: NASM works too (but syscalls differ).
■ Example 1: Hello World (Linux NASM, 64-bit)
section .data
msg db "Hello, World!", 0xA ; string to print with newline
len equ $ - msg ; length of the string
section .text
global _start
_start:
; write(1, msg, len)
mov rax, 1 ; syscall number for write
mov rdi, 1 ; file descriptor (stdout)
mov rsi, msg ; address of string
mov rdx, len ; length of string
syscall
; exit(0)
mov rax, 60 ; syscall number for exit
xor rdi, rdi ; exit code 0
syscall
■ Exercise 1
1. Modify the program to print your own name instead of “Hello, World!”.
2. Add another line so it prints two messages.