c / asm / stack and pointers

This is a discussion on c / asm / stack and pointers within the Tech Board forums, part of the Community Boards category; When you declare a variable i.e. Code: int k; how exactly does it create it in the stack... or better ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    c / asm / stack and pointers

    When you declare a variable i.e.
    Code:
    int k;
    how exactly does it create it in the stack...
    or better yet, explain this...
    Code:
    int main()
    {
        int k=5;
        return 0;
    }
    becomes:
    Code:
    .text
            .align 4
    .globl main
            .type    main,@function
    main:
            pushl   %ebp
            movl    %esp, %ebp
            subl    $4, %esp
            movl    $5, -4(%ebp)
            movl    $0, %eax
            leave
            ret
    my explanation (please correct me)
    Code:
    pushl %ebp - pushes the frame pointer onto the stack to be popped later
    movl %esp, %ebp - moves the current stack pointer to become the new frame pointer
    subl $4, %esp - subtract 4 bytes (one word) from the stack\
        pointer, i.e. move the pointer up four to make room for the var.
    movl $5, -4(%ebp) - assign the value 5 to the address of ebp offsetted -4 (4 up)
    movl $0, %eax - the return value
    leave - you know
    ret - return %eax;
    correct me if I'm wrong but if i made another function called foo() could I use foo to get back at the -4(%ebp) and how? without parameters...
    (btw: I think this is ILLEGAL C because it is compiler specific on how the stack is arranged but just for the point of learning, assume its on gcc and a linux system)
    using pointers...

    and another question
    is there a var in C that stores the value of EBP and ESP etc. without using inline asm? To my knowledge there is not but if there is, i would like to learn of it...

    Thanks for any input
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Lynux-Penguin
    my explanation (please correct me)
    Code:
    pushl %ebp - pushes the frame pointer onto the stack to be popped later
    movl %esp, %ebp - moves the current stack pointer to become the new frame pointer
    subl $4, %esp - subtract 4 bytes (one word) from the stack\
        pointer, i.e. move the pointer up four to make room for the var.
    movl $5, -4(%ebp) - assign the value 5 to the address of ebp offsetted -4 (4 up)
    movl $0, %eax - the return value
    leave - you know
    ret - return %eax;

    For reference, the leave instruction will set ESP to the value in EBP, then it will pop into EBP - the opposite of the first few instructions



    Originally posted by Lynux-Penguin
    correct me if I'm wrong but if i made another function called foo() could I use foo to get back at the -4(%ebp) and how? without parameters...
    (btw: I think this is ILLEGAL C because it is compiler specific on how the stack is arranged but just for the point of learning, assume its on gcc and a linux system)
    using pointers...

    and another question
    is there a var in C that stores the value of EBP and ESP etc. without using inline asm? To my knowledge there is not but if there is, i would like to learn of it...

    Thanks for any input
    -LC
    It is possible to traverse the stack to find varibles from calling functions (assuming those variable are normal stack variables - and not static...etc). You cant do it in straight C unless (using your example) main calls foo with a pointer to a variable declared in main..

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ok yeah, i thought so, thanks a lot

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Man you guys are killing me with this AT&T crap.



    Code:
    .text
            .align 4
    
    .global main
            .type    main,@function
    
    main:
            push ebp               ;pushl   %ebp
            mov  esp,ebp         ;movl    %esp, %ebp
            sub   esp,4             ;subl    $4, %esp
            mov   [ebp-4],5      ;movl    $5, -4(%ebp)
            mov   eax,0            ;movl    $0, %eax
            leave
            ret
    Much better.

    Last edited by VirtualAce; 08-05-2003 at 06:49 AM.

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    what ever gets the job done.
    I like the AT&T Version MORE now, simply because I use it a lot more than the other kinds.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Since when has this become an asm forum? Please try to stay on topic
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Since when has this become an asm forum? Please try to stay on topic


    Thanks for making us feel right at home. It wouldn't be the same if someone didn't whine about us using asm, even if yours was completely sarcastic.

    I take it you read the thread where I got blasted for using assembly. Told off is more like it.

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Are you talking about the thread where the power-tripping immature brat made a fool of himself? Ah, we are talking about the same thread.

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    > Please try to stay on topic

    Well, if you see my original thread, there was C. It just slowly got mutated into ASM because the question was easier to understand if we ripped it apart into ASM.

    However, gotcha

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Are you talking about the thread where the power-tripping immature brat made a fool of himself? Ah, we are talking about the same thread.
    That would be the one.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers - stack dumping
    By steve1_rm in forum C Programming
    Replies: 3
    Last Post: 06-04-2009, 10:21 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. Making a Stack using Pointers
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-27-2002, 11:51 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21