Consider the struct defined below:
struct data {
int marks [100];
char grade;
int cnumber;
};
struct data student;
The base address of student is available in register . The field student.grade can be accessed efficiently using:
View worked solution
Answer: D
To access a specific field of a structure, we need to calculate its memory address relative to the base address of the structure.
The layout of the struct data in memory is sequential:
marks(an array of 100 integers) starts at the base address of the structure.grade(a character) is located immediately after themarksarray.
Let the base address of student (stored in register ) be . The member student.grade is located at a fixed offset from the base address, where:
Therefore, the effective address of student.grade is calculated as:
This is precisely how Index addressing mode (also referred to as Base-displacement addressing mode), represented as , operates. The constant offset is determined at compile-time and can be easily represented as a 16-bit 2's complement displacement.
Why other options are incorrect:
- Options A & B (Post-increment / Pre-decrement): These modes modify the contents of register (the base address pointer) as a side effect, which is undesirable because we want to preserve the base address of the structure in for future accesses.
- Option C (Register direct): This mode accesses the contents of the register itself, rather than the memory location pointed to by .
Free practice · Your answers on this page are not saved.
