Search code examples
cassembly32-bit

ANDing the stack pointer


I'm working on a computer system exam review question asking for the stack pointer offset at various points in the program, and I'm confused about how the stack frame is set up. I've been learning assembly between 32-bit and 64-bit systems for a couple weeks but I haven't seen an instruction to AND the stack pointer before.

Can someone explain what purpose this serve, what would would be the offset of %esp after fourth instruction, and I calculating %esp correctly? I've commented the code with what I think the value of the offset would be after each instruction.

main:
0x0x0804848a <+0>:  push    %ebp                 ;-0x4
0x0x0804848b <+1>:  mov     %esp, %ebp           
0x0x0804848d <+3>:  and     $0xfffffff0, %esp    ;[-0x4, -0x13]
0x0x08048490 <+6>:  sub     $0x20, %esp          ;[-0x24, -0x33]
0x0x08048493 <+9>:  movl    $0xa, 0x1c(%esp)         
0x0x0804849b <+17>: movl    $0xa, (%esp)          
0x0x080484a2 <+24>: call    0x804841d <r>        ;[-0x28, -0x37]  

Solution

  • You will be enlightened if you repeat your math with a stack pointer that starts at 0x3.

    Alignment. Many CPU types require alignment for best performance or for vector instructions like SSE, to work at all.