You are on page 1of 25

BCT 2302:

Assembly Language
Arithmetic Instructions

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken


1
Ogada
Acknowledgement

• Notes adapted from tutorialspoint.com and emu8086 tutorial.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 2


Learning Outcomes

•By the end of this chapter, the learner

should be able to:

•Write assembly language programs

using arithmetic instructions

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 3


Introduction

• Most Arithmetic and Logic Instructions

affect the processor status register (or Flags).

• There are 16 bits in this register, each bit is

called a flag and can take a value of 1 or 0

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 4


Processor Status Register

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken


5
Ogada
Flags

• Carry Flag (CF)

• Zero Flag (ZF)

• Sign Flag (SF)

• Overflow Flag (OF)

• Parity Flag (PF)

• Auxiliary Flag (AF)

• Interrupt enable Flag (IF)

• Direction Flag (DF)


BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 6
Flags

• Carry Flag (CF):


• This flag is set to 1 when there is an
unsigned overflow.
• For example when you add bytes 255 + 1
(result is not in range 0...255).
• When there is no overflow this flag is set to 0.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 7


Flags

• Zero Flag (ZF):


• Set to 1 when result is zero.
• For none zero result this flag 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 flag
take the value of the most significant bit.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 8


Flags

• Overflow Flag (OF):


• Set to 1 when there is a signed overflow.
• E.g., when you add bytes 100 + 50 (result is not in range -
128...127).

• Parity Flag (PF)


• This flag 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.
BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 9
Flags

• Auxiliary Flag (AF):


• Set to 1 when there is an unsigned overflow for low
nibble (4 bits).

• Interrupt enable Flag (IF):


• when this flag is set to 1 CPU reacts to interrupts
from external devices.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 10


Flags

• Direction Flag (DF):


• This flag is used by some instructions to process data
chains, when this flag is set to 0 - the processing is
done forward, when this flag is set to
1 the processing is done backward.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 11


Defining Data in a program(CONT..)
• Pseudo-op Description Data size (in bytes)
DB Define Byte 1
DW Define Word 2
DD Define Double word 4
DQ Define Quad-word 8
DT Define Ten bytes 10
• Examples:
var DW 1234H;define var as word and initialize with value
1234(in Hex).
var DB ? Define var as Byte and its initial value is unknown.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 12


The ADD and SUB Instructions

• The ADD and SUB instructions are used for


performing simple addition/subtraction of
binary data in byte, word and doubleword size,
i.e., for adding or subtracting 8-bit, 16-bit or 32-
bit operands, respectively.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 13


The ADD and SUB Instructions

• The ADD and SUB instructions have the following syntax

ADD/SUB destination, source

The ADD/SUB instruction can take place between:


• Register to register
• Memory to register
• Register to memory
• Register to constant data
• Memory to constant data

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 14


Add

• Source is unchanged by the operation, and the sum is stored in the


destination operand.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 15


The INC Instruction

• The INC instruction is used for incrementing an


operand by one.

• It works on a single operand that can be


either in a register or in memory.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 16


INC Syntax

• The INC instruction has the following syntax:

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 17


The DEC Instruction

• The DEC instruction is used for decrementing


an operand by one.

• It works on a single operand that can be either


in a register or in memory.

• The DEC instruction is DEC destination

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 18


The MUL/IMUL Instruction

• There are two instructions for multiplying binary data.


• The MUL (Multiply) instruction handles unsigned
data.
• The IMUL (Integer Multiply) handles signed data.

• Both instructions affect the Carry and Overflow flag

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 19


The MUL/IMUL Instruction

• The syntax for the MUL/IMUL instructions is

MUL/IMUL multiplier

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 20


The DIV/IDIV Instructions

• The division operation generates two elements:


• A quotient and
• A remainder.

• In case of multiplication, overflow does not occur


because double-length registers are used to keep the
product.

• However, in case of division, overflow may occur.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 21


The DIV/IDIV Instructions

• The processor generates an interrupt if overflow


occurs.
• The DIV (Divide) instruction is used or unsigned
data and the IDIV (Integer Divide) is used for
signed data.
• The format for the DIV/IDIV instruction is
DIV/IDIV divisor

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 22


The DIV/IDIV Instructions

•The dividend is in an accumulator.

•Both the instructions can work with 8-bit,


16-bit or 32-bit operands.

•The operation affects all six status flags.

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 23


Exercise

• Write the 8086 Assembly instruction which will perform the


following indicated operation
• Copy AL to BL.

• Load 43H into CL.

• Increment the contents of CX by 1.

• Copy SP TO BP

• Copy 07H to DL

• Multiply AL times BL.

• Decrement SP by 1

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken Ogada 24


End of lesson

BCT 2302 Assembly Language: Arithmetic Instructions. Dr Ken


25
Ogada

You might also like