Which Addressing Modes Minimize Instructions?

  • Thread starter Thread starter SuperSusanoo
  • Start date Start date
  • Tags Tags
    Modes
Click For Summary
SUMMARY

The discussion focuses on identifying addressing modes that minimize instructions in various programming scenarios. The addressing modes covered include Displacement-based, Memory Indirect, and Auto-increment. For the provided code examples, Displacement-based addressing is determined to be the most efficient for accessing array elements, while Memory Indirect is relevant for pointer dereferencing. The analysis emphasizes the importance of understanding how different data types affect memory addressing and instruction count.

PREREQUISITES
  • Understanding of MIPS assembly language
  • Familiarity with C/C++ programming concepts
  • Knowledge of memory addressing modes
  • Basic understanding of pointers and indirection
NEXT STEPS
  • Research MIPS assembly language syntax and instruction set
  • Learn about the implications of data types on memory allocation in C/C++
  • Study the differences between direct and indirect addressing modes
  • Explore optimization techniques for reducing instruction counts in assembly programming
USEFUL FOR

Students studying computer architecture, software developers working with low-level programming, and anyone interested in optimizing memory access patterns in their code.

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   Reactions: 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   Reactions: 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
6K
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