Computer organization : Explain auto-decrement addressing

Auto-decrement addressing is an addressing mode used in computer architectures that automatically decrements the memory address register (MAR) or an index register after accessing the memory location. This addressing mode is commonly used in assembly language programming to facilitate certain operations, especially those involving data structures that require backward traversal or processing elements in reverse order.

In auto-decrement addressing, the memory location pointed to by the MAR or index register is first accessed for the operation, and then the register’s value is automatically decremented by a fixed value after the access. This means that the next access will be to the memory location immediately preceding the current one.

An example:

Consider a simple scenario where we have an array of values stored in memory. The array starts at memory address 200, and each element occupies four bytes.

MOV R1, #216      ; Load the memory address of the last element into register R1 (address 200 + 4 * (number of elements - 1))
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 decrement R1
  ; Here, you can perform operations with the value in R3 (e.g., print, compare, 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 LOOP if the counter is not equal to 10 (i.e., not all elements processed)

We use the [R1]- auto-decrement addressing mode to access each element of the array in reverse order. The loop starts with R1 pointing to the last element of the array (address 200 + 4 * (number of elements – 1)). After accessing the value, R1 is automatically decremented by 4, so the next access will be to the previous element in the array.

Auto-decrement addressing is particularly useful in situations where you need to traverse data structures backward, process elements in reverse order, or work with stacks (which are inherently last-in, first-out data structures).

Author: user

Leave a Reply