Jump to content

x86 Assembly/Logic

From Wikibooks, open books for an open world

Logical instructions

[edit | edit source]

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

All logical instructions presented in this section are executed in the, as the name already suggests, the arithmetic logic unit.

binary operations

[edit | edit source]

These instructions require two operands.

and mask, destination GAS Syntax
and destination, mask Intel Syntax


operation
[edit | edit source]

and performs a bit-wise and of the two operands, and stores the result in destination.

side effects
[edit | edit source]

See below.

example
[edit | edit source]
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
[edit | edit source]
  • An and can be used to calculate the intersection of two “sets”, or a value representing a “mask”. Some programming language require that Boolean values are stored exactly as either 1 or 0. An and rax, 1 will ensure only the LSB is set, or not set.
  • If partial register addressing is not available in the desired size, an and can be used for a operation, that is the remainder of integer division. For that, mask has to contain the value (i. e. all lower bits set until a certain threshold), where equals your desired divisor.

logical or

[edit | edit source]
or addend, destination GAS Syntax
or destination, addend Intel Syntax


operation
[edit | edit source]

The or instruction performs a bit-wise or of the two operands, and stores the result in destination.

side effects
[edit | edit source]

See below.

example
[edit | edit source]
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
[edit | edit source]
  • An or can be used to calculate the union of two “sets”, or a value representing a “mask”.

logical xor

[edit | edit source]
xor flip, destination GAS Syntax
xor destination, flip Intel Syntax


operation
[edit | edit source]

Performs a bit-wise xor of the two operands, and stores the result in destination.

side effects
[edit | edit source]

See below.

example
[edit | edit source]
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
[edit | edit source]
  • 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 | edit source]
side effects for and, or, and xor
[edit | edit source]
  • 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 | edit source]

logical not

[edit | edit source]

not argument

operation
[edit | edit source]

Performs a bit-wise inversion of argument.

side-effects
[edit | edit source]

None.

example
[edit | edit source]
movl $0x1, %edx ; edx ≔ 1
notl %edx ; edx ≔ ¬edx
; here edx would be 0xFFFFFFFE because a bitwise NOT 0x00000001 = 0xFFFFFFFE
application
[edit | edit source]
  • not is frequently used to get a register with all bits set.