Intel Assembly

Arithmetic Operations:
n Addition
n Subtraction
n Multiplication
n Division
n Comparison
n Negation
n Increment
n Decrement
Logic Operations:
n AND
n OR
n XOR
n NOT
n shift
n rotate
n compare (test)
Arithmetic Operations

Addition, Increment, Add-with-carry and Exchange-and-add:
Contents of the rightmost 8 bits of the FLAGS register can change (+ Overflow) for arithmetic and logic instructions.

Flags include:
n Z (result zero)
n C (carry out)
n A (half carry out)
n S (result positive)
n P (result has even parity)
n O (overflow occurred)


Arithmetic Operations

Subtraction, Decrement and Subtract-with-borrow:


Comparison:

Changes only the flag bits.
Often followed with a conditional branch:


Arithmetic Operations
Multiplication and Division:
imul/idiv: Signed integer multiplication/division.
mul/div: Unsigned.

al always holds the multiplicand (or ax or eax).
Result is placed in ax (or dx and ax or edx or eax).


C and O bits are cleared if most significant 8 bits of the 16-bit product are zero (result of an 8-bit multiplication is an 8-bit result).
Division by zero and overflow generate errors.
Overflow occurs when a small number divides a large dividend.


Logic Operations

Allow bits to be set, cleared and complemented.
Commonly used to control I/O devices.

Logic operations always clear the carry and overflow flags.

m AND: 0 AND anything is 0.
Commonly used with a MASK to clear bits:


m OR: 1 OR anything is 1.
Commonly used with a MASK to set bits:


Logic Operations
m XOR: Truth table: 0110.
Commonly used with a MASK to complement bits:


m TEST: Operates like the AND but doesn't effect the destination.
Sets the Z flag to the complement of the bit being tested:


m BT: Test the bit, BTC: Tests and complements...

m NOT (logical one's complement)
m NEG (arithmetic two's complement - sign of number inverted)


Logic Operations
Shift: Logical shifts insert 0, arithmetic right shifts insert sign bit.


Double precision shifts (80386 and up):


Rotate: Rotates bits from one end to the other or through the carry flag.


Commonly used to operate on numbers wider than 32-bits:


Bit/String Scan

Bit Scan Instruction (80386 and up):
Scan through an operand searching for a 1 bit.
Zero flag is set if a 1 bit is found, position of bit is saved in destination register.


String Scan Instructions:
scasb/w/d compares the al/ax/eax register with a byte block of memory and sets the flags.
Often used with repe and repne
cmpsb/w/d compares 2 sections of memory data.

Program Control Instructions
Conditional and Unconditional Jumps, Calls, Returns, Interrupts
Unconditional Jumps
n Short jump: PC-relative using two bytes (+127/-128 bytes).
(PC-relative: constant added to eip).


n Near jump:
Within segment (max of +/- 2G).


n Far jump:
Four bytes give the offset and two bytes give a new segment address.
The segment value refers to a descriptor in protected mode.


Flow-of-Control Instructions

Conditional Jumps:
Test flag bits S, Z, C, P and O.
For unsigned numbers:


For signed numbers


For either signed or unsigned:


Test cx instead of flags:


Flow-of-Control Instructions

Conditional Set instructions:

Set a byte to either 01H or 00H, depending on the outcome of condition under test.


LOOP Instruction:
Combination of decrement ecx and jnz conditional jump.
Decrement ecx
If ecx != 0, jump to label
else fall through.

Example