0% found this document useful (0 votes)
10 views19 pages

Lecture Notes - Unit II

This document covers the architecture and programming of the 8086 microprocessor, focusing on assembly language programming and the steps involved in program development. It explains the use of flowcharts for representing algorithms, various addressing modes, and provides a detailed overview of data transfer and arithmetic instructions. Additionally, it includes examples of instructions and their explanations to aid in understanding microprocessor operations.

Uploaded by

Lokesh Upputuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Lecture Notes - Unit II

This document covers the architecture and programming of the 8086 microprocessor, focusing on assembly language programming and the steps involved in program development. It explains the use of flowcharts for representing algorithms, various addressing modes, and provides a detailed overview of data transfer and arithmetic instructions. Additionally, it includes examples of instructions and their explanations to aid in understanding microprocessor operations.

Uploaded by

Lokesh Upputuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

10211CS109 – Microprocessor and Microcontroller

UNIT 2 MICROPROCESSOR ARCHITECTURE – 8086


(Prepared by Dr. G. Dhanabalan ASP / CSE)
(2023 – 24 WINTER SEMESTER)
Unit 2 – Programming in 8086

Assembly language programming


Program development steps
Defining the Problem
The first step in writing a program is to think about the problem that one wants the
program to solve. It is a good Idea to write down exactly what the program has to do and the
order in which the program must be executed. An example for a simple programming,
problem might be
 Read temperature from sensor.
 Add correction factor of + 7.
 Save result in a memory location.
For a program as simple as this, the three actions desired are very close to the eventual
assembly language statements.
Solution

Flow chart for the problem mentioned


Representing Program Operations
The formula or sequence of operations used to solve a programming problem is often
called the algorithm of the program.
Flowcharts
Flowcharts use graphic shapes to represent different types of program operations. The
specific operation desired is written in the graphic symbol. Below figure shows some of the
common flowchart symbols.

Flow chart symbols

 A rectangular box symbol is used to represent simple operations other than input and
output operations.
 A rectangular box with double lines at each end is often used to represent a subroutine
or procedure that will be written separately from the main program.
 When a Set of operations must be done several times during a program, it is usually
more efficient to write the series of operations as a separate subprogram
 A diamond-shaped box represents a decision point or crossroad. Usually it indicates
that some condition is to be checked at this point in the program. If the condition is
found to be true, one set of actions is to be done: if the Condition is false, another set of
actions is to be done.
 Race track-shaped symbol at the bottom of the flowchart indicates the end of the
program.
 If a flowchart column gets to the bottom of the paper, but not all the program has been
represented. Put a small circle with a letter in it at the bottom of the column, then start
the next Column at the top of the same paper with a small circle containing the same
letter
 Flowchart on the first page can be ended with the five-sided off .page connector symbol
containing a letter or number.
 Next page can be started with an off-page connector symbol containing the same letter
or number.
 A parallelogram is used to represent an input or an output operation.

Addressing modes
The different ways in which a processor can access data are referred to as its addressing
modes. Different types of addressing modes are listed below.
 Immediate addressing mode
 Register addressing mode
 Direct addressing mode

Immediate addressing mode


Suppose that in a program you need to put the number 437BH in the CX register. The
MOV CX, 437BH Instruction can be used to do this. When it executes, this instruction will
put the immediate hexadecimal number 437BH in the 16-bit CX register. This is
referred to as immediate addressing mode. Because, the number to be loaded into the CX
register will be put in the two memory locations immediately following the code for MOV
instruction. A similar instruction MOV CL, 48H could be used to load the 8-bit immediate
number 48H into the 8-bit CL register. You can also write instructions to load an 8- bit
immediate number into an 8-bit memory location or to load a 16-bit number into two
consecutive memory locations.
Register addressing mode
Register addressing mode means that a register is the source of an operand for an
instruction. The Instruction MOV CX, AX for example, copies the contents of the 16-bit AX
register into the l6-bit CX register. Remember that the destination location is specified in the
instruction before the comma, and the source is specified after the comma. Also note that the
Contents of AX are just copied to CX, but not actually moved. In other words. the previous
contents of CX are written over, but the contents of AX are not changed.
For example, if CX contains 2A84H and AX contains 4971H before the MOV CX, AX
instruction executes, then after the instruction executes, CX will contain 4971 H and AX will
still contain 4971H.
You can MOV any 16-bit register to any 16-bit register. or you can MOV any 8-bit
register to any 8-bit register. However, you cannot use an instruction such as MOV CX, AL
because this is an attempt to copy a byte type operand (AL) into a word type destination
(CX). If you try to write an instruction like this and you are using a good assembler, the
assembler will tell you that the instruction contains a type error. To copy the byte from AL to
the high byte of CX, you can use the instruction MOV CH, AL. To copy the byte from AL to
the low byte of CX, You can use the Instruction MOV CL. AL.
Direct addressing mode
For the simplest memory addressing mode, the effective address is just a 16-bit number
written directly in the instruction. The instruction MOV BL, [437AH] is an example. The
square brackets around the 437AH are shorthand for the contents of the memory location(s)
at a displacement from the segment base of." When executed, this instruction will copy the
contents of the memory location at a displacement from the data segment base of 437AH into
the BL register. The BIU calculates the 20-bit physical memory address by adding the
effective address 437AH to the data segment base. This addressing mode is called direct
because the displacement of the operand from the segment base is specified directly in
the instruction. The displacement in the instruction will be added to the data segment base in
DS unless you tell the BIU to add it to some other segment base.
Another example of the direct addressing mode is the instruction MOV BX. [437AH].
When executed, this instruction copies a 16-bit word from memory into the BX register.
Since each memory address of the 8086 represents a byte of storage, the word must come
from two memory locations. The byte at a displacement of 437AH from the data segment
base will be copied into BL. The contents of the next higher address, displacement 437BH
will be copied into the BH register. From the Instruction coding, the 8086 will automatically
determine the number of bytes that it must access in memory. An important point here is that
an 8086 always stores the low byte of a word in the lower of the two addresses and stores the
high byte of a word in the higher address. To stick this in your mind, remember: Low byte—
low address, high byte—high address
The previous two examples showed how the direct addressing mode can be used to
specify the source of an operand. Direct addressing can also be used to specify the
destination of an operand in memory. The instruction MOV [437AH], BX for example,
will copy the contents of the BX register to two memory locations in the data segment. The
contents of BL will be copied to the memory location at a displacement of 437AH. The
Contents of BH will be copied to the memory location at a displacement of 437BH.

Instruction set:
Data transfer instructions
Instruction Explanation
MOV Copy byte or word from specified source to specified destination.
This instruction copies a word or a byte of data from some source to a destination. The
destination can be a register or a memory location. The source can be a register, a memory
location, or an immediate number.
MOV AX,BX
MOV AX,5000H
MOV AX,[SI]
MOV AX,[2000H]
MOV AX,50H[BX]
MOV [734AH],BX
MOV DS,CX
MOV CL,[357AH]
Direct loading of the segment registers with immediate data is not permitted.
PUSH Copy specified word to top of slack.
This instruction pushes the contents of the specified register/memory location on to the stack.
The stack pointer is decremented by 2, after each execution of the instruction.
PUSH AX
PUSH DS
PUSH [5000H]

POP Copy word from top of stack to specified location.


This instruction loads the specified register/memory location with the contents of the memory
location of which the address is formed using the current stack segment and stack pointer. The
stack pointer is incremented by 2
POP AX
POP DS
POP [5000H]

XCHG Exchange bytes or exchange words.


This instruction exchange the contents of the specified source and destination operands
XCHG [5000H], AX
XCHG BX, AX
XLAT Translate a byte in AL using a table in memory.
XLAT : Translate byte using look-up table
LEA BX, TABLE1
MOV AL, 04H
XLAT
IN Copy a byte or word from specified port to accumulator.
Copy a byte or word from specified port to accumulator.
IN AL,03H
IN AX,DX
OUT Copy byte or word from accumulator to specified port.
OUT: Copy a byte or word from accumulator specified port.
OUT 03H, AL
OUT DX, AX
LEA Load effective address of operand into specified register.
Load effective address of operand in specified register. [reg] offset portion of address in DS
LEA reg, offset
LDS Load OS register and other specified register from memory.
Load DS register and other specified register from memory. [reg] [mem] [DS] [mem + 2]
LDS reg, mem
LES Load ES register and other specified register from memory.
Load ES register and other specified register from memory. [reg] [mem] [ES] [mem + 2]
LES reg, mem
LAHF Load (copy to) AH with the low byte of the flag register.
Load (copy to) AH with the low byte the flag register.
[AH] [ Flags low byte]
LAHF
SAHF Store (copy) AH register to low byte of flag register.
Store (copy) AH register to low byte of flag register.
[Flags low byte] [AH]
SAHF
PUSHF: Copy flag register to top of stack.
[SP] [SP] – 2
[SP] [Flags]
PUSHF
POPF : Copy word at top of stack to flag register.
[Flags] [SP]
[SP] [SP] + 2

Arithmetic and logical instructions


Instruction Explanation
The add instruction adds the contents of the source operand to the
ADD :
destination operand.
ADD AX, 0100
ADD AX, BX
ADD AX, [SI]
ADD AX, [5000]
ADD [5000H], 0100
ADC : Add with This instruction performs the same operation as ADD instruction, but
Carry adds the carry flag to the result.
ADC 0100H, AX
ADC BX, AX
ADC [SI], AX
ADC [5000], 0100
The subtract instruction subtracts the source operand from the
SUB : Subtract
destination operand and the result is left in the destination operand.
SUB AX, 0100
SUB AX, BX
SUB AX, [5000]
SUB [5000], 0100
The subtract with borrow instruction subtracts the source operand and
SBB : Subtract with
the borrow flag (CF) which may reflect the result of the previous
Borrow
calculations, from the destination operand
SBB AX, 0100
SBB AX, BX
SBB AX, [5000]
SBB [5000], 0100
This instruction increases the contents of the specified Register or
INC : Increment memory location by 1. Immediate data cannot be operand of this
instruction
INC AX
INC [BX]
INC [5000]
The decrement instruction subtracts 1 from the contents of the specified
DEC : Decrement
register or memory location.
DEC AX
DEC [5000]
The negate instruction forms 2‟s complement of the specified
destination in the instruction. The destination can be a register or a
NEG : Negate
memory location. This instruction can be implemented by inverting each
bit and adding 1 to it.
NEG AL
AL = 0011 0101 35H
Replace number in AL with its 2’s complement
AL = 1100 1011 = CBH
This instruction compares the source operand, which may be a register
CMP : Compare or an immediate data or a memory location, with a destination operand
that may be a register or a memory location
CMP BX, 0100
CMP AX, 0100
CMP [5000], 0100
CMP BX, [SI]
CMP BX, CX
MUL :Unsigned
This instruction multiplies an unsigned byte or word by the contents of
Multiplication Byte
AL
or Word
MUL BH --------- (AX) (AL) * (BH)
MUL CX --------- (DX)(AX) (AX) * (CX)
MUL WORD PTR [SI] --------- (DX)(AX) (AX) * ([SI])
IMUL :Signed This instruction multiplies a signed byte in source operand by a signed
Multiplication byte in AL or a signed word in source operand by a signed word in AX.
IMUL BH
IMUL CX
IMUL [SI]
CBW : Convert
This instruction copies the sign of a byte in AL to all the bits in AH. AH
Signed Byte to
is then said to be sign extension of AL.
Word
CBW
AX= 0000 0000 1001 1000
Convert signed byte in AL signed word in AX.
Result in AX = 1111 1111 1001 1000
CWD : Convert
This instruction copies the sign of a byte in AL to all the bits in AH. AH
Signed Word to
is then said to be sign extension of AL.
Double Word
CWD
Convert signed word in AX to signed double word in DX : AX
DX= 1111 1111 1111 1111
Result in AX = 1111 0000 1100 0001
DIV : Unsigned This instruction is used to divide an unsigned word by a byte or to divide
division an unsigned double word by a word.
DIV CL ;
Word in AX / byte in CL
Quotient in AL, remainder in AH
DIV CX ;
Double word in DX and AX / word in CX
Quotient in AX, ; remainder in DX
This instruction bit by bit ANDs the source operand that may be an
AND : Logical immediate register or a memory location to the destination operand that
AND may a register or a memory location. The result is stored in the
destination operand.
AND AX, 0008H
AND AX, BX
This instruction bit by bit ORs the source operand that may be an
immediate , register or a memory location to the destination operand that
OR : Logical OR
may a register or a memory location. The result is stored in the
destination operand.
OR AX, 0008
OR AX, BX
NOT : Logical This instruction complements the contents of an operand register or a
Invert memory location, bit by bit.
NOT AX
NOT [5000]
This instruction bit by bit XORs the source operand that may be an
XOR : Logical immediate , register or a memory location to the destination operand that
Exclusive OR may a register or a memory location. The result is stored in the
destination operand.
XOR AX, 0098
XOR AX, BX
The TEST instruction performs a bit by bit logical AND operation on
TEST : Logical
the two operands. The result of this ANDing operation is not available
Compare Instruction
for further use, but flags are affected.
TEST AX, BX
TEST [0500], 06
SAL and SHL are two mnemonics for the same instruction. This
SAL/SHL : SAL / instruction shifts each bit in the specified destination to the left and 0 is
SHL destination, stored at LSB position. The MSB is shifted into the carry flag. The
count. destination can be a byte or a word. It can be a register or in a memory
location. The number of shifts is indicated by count.
SAL CX, 1
SAL AX, CL
This instruction shifts each bit in the specified destination to the right
SHR : SHR and 0 is stored at MSB position. The LSB is shifted into the carry flag.
destination, count The destination can be a byte or a word. It can be a register or in a
memory location. The number of shifts is indicated by count.
SHR CX, 1
MOV CL, 05
SHR AX, CL
This instruction shifts each bit in the specified destination some number
SAR : SAR of bit positions to the right. As a bit is shifted out of the MSB position, a
destination, count copy of the old MSB is put in the MSB position. The LSB will be
shifted into CF.
SAR BL, 1
MOV CL, 04
SAR DX, CL
ROL Instruction : This instruction rotates all bits in a specified byte or word to the left
ROL destination, some number of bit positions. MSB is placed as a new LSB and a new
count CF.
ROL CX, 1
MOV CL, 03
ROL BL, CL
ROR Instruction : This instruction rotates all bits in a specified byte or word to the right
ROR destination, some number of bit positions. LSB is placed as a new MSB and a new
count CF.
ROR CX, 1
MOV CL, 03
ROR BL, CL
RCL Instruction : This instruction rotates all bits in a specified byte or word some number
RCL destination, of bit positions to the left along with the carry flag. MSB is placed as a
count new carry and previous carry is place as new LSB.
RCL CX, 1
MOV CL, 04
RCL AL, CL
RCR Instruction : This instruction rotates all bits in a specified byte or word some number
RCR destination, of bit positions to the right along with the carry flag. LSB is placed as a
count new carry and previous carry is place as new MSB.
RCR CX, 1
MOV CL, 04
RCR AL, CL
ROR Instruction : This instruction rotates all bits in a specified byte or word to the right
ROR destination, some number of bit positions. LSB is placed as a new MSB and a new
count CF.
ROR CX, 1
MOV CL, 03
ROR BL, CL
RCL Instruction : This instruction rotates all bits in a specified byte or word some number
RCL destination, of bit positions to the left along with the carry flag. MSB is placed as a
count new carry and previous carry is place as new LSB.
RCL CX, 1
MOV CL, 04
RCL AL, CL
RCR Instruction : This instruction rotates all bits in a specified byte or word some number
RCR destination, of bit positions to the right along with the carry flag. LSB is placed as a
count new carry and previous carry is place as new MSB.
RCR CX, 1
MOV CL, 04
RCR AL, CL

Branch instructions
Branch Instructions transfers the flow of execution of the program to a new address
specified in the instruction directly or indirectly. When this type of instruction is executed,
the CS and IP registers get loaded with new values of CS and IP corresponding to the
location to be transferred.
The Branch Instructions are classified into two types
i. Unconditional Branch Instructions
ii. Conditional Branch Instructions
Unconditional Branch Instructions :
In Unconditional control transfer instructions, the execution control is transferred to the
specified location independent of any status or condition. The CS and IP are unconditionally
modified to the new CS and IP.
CALL : Unconditional Call
This instruction is used to call a Subroutine (Procedure) from a main program. Address
of procedure may be specified directly or indirectly. There are two types of procedure
depending upon whether it is available in the same segment or in another segment.
i. Near CALL i.e., ±32K displacement.
ii. Far CALL i.e., anywhere outside the segment.
On execution, this instruction stores the incremented IP & CS onto the stack and loads the CS
& IP registers with segment and offset addresses of the procedure to be called.
RET: Return from the Procedure
At the end of the procedure, the RET instruction must be executed. When it is executed,
the previously stored content of IP and CS along with Flags are retrieved into the CS, IP and
Flag registers from the stack and execution of the main program continues further.
INT N: Interrupt Type N
In the interrupt structure of 8086, 256 interrupts are defined corresponding to the types
from 00H to FFH. When INT N instruction is executed, the type byte N is multiplied by 4
and the contents of IP and CS of the interrupt service routine will be taken from memory
block in 0000 segment.
INTO: Interrupt on Overflow
This instruction is executed, when the overflow flag OF is set. This is equivalent to a
Type 4 Interrupt instruction.
JMP: Unconditional Jump
This instruction unconditionally transfers the control of execution to the specified
address using an 8-bit or 16-bit displacement. No Flags are affected by this instruction.
IRET: Return from ISR
When it is executed, the values of IP, CS and Flags are retrieved from the stack to
continue the execution of the main program.
Loop instructions
LOOP : LOOP Unconditionally
This instruction executes the part of the program from the Label or address specified in
the instruction upto the LOOP instruction CX number of times. At each iteration, CX is
decremented automatically and JUMP IF NOT ZERO structure.
Example:
MOV CX, 0004H
MOV BX, 7526H
Label 1 MOV AX, CODE
OR BX, AX
LOOP Label 1
Conditional Branch Instructions
When this instruction is executed, execution control is transferred to the address
specified relatively in the instruction, provided the condition implicit in the Opcode is
satisfied. Otherwise execution continues sequentially.
JZ/JE Label
Transfer execution control to address, Label, if ZF =1.
JNZ/JNE Label
Transfer execution control to address, Label, if ZF =0
JS Label
Transfer execution control to address, Label, if SF = 1.
JNS Label
Transfer execution control to address, Label, if SF = 0.
JO Label
Transfer execution control to address, Label, if OF = 1.
JNO Label
Transfer execution control to address, Label, if OF = 0.
JNP Label
Transfer execution control to address, Label, if PF = 0.
JP Label
Transfer execution control to address, Label, if PF = 1.
JB Label
Transfer execution control to address, Label, if CF = 1.
JNB Label
Transfer execution control to address, Label, if CF = 0.
JCXZ Label
Transfer execution control to address, Label, if CX = 0.
Conditional LOOP Instructions
LOOPE / LOOPZ Label
Loop through a sequence of instructions from label while ZF = 1 and CX ≠ 0.
LOOPNZ / LOOPENE Label
Loop through a sequence of instructions from label while ZF = 0 and CX ≠ 0.

While – Do programs
WHILE some condition is present
DO
action
action
An Important point about this structure is that the condition is checked before any action is
done. In Industrial control applications of microprocessors, there are many cases where this is
required. The following very simple example will show how to implement this structure in
8086 assembly language.
Defining the Problem and Writing the Algorithm
In controlling a chemical process, it is necessary to bring the temperature of a
solution up to 100°C before going on to the next step in the process. If the solution
temperature is below 100°, heater must be turned ON and wait for the temperature to
reach 100°. If the solution temperature is at or above 100°, then move to the next step in
the process.
The WHILE-DO structure fits this problem to check the condition (temperature) before
switching ON the heater. Below Figure shows a flowchart and the pseudocode of an
algorithm for this problem.
 The first step in the algorithm is to read In the temperature from a sensor connected to a
port. The temperature read in is then compared with 100°. These two parts represent the
condition.
 If the temperature is at or above 100°, execution will exit the structure and do the next
mainline action, turn off the heater.
 If the temperature is less than 100°, the heater is turned ON and the temperature will be
rechecked. Execution will stay in this loop while the temperature is below 100°. Inclden.
 When the temperature reaches 100°, execution will exit the structure and go on to the next
mainline action, turn OFF the heater.

Assume that the temperature sensor inputs an 8-bit binary value for the Celsius temperature
to port FFF8H. Also assume that the heater control output is connected to the most significant
bit of port FFFAH. After Initializing the heater control port for output, read in the
temperature and compare the value read with 100. The JAE instruction after the compare can
be read as "Jump to the label HEATER_OFF if AL is above or equal to 100. On the first
check, the temperature would not be equal to 100o, so the processor would turn ON the
heater. If the heater temperature is below 100, turn ON the heater by loading a 1 in the most
significant bit of AL and outputting this value to the most significant bit of port FFFAH.
Then, perform unconditional JMP to loop back and check the temperature again. When the
temperature is at or above 100o. Load a 0 in the most significant bit of AL and output this to
port FFFAH to turn OFF the heater. Note that the action of turning OFF the heater is outside
the basic WHILE-DO structure.

Strings
A string is a series of bytes or words stored in successive memory locations. Often a
string consists of a series of ASCII character codes. One important feature of a word
processor is the ability to move a sentence or group of sentences from one place in the text to
another. Doing this involves moving a siring of ASCII characters from one place in memory
to another. 8086 Move String instruction MOVS allows to do operations like this very easily.
Another important feature of most word processors is the ability to search through the text
looking for a given word or phrase: The 8086 Compare String instruction CMPS can be used
to do operations of this type. In a similar manner, the 8086 SCAS instruction can be used to
search a string to see whether it contains a specified character.
String instructions
Move String : MOV SB, MOV SW:
An element of the string specified by the source index (SI) register with respect to the
current data segment (DS) register is moved to the location specified by the destination index
(DI) register with respect to the current extra segment (ES) register. The move can be
performed on a byte (MOV SB) or a word (MOV SW) of data. After the move is complete,
the contents of both SI & DI are automatically incremented or decremented by 1 for a byte
move and by 2 for a word move. Address pointers SI and DI increment or decrement depends
on how the direction flag DF is set.
Example : Block move program using the move string instruction
MOV AX, DATA SEG ADDR
MOV DS, AX
MOV ES, AX
MOV SI, BLK 1 ADDR
MOV DI, BLK 2 ADDR
MOV CK, N
CDF
NEXT : MOV SB
LOOP NEXT
HLT
REP : Repeat Instruction Prefix
This instruction is used as a prefix to other instructions. The instruction to which the REP
prefix is provided, is executed repeatedly until the CX register becomes zero (at each
iteration CX is automatically decremented by one).
i. REPE / REPZ- repeat operation while equal / zero.
ii. REPNE / REPNZ - repeat operation while not equal / not zero. These are used for
CMPS, SCAS instructions only, as instruction prefixes.
CMPS : Compare String Byte or String Word
The CMPS instruction can be used to compare two strings of byte or words. The length of
the string must be stored in the register CX. If both the byte or word strings are equal, zero
Flag is set.
The REP instruction Prefix is used to repeat the operation till CX (counter) becomes zero
or the condition specified by the REP Prefix is False.
SCAN : Scan String Byte or String Word
This instruction scans a string of bytes or words for an operand byte or word specified in
the register AL or AX. The String is pointed to by ES:DI register pair. The length of the
string s stored in CX. The DF controls the mode for scanning of the string. Whenever a match
to the specified operand, is found in the string, execution stops and the zero Flag is set. If no
match is found, the zero flag is reset.
LODS : Load String Byte or String Word
The LODS instruction loads the AL / AX register by the content of a string pointed to by
DS : SI register pair. The SI is modified automatically depending upon DF, If it is a byte
transfer (LODSB), the SI is modified by one and if it is a word transfer (LODSW), the SI is
modified by two. No other Flags are affected by this instruction.
STOS : Store String Byte or String Word
The STOS instruction Stores the AL / AX register contents to a location in the string
pointer by ES : DI register pair. The DI is modified accordingly, No Flags are affected by this
instruction. The direction Flag controls the String instruction execution, The source index SI
and Destination Index DI are modified after each iteration automatically. If DF=1, then the
execution follows autodecrement mode, SI and DI are decremented automatically after each
iteration. If DF=0, then the execution follows autoincrement mode. In this mode, SI and DI
are incremented automatically after each iteration.

Procedures
Particular sequence of instructions may be repeated at several different points in a
program. To avoid writing the sequence of instructions in the program each time needed, one
can write the sequence as a separate subprogram called a procedure. CALL instruction is
used to call the procedure by sending the starting address of the procedure in memory to the
processor. A CALL instruction causes execution to go from the mainline program to a
procedure. A RET instruction at the end of the procedure returns execution to the next
instruction in the mainline.

As shown in the below figure, procedures can even be nested. This means that one
procedure calls another procedure as part of its instruction sequence. Follow the arrows in in
the figure to see how this works.
CALL Instruction in the mainline program loads the instruction pointer and in some cases
also the code segment register with the starting address of the procedure, The next instruction
fetched will be the first instruction of the procedure. At the end of the procedure, a RET
instruction sends execution back to the next instruction after the CALL in the mainline
program. The RET instruction does this by loading the Instruction pointer and if necessary,
the code segment register with the address of the next instruction after the CALL instruction.
8086 CALL Instruction performs two operations when it executes. First, it stores the
address of the Instruction after the CALL instruction on the stack. This address is called the
return address because it is the address that execution will return to after the procedure
executes. If the CALL is to a procedure in the same code segment, then the call is near, and
only the Instruction pointer contents will be saved on the stack. If the CALL is to a procedure
in another code segment, the call is far. Iii this case, both the instruction pointer and the code
segment register contents will be saved on the stack.
Four forms of CALL instructions
 Direct within-segment near call
 Indirect within-segment near call
 Direct intersegment far call
 Indirect intersegment far call

Macros
When the repeated group of instructions is too short or not appropriate to be written as a
procedure, macro is used. A macro is a group of instructions bracketed and given a name at
the start of the program. Each time the Macro is called in a program, the assembler will insert
the defined group of instructions in place of the ‘call’. In other words, the macro call is like a
shorthand expression which tells the assembler, ‘Every time a macro name is seen in the
program, replace it with the group of instructions defined as that macro at the start of the
program. An important point here is that the assembler generates machine codes for the group
of instructions each time the macro is called.
Replacing the macro with the instructions it represents is commonly called "expanding"
the macro. Since the generated machine codes are right in-line with the rest of the program,
the processor does not have to go off to a procedure and return. Therefore, using a macro
avoids the overhead time involved in calling and returning from a procedure. A disadvantage
of generating in-line code each time a macro is called is that this will make the program take
up more memory than using a procedure.
Here's how we write a macro to save all the registers.
PUSH_ALL MACRO
PUSHF
PUSH AX
PUSH BX
PUSH CX
PUSH Dx
PUSH BP
PUSH SI
PUSH DI
PUSH DS
PUSH ES
PUSH SS
ENDM
The PUSILALL MACRO statement identifies the start or the macro and gives the macro a
name. The ENDM identifies the end of the macro. Now, to call the macro in one of the
procedures, put in the name of the macro. The start of the procedure which does this might
look like this:
BREATH_RATE PROC FAR
ASSUME CS:PROCEDURES. DS:PATIENT_PARAMETERS
PUSHALL: Maoro call
MOV AX, PATIENT_PARAMETERS : Initialize data
MOV DS, AX : Segment reg
When the assembler assembles this program section, it will replace PUSH_ALL with the
Instructions that it represents and insert the machine codes for these instructions in the object
code version of the program. The assembler listing tells you which lines were inserted by a
macro call by putting a + in each program line inserted by a macro call. Using a macro makes
the source program much more readable because the source program does not have the long
series of push instructions cluttering it up.

You might also like