• edited by
26,608 views
76 76 votes

Consider the $C$  struct defined below:

struct data {
    int marks [100];
    char grade;
    int cnumber;
};
struct data student;

The base address of student is available in register $R1$. The field student.grade can be accessed efficiently using:

  1. Post-increment addressing mode, $(R1)+$
  2. Pre-decrement addressing mode, $-(R1)$
  3. Register direct addressing mode, $R1$
  4. Index addressing mode, $X(R1)$, where $X$ is an offset represented in $2's$ complement $16\text{-bit}$ representation

6 Answers

Best answer
92 92 votes

Answer is option (D).

Displacement Mode :-

Similar to index mode, except instead of a index register a base register will be used. Base register contains a pointer to a memory location. An integer (constant) is also referred to as a displacement. The address of the operand is obtained by adding the contents of the base register plus the constant. The difference between index mode and displacement mode is in the number of bits used to represent the constant. When the constant is represented a number of bits to access the memory, then we have index mode. Index mode is more appropriate for array accessing; displacement mode is more appropriate for structure (records) accessing.

Reference:

http://www.cs.iit.edu/~cs561/cs350/addressing/addsclm.html

• edited by
48 48 votes
struct data {
    int marks[100];  // 100 * 4 bytes = 400 bytes (assuming 4-byte ints)
    char grade;      // 1 byte
    int cnumber;     // 4 bytes (assuming int is 4 bytes)
};
struct data student;

Aur maan lo register R1 me student ka base address hai. Humein student.grade waali location chahiye.

  1. Index (displacement) addressing: X(R1)

    • Yahaan hum R1 ko base maante hain aur usme ek offset (X) add karke particular field pe pahunch jaate hain.
    • Offset 16-bit 2’s complement representation mein diya hota hai, jisse hum structure ke andar koi field direct access kar sakte hain.

 

Post-increment (R1)+

  • Isme pehle R1 se memory access hoti hai, fir R1 ko increment kiya jaata hai.
  • Zyada tar ye array traversal mein kaam aata hai (element by element).

Pre-decrement –(R1)

  • Pehle R1 decrement hota hai, fir memory access.
  • Ye stack push/pop jaisi cheezo ke liye convenient hai.

Register direct addressing (R1)

  • Isme hum R1 ko hi data ki tarah treat karte hain.
  • Par yahan R1 me base address hai, field offset handle karne ke liye koi facility nahi.

Kyun “Index addressing mode” Sahi Hai?

  • student.grade structure ke andar 400 bytes baad (yani marks[100] ke baad) aata hai.
  • Humein R1 (base) + offset (400) pe jana hai.
  • Post-increment ya pre-decrement use karne se R1 badal jaayega, jo hum structure ke baaki fields ke liye nahi chahte.
  • Register direct me toh offset handle nahi kar sakte.

Isliye sabse efficient tariqa hai “Index addressing mode, X(R1)”, jahan X = grade field ka offset ho

 
12 12 votes

Actually answer can be both C or D.It depends on the architecture.If the machine is byte addressable then option D is correct.

For instance, say int takes 4 bytes and char takes 8 bytes.If it is byte addressable then the memory layout will be somewhat shown in the figure.If we are using for e.g decimal addressing and lets take the base address as 1000,then we need displacement of 51 to access student.grade.

Now if the machine is word addressable and say 1 word=100*4(for marks array)+8(for grade)+4(for cnumber)=412bytes .Then the picture will look something like..

In this case we dont need displacement...it can be done with direct addressing mode only.

2 2 votes

sruct data
{
int marks[100];
char grade;
int cnumber;
}; struct data student
Base Address of student is available in R1.
So student.grade can be accessed efficiently by Relative Indexed Addressing Mode.
It is clearly mentioned X is the offset address to be summed with Base Address of R1.

Hence Index Addressing mode X(R1), where X is an offset represented in 2’s complement 16-bit representation.
⇾ Relative, Base Indexed & all subtypes of Indirect addressing modes are used with Arrays.

2 2 votes
Ans D) The struct student has a marks[100] array (400 bytes), followed by a grade field (1 byte). To access student.grade, we need to address it 400 bytes after the base address of student.

Indexed addressing mode is the most efficient here because we know the exact offset of 400 bytes from the base. This mode allows us to compute the address as base_address + offset efficiently.

Other modes like post-increment and pre-decrement are suited for sequential access, not for direct field access. Register direct would require loading the precise address, making it less efficient. Hence, indexed addressing mode is ideal for accessing student.grade using the known offset.
0 0 votes
 

sruct data
{
int marks[100];
char grade;
int cnumber;
}; struct data student
Base Address of student is available in R1.
So student.grade can be accessed efficiently by Relative Indexed Addressing Mode.
It is clearly mentioned X is the offset address to be summed with Base Address of R1.

And Index Addressing mode X(R1), where X is an offset represented in 2’s complement 16-bit representation.And Relative, Base Indexed & all subtypes of Indirect addressing modes are used with Arrays.
Answer:
Position:
Show:

Related questions

89 89 votes
12 answers 12 answers
28.8k
28.8k views
Arjun asked Feb 14, 2017
28,846 views
A cache memory unit with capacity of $N$ words and block size of $B$ words is to be designed. If it is designed as a direct mapped cache, the length of the $\textsf{TAG}$...
145 145 votes
11 answers 11 answers
61.5k
61.5k views
Arjun asked Feb 14, 2017
61,541 views
Consider a $2$-way set associative cache with $256$ blocks and uses $\text{LRU}$ replacement. Initially the cache is empty. Conflict misses are those misses which occur d...
69 69 votes
10 answers 10 answers
31.5k
31.5k views
khushtak asked Feb 14, 2017
31,493 views
Instruction execution in a processor is divided into $5$ stages, Instruction Fetch (IF), Instruction Decode (ID), Operand fetch (OF), Execute (EX), and Write Back (WB). T...
85 85 votes
6 answers 6 answers
26.2k
26.2k views
Arjun asked Feb 14, 2017
26,237 views
Consider a RISC machine where each instruction is exactly $4$ bytes long. Conditional and unconditional branch instructions use PC-relative addressing mode with Offset sp...