This guide is designed to help you learn assembly language from the ground up, focusing on the 64-bit Windows architecture. We’ll cover everything from setting up your environment to understanding registers and writing basic programs. High-level programming language comparisons are included to make concepts more understandable.
An assembly program is divided into sections:
section .data
hello db 'Hello, world!', 0
section .text
global _start
_start:
; Your code here
Registers are limited storage locations in the CPU, each with specific purposes.
C Code:
int main() {
int a = 5;
int b = 10;
int sum = a + b;
return sum;
}
Assembly Code:
section .text
global _start
_start:
mov rax, 5 ; a = 5
mov rbx, 10 ; b = 10
add rax, rbx ; sum = a + b
mov rdi, rax ; return sum
call ExitProcess
C Code:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Assembly Code:
section .data
hello db 'Hello, World!', 0
section .text
global _start
extern GetStdHandle
extern WriteConsoleA
extern ExitProcess
_start:
mov rcx, -11 ; STD_OUTPUT_HANDLE
call GetStdHandle
mov rcx, rax ; handle to stdout
mov rdx, hello ; pointer to string
mov r8, 13 ; string length
lea r9, [rsp+8] ; dummy variable
call WriteConsoleA
xor rcx, rcx ; exit code 0
call ExitProcess
C Code:
int add(int a, int b) {
return a + b;
}
Assembly Code:
section .text
global _start
_start:
mov rax, 5 ; a = 5
mov rbx, 10 ; b = 10
add rax, rbx ; result = a + b
call ExitProcess
C Code:
int sum_n(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
Assembly Code:
section .text
global _start
_start:
mov rcx, 10 ; n = 10
mov rax, 0 ; sum = 0
mov rbx, 1 ; i = 1
loop_start:
add rax, rbx ; sum += i
inc rbx ; i++
cmp rbx, rcx
jle loop_start ; if i <= n, repeat
call ExitProcess
C Code:
int multiply(int a, int b) {
return a * b;
}
int main() {
int result = multiply(5, 10);
return result;
}
Assembly Code:
section .text
global _start
extern multiply
_start:
mov rcx, 5 ; a = 5
mov rdx, 10 ; b = 10
call multiply
; result is in rax
call ExitProcess
multiply:
imul rax, rcx, rdx
ret
gdb
can help you step through your code and understand what’s happening.