Arithmetic and Logic Instructions: By: Ngo Carlo
Arithmetic and Logic Instructions: By: Ngo Carlo
Quick Review!
Addressing Modes
Section Introduction
In this chapter, we examine the arithmetic instructions:
Addition
Subtraction
Multiplication
Division
Comparison
Increment and Decrement.
Addition
1. Addition (ADD))
2. Increment (INC)
3. Add with Carry(ADC)
4. Exchange and Add (XADD)
1. Addition (ADD)
-Simply adds the value of the destination and source
operands and stores the sum, of course at the
destination operand.
2. Increment
Adds a value of 1 to the one and only parameter that
the operand requires and you gave. Simple!
INC AX is similar to AX=AX+1
Subtraction
1. Subtraction (SUB)
2. Decrement (DEC)
3. Subtract with Borrow (SBB)
4. Comparison (CMP)
1. Subtraction (SUB)
Destination Source
Minuend will go to the Destination
2. Decrement (DEC)
Decrement subtraction (DEC) subtracts 1 from a register
or the contents of a memory location
DEC AX is similar to AX=AX-1
Comparison (CMP)
The comparison instruction (CMP) is a subtraction that
changes only the flag bits; the destination operand
never changes.
Multiplication
Multiplication is performed on bytes, words, or
doublewords, and can be signed integer (IMUL) or
unsigned integer (MUL).
If two 8-bit numbers are multiplied, they generate a 16bit product; if two 16-bit numbers are multiplied, they
generate a 32-bit product;
8 bit MUL
16 bit MUL
32 bit MUL
64 bit MUL
8 bit Multiplication
With 8-bit multiplication, the multiplicand is always in
the AL register. whether signed or unsigned.
The multiplier can be any 8-bit register or any memory
location.
The multiplication instruction contains one operand
because it always multiplies the operand times the
contents of register AL.
After the multiplication, the unsigned product is placed
in AX
the choice of the multiplier is up to the programmer.
16-Bit Multiplication
AX contains the multiplicand instead of AL
DXAX instead of AX. (Where we store the product)
the choice of the multiplier is up to the programmer.
32-Bit Multiplication
With 32-bit multiplication, the contents of EAX are
multiplied by the operand specified with the instruction.
The product (64 bits wide) is found in EDXEAX, where
EAX contains the least significant 32 bits
64-Bit Multiplication.
The result of a 64-bit multiplication in the Pentium 4
appears in the RDX:RAX register pair as a 128-bit
product.
The value if the RAX is the multiplicand
Division
These numbers are signed (IDIV) or unsigned (DIV) integers.
an 8-bit division divides a 16-bit number by an 8-bit number; a 16bit division divides a 32-bit number by a 16-bit number
None of the flag bits change predictably for a division. A division can
result in two different types of errors; one is an attempt to divide by
zero and the other is a divide overflow.
8 bit DIV
16 bit DIV
32 bit DIV
64 bit DIV
Remainder
8 Bit Division
An 8-bit division uses the AX register to store the
dividend that is divided by the contents of any 8-bit
register or memory location.
The quotient moves into AL after the division with AH
containing a whole number remainder.
Sign of the remainder is the sign of the dividend
We can also use CBW (convert byte to word) to convert
the contents of the AL as a byte to AX as a word
16-Bit Division
Similar to 8-bit division, except, instead of dividing into
AX, the 16-bit number is divided into DXAX, a 32-bit
dividend.
The quotient appears in AX and the remainder appears
in DX after a 16-bit division.
If AX is a 16-bit signed number, the CWD (convert word
to doubleword) instruction sign-extends it into a signed
32-bit number.
Remainder
The remainder could be used to round the quotient or
just dropped to truncate the quotient.
32-Bit Division
The 64-bit contents of EDXEAX are divided by the
operand specified by the instruction
32-bit quotient in EAX and a 32-bit remainder in EDX.
CDQ (convert doubleword to quadword) instruction is
used to convert the 32-bit contents of EAX into a 64-bit
signed number in EDXEAX.
64 bit division
The 64-bit division uses the RDX:RAX register pair to
hold the dividend and the quotient is found in RAX and
the remainder is in RDX after the division.