Jump to content
  • Advertisement
Sign in to follow this  
xinsan

andl instruction

This topic is 3747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

.file "hello.c" .def ___main; .scl 2; .type 32; .endef .text LC0: .ascii "Hello, world!\12\0" .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax call __alloca call ___main movl $LC0, (%esp) call _printf movl $0, %eax leave ret .def _printf; .scl 2; .type 32; .endef In the above assembly, "andl $-16, %esp" This code "and"s ESP with 0xFFFFFFF0, aligning the stack with the next lowest 16-byte boundary. I don't understand this instruction andl. Could someone explain it for me?

Share this post


Link to post
Share on other sites
Advertisement
The instruction andl is a binary and on a long (32bit integer). In the example you gave, the value 0xFFFFFFF0 is anded with the esp register to perform stack alignment. The instruction in this form just sets the lowest four bits of esp to zero, while leaving the other bits as they are. This way esp ends up with a value that is a multiple of 16.

Share this post


Link to post
Share on other sites
It sounds to me like you just explained it to us [smile].

Do you understand what a bitwise 'and' does?
It performs the 'and' truth table operation on each bit from each of the numbers. If you and a binary bit with a one you get that bit, if you and with a zero you get zero. So since that hex number has all ones except for the last four bits, the last four bits become zero, hence truncating the original value of esp down to a multiple of 16 bytes.

Afaik the l means it operates on a long (32-bits).

Edit: Crosspost

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!