Homework 3.67

Source: Internet
Author: User

Topic:

Analytical:

    1. All members of a union have an offset of 0 relative to the base address;
    2. This structure space is large enough to accommodate the most "wide" members
    3. Its way to fit all of the members (4 bytes)


For explanations of 2 and 32 points:

The space occupied by a consortium depends not only on the widest member, but also on all members, i.e., its size must meet two conditions: 1) large enough to accommodate the widest member; 2) the size can be divisible by the size of all the basic data types it contains.

Specific examples:

union U1 { char  s[ 10 ];   int  N; double  D; };

S is 10 bytes, n is 4 bytes, D is 8 bytes, so it requires at least 10 bytes of space. However, its actual size is not 10, and the operator sizeof tests its size to 16. This is because there is a problem with byte alignment, and 10 cannot be divisible by 4 or divisible by 8. So the bytes are added to 16 so that they align with the self of all members.

Answer

A:

This problem can be made by the fact that all members of a union have an offset of 0 relative to the base address, plus an int occupies 4 bytes, and the address (*next) occupies four bytes .

e1.p:0

E1.x:4

e2.y:0

E2.next:4

B:

This can be done based on the byte complement of the Union and the two conditions it satisfies.

E1 int *p--4 bytes int x---4 bytes and 8 bytes

E2 int y--4 byte Union ele *next--4 bytes and 8 bytes

Because two structs have a byte size of 8 and conform to the rules of byte completion, the union structure requires a total of 8 bytes.

C:

Before doing this, make it clear that the consortium requires access to the member before it can access the variables in the member, that is, you want x, you must obtain the E1 first, and then enter the E1 structure to access X, eventually forming e1.x.

1. Obviously MOVL 8 (%EBP),%edx this statement functions as get up at%edx

2. Try to write the statements sequentially, but find that it is not possible to determine exactly what each statement represents, so it is analyzed by observing the statements that have the characteristics.

3. Found in the statement Subl (%EDX),%eax is represented as%eax = eax-(%edx), (%edx) is an integer, and%edx is saved up, satisfies the condition and the offset of 0 is the case only e2.y that is, the meiosis is up->e2.y. The minuend can also be stored in the%eax.

4. The title of * (up->__) denotes an integer this information, and then corresponding to the union structure only int *p satisfies the condition, and in the assembly code, from the previous step to know minuend stored in%eax, MOVL (%EAX),%eax is the statement of the value of the meiosis, This statement can be%eax before the address, and should be UP->E1.P, that is, the UP->E1.P should be subtracted.

5. But then look up the third row of%eax is obtained by (%ECX), and%ecx obtained by 4 (%edx), but%edx points to up->e2.y, so the meiosis should be 4 offset, the obvious%ecx should be up->e2.next, corresponding% EAX for UP->E2.NEXT->E1.P.

6. Finally, only the MOVL%eax,4 (%ECX) statement is left, and the%ECX is up->e2.next,%ecx+4 should be up->e2.next->e1.x, that is, up-> e2.next->e1.x.

The final result:

up->e2.next->e1.x = * (UP->E2.NEXT->E1.P)-up->e2.y

Homework 3.67

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.