{"id":217645,"date":"2018-09-15T23:00:19","date_gmt":"2018-09-15T23:00:19","guid":{"rendered":"http:\/\/localhost\/wordpress\/index.php\/2018\/09\/15\/gsp-215-week-3-homework-representing-and-manipulating-information\/"},"modified":"2018-10-24T08:56:52","modified_gmt":"2018-10-24T08:56:52","slug":"gsp-215-week-3-homework-representing-and-manipulating-information","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/gsp-215-week-3-homework-representing-and-manipulating-information\/","title":{"rendered":"GSP 215 Week 3 Homework Representing and Manipulating Information"},"content":{"rendered":"<p>GSP 215 Week 3 Homework Assignment\t\u00a0\tWeek 3 Homework&#8221;\ufffdRepresenting and Manipulating Information\t\u00a0\tStructures 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.\u00a0\tThe example on the book on page 242 shows the following structure.\tstruct rec {\tint i;\tint j;\tint a[3];\tint *p;\t};\tThis structure contains four fields: two 4-byte int&#8217;s, an array consisting of three 4-byte int&#8217;s, and a 4-byte int pointer giving a total of 24 bytes. j is offset 4 bytes.\t\u00a0 \u00a0 0  \u00a0 \u00a0 \u00a0 \u00a04  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a08  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 16  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 20 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a024\ti j a[0] a[1] a[2]  p\t\u00a0\tAssuming variable 4 is of type struct rec * and is in register %edx, the following code copies element r->i to element r->j.\t\u00a0 \u00a0 movl \u00a0(%edx), %eax \u00a0 \u00a0\/\/ Get r->i\t\u00a0 \u00a0 movl %eax, 4(%edx) \/\/Store r->j \u00a0To store into the field j, the code adds offset 4 to the address of r\tConsider the following structure declaration containing a structure within a structure, and answer the following questions.\tstruct person{\tstruct size{\tint height;\tint weight;\t}s;\tint *hp;\tint games[2];\t}person1, person2;\t1. How many total bytes does the structure require?\t\u00a0\theight weight hp games[0] games[1]\t\u00a0\t2. What are the offsets in bytes of the following fields?\ts.height: ______________\thp:_______________\t\u00a0 \u00a0games[1]:______________\t\u00a0\t3. The compiler generates the following assembly code for the body of str_init (shown below).\u00a0\t\u00a0 \u00a0 \u00a0 \u00a0\u00a0\t\u00a0 \u00a0 \u00a0 \u00a0 movq \u00a0 \u00a016(%rbp), %rax \u00a0 \u00a0 \/\/Get p1 into register %rax\t\u00a0 \u00a0 \u00a0 \u00a0 movl \u00a0 \u00a04(%rax), %edx \u00a0 \u00a0 \u00a0\/\/Get p1->s.weight store in register %edx\t\u00a0 \u00a0 \u00a0 \u00a0 movq \u00a0 \u00a016(%rbp), %rax \u00a0 \u00a0 \/\/Get p1 into register %rax\t\u00a0 \u00a0 \u00a0 \u00a0 movl \u00a0 \u00a0%edx, (%rax) \u00a0 \u00a0 \u00a0 \/\/Store in p1->s.height\t\u00a0 \u00a0 \u00a0 \u00a0 movq \u00a0 \u00a016(%rbp), %rax \u00a0 \u00a0 \/\/Get p1 into register %rax\t\u00a0 \u00a0 \u00a0 \u00a0 leaq \u00a0 \u00a04(%rax), %rdx \u00a0 \u00a0 \u00a0\/\/Compute address of p1->s.weight in register %rdx\t\u00a0 \u00a0 \u00a0 \u00a0 movq \u00a0 \u00a016(%rbp), %rax \u00a0 \u00a0\/\/Get p1 into register %rax\t\u00a0 \u00a0 \u00a0 \u00a0 movq \u00a0 \u00a0%rdx, 8(%rax) \u00a0 \u00a0 \/\/Store in p1->hp\t\u00a0\t\u00a0\t\u00a0\t\u00a0\t4. On the basis of this information, fill in the missing expressions in the code for str_init.\t\u00a0 \u00a0void str_init(person *p1)\t\u00a0 \u00a0{\tp1->s.height = _____________;\tp1->hp = _________________;\t}\t\u00a0\t5. How would you call str_init with the struct person1 passed to it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GSP 215 Week 3 Homework Assignment \u00a0 Week 3 Homework&#8221;\ufffdRepresenting and Manipulating Information \u00a0 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 href=\"https:\/\/www.benedictsol.com\/blogs\/gsp-215-week-3-homework-representing-and-manipulating-information\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-217645","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=217645"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217645\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=217645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=217645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=217645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}