LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2019, 05:03 PM   #1
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Rep: Reputation: Disabled
help understanding this assembly code


I have the following assembly code:
Code:
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...
 
Old 11-10-2019, 05:32 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
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.

this may help
https://en.wikibooks.org/wiki/X86_Di...ng_Conventions

Last edited by GazL; 11-10-2019 at 05:45 AM.
 
1 members found this post helpful.
Old 11-10-2019, 08:44 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Code:
   0x08048b53 <+3>:	movl   $0x804a2e8,0x4(%esp) // Uses bytes 4..7 above %esp
   [...]
   0x08048b5f <+15>:	mov    %eax,(%esp) // Uses bytes 0..3 above %esp
Quote:
only four bytes of space is used in movl $0x804a2e8,0x4(%esp)?
Just wanted to point out that it's using 8 bytes of stack space. For the rest, I've nothing to add beyond what gazL already said.
 
2 members found this post helpful.
Old 11-10-2019, 01:06 PM   #4
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Code:
   0x08048b53 <+3>:	movl   $0x804a2e8,0x4(%esp) // Uses bytes 4..7 above %esp
   [...]
   0x08048b5f <+15>:	mov    %eax,(%esp) // Uses bytes 0..3 above %esp


Just wanted to point out that it's using 8 bytes of stack space. For the rest, I've nothing to add beyond what gazL already said.
How is it using 8bytes of space? 0x4 is 4 in decimal, so 4 bytes of space is subtracted, right?
 
Old 11-10-2019, 01:23 PM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
It's a start point.

sp+4 to sp+7 4 bytes
sp+0 to sp+3 4 bytes.

= 8 bytes.

Last edited by GazL; 11-10-2019 at 01:25 PM.
 
1 members found this post helpful.
Old 11-10-2019, 01:39 PM   #6
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
It's a start point.

sp+4 to sp+7 4 bytes
sp+0 to sp+3 4 bytes.

= 8 bytes.
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?
 
Old 11-10-2019, 03:36 PM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
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

Last edited by GazL; 11-10-2019 at 03:40 PM.
 
1 members found this post helpful.
Old 11-10-2019, 03:55 PM   #8
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
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?
 
Old 11-10-2019, 03:59 PM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
because it's already grown the stack by subtracting the 0x1c, so it doesn't need to make the stack any bigger, it's just backfilling.

I don't think a human would write the code like that. It looks like compiler optimisations at play to me.

Last edited by GazL; 11-10-2019 at 04:04 PM.
 
1 members found this post helpful.
Old 11-10-2019, 04:02 PM   #10
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
because it's already grown the stack by subtracting the 0x1c, so it doesn't need to make the stack and bigger, it's just backfilling.

I don't think a human would write the code like that. It looks like compiler optimisations at play to me.
is eax filling the space between %esp and 4(%esp)? 4 bytes isn't enough space though... The input string is quite long
 
Old 11-10-2019, 05:24 PM   #11
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
char arrays a.k.a. strings are pass by reference. The value passed is the address of the string, not the string itself.
 
1 members found this post helpful.
  


Reply

Tags
assembly, stack



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux Assembly: write and receive assembly data errigour Programming 2 09-22-2012 09:54 AM
Could use some help understanding assembly WilliamR Programming 6 12-17-2009 12:58 PM
Practical Assembly: Help Understanding gcc -S for simple.c? jhwilliams Programming 2 11-30-2009 05:30 PM
How to convert Assembly code to "C" source code ssg14j Programming 2 08-01-2005 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration