and esp, 0xfffffff0

assembly, x86

Solution

`and esp, 0xfffffff0` does a bitwise AND between the stack pointer and a constant, and stores the result back in the stack pointer.

The constant is chosen so that its low four bits are zero. Therefore the AND operation will set these bits to zero in the result, and leave the other bits of `esp` intact. This has the effect of rounding the stack pointer down to the nearest multiple of 16.

Problem

I don't entirely understand the line with comment in it below. I read a few posts on SO and in the `gcc` manual and learned that it is for stack address alignment but fail to understand how it does so. The code is show below: ``` (gdb) disas main Dump of assembler code for function main: 0x08048414 <+0>: push ebp 0x08048415 <+1>: mov ebp,esp 0x08048417 <+3>: and esp,0xfffffff0 ; why?? 0x0804841a <+6>: sub esp,0x10 0x0804841d <+9>: mov DWORD PTR [esp],0x8048510 0x08048424 <+16>: call 0x8048320 <puts@plt> 0x08048429 <+21>: mov DWORD PTR [esp],0x8048520 0x08048430 <+28>: call 0x8048330 <system@plt> 0x08048435 <+33>: leave 0x08048436 <+34>: ret End of assembler dump. ``` The code was generated using `gcc` (version 4.6.3) on linux. Thanks.

Original source

Related problems