stack pointer alignment on x86 and x86_64

O

omkarenator

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.
 
K

Kenny McCormack

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top