The following instructions can affect the flags CF, ZF, SF, OF, PF, and AF.
Assume X = reg | mem and Y = reg | mem | immed
add X, Y ; X = X + Y
sub X, Y ; X = X – Y
cmp X, Y ; X – Y
and X, Y ; X = X & Y
test X, Y ; X & Y
or X, Y ; X = X | Y
xor X, Y ; X = X ^ Y
The mul instructions multiples unsigned ints. Assume X8 is an 8-bit register or memory location, etc. Then:
mul X8 ; ax = al * X8
mul X16 ; dx:ax = ax * X16
mul X32 ; edx:eax = eax * X32
There are equivalents of these for imul, the instruction of multiplying signed ints. In addition, there are other formats such as:
imul R32, X32, I32 ; R32 = X32 * I32
Where R32 is a 32-bit register, X32 is a 32 bit register or memory reference, and I32 is a 32 bit immediate.
The mul and imul instructions can affect the CF and OF flags.
The instructions for dividing two unsigned ints are:
div X8 ; al = ax / X8
div X16 ; ax = dx:ax / X16
div X32 ; eax = edx:eax / X32
The formats are the same for idiv, the instruction for dividing signed ints.
These instructions don't affect the flags.
Assume X= reg | mem
inc X ; X = X + 1 affects ZF, SF, OF, PF, AF
dec X ; X = X – 1 affects ZF, SF, OF, PF, AF
not X ; X = ~X does not affect flags
neg X ; X = -X affects CF, ZF, OF, PF, AF
How many bricks does it take to build a staircase of height n?
Clearly the answer is 1 + 2 + 3 + ... + n. This is called the nth triangle numbers (because the staircase is shaped like a right triangle).
One trick is to rearrange the sum as follows:
(1 + n) + (2 + n – 1) + (3 + n – 2) + ... = n * (n + 1) / 2
Here's the solution: