Addressing Modes question

  • Thread starter SuperSusanoo
  • Start date
  • Tags
    Modes
Note that the above is not good programming style, but it illustrates the point. In real-life code, the addresses would be addresses of variables or structures, not just arbitrary addresses.
  • #1
SuperSusanoo
7
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
  • #2
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

1. What are addressing modes in computer architecture?

Addressing modes are methods used by a computer's central processing unit (CPU) to access data or instructions from memory. They specify the way in which an operand, or data, is located and retrieved from memory for processing.

2. What is the purpose of addressing modes?

The purpose of addressing modes is to provide flexibility and efficiency in how data is accessed and manipulated by the CPU. Different addressing modes are used for different types of operations and data, allowing for more efficient execution of instructions.

3. What are the different types of addressing modes?

There are several types of addressing modes, including immediate, direct, indirect, indexed, and relative. Immediate addressing mode directly specifies the value of the operand, while direct addressing mode uses a memory address to locate the operand. Indirect addressing mode uses a memory address that points to the location of the operand, while indexed addressing mode adds an index or offset to a base address to locate the operand. Relative addressing mode uses a fixed reference point, such as the program counter, to locate the operand.

4. How do addressing modes affect program execution?

The choice of addressing mode can greatly impact the efficiency and speed of program execution. Some addressing modes may require more instructions or clock cycles to complete an operation, while others may allow for quicker execution. It is important for programmers to carefully consider the use of addressing modes to optimize program performance.

5. Can multiple addressing modes be used in a single instruction?

Yes, some instructions can use multiple addressing modes to access different operands. This is known as "complex addressing mode" and can be useful for certain types of operations. However, not all instructions support complex addressing modes, and their use should be carefully considered for efficiency and clarity.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
671
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
734
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
16K
Back
Top