ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Dump of assembler code for function phase_1:
0x08048b50 <+0>: sub $0x1c,%esp
0x08048b53 <+3>: movl $0x804a2e8,0x4(%esp)
0x08048b5b <+11>: mov 0x20(%esp),%eax
0x08048b5f <+15>: mov %eax,(%esp)
0x08048b62 <+18>: call 0x804903a <strings_not_equal>
0x08048b67 <+23>: test %eax,%eax
0x08048b69 <+25>: je 0x8048b70 <phase_1+32>
0x08048b6b <+27>: call 0x80492dd <explode_bomb>
0x08048b70 <+32>: add $0x1c,%esp
0x08048b73 <+35>: ret
The phase_1 function takes a string as the only input. The strings_not_equal takes two strings and compares them. Where is phase_1 getting its parameter? I don't see the edi register used everywhere... Is it using the eax register for the input string? Also, why is the stack pointer subtracted by 0x1c (28 bytes) when only four bytes of space is used in movl $0x804a2e8,0x4(%esp)? Is 0x4(%esp) used as a parameter for strings_not_equal? I am so confused...
Assuming that code is following standard conventions, for x86-32, function parameters are usually pushed onto the stack in reverse order.
The extra space on the stack is either for alignment, or other shenanigans related to maintaining stack frames that happens when you call functions (such as dealing with %EBP correctly).
It's been a while since I looked into the detail of this so I'm a bit vague on the details (especially for 32bit which I haven't used in a long time). I'd suggest you google to find some official documentation on 32bit linux calling conventions if you want to understand this fully.
Oh sorry... I didn't read your comments. I do have one more question though... sub 0x1c %esp subtracts 28 bytes of space, but 0x20(%esp) only adds 32 bytes. Isn't 4 bytes above the current frame the return address? Shouldn't it be 8 bytes above to fetch the arguments?
If the phase_1 function started a new stack frame with the usual prologue: push %ebp
mov %esp, %ebp
then, yes, 4(%esp) would then be the return address, because %esp would have been incremented by 4 due the push. In this case however, it seems saving of %ebp is being skipped, so on entry %esp will contain the return address. Thus, %esp - 0x1c + 0x20 will point to the first 32bit value above the return address, which should be the first (or only) argument passed to the function.
Unless I'm mistaken, this code is comparing 4(%esp at entry) to whatever is at 0x804a2e8
If the phase_1 function started a new stack frame with the usual prologue: push %ebp
mov %esp, %ebp
then, yes, 4(%esp) would then be the return address, because %esp would have been incremented by 4 due the push. In this case however, it seems saving of %ebp is being skipped, so on entry %esp will contain the return address. Thus, %esp - 0x1c + 0x20 will point to the first 32bit value above the return address, which should be the first (or only) argument passed to the function.
Unless I'm mistaken, this code is comparing 4(%esp at entry) to whatever is at 0x804a2e8
Thanks one last question... why is the code doing a mov %eax (%esp) instead of a push %eax? Mov doesn't increment the stack pointer, right?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.