1. Oct 15, 2016

### SuperSusanoo

1. The problem statement, all variables and given/known data
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 (Text):
int a = 0; uint8_t D[100]; // D is allocated in memory
While (a < 100){
D[a] =  a + 5;
a += 1;  }
(2)
Code (Text):
int a = 0; int D[100]; // D is allocated in memory
While (a < 100) {
D[a] =  a + 5;
a += 1;  }
(3)
Code (Text):
int *p; // *p is allocated in memory
*p = 100;
(4)
Code (Text):
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.)

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

3. The attempt at a solution

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: Oct 15, 2016
2. Oct 15, 2016

### Staff: Mentor

Here are some comments about each of your examples that might be of some help to you.

1)
Code (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)
Code (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)
Code (C):
int *p; // *p is allocated in memory
*p = 100;

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.
Code (C):
int **p; // *p and **p are allocated in memory
**p = 100;
Here's a working example of the above:
Code (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.