X86-assembly/Instructions/and

From aldeid
Jump to navigation Jump to search
You are here:
and

Description

  • The and instruction performs a logical AND operation.
  • This is the equivalent to the "&" operator in python:
>>> hex(0x18 & 0x7575)
'0x10'

Syntax

and destination, value

Examples

Example 1

mov eax, 0x18   ;11000 in binary
and eax, 0x7575  ;111010101110101 in  binary

The above example will store 0x10 (10000 in binary) into EAX.

00000000 00011000
01110101 01110101
-----------------
00000000 00010000

Modulo

The and instruction can also be used as a modulo, as shown below:

.text:004014BD mov     ecx, [ebp+counter_i] ; ecx= i
.text:004014C0 and     ecx, 80000003h  ; ecx = i % 4

Explanation in python:

>>> for i in range(10):
...  print "i=%d\t%d\t%d" % (i, i & 0x80000003, i%4)
... 
i=0    0    0
i=1    1    1
i=2    2    2
i=3    3    3
i=4    0    0
i=5    1    1
i=6    2    2
i=7    3    3
i=8    0    0
i=9    1    1

Comments