X86 Assembly/Arithmetic< X86 Assembly
Arithmetic instructions take two operands: a destination and a source. The destination must be a register or a memory location. The source may be either a memory location, a register, or a constant value. Note that at least one of the two must be a register, because operations may not use a memory location as both a source and a destination.
|add src, dest||GAS Syntax|
|add dest, src||Intel syntax|
dest. If you are using the MASM syntax, then the result is stored in the first argument, if you are using the GAS syntax, it is stored in the second argument.
|sub src, dest||GAS Syntax|
|sub dest, src||Intel syntax|
Like ADD, only it subtracts source from destination instead. In C:
dest -= src;
arg by the value of corresponding byte-length in the
operand size 1 byte 2 bytes 4 bytes other operand
higher part of result stored in
lower part of result stored in
result registers used by
In the second case, the target is not
EAX for backward compatibility with code written for older processors.
mul, only signed. The
imul instruction has the same format as
mul, but also accepts two other formats like so:
|imul src, dest||GAS Syntax|
|imul dest, src||Intel syntax|
dest. If you are using the NASM syntax, then the result is stored in the first argument, if you are using the GAS syntax, it is stored in the second argument.
|imul aux, src, dest||GAS Syntax|
|imul dest, src, aux||Intel syntax|
aux and places it into
dest. If you are using the NASM syntax, then the result is stored in the first argument, if you are using the GAS syntax, it is stored in the third argument.
This divides the value in the dividend register(s) by
arg, see table below.
divisor size 1 byte 2 bytes 4 bytes dividend
remainder stored in
quotient stored in
result registers for
The colon (:) means concatenation. With divisor size 4, this means that
EDX are the bits 32-63 and
EAX are bits 0-31 of the input number (with lower bit numbers being less significant, in this example).
As you typically have 32-bit input values for division, you often need to use CDQ to sign-extend
EDX 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.
div, only signed.
Arithmetically negates the argument (i.e. two's complement negation).
Carry Arithmetic InstructionsEdit
|adc src, dest||GAS Syntax|
|adc dest, src||Intel syntax|
Add with carry. Adds
dest, storing result in
dest. Usually follows a normal add instruction to deal with values twice as large as the size of the register. In the following example,
source contains a 64-bit number which will be added to
mov eax, [source] ; read low 32 bits mov edx, [source+4] ; read high 32 bits add [destination], eax ; add low 32 bits adc [destination+4], edx ; add high 32 bits, plus carry
|sbb src, dest||GAS Syntax|
|sbb dest, src||Intel syntax|
Subtract with borrow. Subtracts
dest, storing result in
dest. Usually follows a normal sub instruction to deal with values twice as large as the size of the register.
Increment and DecrementEdit
Increments the register value in the argument by 1. Performs much faster than
add arg, 1.
Decrements the register value in the argument by 1. Performs much faster than
sub arg, 1.
lea instruction can be used for arithmetic, especially on pointers. See chapter “data transfer”, § “load effective address”.