TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Devry gsp 215 week 3 homework representing and manipulating information new
1. DEVRY GSP 215 Week 3 Homework Representing and
Manipulating Information NEW
Check this A+ tutorial guideline at
http://www.homeworkrank.com/gsp-215-devry/gsp-215-
week-3-homework-representing-and-manipulating-
information-updated
For more classes visit
http://www.homeworkrank.com
GSP 215 Week 3 Homework Assignment
Week 3 Homework—Representing and
Manipulating Information
Structures are a mechanism for creating a
data type to aggregate multiple objects
into a single unit. All the components of a
structure are stored in a contiguous
region of memory and a pointer to a
structure is the address of its first byte. To
access the fields of a structure, the
compiler generates code that adds the
appropriate offset to the address of the
structure.
The example on the book on page 242
shows the following structure.
2. struct rec {
int i;
int j;
int a[3];
int *p;
};
This structure contains four fields: two 4-
byte int's, an array consisting of three 4-
byte int's, and a 4-byte int pointer giving a
total of 24 bytes. j is offset 4 bytes.
0 4 8 16 20
24
i j a[0] a[1] a[2] p
Assuming variable 4 is of type struct rec *
and is in register %edx, the following code
copies element r->i to element r->j.
movl (%edx), %eax // Get r->i
movl %eax, 4(%edx) //Store r->j To store into the field j, the
code adds offset 4 to the address of r
Consider the following structure
declaration containing a structure within
a structure, and answer the following
questions.
3. struct person{
struct size{
int height;
int weight;
}s;
int *hp;
int games[2];
}person1, person2;
1. How many total bytes does the structure require?
height weight hp games[0] games[1]
2. What are the offsets in bytes of the following fields?
s.height: ______________
hp:_______________
games[1]:______________
3. The compiler generates the following assembly code for
the body of str_init (shown below).
4. movq 16(%rbp), %rax //Get p1 into
register %rax
movl 4(%rax), %edx //Get p1-
>s.weight store in register %edx
movq 16(%rbp), %rax //Get p1 into
register %rax
movl %edx, (%rax) //Store in p1-
>s.height
movq 16(%rbp), %rax //Get p1 into
register %rax
leaq 4(%rax), %rdx //Compute
address of p1->s.weight in register %rdx
movq 16(%rbp), %rax //Get p1 into
register %rax
movq %rdx, 8(%rax) //Store in p1-
>hp
4. On the basis of this information, fill in the missing
expressions in the code for str_init.
void str_init(person *p1)
{
p1->s.height = _____________;
p1->hp = _________________;
5. }
5. How would you call str_init with the structperson1 passed
to it?
6. }
5. How would you call str_init with the structperson1 passed
to it?