Hey there, fellow curious mind! 👋 Did you know that the word computer comes from the Latin computare, which means to calculate? 🧠 A computer is basically a hyper-fast calculator with fancy tricks. These tricks are powered by a set of interconnected circuits running programs. Cool, right? 😎
Now, let’s go a step further—every modern computer you’ve ever used is based on something called the von Neumann architecture. It’s got four major parts:
- 1️⃣ ALU (Arithmetic Logic Unit): Does the number crunching and logical operations. 🧮
- 2️⃣ Control Unit: The “manager” that keeps everything running smoothly. 🕹️
- 3️⃣ Memory: Stores your data and programs. 🧠
- 4️⃣ Input/Output devices: These let you talk to your computer and vice versa (e.g., keyboard, monitor). 📠
All these parts are connected by highways of data called buses. 🚌
Alright, that’s enough history—let’s dive into the nitty-gritty and learn Assembly Language, the secret sauce of speaking to your CPU directly. Let’s gooo! 🚀
Step 1: Setting Up Your Toolkit 🛠️
Windows
- Download the NASM assembler from the official site.
- Install it and add it to your system PATH so you can call nasm from anywhere.
- Use a text editor (like NeoVim) to write your .asm or .s files.
Linux
- Open your terminal.
- Install NASM by running:
sudo apt update && sudo apt install nasm
- You're all set to write .asm files using any text editor!
Step 2: Learn the Basics 📖
Registers: Your CPU's Pocket Notebook 📓
Registers are small storage spaces in the CPU for quick data access. Here are the essential ones:
- RAX: General-purpose accumulator. Often used for return values.
- RBX: Another general-purpose register.
- RCX/RDX: Used for counters and data handling.
- RSI/RDI: For string operations.
- RSP/RBP: Stack Pointer and Base Pointer for managing the stack.
System Calls: How the CPU Talks to the OS 📞
Syscalls are like knocking on the operating system’s door to ask for services like printing or reading input. Each syscall has a number. For example:
1 (write): Write something to the screen.
60 (exit): Terminate the program.
Step 3: Write and Run Your First Program ✍️💻
Here’s a simple Assembly program that prints "Hello, world!" and exits.
Code: hello.asm
section .data
message db "Hello, world!", 0xA ; Define a string with a newline
section .text
global _start ; Entry point for the linker
_start:
mov rax, 1 ; Syscall number for write
mov rdi, 1 ; File descriptor (1 = stdout)
mov rsi, message ; Address of the message
mov rdx, 14 ; Message length
syscall ; Call the kernel
mov rax, 60 ; Syscall number for exit
xor rdi, rdi ; Exit code 0
syscall ; Call the kernel
Assemble and Run
On Linux:
Assemble the program:
nasm -f elf64 hello.asm
Link it:
ld -o hello hello.o
Run it:
./hello
On Windows:
Assemble the program:
nasm -f win64 hello.asm -o hello.obj
Link it using a linker like GoLink or MinGW:
golink /console /entry:_start hello.obj
Run it:
hello.exe
Step 4: Bonus: A Read and Print Example 📖✏️
Let’s read user input and print it back!
Code: echo.asm
section .bss
input resb 32 ; Reserve 32 bytes for input
section .text
global _start
_start:
; Read input
mov rax, 0 ; Syscall number for read
mov rdi, 0 ; File descriptor (0 = stdin)
mov rsi, input ; Buffer for input
mov rdx, 32 ; Max number of bytes to read
syscall
; Write input back to stdout
mov rax, 1 ; Syscall number for write
mov rdi, 1 ; File descriptor (1 = stdout)
mov rsi, input ; Address of the input buffer
syscall
; Exit
mov rax, 60 ; Syscall number for exit
xor rdi, rdi ; Exit code 0
syscall
And that’s it! 🎉 You’ve just learned the basics of x86-64 Assembly! Keep experimenting, and soon you’ll feel like a wizard casting CPU spells. 🧙♂️✨
SOCIAL SHARE CARD GENERATOR