Hello,

I have observed that when generating assembly code from simple C programs using GCC, the stack is initialized as follows:

pushl   %ebp
movl    %esp,  %ebp
andl    $-16,  %esp

my question concerns the last instruction where -16 is anded with the stack pointer. Am I correct in assuming that this is for proper memory alignment for 32 bit data? If so, then why not -4?

I have also observed that in a simple program I have written that compares data in memory, byte by byte, removing the said instruction significantly slows down the process. If this has to do with memory alignment, then I don't understand why it would have an affect since I am accessing bytes in memory individually anyway.

Thank you,

Allasso

Recommended Answers

All 4 Replies

Would have to see the C code but most likely the -16 is allocating 16 bytes on the stack for local variables, such as four 4-byte integers.

did you notice that is "and", not "add"? It is zeroing out the four least significant bits in the stack pointer.

Sorry, no I didn't notice that. My error.

It is different, and yes probably has something to do with alignment, but without seeing code in its entirety and more importantly the switches your passing to GCC it's hard to say why this is happening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.