You are on page 1of 9

Lab 05: Arithmetic Instructions

Objective: To be familiar with the Arithmetic instructions

Theory:
Addition and Subtraction
INC and DEC Instructions

The INC (increment) and DEC (decrement) instructions, respectively, add 1 and subtract 1 from
a single operand. The syntax is

INC reg/mem
DEC reg/mem

The Overflow, Sign, Zero, Auxiliary Carry, and Parity flags are changed according to the value
of the destination operand. The INC and DEC instructions do not affect the Carry flag.

ADD Instruction

The ADD instruction adds a source operand to a destination operand of the same
size. The syntax is
ADD dest,source
Source is unchanged by the operation, and the sum is stored in the destination operand.
Flags The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according
to the value that is placed in the destination operand.

SUB Instruction

The SUB instruction subtracts a source operand from a destination operand. The set of possible
operands is the same as for the ADD and MOV instructions (see Section 4.1.4). The syntax is

SUB dest,source

Department of Computer Sciences 32 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Internally, the CPU can implement subtraction as a combination of negation and addition. Figure
shows how the expression 4 -1 can be rewritten as 4 +(-1). Two’s-complement notation is used
for negative numbers, so _1 is represented by 11111111.

NEG Instruction

The NEG (negate) instruction reverses the sign of a number by converting


the number to its two’s complement. The following operands are permitted:

NEG reg
NEG mem
(Recall that the two’s complement of a number can be found by reversing all the
bits in the destination operand and adding 1.)

Flags The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags
are changed according to the value that is placed in the destination
operand.

Multiplication and Division Instructions


The MUL and IMUL instructions perform unsigned and signed integer
multiplication, respectively. The DIV instruction performs unsigned integer
division, and IDIV performs signed integer division.

MUL Instruction

The MUL (unsigned multiply) instruction comes in three versions: the first version
multiplies an 8-bit operand by the AL register. The second version multiplies a 16-bit
operand by the AX register, and the third version multiplies a 32-bit operand by the
EAX register. The multiplier and multiplicand must always be the same size, and the
product is twice their size. The three formats
accept register and memory operands, but not immediate operands:
MUL reg/mem8
MUL reg/mem16
MUL reg/mem32
The single operand in the MUL instruction is the multiplier

MUL Examples

The following statements multiply AL by BL.


mov al,5d
mov bl,10d
mul bl

Department of Computer Sciences 33 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
DIV Instruction

The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit unsigned integer
division. The single register or memory operand is the divisor.The formats are
DIV reg/mem8
DIV reg/mem16
DIV reg/mem32
The following table shows the relationship between the dividend, divisor,
quotient, and remainder:

DIV Examples

The following instructions perform 8-bit unsigned division (83h / 2), producing a quotient of
41h and a remainder of 1:
mov ax,0083h ; dividend
mov bl,2 ; divisor
div bl ; AL = 41h, AH = 01h

Procedure:
Start Emu8086 by selecting its icon.

Write the following codes in the text editor and observe the operation of Arithmetic instructions.

Observe the values of Flags and fill the observation tables.

Department of Computer Sciences 34 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Program 01:
Algorithm:
operand = operand + 1

org 100h
MOV AL, 4
INC AL
RET ;AL=5

Department of Computer Sciences 35 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Program 02:
Algorithm:
operand = operand - 1

org 100h
MOV AL,255 ; AL = 0FFh (255 or -1)DEC
AL ; AL = 0FEh (254 or -2)RET

Program 03:

Algorithm: Invert all bits of the operand


Add 1 to inverted operand

Org 100h
MOV AL, 5 ; AL = 05h
NEG AL ; AL = 0FBh (-5)

Department of Computer Sciences 36 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Program 04:
Algorithm:
operand1 = operand1 + operand2

Org 100h
MOV AL, 5 ;AL=5
ADD AL, -3 ;AL=2
RET

Flags

C Z S O P A

Program 05:
Algorithm:
operand1 = operand1 - operand2

org 100h
MOV AL, 5
SUB AL,1 ;AL=4
RET
Flags

C Z S O P A

Program 06:

Algorithm:
when operand is a byte: AX = AL * operand.
when operand is a word: (DX AX) = AX * operand.

; Unsigned multiply
Org 100h
MOV AL, 200 ; AL = 0C8h
MOV BL, 4
MUL BL ; AX = 0320h (800)
RET

Department of Computer Sciences 37 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Flags

Exercise:

Exercise 01
Perform above examples on Emulator.

Exercise 02

What will be the contents of AX after the following operation? Write lower and

higher bits.

mov ax,63h

mov bl,10h

div bl
AH AL

Exercise 03
Write an assembly language program that will multiply 2 by 3 and store the result in a 16-
bit variable var1.Store values in registers and declare another variable var1 to store the
result.

Department of Computer Sciences 38 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Exercise 04

Write an assembly language program that divide 276 by 10 and store the result in a 16-bit
variable var1.

Exercise 05

Declare two variables and initialize them with two 8-bit integers. Load integer values in two 8-
bit registers. Perform addition operation and load the result in third variable.

Exercise 06
Declare two variables and initialize them with two 16-bit integers. Load integer values in
two 16-bit registers. Perform subtraction operation and load the result in third variable

Exercise 07

Write an assembly language program that will swap values of two registers. (You can use three
registers to solve this task)

Exercise 08

Write a program in assembly language that will load an 8-bit un-signed integer in accumulator register
and adds 1 to the number.

Exercise 10
Write a program in assembly language that will load a 16-bit un-signed integer in base register and
subtracts 1 to the number.

38
Department of Computer Sciences 39 Semester BS CS 03
CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions
Exercise 11
Write a program in assembly language that will load an 8-bit un-signed integer in data register and
produce its respective negative value.

Department of Computer Sciences 40 Semester BS CS 03


CEL21: Computer Organization & Assembly Language Lab Lab 05: Arithmetic Instructions

You might also like