public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Understanding GCC Behaviour
@ 2024-03-01  8:39 Pouria Khajepour
  2024-03-01  9:29 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Pouria Khajepour @ 2024-03-01  8:39 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1257 bytes --]

Hi everyone,
I'm a computer engineering student and recently I got interested in
understanding how compilers work, especially gcc.
I have a rather noobish question which made me busy during last week.
I basically want to understand what gcc is actually doing.
To be more precise, I want to know: when we do a function call,

   1. What happens to $esp, $ebp, etc.  and what are the things that are
   pushed to the stack?
   2. Where in the stack the function arguments are being pushed?
   3. How storing the local variables in the stack is being handled?

To be more specific, i wrote a code as an example, i would be very thankful
if you can tell me what will happen in the stack after running each line of
the code down bellow:

void func(int a, int b, int c){

        int x[4] = {5,5,5,5,5};

}

int main(){

        int a = 1;

        int b = 2;

        int c = 3;

        func(10,9,8);

        return 0;

}


I'm using gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2) on a
x86_64-linux-gnu machine.
I would appreciate it if anyone can also point me in the right direction to
find out what happens when using other gcc versions or other architectures.
I'm lost around using gdb and other tools, but haven't got an answer yet.


Thanks, Pouria.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Understanding GCC Behaviour
  2024-03-01  8:39 Understanding GCC Behaviour Pouria Khajepour
@ 2024-03-01  9:29 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-03-01  9:29 UTC (permalink / raw)
  To: Pouria Khajepour; +Cc: gcc-help

On Fri, 1 Mar 2024 at 08:41, Pouria Khajepour via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> Hi everyone,
> I'm a computer engineering student and recently I got interested in
> understanding how compilers work, especially gcc.
> I have a rather noobish question which made me busy during last week.
> I basically want to understand what gcc is actually doing.
> To be more precise, I want to know: when we do a function call,
>
>    1. What happens to $esp, $ebp, etc.  and what are the things that are
>    pushed to the stack?
>    2. Where in the stack the function arguments are being pushed?

GCC doesn't get to choose this, it has to follow the target platform's
ABI so that code compiled by GCC can interoperate with code compiled
by other compilers on the same platform.

See https://wiki.osdev.org/System_V_ABI for an intro to the topic, and
links to the psABI for x86.


>    3. How storing the local variables in the stack is being handled?
>
> To be more specific, i wrote a code as an example, i would be very thankful
> if you can tell me what will happen in the stack after running each line of
> the code down bellow:
>
> void func(int a, int b, int c){
>
>         int x[4] = {5,5,5,5,5};
>
> }
>
> int main(){
>
>         int a = 1;
>
>         int b = 2;
>
>         int c = 3;
>
>         func(10,9,8);
>
>         return 0;
>
> }
>
>
> I'm using gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2) on a
> x86_64-linux-gnu machine.
> I would appreciate it if anyone can also point me in the right direction to
> find out what happens when using other gcc versions or other architectures.
> I'm lost around using gdb and other tools, but haven't got an answer yet.
>
>
> Thanks, Pouria.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-01  9:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-01  8:39 Understanding GCC Behaviour Pouria Khajepour
2024-03-01  9:29 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).