YouTip LogoYouTip

Assembly Memory Segment

## Assembly Language - Memory Segmentation Memory Segmentation is a core concept in the x86 architecture that determines how programs organize 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 address). 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, each with a base address and offset, combining a segment address + offset to form a complete physical address. Physical address formula: Physical Address = Segment Address Γ— 16 + Offset Address Although in protected mode (32-bit), the segmentation mechanism is more used for memory protection and access control, understanding segmentation is still essential 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, local variables | Automatically managed by the OS | * * * ## Code Segment (.text) The code segment stores all machine instructions of the program, and is a read-only, executable memory area. When the operating system loads the program, it maps the code segment to read-only memory pages to prevent the program from accidentally modifying instructions. ## Example ; File path: code_segment.asm ; Demonstrate code segment usage section.text; Start 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, and the program can modify its contents during runtime. ## Example ; File path: data_segment.asm ; Demonstrate data segment usage section.data ; Define various types of initialized variables msg db'Hello, tutorial!',0; String variable (byte sequence) count db 100; Byte variable, initial value 100 pi dd 314159; Double word variable, stores Ο€ approximation Γ— 100000 array db 1,2,3,4,5; Byte array section.text global _start _start: ; Read variables from the data segment mov al,; 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 does not occupy actual space in the executable file. The operating system only allocates and zeros the memory when the program is loaded. ## Example ; File path: bss_segment.asm ; Demonstrate BSS segment usage section.bss buffer resb 256; Reserve 256 bytes of buffer (uninitialized) num_array resd 100; Reserve 100 double words (400 bytes) section.data ; Initialized data section.text global _start _start: ; Use the buffer from the BSS segment mov byte,'A'; Write character 'A' to the buffer ; ... mov eax,1 mov ebx,0 int 0x80 > Placing uninitialized data in the BSS segment instead of the .data segment can reduce the size of the executable file. For example, a 10KB uninitialized buffer does not occupy file space in the BSS segment, but if placed in the .data segment, it would increase the file size by 10KB. * * * ## Segment Registers The x86 architecture has 6 segment registers, used to track the currently in-use segment: | Segment Register | English Full Name | Purpose | | --- | --- | --- | | CS | Code Segment | Points to the code segment, stores the segment base address where the currently executing instruction is located | | DS | Data Segment | Points to the data segment, stores the segment base address for most data access | | SS | Stack Segment | Points to the stack segment, stores the segment base address where the stack is located | | ES | Extra Segment | Extra segment register, used for string operations and other additional data segments | | FS | General Purpose | General-purpose segment register, commonly used for thread-local storage | | GS | General Purpose | General-purpose segment register, commonly used for thread-local storage | In 32-bit protected mode, programmers usually don't need to manually set segment registers; the operating system and linker handle this. * * * ## Memory Segmentation Diagram The distribution of a running program in memory is as follows: ![Image 1: Program Memory Layout](https://example.com/wp-content/uploads/2026/04/memory-layout.svg) The distribution of a running program in memory is roughly as follows: High Address+-------------------+| Stack | <-- SS:ESP points to stack top| Grows Downward |+-------------------+| || Free Memory || |+-------------------+| BSS Segment (.bss) | Uninitialized Global Variables+-------------------+| Data Segment (.data) | Initialized Global Variables+-------------------+| Code Segment (.text) | Program Instructions (Read-only)+-------------------+| Reserved Area | For OS Use+-------------------+Low Address * * * ## Real Mode vs Protected Mode | Feature | Real Mode (16-bit) | Protected Mode (
← Assembly SyscallAssembly Env Setup β†’