You are on page 1of 8

Topic Basic

18 Instructions

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. List down seven types of basic instructions for an assembly
language program; and
2. Differentiate between immediate addressing, register addressing
and direct addressing.

INTRODUCTION
A assembly language program usually has the basic instructions that will be
explained in this topic. Each basic instruction has its own purpose and there are
seven basic instructions that will be discussed in this topic. These basic
instructions are MOV, XCHG, INC, DEC, ADD, SUB and LOOP. You are
advised to understand these basic purposes to enable you to determine the
suitable time to use these basic instructions.

18.1 MOV INSTRUCTION


ACTIVITY 18.1

In your opinion, what is the meaning of addressing mode and how do


you differentiate between immediate addressing, register addressing
and direct addressing.
TOPIC 18 BASIC INSTRUCTIONS 151

MOV instruction will copy data from a one operand to another operand. This
operand could be 8 bits or 16 bits.

A simple syntax for MOV instruction:

mov destination, source

MOV Instruction can be used through three addressing modes such as


immediate addressing, register addressing and direct addressing.

Constraints in types of operands for MOV instructions are:


(a) CS and IP cannot be used as destination operand .
(b) Immediate Data and segment register cannot be moved to the segment
register.
(c) The Source and destination operand should be same in size.
(d) If the source is an immediate data then it must be less than 255(FFh) for an
8 byte destination or 65535 (FFFFh) for a 16 destination.

18.1.1 Register Addressing


All types of registers can be used as a source operand and all types of registers
except CS and IP can be used as destination operands. The following are a few
examples that could be of help:

mov ax,bx ; move from bx register to ax register


mov dl,al ; move from al register to dl register
mov bx,c ; move from cs register to bx register

18.1.2 Immediate Addressing


Any immediate value can be moved to any segment register except segment
register or IP. It can also be moved to any destination operand. The following are
a few examples that could help your understanding:

mov bl,01 ;bl value


mov bx,01 ;bx value 01 (decimal)
mov bx,1000h ;bx value 01 (decimal)
1000k (hexadecimal)
mov total,1000h ;total is a variable
152 TOPIC 18 BASIC INSTRUCTIONS

18.1.3 Direct Addressing


Variables can be used as an operand in MOV instructions. As we know, variables
are used to store content value in a memory location.

Example:

For example, count is an 8-bits variable that contains the value of 02

Mov al,count
mov bl,al
Mov count, bl

count 02 AL 02 BL 02 count 02

The Value in the count variable is assigned to the AL register. Then the value in
the AL register is assigned into the BL register and finally the value in the BL
register is assigned to the count variable. Finally, the value in all the registers
and the variables involved is 02.

Example:

This example shows on how to move existing data into an array

Array db 1h,2h,3h,4h

If the data declaration is given as above, thus:

Array +0 +1 +2 +3
1h 2h 3h 4h

Array =1 contains value 2h

Example of instruction:

mov al,array ; value al= 1h


mov al,array+1 ; value al= 2h
mov al,array+3 ; value al= 4h
mov array+2,0 ; value 3h replaced with 0
TOPIC 18 BASIC INSTRUCTIONS 153

18.2 XCHG INSTRUCTIONS


SELF-CHECK 18.1

The MOV instruction is used to assigned the variable value.


How do we perform data exchange?

The XCHG instruction will exchange the values of two registers and variables.
The syntax is as below:

xchg op1,op2

One of the operands must be a register. The other operand can be a register and
memory location.

The XCHG instruction is effective in converting two 8-bits operands or 16-bits


operands because it does not require a third operand to hold the temporary
value.

An example of XCHG usage:

xchg ax,bx
xchg ah,al
xchg variable,bx

Example:

An example an assembly language program involving the usage of MOV and


XCHG instructions is as shown below:

mov Al ,value1 1
xchg Al ,value 2
mov Value1 ,al

.
.
.
; data segment
value1 db 0ah
value2 db 14h
154 TOPIC 18 BASIC INSTRUCTIONS

The following is the explanation for the above program:

mov al,value1:
Meaning: value1=0ah will be assigned to the AL register. As we
know, the AL register can only store 8 bits.

xchg al,value2:
Meaning: value1 and value2 will exchange their positions and this
will cause AL register to store value2 whereas variable value2
will store value1, which was previously from the AL register

mov value1,al:
Meaning: the current value of AL will be assigned to value1. This
changes the current value of value1 to value2.

Once the execution of mov instruction is performed, value1 becomes 14h,


whereas value2 becomes 0ah. In short, the three instructions above are value
exchange processes.

ACTIVITY 18.2
Perform a comparison between MOV and XCHG instructions.

18.3 INC AND DEC INSTRUCTIONS


INC instruction adds 1 to operand and DEC instruction will deducts 1 from the
operand. The following are the example of syntax:

inc destination
dec destination

The size of the Destination Operand would be 8 bits or 16 bits, and it can be a
register or variable that represents memory location.

The usage of INC and DEC instructions are more efficient compared to the
addition (ADD) and subtraction (SUB) instructions.

Example:
inc ax
dec al
TOPIC 18 BASIC INSTRUCTIONS 155

18.4 ADD AND SUB INSTRUCTIONS


ACTIVITY 18.3

INC and DEC instructions are used for addition and subtraction. In
your opinion, what are the differences between these instructions and
the ADD and SUB instructions?

The usage of these instructions depends on the condition that the source operand
and destination operand should be of the same size. The following are the
example of syntaxes:

add destination, source


sub destination, source

The meaning of ADD instruction is:

Destination destination + source

Whereas SUB instruction is:

Destination destination source

Reminder: segment register cannot be used as destination operand. Examples of


the usage are given below:

Example:

add al,1
sub ax,1
add cl,al
sub ax,bx
156 TOPIC 18 BASIC INSTRUCTIONS

ACTIVITY 18.4

Explain step-by-step what happens when we execute the following


instructions.
add al , 1
sub ax , 1
add cl , al
sub ax , bx

18.5 LOOP INSTRUCTION

SELF-CHECK 18.2

The loop instructions used in a high-level language are far and while.
How about the Assembly language?

Loop instruction is the easiest way to perform a repetition of a block in a


program by determining the number of loops that will be executed. The cx
register can be used as a counter for the Loops.

The syntax for the loop instruction is:

Loop destination

Example:

mov cx, 5

start:
.
.
.
loop start
TOPIC 18 BASIC INSTRUCTIONS 157

In this example, start is a label used to show the beginning of a loop. The
statement
loop start

will cause the loop instruction to repeat the steps and the value in CX register to
be reduced by 1. This repetition will occur until the CX value equals to 0.

SELF-CHECK 18.3

In the previous example, how many times has the loop been
executed?

You have learned all the seven basic instructions in an assembly language
program. Now you should able to differentiate each instruction based on the
function and determine when to use these instructions in a program.

ADD instruction MOV instruction


DEC instruction SUB instruction
INC instruction XCHG instruction
LOOP instruction

You might also like