Assembly Language - Introduction

Assembly Language is the layer closest to hardware in the family of computer programming languages; it is a human-readable mnemonic representation of machine instructions.

Understanding the essence of assembly language is the first step to deeply learning computer systems.


What is Assembly Language

A computer's CPU can only understand and execute0s and 1sthat make up binary machine code.

For example, in the x86 architecture, the machine codeB8 2A 00 00 00represents moving the value 42 into the EAX register.

Writing binary machine code directly is extremely difficult and error-prone for humans, so assembly language came into being.

Assembly language uses human-readablemnemonics (Mnemonic)to represent these binary instructions. For example, the machine code above is written in assembly as:

Example

mov eax, 42    ; Move value 42 into EAX register

An assembler is responsible for translating these mnemonics back into machine code that the CPU can execute.

In the example above,movis the mnemonic,eaxis the destination operand,42is the source operand.

Assembly language uses mnemonics to replace machine language operations, supports labels and symbols to refer to addresses and constants, avoiding hardcoding. Assembly language corresponds roughly one-to-one with the corresponding machine language instruction set.


Relationship between Assembly Language and Machine Code

Between assembly instructions and machine instructions, there isa one-to-one correspondencerelationship.

Each assembly instruction precisely maps to one CPU machine instruction, with no intermediate abstraction layer.

FormExampleDescription
Machine code (binary)10111000 00101010 00000000 00000000 00000000Instructions directly executed by the CPU
Machine code (hexadecimal)B8 2A 00 00 00Human-readable machine code representation
Assembly languagemov eax, 42Human-readable mnemonic representation

Assembly language and machine code have a one-to-one correspondence, but CPUs of different architectures have different instruction sets. For example, the assembly syntax of x86 and ARM is completely different.


Differences between Assembly and High-Level Languages

The following figure shows the different paths from source code to execution for high-level languages and assembly language:

高级语言 vs 汇编语言对比

FeatureAssembly languageHigh-level language (e.g., Python)
Abstraction levelLowest level, directly manipulates hardwareHigh-level, operating system and runtime encapsulate details
Code sizeSimple functions require a lot of codeA small amount of code implements complex functions
ReadabilityDifficult to read and maintainHuman-friendly, easy to understand
Execution efficiencyHighest, no runtime overheadLower, has interpretation or compilation overhead
Hardware controlFull control over CPU and memoryIndirect access through abstract interfaces
PortabilityPoor, needs rewriting for different CPUsGood, the same code can run on multiple platforms

Application Scenarios of Assembly Language

Although assembly language is not a mainstream choice for daily development, it is still irreplaceable in specific fields:

FieldSpecific application
Operating system kernelBootloader, context switching, interrupt handling, etc. must be written in assembly
Embedded systemsDevices with extremely limited resources require precise hardware control
Reverse engineeringAnalyzing malware, cracking software protection, understanding closed-source program logic
Performance-sensitive codeCore functions with extremely high performance requirements, such as encryption algorithms and audio/video codecs
Security researchWriting shellcode, exploit code
Compiler developmentUnderstanding target code generation, writing or optimizing compiler backends

Introduction to x86 Architecture

x86It is a microprocessor architecture introduced by Intel, starting with the 8086 processor in 1978, and going through generations such as 80286, 80386 (i386), 80486, Pentium, etc.

x86 is the most widely used desktop and server CPU architecture, and an ideal platform for learning assembly language.

This tutorial uses32-bit x86 architecture(also called IA-32 or i386) as the teaching foundation, because its register model is concise and clear, suitable for beginners.

32-bit x86 assembly is the best starting point for learning assembly language. After mastering 32-bit, transitioning to 64-bit (x86-64) is very natural, because the latter is an extension of the former.


Introduction to NASM Assembler

NASM(Netwide Assembler)It is an open-source x86 assembler that supports multiple output formats and all major operating systems.

Compared to MASM (Microsoft Assembler) and GAS (GNU Assembler), NASM's syntax is clearer and more intuitive, making it the best choice for learning assembly language.

AssemblerSyntax stylePlatformFeatures
NASMIntel syntaxCross-platformOpen-source, clear syntax, comprehensive documentation
MASMIntel syntaxWindowsMicrosoft official, powerful but platform-limited
GASAT&T syntaxCross-platformGNU toolchain default, syntax is counterintuitive

This tutorial exclusively uses the NASM assembler with Intel-style syntax.

Other extensions