X86 Assembly/Arithmetic

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Arithmetic instructions

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


This adds src to 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.


sub src, dest GAS Syntax
sub dest, src Intel syntax


Like ADD, only it subtracts source from destination instead.

mul arg

This multiplies "arg" by the value of corresponding byte-length in the A register, see table below.

operand size 1 byte 2 bytes 4 bytes
other operand AL AX EAX
higher part of result stored in: AH DX EDX
lower part of result stored in: AL AX EAX

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

imul arg

As MUL, only signed.

div arg

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

divisor size 1 byte 2 bytes 4 bytes
dividend AX DX:AX EDX:EAX
remainder stored in: AH DX EDX
quotient stored in: AL AX EAX

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

idiv arg

As DIV, only signed.

neg arg

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

[edit] Carry Arithmetic Instructions

adc src, dest GAS Syntax
adc dest, src Intel syntax


Add with carry. Adds src + carry flag to dest, storing result in dest. Usually follows a normal add instruction to deal with values twice as large as the size of the register.


sbb src, dest GAS Syntax
sbb dest, src Intel syntax


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

[edit] Increment and Decrement

inc arg

Increments the register value in the argument by 1. Performs much faster than ADD arg, 1.

dec arg

Decrements the register value in the argument by 1.