Lesson 01-Fundamentals of Assembly Language

Core Concepts of Assembly Language

Overview of Assembly Language

Definition and Function:
Assembly language is a low-level programming language that directly corresponds to machine instructions. It serves as a human-readable representation of machine code, enabling precise control over hardware.

Core Functions Include:

  • Register operations
  • Memory access
  • Arithmetic and logical operations
  • Program flow control

Historical Development:

ArchitectureRepresentative AssemblerCharacteristics
x86NASM / MASMComplex Instruction Set (CISC), backward compatible
ARMARMASMReduced Instruction Set (RISC), low power consumption
RISC-VGNU AssemblerOpen-source instruction set, modular design

Application Scenarios:

System Programming:

  • Operating system kernel development
  • Device driver implementation
  • Memory management

Embedded Systems:

  • Microcontroller programming
  • FPGA firmware development
  • Real-time control systems

Reverse Engineering:

  • Binary analysis
  • Vulnerability research
  • Security studies

Toolchain Ecosystem:

NASM: Cross-platform, concise syntax
MASM: Windows-specific, Microsoft-supported
GAS: Part of the GNU toolchain
FASM: Self-contained assembler with fast compilation

Development Environment Setup

Environment Setup Guide

Tool Installation:

# Linux (Ubuntu example)
sudo apt install nasm qemu-system-x86 gdb-multiarch

# Windows
# 1. Download NASM: https://www.nasm.us/
# 2. Install MASM32: http://www.masm32.com/
# 3. Install Keil: https://www.keil.com/

# macOS
brew install nasm qemu gdb

Debugging Tool Configuration:

GDB:

gdb ./program
(gdb) layout asm        # Display assembly code
(gdb) break *0x8048000  # Set breakpoint
(gdb) stepi             # Step through instructions

OllyDbg:

  • Graphical interface
  • Supports 32-bit Windows programs
  • Built-in disassembler

WinDbg:

  • Windows kernel debugging
  • JIT debugging support
  • Powerful command-line features

Emulator Usage:

# Run x86 program with QEMU
qemu-x86_64 ./program

# Bochs debugger
bochs -f bochsrc.txt

# Run 16-bit program with DOSBox
dosbox ./program.com

Installation Verification:

# Check NASM version
nasm -v

# Compile test program
nasm -f elf64 hello.asm -o hello.o
ld hello.o -o hello
./hello

IDE Configuration:

VS Code:

  • Install extensions: Assembly Language, CodeLLDB
  • Configure tasks.json for build tasks
  • Set up launch.json for debugging

RadASM:

  • IDE designed specifically for MASM
  • Integrated debugger
  • Resource editor integration

Emu8086:

  • 8086 instruction set simulator
  • Graphical interface
  • Beginner-friendly learning mode

Basic Syntax Explained

Instruction System

Core Instructions:

InstructionFunctionExample
MOVData transfermov eax, 42
ADDAdditionadd ebx, ecx
SUBSubtractionsub edx, 10
JMPUnconditional jumpjmp label
CMPComparecmp eax, ebx
JEJump if equalje equal_label

Register Set:

General-Purpose Registers: EAX (Accumulator) | EBX (Base) | ECX (Counter) | EDX (Data)
ESI (Source Index) | EDI (Destination Index) | EBP (Base Pointer) | ESP (Stack Pointer)

64-bit Extensions:
RAX | RBX | RCX | RDX | RSI | RDI | RBP | RSP
R8–R15

Memory Addressing Modes:

; Direct addressing
mov eax, [0x1234]

; Register indirect addressing
mov ebx, [esi]

; Base-index addressing
mov ecx, [ebx + esi*4]

; Addressing with displacement
mov edx, [ebp - 8]

Program Control Flow:

; Conditional jump
cmp eax, ebx
jg greater_label   ; Jump if greater

; Loop structure
mov ecx, 10
loop_start:
    dec ecx
    jnz loop_start

; Function call
call my_function
ret

Data Representation and Organization

Basic Data Types:

TypeSizeInstruction SuffixExample
Byte1 bytebmov al, 0x41
Word2 byteswmov ax, 0x1234
Dword4 byteslmov eax, 0xDEADBEEF
Qword8 bytesqmov rax, 0x1122334455667788

Data Definition Pseudo-instructions:

; Data definition
db 0x41, 0x42, 0x43      ; Define byte sequence
dw 0x1234, 0x5678        ; Define word sequence
dd 0xDEADBEEF            ; Define double-word

; String definition
msg db 'Hello', 0        ; null-terminated string

Immediate Value Representation:

mov eax, 42        ; Decimal
mov ebx, 0x2A      ; Hexadecimal
mov ecx, 0b101010  ; Binary
mov edx, 101010b   ; Binary (alternative syntax)

Program Structure and Organization

Program Construction Methods

Segment Structure Example:

section .text
    global _start
_start:                 ; Code segment
    mov eax, 1
    int 0x80

section .data
    msg db 'Hello', 0xA
    len equ $ - msg

section .bss
    buffer resb 1024    ; Reserve 1024 bytes of uninitialized space

Pseudo-instruction Details:

Pseudo-instructionFunctionExample
ORGSet starting addressorg 0x7C00
EQUDefine constantMAX_SIZE equ 100
%defineMacro definition%define DEBUG 1
SECTIONDefine segmentsection .text

Linking Process:

# Compile object file
nasm -f elf32 program.asm -o program.o

# Link executable
ld -m elf_i386 program.o -o program

# Run program
./program

System Call Interface:

; Linux x86_64 system call
mov rax, 1        ; syscall number (sys_write)
mov rdi, 1        ; file descriptor (stdout)
mov rsi, msg      ; buffer address
mov rdx, len      ; length
syscall

; Windows system call (example)
mov eax, 0x31     ; NtCreateFile
mov r10, rcx
syscall

Through the above foundational framework, a complete assembly language development environment can be established, and core skills ranging from simple programs to system-level programming can be mastered. It is recommended to start with the x86 architecture and gradually expand to ARM and RISC-V architectures to understand the design philosophies and optimization methods of different instruction sets.

Share your love