Which Addressing Modes Minimize Instructions?

  • Thread starter Thread starter SuperSusanoo
  • Start date Start date
  • Tags Tags
    Modes
Click For Summary
The discussion focuses on identifying which addressing modes minimize instructions for various high-level programs involving arrays and pointers. It highlights that displacement-based addressing is effective for accessing array elements, as it computes the address using a base plus an immediate offset. The examples illustrate how the size of data types (e.g., uint8_t vs. int) affects memory access calculations. Additionally, the complexity of pointer dereferencing is discussed, particularly with single and double indirection. Understanding these addressing modes is crucial for optimizing instruction count in low-level programming.
SuperSusanoo
Messages
7
Reaction score
1

Homework Statement


We covered the following addressing modes in class:
• Absolute
• Register indirect
• Based (base + displacement)
• Scale indexed (base + index × constant)
• Memory indirect
• Auto increment/decrement (by 1 byte)
Consider the following high-level programs:

(1)
Code:
int a = 0; uint8_t D[100]; // D is allocated in memory
While (a < 100){
        D[a] =  a + 5;
         a += 1;  }
(2)
Code:
 int a = 0; int D[100]; // D is allocated in memory
      While (a < 100) {
           D[a] =  a + 5;
           a += 1;  }
(3)
Code:
int *p; // *p is allocated in memory
       *p = 100;
(4)
Code:
 int **p; // *p and **p are allocated in memory
      **p = 100;

Assume that in the first two programs, a register contains the address of the start of the array, and in the last two programs, a register contains the value of p.
For each of the above four programs, which of the addressing modes, do you think, would lead to the minimum number of instructions? (Note that no addressing mode fits perfectly. You might require other instructions for address computation.)

Homework Equations


• Displacement (based): address = [R2+immed], e.g., #20(R2)

• Index-base: address = [R2+R3]

• Memory-indirect: address =[mem[R2]]

• Auto-increment: address=[R2], R2= R2+1

• Auto-indexing: address =[R2+immed], R2=R2+immed

• Scaled (scale-indexed): address =[R2+R3*immed1+immed2]

• PC-relative: address =[PC+imm]

The Attempt at a Solution


[/B]
At this point , I am looking for approaches. What would be a good start?. I know that giving solutions away is not allowed. Maybe it would be good if someone can walk me through the first piece code and then I can do the others. I think a good start would be to write the code in MIPS and then analyze each instruction, nevertheless I don't understand how instructions relate to addressing modes. I will be updating the thread with future appoaches

1) Displacement based because we will be computing the address of each index by using an offset which is the immediate value. In this case the offset is 4 since it's an array of integers and the register contains the address of the first index of the array
 
Last edited:
  • Like
Likes berkeman
Physics news on Phys.org
Here are some comments about each of your examples that might be of some help to you.

1)
C:
int a = 0; uint8_t D[100]; // D is allocated in memory
While (a < 100){
        D[a] =  a + 5;
         a += 1;  }
D is an array of 8-bit bytes, so access to D requires the base (starting address of the array) plus the offset of the particular element to write to. In terms of C or C++, D is the address of the start of the array (i.e., the address of D[0]) and D + 3 would be the address of the byte at array index 3.

2)
C:
int a = 0; int D[100]; // D is allocated in memory
      While (a < 100) {
           D[a] =  a + 5;
           a += 1;  }
The only difference between this code and the code in example 1 is that here D is an array of int values, which we can reasonably assume are 32-bit (four bytes) quantities. In terms of C/C++ D is again the address of the starting element of the array, and D + 3 is the address of the array element at index 3. Note that in terms of bytes, though, the address of D + 3 is really 12 bytes higher in memory than the address of D. C/C++ does the address scaling for you, but low-level languages make you do that yourself.

In both of the preceding examples, the statement a += 1; has the same effect as a++; .

3)
C:
int *p; // *p is allocated in memory
       *p = 100;
The variable p contains an address. *p gives you indirect read or write access to the memory represented by that address.

4)In this example we have double indirection, although if you tried to compile this code you would either get a compiler error or you would get a run-time error.

Assuming the code is fixed so that all pointers are properly initialized, the situation is that p contains a memory address, and the memory at that address contains another address, so here we have double indirection.

Suppose p contains the hypothetical address 1000, and suppose further that at address 1000, the memory contains 1020 (considered as an address).
Then with this setup, the value of p is 1000, the value of *p is 1020, and the value of **p is whatever number is at location 1020.
C:
int **p; // *p and **p are allocated in memory
      **p = 100;

Here's a working example of the above:
C:
#include <stdio.h>

int main()
{
   int a = 0;
   int *int_ptr = &a;
   int **p = &int_ptr;
   **p = 100;
   printf("%d", a);
   return 0;
}[code]
The call to printf() displays 100 in the console.
 
  • Like
Likes SuperSusanoo

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 23 ·
Replies
23
Views
5K
Replies
8
Views
3K
Replies
2
Views
16K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K