X86 Assembly/Logic

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Logical instructions[edit]

The instructions on this page deal with bit-wise logical instructions. For more information about bit-wise logic, see Digital Circuits/Logic Operations.

binary operations[edit]

and[edit]

and src, dest GAS Syntax
and dest, src Intel Syntax
operation
Performs a bit-wise and of the two operands, and stores the result in dest.
side effects
see below
example
movl $0x1, %edx ; edx ≔ 1
movl $0x0, %ecx ; ecx ≔ 0
andl %edx, %ecx ; ecx ≔ edx ∧ ecx
; here ecx would be 0 because 1 ∧ 0 ⇔ 0
application
  • An and can be used to calculate the intersection of two “sets”, or a value representing a “mask”.

or[edit]

or src, dest GAS Syntax
or dest, src Intel Syntax
operation
Performs a bit-wise or of the two operands, and stores the result in dest.
side effects
see below
example
movl $0x1, %edx ; edx ≔ 1
movl $0x0, %ecx ; ecx ≔ 0
orl  %edx, %ecx ; ecx ≔ edx ∨ ecx
; here ecx would be 1 because 1 ∨ 0 ⇔ 1
application
  • An or can be used to calculate the union of two “sets”, or a value representing a “mask”.

xor[edit]

xor src, dest GAS Syntax
xor dest, src Intel Syntax
operation
Performs a bit-wise xor of the two operands, and stores the result in dest.
side effects
see below
example
movl $0x1, %edx ; edx ≔ 1
movl $0x0, %ecx ; ecx ≔ 0
xorl %edx, %ecx ; ecx ≔ edx ⊕ ecx
; here ecx would be 1 because 1 ⊕ 0 ⇔ 1
application
  • xor rax, rax (or any GPR twice) will clear all bits. It is a specially recognized word. However, since xor affects flags it might introduce bogus dependencies.

common remarks[edit]

side effects for and, or, and xor
  • OF ≔ 0
  • CF ≔ 0
  • SF becomes the value of the most significant bit of the calculated result
  • ZF ≔ result = 0
  • PF is set according to the result

unary operations[edit]

not arg

operation
Performs a bit-wise inversion of arg.
side-effects
None.
example
movl $0x1, %edx ; edx ≔ 1
notl %edx ; edx ≔ ¬edx
; here edx would be 0xFFFFFFFE because a bitwise NOT 0x00000001 = 0xFFFFFFFE