I am working on a procedure call problem with this code where initially %ebp = 0x800060, and %esp = 0x800040. I'm trying to find what memory address arg2 is stored at.
C Code:
int caller()
{
int arg1 = 534;
int arg2 = 1057;
int sum = swap_add(&arg1, &arg2);
int diff = arg1 - arg2;
return sum * diff;
}
Assembly Code:
caller:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $534, -4(%ebp)
movl $1057, -8(%ebp)
leal -8(%ebp), %eax
movl %eax, 4(%esp)
leal -4(%ebp), %eax
movl %eax, (%esp)
call swap_add
Below are the possible answer choices:
I keep following through the code and finding that arg2 is stored at 0x800028, which is not in the above answers. I'll show my math through the comments below:
caller:
pushl %ebp /* ebp pushed onto stack, esp decrease counter by 4, becomes: 0x80003C */
movl %esp, %ebp /* copies value to ebp, ebp & esp = 0x80003C */
subl $24, %esp /* 24 is 0x18, esp= 0x800024 */
movl $534, -4(%ebp) /* arg1 stored at address 0x800038 */
movl $1057, -8(%ebp) /* arg2 stored at address 0x800034 */
leal -8(%ebp), %eax /* store arg2 at eax */
movl %eax, 4(%esp) /* copy arg2 to esp increased by counter of 4, esp + 0x4 = 0x800024 +
0x4 = 0x800028, so arg2 is stored at 0x800028 */
leal -4(%ebp), %eax /*same process but arg 1 is copied to esp*/
movl %eax, (%esp)
call swap_add
So you can see, I end up with 0x800028. I'm sure my math is wrong somewhere, but I'm still a beginner in Assembly, so I just can't tell where. Any help is appreciated.
movl $1057, -8(%ebp) /* arg2 stored at address 0x800034 */
The way I see it, after this line you're done, you've found where arg2
is.
Then after that, the addresses of arg2
and arg1
are put on the stack because they're arguments of the call to swap_add
, but none of that changes where the variable arg2
is. 0x800028 is where the address of arg2
is put for the purpose of passing it as an argument to swap_add
, it's not arg2
itself. If the question was "where is &arg2
stored" then that could have been the answer.