ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

ASM x86 Memory Addressing: Indirect Indexed Access

ASM x86

Goal -- WPM

Ready
Exercise Algorithm Area
1; Accessing Memory with Indirect Indexed Addressing
2; Sums elements of an array using indirect indexed addressing.
3; Demonstrates base + index * scale addressing mode.
4
5section .data
6my_array dq 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ; Array of 10 QWORDs
7array_size equ 10
8
9section .text
10global _start
11
12_start:
13; Example usage: sum elements of my_array
14mov rdi, my_array ; Base address of the array
15mov rsi, array_size ; Number of elements to sum
16call sum_array_indirect_indexed
17; Result (sum) is in RAX
18
19; Exit program
20mov rax, 60
21mov rdi, 0
22syscall
23
24; Function: sum_array_indirect_indexed
25; Sums elements of an array using indirect indexed addressing.
26; Input:
27; RDI = Base address of the array (QWORDs)
28; RSI = Number of elements to sum
29; Output:
30; RAX = Sum of the elements
31sum_array_indirect_indexed:
32; --- Prologue ---
33push rbp
34mov rbp, rsp
35push rbx ; Save callee-saved register (for sum)
36push r12 ; Save callee-saved register (for index)
37
38mov rbx, 0 ; Initialize sum to 0
39mov r12, 0 ; Initialize index to 0
40
41; --- Loop ---
42.loop_start:
43cmp r12, rsi ; Compare current index with number of elements
44jge .loop_end ; If index >= size, exit loop
45
46; Calculate effective address: RDI + R12 * 8
47; RDI is the base address
48; R12 is the index (0, 1, 2, ...)
49; 8 is the scale factor for QWORDs (8 bytes)
50; The instruction format is [base + index * scale]
51; Example: mov rax, [rdi + r12 * 8]
52
53; Load element using indirect indexed addressing
54mov rax, [rdi + r12 * 8] ; Load element at address base + index*scale
55
56; Add element to sum
57add rbx, rax ; Add the loaded element to the sum
58
59inc r12 ; Increment index
60jmp .loop_start ; Continue loop
61
62.loop_end:
63mov rax, rbx ; Move the final sum to RAX for return
64
65; --- Epilogue ---
66pop r12 ; Restore callee-saved register
67pop rbx ; Restore callee-saved register
68pop rbp
69ret
Algorithm description viewbox

ASM x86 Memory Addressing: Indirect Indexed Access

Algorithm description:

This ASM x86 code demonstrates summing elements of an array using indirect indexed memory addressing. It calculates the effective memory address by combining a base pointer, an index register, and a scale factor. This addressing mode is fundamental for accessing elements in arrays and structures efficiently.

Algorithm explanation:

The `sum_array_indirect_indexed` function calculates the sum of elements in an array. It iterates through the array using an index (`R12`). For each element, it computes the effective memory address using the indirect indexed addressing mode: `[base + index * scale]`. Here, `RDI` is the base address, `R12` is the index, and `8` is the scale factor because the array elements are QWORDs (8 bytes). The value at the calculated address is loaded into `RAX`, and then added to the running sum stored in `RBX`. The loop continues until all `RSI` elements have been processed. The final sum is then moved to `RAX` for the return value. This method is efficient for accessing array elements sequentially. Time complexity is O(N) where N is the number of elements, and space complexity is O(1).

Pseudocode:

FUNCTION sum_array_indirect_indexed(base_address, num_elements):
  sum = 0
  index = 0

  WHILE index < num_elements:
    effective_address = base_address + index * sizeof(element)
    element_value = MEMORY[effective_address]
    sum = sum + element_value
    index = index + 1
  END WHILE

  RETURN sum