X86 Assembly/Arithmetic

All arithmetic instructions are executed in (one of) the ALUs. The ALU can only perform integer arithmetics, for floating point instructions see chapter “Floating Point”.

Basic operations
Arithmetic instructions take two operands: a destination and a source. Note that at most one operand may be a memory location.
 * The destination must be a register or a memory location.
 * The source may be either a memory location, a register, or a constant value.

Addition and Subtraction
This adds addend to destination and stores the result in destination.

Like, only it subtracts subtrahend from destination instead. In C:

Unsigned Multiplication
This multiplies multiplicand by the value of corresponding byte-length in the accumulator.

In the second case, the target is not  for backward compatibility with code written for older processors.

Affected flags are: All other flags are undefined.
 * OF  ≔ higher part of product  ≠ 0
 * CF  ≔ higher part of product  ≠ 0

Signed Multiplication
This instruction is almost like, but it treats the sign bit (the MSB), differently.

The  instruction also accepts two other formats:

This multiplies destination by multiplicand and puts the result, the product, in destination.

This multiplies multiplier by multiplicand and places it into product.

Division
This divides the value in the dividend register(s) by divisor, see table below.

As, only signed.

The circle (￮) means concatenation. With divisor size 4, this means that  are the bits 32-63 and   are bits 0-31 of the input number (with lower bit numbers being less significant, in this example).

As you typically have 32 or 64-bit input values for signed division, you often need to use CDQ or CQO to sign-extend  into   or  into   just before the division.

If quotient does not fit into quotient register, arithmetic overflow interrupt occurs. All flags are in undefined state after the operation.

Sign Inversion
Arithmetically negates the argument (i.e. two's complement negation).

Carry Arithmetic Instructions
Add with carry. Adds  +   to , storing result in. Usually follows a normal add instruction to deal with values twice as large as the size of the register. In the following example,  contains a 64-bit number which will be added to.

Subtract with borrow. Subtracts  +   from , storing result in. Usually follows a normal sub instruction to deal with values twice as large as the size of the register.

Increment
This instruction increments the register value augend by. It performs much faster than, but it does not affect the CF.

Operation
Decrements the value in minuend by 1, but this is much faster than the semantically equivalent.

Operands
Minuend may be either a register or memory operand.

Application

 * Some programming language represent Boolean values with either all bits zero, or all bits set to one. When you are programming Boolean functions you need to take account of this. The  instruction can help you with this. Very often you set the final (Boolean) result based on flags. By choosing an instruction that is opposite of the intended and then decrementing the resulting value you will obtain a value satisfying the programming language’s requirements. Here is a trivial example testing for zero.  If you intend to set false the “erroneously” set   will be “fixed” by  . If you intend to set true, which is represented by −1, you will decrement the value zero, the “underflow” of which causing all bits to flip. Note, some architectures execute   slowly, because of the fact that the flags register is overwritten only partially. It therefore is usually more efficient to use   which will affect the CF too, though.
 * Since  and   do not affect the CF, you can use these instructions to update a loop’s counting variable without overwriting some information stored in it. If you need an instruction that does not affect any flags while implicitly also performing a , you could use the rather slow.

Pointer arithmetic
The  instruction can be used for arithmetic, especially on pointers. See chapter “data transfer”, § “load effective address”.