The Gateway to Computer Science Excellence
+25 votes
4.2k views

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-bit$ representation
in CO and Architecture by Veteran (425k points)
edited by | 4.2k views
–2
The correct answer is option D i.e index addressing mode
+11

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 

Ref2: https://stackoverflow.com/questions/2748995/c-struct-memory-layout

4 Answers

+32 votes
Best answer

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

by (323 points)
edited by
0
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.
as like A[10]=*(A+10)=element at(base address+displacement)
+3

@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.
+3

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..
+2 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.

by Active (3.8k points)
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.

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.

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.

by Junior (991 points)
–2 votes
pls provide answer
by Junior (707 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,497 answers
195,491 comments
100,821 users