NASA

A Handbook for Assembly | Chapter 3: Data Storage in Memory


 Chapter 3: Data Storage in Memory


### 3.1 Introduction


Understanding how data is stored in memory is essential for effective assembly language programming. This chapter explores the basics of memory organization, addressing modes, and the principles governing data storage in a computer's memory.


### 3.2 Memory Basics


#### 3.2.1 Memory Cells


- **Memory Cells:**

  - Smallest units of storage in memory.

  - Typically 8 bits (1 byte) or larger.


#### 3.2.2 Addressing Memory


- **Addresses:**

  - Each memory cell has a unique address.

  - Addresses are used to access and manipulate data.


#### 3.2.3 Endianness


- **Endianness:**

  - Defines the order in which bytes are stored in memory.


- **Little-Endian:**

  - Least significant byte stored at the lowest memory address.


- **Big-Endian:**

  - Most significant byte stored at the lowest memory address.


### 3.3 Memory Segments


#### 3.3.1 Code Segment


- **Code Segment:**

  - Stores executable code.


- **Example (NASM):**

  ```assembly

  section .text

      global _start

  _start:

      ; Code here

  ```


#### 3.3.2 Data Segment


- **Data Segment:**

  - Stores initialized data.


- **Example (NASM):**

  ```assembly

  section .data

      variable db 42

  ```


#### 3.3.3 BSS Segment


- **BSS Segment:**

  - Stores uninitialized data.


- **Example (NASM):**

  ```assembly

  section .bss

      uninitialized_var resb 4

  ```


### 3.4 Addressing Modes


#### 3.4.1 Immediate Addressing


- **Immediate Addressing:**

  - Operand is a constant value.


- **Example:**

  ```assembly

  mov eax, 42    ; Move the immediate value 42 into register eax

  ```


#### 3.4.2 Register Addressing


- **Register Addressing:**

  - Operand is located in a register.


- **Example:**

  ```assembly

  mov ebx, eax   ; Move the value in register eax to register ebx

  ```


#### 3.4.3 Direct Addressing


- **Direct Addressing:**

  - Operand is located at a specific memory address.


- **Example:**

  ```assembly

  mov eax, [variable]   ; Move the value stored at the memory address 'variable' into register eax

  ```


#### 3.4.4 Indirect Addressing


- **Indirect Addressing:**

  - Operand is located at the address stored in another register or memory location.


- **Example:**

  ```assembly

  mov eax, [ebx]   ; Move the value stored at the memory address in register ebx into register eax

  ```


#### 3.4.5 Indexed Addressing


- **Indexed Addressing:**

  - Operand is located at the sum of a register and a constant (index).


- **Example:**

  ```assembly

  mov eax, [ebx + 4]   ; Move the value stored at the memory address (ebx + 4) into register eax

  ```


#### 3.4.6 Base-Register Addressing


- **Base-Register Addressing:**

  - Operand is located at the sum of a base register and an index register.


- **Example:**

  ```assembly

  mov eax, [ebx + ecx]   ; Move the value stored at the memory address (ebx + ecx) into register eax

  ```


### 3.5 Memory Alignment


- **Memory Alignment:**

  - Ensures that data is stored in memory at addresses that are multiples of a certain value.


- **Alignment Requirements:**

  - Vary based on the data type and the architecture.


### 3.6 Conclusion


Understanding how data is stored in memory and how addresses are used to access that data is fundamental to assembly language programming. Addressing modes provide flexibility in specifying operands, allowing for efficient manipulation of data.


Comments

Houston We Have a Podcast

Translate