5.5k views

Consider the $C$  struct defined below:

struct data {
int marks [100];
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-bit$ representation

edited | 5.5k views
–2
+12

Assume memory is byte addressable, 'int' takes 4B, 'char' takes 1B. Thus, student.marks[0] will be at offset 0, student.grade will be at offset 400, student.cnumber will be at offset 401.

Ref1: http://minnie.tuhs.org/CompArch/Lectures/week03.html    Navigate to 3.6

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:

edited by
+1
Can we simply say as base address of student in register $R_{1}$

and it will be in an array

So index addressing mode will be more preferred.

what is meaning of " where X is an offset represented in 2's complement 16-bit representation. "
0
x is used to fetch element  at certain position from base address.
+5

@Niraj

what does these lines mean?

"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."

0
Significance of offset is to determine attribute of that array element. Once you got the EA, (X + R1), offset field will help to determine the attribute of that element since arrays elements are saved in successive memory locations.
0
In simple words, X (offset) is the position from the program counter, where the next operand should be fetched from.
+4

Suppose memory is byte addressable and an int takes 4 bytes space.
Assume that base address of student is $1000$ then $R1$ contains $1000$.
student.grade can be accessed by $X(R1)$
Here in our case $X = 100*4=400,$ so address of $student.grade = 400(1000) =400+1000= 1400.$
https://ideone.com/ZviClG

0
@Ayush Upadhyaya He meant to say that in PC addressing mode, the address field is generally treated as a 2's complement number, whereas in the base register, the address field is an unsigned integer representation.
0
first.  in options there's no displacement mode

2nd.  in BEST ANSWER last line implicitly days index is best for array accessing and displacement is best for structure accessing...

then how option d which is indexed addressing can be a answer..

index and displacement are totally different..

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.

0
can u tell me wat is register direct addressing ??? and if u consider C as answer hw r u going to retrieve student.grade ??
0

register direct addressing mode  ADDRESS OF THE DATA IS DIRECTLY PRESENT IN THE REGISTER.

sruct data
{
int marks[100];
int cnumber;
}; struct data student
Base Address of student is available in R1.