c - x86 Procedure Call Memory Allocation -


so have problem textbook (computer systems: programmer's perspective problem 3.64):

it gives code this:

typedef struct {  int a;  int *p; } str1;  typedef struct {  int sum;  int diff; } str2;  str2 word_sum(str1 s1) {  str2 result;  result.sum = s1.a + *s1.p;  result.diff = s1.a - *s1.p;  return result; }  int prod(int x, int y) {  str1 s1;  str2 s2;  s1.a = x;  s1.p = &y;  s2 = word_sum(s1);  return s2.sum * s2.diff; } 

and assembly code prod & word_sum functions:

1 word_sum: 2  pushl %ebp 3  movl %esp, %ebp 4  pushl %ebx 5  movl 8(%ebp), %eax 6  movl 12(%ebp), %ebx 7  movl 16(%ebp), %edx 8  movl (%edx), %edx 9  movl %ebx, %ecx 10 subl %edx, %ecx 11 movl %ecx, 4(%eax) 12 addl %ebx, %edx 13 movl %edx, (%eax) 14 popl %ebx 15 popl %ebp  1 prod: 2  pushl %ebp 3  movl %esp, %ebp 4  subl $20, %esp 5  leal 12(%ebp), %edx 6  leal -8(%ebp), %ecx 7  movl 8(%ebp), %eax 8  movl %eax, 4(%esp) 9  movl %edx, 8(%esp) 10 movl %ecx, (%esp) 11 call word_sum 12 subl $4, %esp 13 movl -4(%ebp), %eax 14 imull -8(%ebp), %eax 15 leave 16 ret 

and asks why prod allocates 20 bytes on stack in assembly code line 4.

i can see allocate 8 bytes each str1 , str2 have no idea 5th 4-byte memory allocation be.

also, guys have recommendations (videos, articles, blog posts) on learning x86 stack frame structure , procedure calls? lost in computer architecture course @ moment.

the allocations 8 bytes s1, 8 bytes s2, , 4 bytes pass word_sum address store it's result at.


how did figure out?

if @ top of prod, see:

5  leal 12(%ebp), %edx 6  leal -8(%ebp), %ecx 7  movl 8(%ebp), %eax 

lines 5 , 7 instructions accessing our caller's stack frame, must grabbing x , y. know we're storing pointer y , line 5 lea instruction, can assume edx holds &y , eax holds x. still leaves ecx, holds pointer in our stack frame.

moving on, see it's storing eax, edx, , ecx on our stack, , calling word_sum:

8  movl %eax, 4(%esp) 9  movl %edx, 8(%esp) 10 movl %ecx, (%esp) 11 call word_sum 

we know eax , edx hold values need stored in s1. know s1 passed word_sum, , arguments passed @ top of stack. lines 8 , 9 storing eax , edx close top of stack, can assume s1.

functions return struct expect pointer passed @ top of stack. address should store it's return value at. other thing we're storing on top of stack ecx, , know we're storing result of word_sum in s2, ecx must pointer s2.

we've surmised each register holds; eax x, edx &y, , ecx &s2.

if lower, can confirm our expectations:

13 movl -4(%ebp), %eax 14 imull -8(%ebp), %eax 

we know result of function s2.sum * s2.diff. there's imul instruction, , we're multiplying s2.sum s2.diff, ebp-8 must point s2.sum , ebp-4 must point s2.diff.

if backtrack line 6, see ebp-8 stored in ecx, correctly suspected pointer s2.


in general, debugging problems entirely using knowledge of code generated assembly make educated guesses, , using process of elimination confirm guess correct.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -