Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

I had a question about this sentence:

"It's often necessary to either push useless data or simply align the stack pointer when the pushed values don't happen to be aligned."

That's kind of hand-wavy. How do we "simply align the stack pointer"?



All modern platforms have decreasing stacks, so just AND it with ~(alignment-1).

Of course, you now need to keep track of the old stack pointer so you can restore it. Most code saves the old stack pointer into the fp, so you can just do sp = fp to undo any pushes without needing to care about how much was pushed; but it's cleaner and more efficient to have the compiler arrange things on the stack so that everything's already aligned and you don't need to do it programmatically.

...why, yes, I have spend the past couple of months with my head buried inside a compiler backend; why do you ask?


On most architectures, by decrementing it appropriately. E.g. subtract 4 to align from a 4-byte to an 8-byte boundary.


Thanks for the responses.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: