NASA

A Handbook for Assembly | Chapter 4: Indexed Addressing


 

Chapter 4: Indexed Addressing


### 4.1 Introduction


Indexed addressing is a versatile addressing mode in assembly language that allows efficient access to array elements or structured data by using an index value. This chapter explores the concept of indexed addressing, its syntax, and its applications in data manipulation.


### 4.2 Understanding Indexed Addressing


#### 4.2.1 Definition


- **Indexed Addressing:**

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


#### 4.2.2 Memory Access


- **Accessing Data:**

  - Retrieves data from the memory location calculated as the sum of a base register and a constant index.


#### 4.2.3 Syntax


- **Example:**

  ```assembly

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

  ```


### 4.3 Use Cases


#### 4.3.1 Array Access


- **Example:**

  ```assembly

  mov ecx, [array_base + 2*index]   ; Move the value at the memory location (array_base + 2*index) into register ecx

  ```


#### 4.3.2 Data Structures


- **Example:**

  ```assembly

  mov edx, [struct_ptr + field_offset]   ; Move the value at the memory location (struct_ptr + field_offset) into register edx

  ```


#### 4.3.3 Dynamic Memory Access


- **Example:**

  ```assembly

  mov esi, [buffer_ptr + 8*index]   ; Move the value at the memory location (buffer_ptr + 8*index) into register esi

  ```


### 4.4 Modifying Data in Memory


#### 4.4.1 Updating Values


- **Example:**

  ```assembly

  add dword [array_base + 4*index], 10   ; Add 10 to the value at the memory location (array_base + 4*index)

  ```


#### 4.4.2 Writing to Memory


- **Example:**

  ```assembly

  mov [result_ptr + 2*index], eax   ; Move the value in register eax to the memory location (result_ptr + 2*index)

  ```


### 4.5 Address Calculations


#### 4.5.1 Effective Address


- **Calculation:**

  - The effective address is determined by the sum of the base register and the scaled index.


#### 4.5.2 Example


- **Example:**

  ```assembly

  mov esi, [base_register + 2*index]   ; Calculate the effective address (base_register + 2*index) and move its value to esi

  ```


### 4.6 Conclusion


Indexed addressing is a powerful addressing mode that simplifies the access and manipulation of data in arrays and data structures. It provides a concise and efficient way to perform operations on elements with a constant offset. Understanding how to use indexed addressing is crucial for optimizing assembly language programs.


Comments

Houston We Have a Podcast

Translate