So I'm trying to write some x86 to allocate memory for a struct. 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 disassemble 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 struct is 16 but why does the assembly only shows it allocating 12?
2) What is the meaning for the
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
Isn't is just putting the contents of eax into the address of ebp - 12. Then the second statement would be redundant?
3) Why is the esp being decremented by 40 when there are no other local variables or parameters to be pushed on the stack? I would've thought it only needs to be decremented 16.
Any help is appreciated, as well as anything I may have missed that you deem relevant. I'm pretty new to assembly. Thanks.