Example with Arrays:
Consider an array of integers stored in memory. We can use auto-increment addressing to access and process each element of the array sequentially. Assume the array starts at memory address 100:
MOV R1, #100 ; Load base address of the array into register R1
MOV R2, #0 ; Initialize a counter in R2 to access elements sequentially
LOOP:
LOAD R3, [R1]+ ; Load the value at the memory address pointed by R1 and increment R1
; Here, you can perform operations with the value in R3 (e.g., add, subtract, etc.)
; Example: Print the value in R3 (assuming an output routine to display R3 value)
OUT R3
INC R2 ; Increment the counter to move to the next element
CMP R2, #10 ; Compare the counter with the number of elements in the array (10 in this case)
JNZ LOOP ; Jump back to the LOOP if the counter is not equal to 10 (i.e., not all elements processed)
In this example, we use the [R1]+ auto-increment addressing mode to access each element of the array one by one.
Example with Linked Lists:
Consider a singly linked list, where each node has a value and a pointer to the next node. We can use auto-increment addressing to traverse the linked list:
MOV R1, #500 ; Load the memory address of the head node into register R1
TRAVERSE:
LOAD R2, [R1] ; Load the value at the memory address pointed by R1 (current node value)
; Here, you can perform operations with the value in R2 (e.g., print, compare, etc.)
; Example: Print the value in R2 (assuming an output routine to display R2 value)
OUT R2
LOAD R1, [R1+4] ; Load the next node's address by reading the pointer at (R1 + 4) and increment R1
CMP R1, #0 ; Compare R1 with 0 to check if we reached the end of the list (null pointer)
JNZ TRAVERSE ; Jump back to TRAVERSE if R1 is not equal to 0 (i.e., list traversal is not completed)
We use the [R1]+4 auto-increment addressing mode to move from one node to the next in the linked list.