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. 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. 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). 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. How would you call str_init with the struct person1 passed to it?
Previous answers to this question
This is a preview of an assignment submitted on our website by a student. If you need help with this question or any assignment help, click on the order button below and get started. We guarantee authentic, quality, 100% plagiarism free work or your money back.
Get The Answer