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-28-2012, 04:52 PM   #1
Alkerak
LQ Newbie
 
Registered: Nov 2012
Distribution: Ubuntu
Posts: 6

Rep: Reputation: Disabled
ASM && GDB Memory Examination Question


Hello

I started programming in ASM as a hobby. I wrote a simple program in C which assigns the value 5 to a variable and the quits in order to see how this assignment looks.

So it's something like this:

Code:
#include <stdio.h>

int main()
{
    int x = 5;
}
I compile with debug symbols and I run the program.
I then disassemble the main function and I see this.

Code:
0x08048344 <main+0>:    push   ebp
0x08048345 <main+1>:    mov    ebp,esp
0x08048347 <main+3>:    sub    esp,0x8
0x0804834a <main+6>:    and    esp,0xfffffff0
0x0804834d <main+9>:    mov    eax,0x0
0x08048352 <main+14>:   sub    esp,eax
0x08048354 <main+16>:   mov    DWORD PTR [ebp-4],0x5
0x0804835b <main+23>:   leave  
0x0804835c <main+24>:   ret
I make a break for "int x = 5" meaning "mov DWORD PTR [ebp-4],0x5".

Now the interesting thing happens. While the program is stopped at this line I chose to examine what instruction is on ebp-4 (if there is something there). So I type "x/i $ebp-4".
And I get this:

Code:
0xbffff814:     jo     0xbffff799
Should not this location be full of garbage or something like that? I mean it will be overwritten with 5 right? So why is there an asm instruction at $ebp-4?
 
Old 11-28-2012, 05:21 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Alkerak View Post
So why is there an asm instruction at $ebp-4?
There is an asm instruction almost anywhere there is a value, because most values can be disassembled as asm instructions.

If you ask to see it as an asm instruction, that is what you get.
 
1 members found this post helpful.
Old 11-28-2012, 05:50 PM   #3
Alkerak
LQ Newbie
 
Registered: Nov 2012
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
There is an asm instruction almost anywhere there is a value, because most values can be disassembled as asm instructions.

If you ask to see it as an asm instruction, that is what you get.
Thanks a lot. This is most interesting!
 
Old 12-02-2012, 07:33 AM   #4
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Anyone care to explain what all that stuff is before mov DWORD PTR? Why is it subtracting 0 from esp?
 
Old 12-02-2012, 07:40 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by resetreset View Post
Anyone care to explain what all that stuff is before mov DWORD PTR?
Code:
0x08048344 <main+0>:    push   ebp
0x08048345 <main+1>:    mov    ebp,esp
...
0x0804835b <main+23>:   leave
Those two instructions at the beginning balanced by that one instruction before the return, are the simple (not necessarily efficient) way to establish a basic stack frame for a function.

The old value of ebp is pushed on the stack, then ebp is set to point to where that old ebp was saved.

The leave instruction uses ebp to restore the prior value of both ebp and esp. So, prior to leave, it is not necessary to clean up any excess positions (nested function parameters, etc.) that might have been pushed by this function.

Code:
0x08048347 <main+3>:    sub    esp,0x8
Reserving 8 bytes of space at [ebp-8].

I think the stack was being kept 8-byte aligned. So the need to allocate 4 bytes for x caused it to allocate 8 bytes total.

Code:
0x0804834a <main+6>:    and    esp,0xfffffff0
16-byte aligning the stack. Some operations (none of which are in this function) need a 16-byte aligned stack. But the calling convention is not 16-byte aligned. So any function that thinks it needs 16-byte alignment must create that for itself.

Quote:
Why is it subtracting 0 from esp?
I guess it was reserving space for all of the variables that need 16 byte alignment. The total size of such variables in this function appears to be zero.

Non optimized code generation was following a general pattern of code for entry to a function, without caring that the steps were pointless in this trivial function.

Everything but the ret was pointless. x could be optimized away, so you don't need to allocate space for it nor set its value. The stack frame is optional. If you aren't calling nested functions nor need 16 byte alignment of the stack, there is no need for the overhead of using ebp.

Last edited by johnsfine; 12-02-2012 at 08:16 AM.
 
Old 12-02-2012, 12:19 PM   #6
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
There's no help for it. John. You. Are. A. God.


*How* do you know these things?


Did you write compilers or something?
 
Old 12-02-2012, 01:22 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by resetreset View Post
*How* do you know these things?


Did you write compilers or something?
I expect several of the experts in this forum know all that, but this time didn't have patience to re explain old stuff.

I know it mainly from many occasions of using asm level debugging to help programmers I work with in diagnosing the occasional bug in C++ code that makes no sense when looked at in the debugger at a source level.

I never wrote a C++ compiler, nor a compiler that generates 32-bit or 64-bit x86 output. It has been a very long time since I wrote a compiler that generates 16-bit x86 (and that wasn't for much of a language) and even longer since I wrote a C compiler.

The "compiler" that is part of my current responsibilities at work generates C++ code as its output (then it uses Mingw to create a .dll from the C++ code). Many people wouldn't even call our part a "compiler"; Mingw is the compiler and we haven't touched the internals of Mingw. Anyway, low level details of compiler output such as discussed earlier in this thread don't enter into that project at all. I'm not sure about LQ policy regarding next detail: I have an opening in my team (in Burlington Ma.) for someone experienced in the kind of data structures and processing are typical inside a compiler to work on that project, which might not really be a "compiler" but internally uses the same techniques as a compiler. If you know someone who might fit that job, please tell me. (Overly optimistic amateurs considered only if you know Mingw internals a lot better than I do myself. Otherwise only those with relevant work experience. We won't be modifying Mingw internals, but understanding them helps and is also a good example of the kind of skill required.)
 
  


Reply



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
LXer: Power & Memory Usage Of GNOME, KDE, LXDE & Xfce LXer Syndicated Linux News 0 03-08-2010 07:12 PM
Where to install & memory question anibis Linux - Software 3 08-10-2006 12:05 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: &#12363;&#12394;&#28450;&#23383;&#22793;&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM

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

All times are GMT -5. The time now is 11:05 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