Structure distribution in x86 assembly

So, I'm trying to write x86 to allocate memory for a structure. My c code looks like this:

   struc *uno = malloc(sizeof(struc));
   uno->first = 0;
   uno->second = 0;
   uno->third = 0;

   //And the struct
   struct struc {
       int first;
       int second;
       int *third;
   }

And the disassembler looks like ...

    pushl   %ebp
    movl    %esp, %ebp
    subl    $40, %esp
    movl    $12, (%esp)
    call    malloc
    movl    %eax, -12(%ebp)
    movl    -12(%ebp), %eax
    movl    $0, (%eax)
    movl    -12(%ebp), %eax
    movl    $0, 4(%eax)
    movl    -12(%ebp), %eax
    movl    $0, 8(%eax)
    movl    $0, %eax

So I have a few questions ...

1) The size of the structure is 16, but why does the assembly only show that it highlights 12?

2) What is the point

    movl    %eax, -12(%ebp)
    movl    -12(%ebp), %eax

Doesn't that just put the contents of eax in the address of ebp - 12. Then the second statement would be redundant?

3) Why does esp decrease by 40 when there are no other local variables or parameters on the stack? I would think that you only need to reduce 16.

Any help is appreciated, as well as anything I might have missed that you consider relevant. I am new to assembly. Thank.

+4
source share
1

12. - 4 x86, , 12. , sizeof, .


, .

movl    $12, (%esp)
call    malloc
movl    %eax, -12(%ebp)

malloc, , %eax, uno, -12(%ebp). , malloc: , , .

.

uno->first = 0;

, , %eax uno, ,

movl    -12(%ebp), %eax
movl    $0, (%eax)

uno->second = 0;

uno %eax , 4

movl    -12(%ebp), %eax
movl    $0, 4(%eax)

, , .

, -. uno %eax . :

movl    $12, (%esp)
call    malloc
movl    $0, (%eax)
movl    $0, 4(%eax)
movl    $0, 8(%eax)

, , 16 . 4 , , malloc, . 8, , 4 4 . , 40 . , :

, . , . , , , .

+9

All Articles