Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Assembly Language – Introduction

Assembly Language - Introduction

Assembly language is the layer of the computer programming language family closest to the hardware; it is a human-readable representation of machine instruction mnemonics.

Understanding the essence of assembly language is the first step toward a deeper study of computer systems.


What is assembly language

A computer's CPU can only understand and execute binary machine code made up of 0s and 1s.

For example, in the x86 architecture, the machine code B8 2A 00 00 00 means 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 represents these binary instructions with human-readable mnemonics; for example, the machine code above is written in assembly as:

Example

mov

eax

,

42

   

; Move the value 42 into the EAX register

The assembler translates these mnemonics back into machine code the CPU can execute.

In the example above,mov is the mnemonic,eax is the destination operand,42 is the source operand.

Assembly language replaces machine-language operations with mnemonics, supports labels and symbols standing for addresses and constants to avoid hardcoding, and maps almost one-to-one to the corresponding machine-language instruction set.


The relationship between assembly language and machine code

There is a one-to-one correspondence between assembly instructions and machine instructions.

Every assembly instruction maps exactly to one CPU machine instruction, with no intermediate abstraction layer.

form Example Description
Machine code (binary) 10111000 00101010 00000000 00000000 00000000 Instructions executed directly by the CPU
Machine code (hexadecimal) B8 2A 00 00 00 A human-readable representation of machine code
Assembly language mov eax, 42 A human-readable mnemonic representation

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


Differences between assembly and high-level languages

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

High-Level Language vs Assembly Language Comparison

Feature Assembly language High-level languages (e.g. Python)
Abstraction level Lowest level, directly manipulates hardware High level, the OS and runtime encapsulate the details
Amount of code A simple function requires a lot of code A small amount of code implements complex features
Readability Hard to read and maintain Human-friendly and easy to understand
Execution Efficiency Highest, with no runtime overhead Lower, with interpretation or compilation overhead
Hardware control Full control over the CPU and memory Indirect access through abstract interfaces
Portability Poor: it must be rewritten for different CPUs Good: the same code can run on multiple platforms

Application scenarios of assembly language

Although assembly language is not the mainstream choice for everyday development, it remains irreplaceable in certain domains:

Domain Specific applications
Operating system kernels Bootloaders, context switching, interrupt handling, etc. must be written in assembly
Embedded systems Devices with extremely constrained resources that require precise control of the hardware
Reverse engineering Analyzing malware, cracking software protection, understanding closed-source program logic
Performance-critical code Core functions with extremely high performance demands, such as encryption algorithms and audio/video codecs
Security research Writing shellcode and exploit code
Compiler development Understanding target code generation, writing or optimizing compiler backends

Introduction to the x86 architecture

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

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

This tutorial uses the 32-bit x86 architecture (also called IA-32 or i386) as its teaching basis, because its register model is simple and clear, making it suitable for beginners.

32-bit x86 assembly is the best starting point for learning assembly language. Once you have mastered 32-bit, moving to 64-bit (x86-64) is very natural, since the latter is an extension of the former.


Introduction to the NASM assembler

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

Compared with MASM (Microsoft assembler) and GAS (GNU assembler), NASM has clearer and more intuitive syntax, making it the best choice for learning assembly language.

Assembler Syntax Style Platform Feature
NASM Intel syntax Cross-platform Open source, clear syntax, comprehensive documentation
MASM Intel syntax Windows Official Microsoft assembler, powerful but platform-limited
GAS AT&T syntax Cross-platform Default in the GNU toolchain, with counter-intuitive syntax

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