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

Assembly Language - Memory Segmentation

Assembly Language - Memory Segmentation

Memory Segmentation is a core concept in the x86 architecture; it determines how a program organizes code, data, and stack space in memory.


Why segmentation is needed

In x86 real mode, the CPU uses 16-bit registers but needs to access 1MB of memory space (20-bit addresses).

16-bit registers can only represent a 64KB range (2^16 = 65536), which is far from enough.

Intel's solution is to divide memory into segments (Segment), each with a base address and an offset, combining them as segment address + offset to form a full physical address.

Physical address calculation formula:



物理地址 = 段地址 × 16 + 偏移地址

Although in protected mode (32-bit) segmentation is used more for memory protection and privilege control, understanding segmentation is still crucial for understanding assembly program structure.


Three basic segments

A typical assembly program uses three main segments:

Segment name English name Purpose Corresponding Section
Code segment Code Segment Stores executable machine instructions section .text
Data segment Data Segment Stores initialized global variables section .data
Stack segment Stack Segment Stores function call information and local variables Managed automatically by the operating system

Code segment (.text)

The code segment stores all machine instructions of the program and is a read-only, executable memory region.

When loading a program, the OS maps the code segment to read-only memory pages to prevent accidental modification of instructions.

Example

; File path: code_segment.asm

; Demonstrate use of the code segment

section

.text

                 

; Start of the code segment

   

global

_start

_start

:

   

mov

eax

,

1

                 

; These instructions are all stored in the code segment

   

mov

ebx

,

0

   

int

0x80

; The following is a helper function, also stored in the code segment

my_function

:

   

mov

eax

,

42

   

ret


Data segment (.data)

The data segment stores initialized global variables in the program.

The data segment is readable and writable; its contents can be modified at runtime.

Example

; File path: data_segment.asm

; Demonstrate use of the data segment

section

.data

   

; Define various types of initialized variables

    msg

db

'Hello, runoob!'

,

0

   

; String variable (byte sequence)

    count

db

100

                   

; Byte variable, initial value 100

    pi

dd

314159

                   

; Double-word variable, holding an approximation of pi multiplied by 100000

    array

db

1

,

2

,

3

,

4

,

5

       

; Byte array

section

.text

   

global

_start

_start

:

   

; Read variables in the data segment

   

mov

al

,

[

count

]

               

; Load the value of count (100) into the al register

   

; ...

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80


BSS segment (.bss)

The BSS (Block Started by Symbol) segment stores uninitialized global variables.

Unlike the data segment, the BSS segment takes no actual space in the executable; the OS allocates and zero-initializes memory only when the program is loaded.

Example

; File path: bss_segment.asm

; Demonstrate use of the BSS segment

section

.bss

    buffer

resb

256

               

; Reserve a 256-byte buffer (uninitialized)

    num_array

resd

100

             

; Reserve 100 double words (400 bytes)

section

.data

   

; Initialized data

section

.text

   

global

_start

_start

:

   

; Use the buffer in the BSS segment

   

mov

byte

[

buffer

]

,

'A'

         

; Write character 'A' into the buffer

   

; ...

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

Placing uninitialized data in the BSS segment instead of .data reduces the executable's size. For example, a 10KB uninitialized buffer takes no file space in BSS, but in .data it would grow the file by 10KB.


Segment registers

The x86 architecture has 6 segment registers used to track the segments currently in use:

Segment registers Full English name Purpose
CS Code Segment Points to the code segment, holds the base address of the segment containing the currently executing instruction
DS Data Segment Points to the data segment, holds the segment base address for most data accesses
SS Stack Segment Points to the stack segment, holds the base address of the segment containing the stack
ES Extra Segment Extra segment register, used for additional data segments such as string operations
FS General Purpose General-purpose segment register, often used for thread-local storage
GS General Purpose General-purpose segment register, often used for thread-local storage

In 32-bit protected mode, programmers usually do not need to set segment registers manually; the OS and linker handle it.


Memory segmentation diagram

The distribution of a running program in memory is as follows:

The memory layout of a running program is roughly as follows:



高地址

+-------------------+

|   栈 (Stack)      |  <-- SS:ESP 指向栈顶

|   向下增长         |

+-------------------+

|                   |

|   空闲内存         |

|                   |

+-------------------+

|   BSS 段 (.bss)   |  未初始化的全局变量

+-------------------+

|   数据段 (.data)   |  已初始化的全局变量

+-------------------+

|   代码段 (.text)   |  程序指令(只读)

+-------------------+

|   保留区域         |  操作系统使用

+-------------------+

低地址


Real mode vs protected mode

Feature Real mode (16-bit) Protected mode (32-bit)
Memory addressing Segment address x 16 + offset Segment selector looks up descriptor table + offset
Maximum memory 1MB 4GB (32-bit)
Memory protection No protection Page-level and segment-level protection
Multitasking Not supported Supported
Addressing mode segment:offset selector:offset (via descriptor table)

This tutorial mainly covers assembly programming in 32-bit protected mode. In this mode, segments are more of a logical division of memory regions, and physical addresses are managed by paging. You can simply think of section .data/.bss/.text as markers for different parts of the program in memory.