Arithmetic and Logic Instructions: 8086 Assembler

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

8086 Assembler Tutorial for Beginners (Part 6)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

8086 Assembler

Arithmetic and Logic Instructions


Most Arithmetic and Logic Instructions aect the processor
status register (or Flags)

As you may see there are 16 bits in this register, each bit is
called a ag and can take a value of 1 or 0.
Carry Flag (CF) - this ag is set to 1 when there is an
unsigned overow. For example when you add bytes
255 + 1 (result is not in range 0...255). When there is no
overow this ag is set to 0.
Zero Flag (ZF) - set to 1 when result is zero. For none
zero result this ag is set to 0.
Sign Flag (SF) - set to 1 when result is negative. When
result is positive it is set to 0. Actually this ag take the
value of the most signicant bit.
Overow Flag (OF) - set to 1 when there is a signed
overow. For example, when you add bytes 100 + 50
(result is not in range -128...127).
Parity Flag (PF) - this ag is set to 1 when there is even
number of one bits in result, and to 0 when there is odd
number of one bits. Even if result is a word only 8 low bits
are analyzed!
Auxiliary Flag (AF) - set to 1 when there is an unsigned
overow for low nibble (4 bits).
1 of 5

11/13/2015 08:19 AM

8086 Assembler Tutorial for Beginners (Part 6)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

Interrupt enable Flag (IF) - when this ag is set to 1


CPU reacts to interrupts from external devices.
Direction Flag (DF) - this ag is used by some
instructions to process data chains, when this ag is set to
0 - the processing is done forward, when this ag is set to
1 the processing is done backward.

There are 3 groups of instructions.

First group: ADD, SUB,CMP, AND, TEST, OR, XOR


These types of operands are supported:
REG, memory
memory, REG
REG, REG
memory, immediate
REG, immediate
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: [BX], [BX+SI+7], variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
After operation between operands, result is always stored in
rst operand. CMP and TEST instructions aect ags only and
do not store a result (these instruction are used to make
decisions during program execution).
These instructions aect these ags only:
CF, ZF, SF, OF, PF, AF.
ADD - add second operand to rst.
SUB - Subtract second operand to rst.
CMP - Subtract second operand from rst for ags only.

2 of 5

11/13/2015 08:19 AM

8086 Assembler Tutorial for Beginners (Part 6)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

AND - Logical AND between all bits of two operands. These


rules apply:
1
1
0
0

AND
AND
AND
AND

1
0
1
0

=
=
=
=

1
0
0
0

As you see we get 1 only when both bits are 1.


TEST - The same as AND but for ags only.
OR - Logical OR between all bits of two operands. These
rules apply:
1
1
0
0

OR
OR
OR
OR

1
0
1
0

=
=
=
=

1
1
1
0

As you see we get 1 every time when at least one of the


bits is 1.
XOR - Logical XOR (exclusive OR) between all bits of two
operands. These rules apply:
1
1
0
0

XOR
XOR
XOR
XOR

1
0
1
0

=
=
=
=

0
1
1
0

As you see we get 1 every time when bits are dierent


from each other.

Second group: MUL, IMUL, DIV, IDIV


These types of operands are supported:
REG
memory
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

3 of 5

11/13/2015 08:19 AM

8086 Assembler Tutorial for Beginners (Part 6)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

memory: [BX], [BX+SI+7], variable, etc...


MUL and IMUL instructions aect these ags only:
CF, OF
When result is over operand size these ags are set to 1, when
result ts in operand size these ags are set to 0.
For DIV and IDIV ags are undened.
MUL - Unsigned multiply:
when operand is a byte:
AX = AL * operand.
when operand is a word:
(DX AX) = AX * operand.
IMUL - Signed multiply:
when operand is a byte:
AX = AL * operand.
when operand is a word:
(DX AX) = AX * operand.
DIV - Unsigned divide:
when operand is a byte:
AL = AX / operand
AH = remainder (modulus). .
when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus). .
IDIV - Signed divide:
when operand is a byte:
AL = AX / operand
AH = remainder (modulus). .
when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus). .

Third group: INC, DEC, NOT, NEG


4 of 5

11/13/2015 08:19 AM

8086 Assembler Tutorial for Beginners (Part 6)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

These types of operands are supported:


REG
memory
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: [BX], [BX+SI+7], variable, etc...
INC, DEC instructions aect these ags only:
ZF, SF, OF, PF, AF.
NOT instruction does not aect any ags!
NEG instruction aects these ags only:
CF, ZF, SF, OF, PF, AF.
NOT - Reverse each bit of operand.
NEG - Make operand negative (two's complement).
Actually it reverses each bit of operand and then adds 1 to
it. For example 5 will become -5, and -2 will become 2.

<<< Previous Part <<<

5 of 5

>>> Next Part >>>

11/13/2015 08:19 AM

You might also like