0% found this document useful (0 votes)
24 views96 pages

Chapter 4

Chapter 4 discusses the 8086 microprocessor's addressing modes, instruction set, and assembly language programming. It covers the types of instructions including data transfer, arithmetic, bit manipulation, program execution transfer, and string instructions, detailing their functions and examples. The chapter emphasizes the importance of understanding these instructions for effective programming with the 8086 microprocessor.

Uploaded by

umar
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)
24 views96 pages

Chapter 4

Chapter 4 discusses the 8086 microprocessor's addressing modes, instruction set, and assembly language programming. It covers the types of instructions including data transfer, arithmetic, bit manipulation, program execution transfer, and string instructions, detailing their functions and examples. The chapter emphasizes the importance of understanding these instructions for effective programming with the 8086 microprocessor.

Uploaded by

umar
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/ 96

Chapter 4

8086 microprocessor addressing modes, instruction set

and assembly language programming

1
Introduction
❑ An instruction is a basic command given to a microprocessor to perform a
specified operation with given data.
❑ Each instruction has two groups of bits.
❑ One group of bits is known as operation code (opcode), which defines what
operation will be performed by the instruction.
❑ The other field is called operand, which specifies data that will be used in
operations.
❑ The addressing mode is used to locate the operand or data.
❑ There are different types of addressing modes depending upon the location
of data in the 8086 processor. 2
8086 Addressing mode

3
Instruction Set of 8086

17
Instruction Set of 8086
❑ An instruction is a binary pattern designed inside a microprocessor to
perform a specific function.
❑ It’s a command given to the microprocessor to perform a specific task on a
given data.

❑ The entire group of instructions that amicroprocessor supports is


called Instruction Set.

❑ 8086 - Microprocessor has more than 20,000 instructions.

18
Classification of Instruction Set
❑ The 8086 microprocessor supports 6 types of instruction set

1. Data Transfer Instructions

2. Arithmetic Instructions

3. Bit Manipulation Instructions (Logical, shift and rotate instruction)

4. Program Execution Transfer Instructions (Branch & Loop Instructions)

5. String Instructions and

6. Processor Control Instructions

19
1. Data Transfer Instructions
❑ They are used for movement of the data from one location to another
location.
❑ They provides the ability to move data either between its internal
registers or between register to/from memory or I/O.
❑ These instructions are used to transfer data from source to destination.

❑ The operand can be a constant, memory location, register or I/O


port address.

20
Data Transfer Instructions

❑ MOV Des, Src:


❑ Src operand can be register, memory location or immediate operand.
❑ Des operand can be register or memory.
❑ Both Src and Des can not be memory location at the same time.
❑ E.g.:
MOV CX, 037A H MOV [734AH], BX
MOV AL, BL
MOV BX, [0301 H]

21
Data Transfer Instructions

❑ PUSH Src:
❑ It pushes the data from src operand into top of stack

❑ E.g.:
PUSH BX

❑ POP Des:
❑ It pops the operand from top of stack to Des operand.
❑ Des can be a general purpose register, segment register (except CS) or memory
location.
❑ E.g.:
POP AX
22
Data Transfer Instructions

❑ XCHG Des, Src:


❑ This instruction exchanges Src with Des.
❑ It can not exchange two memory locations directly.
❑ E.g.:
XCHG DX, AX

23
Data Transfer Instructions
❑ IN Accumulator, PortAddress:
❑ It transfers the operand from specified port to accumulator register.
❑ E.g.:

IN AX, 0028 H

❑ OUT Port Address, Accumulator:

❑ It transfers the operand from accumulator to specified port.

❑ E.g.:

OUT 0028 H, AX
24
Data Transfer Instructions

❑ LEA Register, Src:


❑ It loads a 16-bit register with the offset address of the data specified by
the Src.

❑ E.g.:

LEABX, [DI]

o This instruction loads the contents of DI (offset) into the BX


register.

25
Data Transfer Instructions

❑ LDS Des, Src:


❑ It loads 32-bit pointer from memory source to destination
register and DS.
❑ The offset is placed in the destination register and the segment is
placed in DS.
❑ To use this instruction the word at the lower memory address must
contain the offset and the word at the higher address must contain the
segment.
❑ E.g.:
LDS BX, [0301 H]
26
Data Transfer Instructions

❑ LES Des, Src:


❑ It loads 32-bit pointer from memory source to destination
register and ES.
❑ The offset is placed in the destination register and the segment is placed
in ES.
❑ This instruction is very similar to LDS except that it initializes ES
instead of DS.
❑ E.g.:

LES BX, [0301 H]

27
Data Transfer Instructions

❑ LAHF:
❑ It copies the lower byte of flag register to AH.

❑ SAHF:
❑ It copies the contents of AH to lower byte of flag register.

❑ PUSHF:
❑ Pushes flag register to top of stack.

❑ POPF:
❑ Pops the stack from top to flag register.
28
2. Arithmetic Instructions
❑ These instructions are used to perform arithmetic operations like
addition, subtraction, multiplication, division, etc.

❑ ADD Des, Src:


❑ It adds a byte to byte or a word to word.

❑ It effects AF, CF, OF, PF, SF, ZF flags.

❑ E.g.:
ADD AL, 74H

ADD DX, AX

ADD AX, [BX]


29
Arithmetic Instructions

❑ ADC Des, Src:


❑ It adds the two operands with CF.

❑ It effects AF, CF, OF, PF, SF, ZF flags.


❑ E.g.:
ADC AL, 74H

ADC DX, AX

ADC AX, [BX]

30
Arithmetic Instructions

❑ SUB Des, Src:


❑ It subtracts a byte from byte or a word from word.
❑ It effects AF, CF, OF, PF, SF, ZF flags.
❑ For subtraction, CF acts as borrow flag.
❑ E.g.:
SUB AL, 74H
SUB DX, AX
SUB AX, [BX]

31
Arithmetic Instructions

❑ SBB Des, Src:


❑ It subtracts the two operands and also the borrow from the
result.
❑ It effects AF, CF, OF, PF, SF, ZF flags.
❑ E.g.:
SBB AL, 74H
SBB DX,AX
SBB AX, [BX]

32
Arithmetic Instructions

❑ INC Src:
❑ It increments the byte or word by one.
❑ The operand can be a register or memory location.
❑ It effects AF, OF, PF, SF, ZF flags.

❑ CF is not effected.
❑ E.g.:
INC AX

33
Arithmetic Instructions

❑ DEC Src:
❑ It decrements the byte or word by one.
❑ The operand can be a register or memory location.
❑ It effects AF, OF, PF, SF, ZF flags.

❑ CF is not effected.
❑ E.g.:
DEC AX

34
Arithmetic Instructions

❑ AAA (ASCII Adjust afterAddition):


❑ The data entered from the terminal is in ASCII format.

❑ In ASCII, 0 – 9 are represented by 30H – 39H.

❑ This instruction allows us to add the ASCII codes.

❑ This instruction does not have any operand.

❑ Other ASCII Instructions:


❑ AAS (ASCII Adjust after Subtraction)

❑ AAM (ASCII Adjust after Multiplication)

❑ AAD (ASCII Adjust Before Division)

35
Arithmetic Instructions

❑ DAA (Decimal Adjust after Addition)


❑ It is used to make sure that the result of adding two BCD numbers is
adjusted to be a correct BCD number.
❑ It only works on AL register.

❑ DAS (Decimal Adjust after Subtraction)


❑ It is used to make sure that the result of subtracting two BCD numbers is
adjusted to be a correct BCD number.
❑ It only works on AL register.

36
Arithmetic Instructions

❑ NEG Src:

❑ It creates 2’s complement of a given number.

❑ That means, it changes the sign of a number.

❑ E.g.:
NEG AL

37
Arithmetic Instructions

❑ CMP Des, Src:


❑ It compares two specified bytes or words.

❑ The Src and Des can be a constant, register or memory location.

❑ Both operands can not be a memory location at the same time.

❑ The comparison is done simply by internally subtracting the source from


destination.
❑ The value of source and destination does not change, but the flags are
modified to indicate the result.

38
Arithmetic Instructions

❑ MUL Src:
❑ It is an unsigned multiplication instruction.
❑ It multiplies two bytes to produce a word or two words to produce a double
word.
AX = AL * Src
DX : AX = AX * Src
❑ This instruction assumes one of the operand in AL or AX.

❑ Src can be a register or memory location.

IMUL Src:
❑ It is a signed multiplication instruction.
39
Arithmetic Instructions

❑ DIV Src:
❑ It is an unsigned division instruction.

❑ It divides word by byte or double word by word.


❑ The operand is stored in AX, divisor is Src and the result is
stored as: AH = remainder AL = quotient

❑ IDIV Src:
❑ It is a signed division instruction.

40
Arithmetic Instructions

❑ CBW (Convert Byte to Word):


❑ This instruction converts byte in AL to word in AX.

❑ The conversion is done by extending the sign bit of AL throughout AH.

❑ CWD (Convert Word to Double Word):

❑ This instruction converts word in AX to double word in DX : AX.

❑ The conversion is done by extending the sign bit of AX throughout DX.

41
3. Bit Manipulation Instructions
❑ These instructions are used at the bit level.
❑ These instructions can be used for:
❑ Testing a zero bit

❑ Set or reset a bit

❑ Shift bits across registers

42
Bit Manipulation Instructions

❑ NOT Src:
❑ It complements each bit of Src to produce 1’s complement of
the specified operand.
❑ The operand can be a register or memory location.

❑ E.g.:
NOT AX

43
Bit Manipulation Instructions
❑ AND Des, Src:
❑ It performs AND operation of Des and Src.

❑ Src can be immediate number, register or memory location.

❑ Des can be register or memory location.

❑ Both operands cannot be memory locations at the same time.

❑ CF and OF become zero after the operation.

❑ PF, SF and ZF are updated.

44
Bit Manipulation Instructions

❑ OR Des, Src:
❑ It performs OR operation of Des and Src.

❑ Src can be immediate number, register or memory location.

❑ Des can be register or memory location.

❑ Both operands cannot be memory locations at the same time.

❑ CF and OF become zero after the operation.

❑ PF, SF and ZF are updated.

45
Bit Manipulation Instructions

❑ XOR Des, Src:


❑ It performs XOR operation of Des and Src.
❑ Src can be immediate number, register or memory location.
❑ Des can be register or memory location.
❑ Both operands cannot be memory locations at the same time.
❑ CF and OF become zero after the operation.
❑ PF, SF and ZF are updated.

46
Bit Manipulation Instructions

❑ SHL Des, Count:

❑ It shift bits of byte or word left, by count.

❑ It puts zero(s) in LSBs.

❑ MSB is shifted into carryflag.

❑ If the number of bits desired to be shifted is 1, then the immediate


number 1can be written in Count.
❑ However, if the number of bits to be shifted is more than 1, then the count
is put in CL register.

47
Bit Manipulation Instructions

❑ SHR Des, Count:


❑ It shift bits of byte or word right, by count.

❑ It puts zero(s) in MSBs.

❑ LSB is shifted into carry flag.

❑ If the number of bits desired to be shifted is 1, then the immediate


number 1can be written in Count.
❑ However, if the number of bits to be shifted is more than 1, then the
count is put in CL register.

48
Bit Manipulation Instructions

❑ ROL Des, Count:


❑ It rotates bits of byte or word left, by count.

❑ MSB is transferred to LSB and also to CF.

❑ If the number of bits desired to be shifted is 1, then the


immediate number 1can be written in Count.
❑ However, if the number of bits to be shifted is more than 1, then
the count is put in CL register.

49
Bit Manipulation Instructions

❑ ROR Des, Count:


❑ It rotates bits of byte or word right, by count.

❑ LSB is transferred to MSB and also to CF.

❑ If the number of bits desired to be shifted is 1, then the


immediate number 1can be written in Count.
❑ However, if the number of bits to be shifted is more than 1, then
the count is put in CL register.

50
4. Program Execution Transfer Instructions

❑ These instructions cause change in the sequence of the


execution of instruction.

❑ This change can be through a conditional or unconditional.

❑ In case of conditional changes, the conditions are represented


by flags.

51
Program Execution Transfer Instructions

❑ CALLDes:
❑ This instruction is used to call a subroutine or function or
procedure.
❑ The address of next instruction after CALL is saved onto stack.

❑ RET:
❑ It returns the control from procedure to calling program.

❑ Every CALL instruction should have a RET.

52
Program Execution Transfer Instructions

❑ JMP Des:
❑ This instruction is used for unconditional jump from one place
to another.

❑ Jxx Des (Conditional Jump):

❑ All the conditional jumps follow some conditional statements

or any instruction that affects the flag.

53
Program Execution Transfer Instructions
Conditional Jump instruction table

54
Program Execution Transfer Instructions
Conditional Jump instruction table

55
Program Execution Transfer Instructions

❑ Loop Des:

❑ This is a looping instruction.

❑ The number of times looping is required is placed in the CX register.

❑ With each iteration, the contents of CX are decremented.

❑ ZF is checked whether to loop again or not.

56
5. String Instructions
❑ String in assembly language is just a sequentially stored
bytes or words.

❑ There are very strong set of string instructions in 8086.

❑ By using these string instructions, the size of the program


is considerably reduced.

57
String Instructions

❑ CMPS Des, Src:


❑ It compares the string bytes or words of source and destination.

❑ SCAS String:
❑ It scans a string and

❑ It compares the String with byte in AL or with word in AX.

58
String Instructions

❑ MOVS / MOVSB / MOVSW:

❑ It causes moving of byte or word from one string to another.

❑ In this instruction, the source string is in Data Segment and

destination string is in Extra Segment.

❑ SI and DI store the offset values for source and destination

index.

59
String Instructions

❑ REP (Repeat):
❑ This is an instruction prefix (label).

❑ It causes the repetition of the instruction until CX becomes


zero.
❑ E.g.:
REP MOVSB STR1, STR2
❑ It copies byte by byte contents.
❑ REP repeats the operation
❑ MOVSB until CX becomes zero.
60
6. Processor Control Instructions
❑ These instructions control the processor itself.

❑ 8086 allows to control certain control flags:

❑ that causes the processing in a certain direction and

❑ processor synchronization if more than one microprocessor

attached.

61
Processor Control Instructions

❑ STC:
❑ It sets the carry flag to 1.

❑ CLC:
❑ It clears the carry flag to 0.

❑ CMC:
❑ It complements the carry flag.
62
Processor Control Instructions

❑ STD:
❑ It sets the direction flag to 1.

❑ If it is set, string bytes are accessed from higher memory


address to lower memory address.

❑ CLD:
❑ It clears the direction flag to 0.

❑ If it is reset, the string bytes are accessed from lower


memory address to higher memory address.
63
Assembler Directive

64
65
66
67
68
69
70
Assembly language programming

71
Assembly language programming
❑ A program is a set of instructions arranged in the specific sequence to
do the specific task.
❑ The process of writing the set of instructions which tells the
microprocessor what to do is called programming.
❑ To do this the programmer must speak to the microprocessor in a
language which processor can understand.
❑ Steps involving in programing:-
❑ Specifying the problem
❑ Designing the problem solution
❑ Coding and
❑ Debugging
72
❑ Specifying the problem:
✓ the first step in the programming is to find out which task is to be
performed.
❑ Designing the problem-solution:
✓ during designing, the exact step by step process that is to be followed is
developed and written down.
❑ Coding:
✓ Implementation begins with the process of coding the program.
✓ It means, to tell the processer the exact step by step process in its language.
✓ Programmer has to choose proper instructions from the instruction set to
build the program.
❑ Debugging:
✓ It is the process of testing the code to see if it dose the given task properly.
✓ If program is not working properly, debugging process helps in finding and
correcting errors. 73
Flow chart
❑ To develop and design the problem-solution programmer has to write
down various actions which are to be performed in proper sequence.

❑ The flow chart is a graphical tool that allows programmer to


represent various actions which are to be performed.

❑ The graphical representation is very useful for clear understanding of


the programing logic.

74
Oval: It indicates start or stop of operation.
Arrow: It indicates flow with direction.

Parallelogram: It indicates input/output operation.


Rectangle: It indicates process operation.
Diamond: It indicates decision making operation.
Double sided rectangle: It indicates execution of pre-
defined process (subroutine)
Circle with alphabet: It indicates continuation of
process.
75
Assembly language programs
❑ A program which has simply a sequence of binary codes (0, 1)for the
instructions is called machine level language program.
❑ However, to write a program in machine language, programmer has to
memorize the thousands of binary instruction codes for a processor.
❑ To make programing easier, usually programmers write programs in
assembly language.
❑ They, then translate to machine language so that it can be loaded into
memory and executed.
76
❑ Assembly language uses two, three or four letter words to represent
each instruction types.
❑ Those word referred to as mnemonics. They are usually initials or a
shortened form of the English word(s) for the operation performed by
the instruction.

❑ E.g.: Mnemonic Instruction


ADD Addition
AND Logical AND
MOV Copying content
77
❑ The assembly text is usually divided into fields, separated by space.
❑ A format for typical line from ALP can be given as:

Lebel: Mnemonic Operand1, Operand2 ; Comment

❑ Label: It is optional, used to specify symbolic labels.


❑ It is an identifier that is assigned to the address of the first byte of the
instruction in which it appears.
❑ A symbolic name can be used in branch instructions to jump to the
labeled instruction line.

78
❑ Mnemonic: which is mandatory, all instructions must contain a
mnemonic which is the opcode (operation code) .
❑ Operands: the presence of operands depends on the instruction.
❑ Some instructions have no operand, some have one and some have
two. If there are two operands, they are separated by comma.
❑ Comment: they starts with a semicolon and continuous to the end of
the line. They tell us what the program is trying to accomplish.

79
Comparison between machine level and assembly language
No. Machine language Assembly language
1 Consists of binary codes which Consists of mnemonics which specify
specifies the operations the operations
2 Processor dependent and hence Processor dependent and hence
requires knowledge of internal details requires knowledge of internal details
of processors to write a program of processors to write a program
3 Programs require less memory Programs require less memory
4 Programs have less execution time Programs have less execution time
5 Program development is difficult Program development is simpler than
machine language
6 It is not user friendly It is less user friendly
80
Assembly language programming tips
❑ To writing a program for specific task, programmer may find a number
of solutions (instruction sequences).

❑ A skilled programmer has to choose an optimum solution out of them


for that specific task.

❑ The technique of choosing an optimum solution is an art of assembly


language programming.

❑ Let us see some tips regarding this with examples:

81
❑ What is an optimum solution ? :

❑ The optimum solution is the solution which takes minimum


memory space for the program and minimum time for the
execution of a task.

❑ When we say memory space for the program we consider space for
program storage (program length), space for data storage and space
used by the stack.

82
❑ Use of proper instructions :
❑ Many time we come across the situation where more than one set of instructions
are available to perform particular functions.

❑ For example, if the function is add 01 in the BX register of 8086 we have two
options : ADD BX, 0001H or INC BX.

❑ In such situations we must check the space and time for both the options and then
select the option which requires less space and time.

❑ ADD BX, 0001H is four byte instruction, hence requires 4 clock cycle to execute it.
❑ INC BX is two byte instruction hence requires 2 clock cycle to execute it.

❑ The instruction INC BX requires less memory space and execution time than
ADD BX, 0001H.
83
❑ Use of advanced instructions:
❑ We must optimally utilize the processor capability.
❑ For example, when it is necessary to write a program to move a block of data
from the source to destination location, a programmer may initialize a pointer
to indicate source location, pointer to indicate destination location and a
counter to count the number of data elements to be transferred.
❑ After transfer of one data element from source to destination location
programmer may use INC, DEC and JNZ instructions to increment source
and destination pointers, decrement counter and to check whether all data
elements are transferred or not, respectively.
❑ The same task can be implemented by MOVS instructions supported by 8086.

❑ Let us see both approaches and compare them:-


84
Part listing of program with general approach:

MOV SI, 1000H ;Initialise source pointer


MOV DI, 2000H ;Initialise destination pointer
MOV CX, 0020H ;Initialise counter
BACK : MOV AX, [SI] ;Gate data element from source
MOV [DI], AX ;Store it at destination
INC SI ;Incrment source pointer
INC DI ;Increment destination pointer
DEC CX ;Decrement counter
JNZ BACK ;If count is not zero, jump to BACK

85
Part listing of program with MOVSB instruction:

MOV SI, 1000H ;Initialize source pointer


MOV DI, 2000H ;Initialize destination pointer
MOV CX, 0020H ;Initialize counter
CLD ;Clear direction flag
REP : MOVSB ;Move the entire block

❑ From the two programmes we can easily notice that the MOVSB instruction needes
not neither counter decrement and jump back nor pointer update instructions.

❑ All these functions are done authomatically, becouse MOVSB instructions copies
multiple bytes from source to destination.

86
❑ After each byte transfers it automatically increments SI and DI
pointers by 1 (since DF = 0) and decrements count in CX register and
it repeats this process until CX = 0.

❑ In the second approach, we require less number of instructions and


memory space.

❑ As number of instructions are less, fetching time required for the


instructions is also saved and hence we can say that, the second approach
requires less memory space and less time to execute the same task.

❑ So skilled programmer uses the second approach.


87
❑ Use of proper addressing modes :
❑ If we compare the various addressing modes regarding access time
required for accessing operands, register addressing takes less time
to access operand than index and indirect addressing modes.
❑ It is obvious that when operands are available in CPU registers
they are immediately available for operation.
❑ However when they are in memory we have to fetch them from
memory. Fetching operands takes some time.
❑ So its advisable to store most of the operands in the CPU registers.
❑ We know that CPU registers are limited in numbers
❑ Therefore, when there are not enough only we should use memory
space for storing the operands. 88
❑ Prepare documentation :
❑ Program must provide enough information so other users can
utilize the program module without having to examine its internal
structure.
❑ So along with the program it is advised to give the following
information:
✓ Description of the purpose of the program module.
✓ In case of subroutine program list of passing parameters and return
value.
✓ Register and memory location used.
✓ Proper comment for each line of instruction used.
89
Programming with assembler
The fig shows steps involved in developing and executing ALP.

90
❑ Assembling process : assembler translates a source file that
was created using the editor in to machine language such as
binary or object code.

❑ The assembler generates two files on the source file path:

1. Object file : contains the binary codes for the instructions


and information about the addresses of the instructions.
2. Assembler list file : contains the assembly language
statements, the binary code for each instruction, and the
offset for each instruction.
91
In the first pass, the assembler performs the following operations :
1. Reading the source program instructions.
2. Creating a symbol table in which all symbols used in the
program, together with their attributes are stored.
3. Replacing all machine codes by their binary codes.
4. Detecting any syntax error in the source program.
5. Assigning relative addresses to instructions and data.
92
❑ On the second pass, through the source program, the
assembler extracts the symbol from the operand field and
searches for it in the symbol table.
❑ If the symbol dose not appear in the table, the corresponding
statement is obviously invalid. If it appear then, the symbol
will replace by it’s address or value.
Transferred from .asm to .obj file
93
❑ Linking process : a linker is a program used to join together
several object files into one large object file.
❑ A linker produces a link file which contains the binary codes
for all the combined modules.
❑ It also produce a link map which contains the address
information about the link file.
❑ The linker dose not assign absolute address to the program, it
only assigns relative address starting from zero. 94
❑ Debugging process : a debugger is a program which allows
us to load our object code program into system memory,
execute the program and debug it.
✓ Allow us to look at the contain of register and memory
locations.
✓ It allow us to change the contains of register and memory
locations.
✓ Allow us to run a single instruction at a time and check
contains change of register and memory location.
✓ Allow us to set a breakpoint up to which the program
execute, then examine the contains.
95
96

You might also like