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