stack pointer alignment on x86 and x86_64

Discussion in 'C Programming' started by omkarenator, May 1, 2009.

  1. omkarenator

    omkarenator Guest

    Hi all,

    I have been struggling with a C program for a while. Its simple
    program that can also be stated in assembly. Following is the C
    version.

    // the MSVC version //

    int main() {
    __asm sub esp, 2;
    printf("Hi, this is a test\n");
    __asm add esp, 2;
    return 0;
    }

    Similar program can be written for gcc.

    I have two machines with me 1. 32-bit windows box 2. 64-bit SLES
    box.

    Above program works well on 64 bit linux box and not on a 32 bit
    windows. This is because of the stack pointer misalignment we have
    done before calling printf().

    Now, I know on 32 bit machines stack pointer should be aligned to 4
    byte boundary and on 64 bit, 16 byte boundary.

    To confirm above statement, if you insert something like :

    __asm and esp,0FFFFFFFCh;

    before calling printf(), it works.

    I have read the abi documentation, it also states so (No reason
    given).

    However no one seems to state why. Whats the exact reason, above
    program does not to work?

    Why are the alignment requirements on the architectures, the way they
    are?

    Thanks.
    omkarenator, May 1, 2009
    #1
    1. Advertising

  2. In article <>,
    omkarenator <> wrote:
    >Hi all,
    >
    >I have been struggling with a C program for a while. Its simple
    >program that can also be stated in assembly. Following is the C
    >version.
    >
    >// the MSVC version //
    >
    >int main() {
    > __asm sub esp, 2;
    > printf("Hi, this is a test\n");
    > __asm add esp, 2;
    > return 0;
    >}


    With love and kindness, let me be the first (of many) to point out that
    your post is:

    Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    --
    Useful clc-related links:

    http://en.wikipedia.org/wiki/Aspergers
    http://en.wikipedia.org/wiki/Clique
    http://en.wikipedia.org/wiki/C_programming_language
    Kenny McCormack, May 1, 2009
    #2
    1. Advertising

  3. omkarenator

    omkarenator Guest

    On May 1, 6:08 pm, (Kenny McCormack) wrote:
    > In article <..com>,
    >
    > omkarenator  <> wrote:
    > >Hi all,

    >
    > >I have been struggling with a C program for a while. Its simple
    > >program that can also be stated in assembly. Following is the C
    > >version.

    >
    > >// the MSVC version //

    >
    > >int main() {
    > >     __asm sub esp, 2;
    > >     printf("Hi, this is a test\n");
    > >     __asm add esp, 2;
    > >     return 0;
    > >}

    >
    > With love and kindness, let me be the first (of many) to point out that
    > your post is:
    >
    > Off topic.  Not portable.  Cant discuss it here.  Blah, blah, blah.
    >
    > --
    > Useful clc-related links:
    >
    >    http://en.wikipedia.org/wiki/Aspergers
    >    http://en.wikipedia.org/wiki/Clique
    >    http://en.wikipedia.org/wiki/C_programming_language


    posting to comp.arch
    omkarenator, May 1, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grumble

    Re: x86 Stack Confusion

    Grumble, Jan 9, 2006, in forum: C Programming
    Replies:
    51
    Views:
    998
    Dave Thompson
    Jan 16, 2006
  2. Replies:
    4
    Views:
    367
    Branimir Maksimovic
    Oct 20, 2005
  3. Don
    Replies:
    60
    Views:
    1,324
    Phred Phungus
    Mar 19, 2010
  4. Suraj Kurapati
    Replies:
    5
    Views:
    99
    Ken Bloom
    Dec 5, 2006
  5. kazaam
    Replies:
    6
    Views:
    206
    kazaam
    Aug 27, 2007
Loading...

Share This Page