Reputation: 111
I am trying to set up two parameters for the compare(int x, int y)
function, but when I pass them in I don't get the expected results, and when I check the registers with gdb, they don't seem to have the correct values in them.
I set up the parameters and call the function like this:
movl $10, (%esp)
movl $10, 8(%esp)
call compare
And this is the function which returns 1 if x==y
and 0 if x!=y
compare:
pushl %ebp
movl %esp, %ebp
movl (%ebp), %eax
movl 8(%ebp), %edx
cmpl %eax, %edx
jne .L28
.L29:
movl $1, %eax
jmp .L30
.L28:
movl $0, %eax
jmp .L30
.L30:
popl %ebp
ret
I'm setting up both variables as 10 for testing purposes, but I'm still getting a result of 0
Upvotes: 1
Views: 85
Reputation: 111
Thanks to @Jester for explaining my mistake to me.
Here is the correct implementation:
movl $10, (%esp)
movl $10, 4(%esp)
call compare
Function:
compare:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl 12(%ebp), %edx
cmpl %eax, %edx
jne .L28
.L29:
movl $1, %eax
jmp .L30
.L28:
movl $0, %eax
jmp .L30
.L30:
popl %ebp
ret
Upvotes: 0
Reputation: 24768
Let's see what the state of the stack is before ebp
is used in compare()
.
The call to compare()
:
movl $10, (%esp)
movl $10, 8(%esp)
call compare
and pushing of ebp
in compare
, i.e.:
pushl %ebp
leave the stack in the following state:
Values Address
------------------
| 10 |
------------------ <- ESP+16
| undefined |
------------------ <- ESP+12
| 10 |
------------------ <- ESP+8
| return address |
------------------ <- ESP+4
| saved EBP |
------------------ <- ESP
After movl %esp, %ebp
in compare()
, both esp
and ebp
have the same value.
movl (%ebp), %eax
movl 8(%ebp), %edx
Looking at the diagram above, these instructions should be instead:
movl 8(%ebp), %eax
movl 16(%ebp), %edx
Upvotes: 1