100% found this document useful (1 vote)
284 views

Computer & Interfacing Chapter Three

The document describes the various addressing modes of the Intel 8086 processor. It outlines 7 addressing modes: 1) register, 2) immediate, 3) direct, 4) register indirect, 5) based relative, 6) indexed relative, and 7) based indexed relative. Each mode is explained with examples of how they calculate physical addresses and access memory locations. Register modes access registers directly, while other modes use constants, offsets, and segment registers to calculate effective addresses and physical addresses to access operands in memory.

Uploaded by

migad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
284 views

Computer & Interfacing Chapter Three

The document describes the various addressing modes of the Intel 8086 processor. It outlines 7 addressing modes: 1) register, 2) immediate, 3) direct, 4) register indirect, 5) based relative, 6) indexed relative, and 7) based indexed relative. Each mode is explained with examples of how they calculate physical addresses and access memory locations. Register modes access registers directly, while other modes use constants, offsets, and segment registers to calculate effective addresses and physical addresses to access operands in memory.

Uploaded by

migad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 148

OUTLINE OF THE

HAWASSA CHAPTER
UNIVERSITY
INISTITUTE OF TECHNOLOGY
DEPARTMENT OF ELECTRICAL AND COMPUTER
ENGINEERING

MICROCOMPUTER AND INTERFACING


(ECEG4161)

CHAPTER THREE
Intel 8086 PROCESSOR PROGRAMING & INSTRUCTION SETS

BY NIGATU A.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
1
INTRODUCTION
 The 8086 has about 117 different instructions with about 300
opcodes.
 The 8086 instruction sets can contain no operand, single operand,
and two operand instructions.
 The 8086 instructions do not permit memory to memory operations
except for string instructions which involve array operations.
 The processor can access memory in different ways that are
collectively called addressing mode.
 The addressing modes describe the types of operands and the way
they are accessed for executing an instruction.
 The number of addressing modes is determined when the
microprocessor is designed and cannot be changed.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


2
8086 ADDRESSING MODES
 The 8086 provides a total of seven distinct addressing modes:
1. Register addressing modes
2. Immediate addressing modes
3. Direct addressing modes
4. Register indirect addressing modes
5. Based relative addressing modes
6. Indexed relative addressing modes
7. Based indexed relative addressing modes
 MOV instructions are used to explain addressing modes.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


3
8086 ADDRESSING MODES (CONT..)
A) REGISTER ADDRESSING MODE: MOV REG1, REG2;
 The register addressing mode involves the use of registers to hold the
data to be manipulated.
 Memory is not accessed when this addressing mode is executed;
 Relatively fast transfer since memory is not accessed.
 Examples:
 MOV BX, DX ; copy the contents of DX into BX
 MOV ES, AX ; copy the contents of AX into ES
 ADD AL, BH ; add the contents of BH to Contents of AL.
 The size of reg1 and reg2 must be the same.
 MOV CL, AX is illegal for instance.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


4
8086 ADDRESSING MODES (CONT..)
B) IMMEDIATE ADDRESSING MODE:- MOV REG, CONSTANT
 In the immediate addressing mode, the source operand is a constant.
 It can be used to load info into any of the registers except the segment
registers and flag register.
 Examples:
 MOV AX, 2550H ; move 2550H into AX
 MOV CX, 625 ; load the decimal value 625 into CX
 MOV BL, 40H ; load 40H into BL
 MOV DS, 0123H; is illegal!
 Instead we can use:
MOV AX, 0123H
MOV DS, AX

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


5
8086 ADDRESSING MODES (CONT..)
C) DIRECT ADDRESSING MODE: MOV reg, [constant] or
MOV [constant], reg
 Here constant is not operand but it is an offset or EA in memory of operand.
 In the direct addressing mode the data is in some memory location(s) and the
address of the data in memory comes immediately after the instruction.
 This address is the offset address and one can calculate the physical address
by shifting left the DS register and adding it to the offset as follows:
 Example:
 MOV DL, [2400H] ; move contents of DS: 2400H into DL
 EXERCISE 3-1: Find the physical address of the memory location and its
contents after the execution of the following,
MOV AL, 99H
MOV [3518H], AL
Assuming that DS = 1512H.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
6
8086 ADDRESSING MODES (CONT..)
D) REGISTER INDIRECT ADDRESSING MODE:
MOV REG1, [REG2] or MOV [REG2], REG1;
 Here the address of the memory location where the operand resides is
held by a register, REG2.
 REG1 can be any general purpose register and REG2 can be either of
SI, DI, or BX and they must be combined with DS in order to
generate the 20-bit physical address.
 Example:
MOV AL, [BX] ; move contents of DS:BX into AL
MOV CL, [SI] ; move contents of DS:SI into CL
MOV [DI], AH ; move contents of AH into DS:DI
MOV DX, [BX] ; move contents of DS:BX into DL and
; contents of DS:BX+1 into DH
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
7
8086 ADDRESSING MODES (CONT..)
 Exercise: Assume that DS = 1120H, SI = 2498H, and AX = 17FEH.
Show the contents of memory locations and its contents after the
execution of MOV [SI], AX
 Solution:
 The contents of AX are moved into memory locations with
logical address DS: SI and DS: SI + 1;
 Therefore, the physical address starts at:
PA = DS (shifted left) + SI = 13698H.
 According to the little endian convention,
 low address 13698H contains FEH, the low byte, and
 high address 13699H will contain 17H, the high byte.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


8
8086 ADDRESSING MODES (CONT..)
E) BASED RELATIVE ADDRESSING MODE:
MOV REG1, [REG2] + CONST or
MOV [REG2] + CONST, REG1;
 CONST is an 8-bit displacement value.
 REG1 can be any general purpose register and REG2 can only be
either of BP or BX
 In the based relative addressing mode, base registers BX and BP, as
well as a displacement value, are used to calculate what is called the
effective address.
 The default segments used for the calculation of the physical address
(PA) are DS for BX and SS for BP.
 PA = DS*10H + BX + const ; EA = BX + const
 PA = SS*10H + BP + const ; EA = BP + const

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


9
8086 ADDRESSING MODES (CONT..)
 EXAMPLES: Determine PA and EA
 MOV CX, [BX]+10 ; move DS:BX + 10 and DS:BX+10+1 into CX.
 PA = DS*10H + BX + 10,
 EA = BX + 10
 MOV AL, [BP] + 5 ;
 PA = SS*10H + BP + 5
 EA = BP + 5
 Alternative codings for MOV reg1, [reg2] + const is
 MOV reg1, [reg2 + const] or
 MOV reg1, const[reg2]
 For instance, MOV CX, [BX]+10 is same as
 MOV CX, [BX+10] or
 MOV CX, 10[BX]

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


10
8086 ADDRESSING MODES (CONT..)
F) INDEXED RELATIVE ADDRESSING MODE:
MOV REG1, [REG2] + CONST OR
MOV [REG2] + CONST, REG1;
 The indexed relative addressing mode works the same as the based
relative addressing mode, except that registers DI and SI hold the
offset address.
 Const is an 8-bit displacement value.
 REG1 can be any general purpose register and REG2 can only be
either of DI or SI
 PA = DS*10H + DI + const ; EA=DI + const or
 PA = DS*10H + SI + const ; EA= SI + const
 Examples:
 MOV DX, [SI]+5 ; PA = DS (shifted left) + SI + 5
 MOV CL, [DI]+20 ; PA = DS (shifted left) + DI + 20
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
11
8086 ADDRESSING MODES (CONT..)
 EXERCISE 3-3: Assume that DS = 4500, SS = 2000, BX = 2100, SI =
1486, DI = 8500, BP = 7814, and AX = 2512. Show the exact physical
memory location where AX is stored in each of the following. All values
are in hex.
a) MOV [BX]+20, AX
b) MOV [SI]+10, AX
c) MOV [DI]+4, AX
d) MOV [BP]+12, AX
 Solution:
 In each case PA = segment register (shifted left) + offset register +
displacement.
(a) DS:BX+20 ;location 47120 = (12) and 47121 =(25)
(b) DS:SI+10 ;location 46496 = (12) and 46497 = (25)
(c) DS:DI+4 ;location 4D504 = (12) and 4D505 = (25)
(d) SS:BP+12 ;location 27826 = (12) and 27827 = (25)
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
12
8086 ADDRESSING MODES (CONT..)
G) BASED INDEXED ADDRESSING MODE:
MOV REG1, [REG2][REG3] + CONST OR
MOV [REG2][REG3] + CONST, REG1;
 In this mode, one base register and one index register are used.
 REG1 can be any general purpose register and REG2 can only be either of
DI or SI and reg3 can only be either of BX or BP.
 PA= DS*10H+BX+DI + const; EA=DI+BX+ const or
PA=SS*10H+BP+SI+const and EA= SI +BP+ const
 EXAMPLES:
 MOV CL, [BX][DI]+8 ;PA = DS (shifted left) + BX + DI + 8
 MOV CH, [BX][SI]+20 ;PA = DS (shifted left) + BX + SI + 20
 MOV AH, [BP][SI]+29 ;PA = SS (shifted left) + BP + SI + 29
 MOV AH, [BP][DI]+29 ;PA = SS (shifted left) + BP + DI + 29
 Note that "MOV AX, [SI][DI]+displacement" is illegal.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
13
8086 ADDRESSING MODES (CONT..)
EXAMPLE: Assume that the registers have the following values (all in
hex) and that CS =1000, DS = 2000, SS = 3000, SI = 4000, DI = 5000,
BX = 6080, BP = 7000, AX = 25FF, CX = 8791, and DX = 1299.
Calculate the physical address of the memory where the operand is
stored and the contents of the memory locations in each of the following
addressing examples.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


14
SUMMARY OF ADDRESSING MODES
 The following table, summarizes the possible offset
registers for various segments.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


15
SUMMARY OF ADDRESSING MODES
 Table 3-2 summarizes sample segment overrides.
 As seen in chapter two, the Intel 8086 allows the program to
override the default segment and use any segment register.
 To do that, specify the segment in the code.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


16
SUMMARY OF ADDRESSING MODES
 .

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


17
INTRODUCTION TO ASSEMBLY LANGUAGE PROGRAMMING
 Program execution in any microprocessor system consists of fetching
binary information from memory and decoding that information to
determine the instruction represented.
 For us it is much easier to remember the mnemonic SUB AX,AX than
the corresponding machine code 29C0H.
 For this reason, we write source files containing all the instruction
mnemonics needed to execute a program.
 The source file is converted into an object file, containing the actual
binary information the machine will understand, by a special program
called an assembler.
 An Assembly language program is a series of statements, or lines
which is, either Assembly language instructions, or Pseudo-
instruction called directives.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
18
INTRODUCTION TO ASSEMBLY LANGUAGE PROGRAMMING
 Directives (pseudo-instructions) give directions to the assembler
about how it should translate the Assembly language instructions into
machine code.
 Assembly language instructions consist of four fields:
[label:] mnemonic [operands] [;comment]
 Brackets indicate that the field is optional, do not type in the brackets.
 The comment field begins with a ";" and may be at the end of a line.
 The assembler ignores comments.
 Comments are optional, but highly recommended to make it easier to
read and understand the program.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


19
DIRECTIVES AND A SAMPLE PROGRAM
 The label field allows the program to refer to a line of code by name.
 The label field can be any character and cannot exceed 31 characters.
 A label must end with a colon when it refers to an opcode and, end
without colon when it refer to directives.
 The mnemonic (instruction) and operand fields together
accomplish the tasks for which the program was written.

 The mnemonic opcodes are ADD and MOV, and "AL, BL" and
"AX, 6764" are the operands.
 Instead of a mnemonic and operand, these fields could contain
assembler pseudo-instructions, or directives.
 Directives do not generate machine code and are used only by the
assembler as opposed to instructions.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
20
DIRECTIVES AND A SAMPLE PROGRAM
 Examples of directives are DB, PROC, END, and ENDP.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


21
MODEL DEFINITION
 After the first two comments is the MODEL directive.
 This directive selects the size of the memory model.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


22
MEMORY MODEL DEFINITION
 Among the options for the memory model are SMALL,MEDIUM,
COMPACT, and LARGE.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


23
SEGMENT DEFINITION
 Every line of an Assembly language program must
correspond to one an x86 CPU segment register.
CS (code segment); DS (data segment).
SS (stack segment); ES (extra segment).
 The simplified segment definition format uses three simple
directives: ".CODE" ".DATA" ".STACK“, which
correspond to the CS, DS, and SS registers.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


24
STACK SEGMENT
 This directive reserves 64 bytes of memory for the stack:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


25
DATA SEGMENT
 The data segment defines three data items: DATA1, DATA2, and
SUM.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


26
DATA SEGMENT
 The DB directive is used by the assembler to allocate memory in
byte-sized chunks.
 Each is defined as DB (define byte).
 Memory can be allocated in different sizes.
 Data items defined in the data segment will be accessed in the
code segment by their labels.
 DATA1 and DATA2 are given initial values in the data section.
 SUM is not given an initial value, but storage is set aside for it.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


27
CODE SEGMENT DEFINITION
 The first line of the segment after the .CODE directive is the PROC
directive.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


28
CODE SEGMENT DEFINITION
 A procedure is a group of instructions designed to accomplish a
specific function.
 A code segment is organized into several small procedures to make
the program more structured.
 Every procedure must have a name defined by the PROC directive,
followed by the assembly language instructions, and closed by the
ENDP directive.
 The PROC and ENDP statements must have the same label.
 The PROC directive may have the option FAR or NEAR.
 The OS requires the entry point to the user program to be a FAR
procedure.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


29
CODE SEGMENT DEFINITION
 Before the OS passes control to the program so it may execute, it
assigns segment registers values.
 When the program begins executing, only CS and SS have the
proper values.
 DS (and ES) values are initialized by the program.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


30
CODE SEGMENT DEFINITION
 The program loads AL & BL with DATA1 & DATA2, ADDs them
together, and stores the result in SUM.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


31
CODE SEGMENT DEFINITION
 The last instructions, "MOV AH, 4CH" & "INT 21H“ return control
to the operating system.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


32
CODE SEGMENT DEFINITION
 The last two lines end the procedure & program, and The label for
ENDP(MAIN) matches the label for PROC.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


33
CODE SEGMENT DEFINITION
 It is handy to keep a sample shell & fill it in with the instructions and
data for your program.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


34
ASSEMBLE, LINK, AND RUN A PROGRAM
 MASM & LINK are the assembler & linker programs.
 Many editors or word processors can be used to create and/or edit the
program, and produce an ASCII file.
 The steps to create an executable Assembly language program are as
follows:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


35
Introduction to Assembly Language Programming

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


36
Introduction to Assembly Language Programming
 The source file must end in ".asm“.
 The ".asm" file is assembled by an assembler, like MASM.
 The assembler will produce an object file and a list file, along
with other files useful to the programmer.
 The ".lst" file, which is optional, is very useful to the
programmer because it lists all the opcodes and offset addresses
as well as errors that MASM detected.
 MASM assumes that the list file is not wanted (NUL.LST
indicates no list).
 The extension for the object file must be ".obj".
 Before feeding the ".obj" file into LINK, all syntax errors
must be corrected.
 This object file is input to the LINK program, to produce the
executable program that ends in ".exe".
 The ".exe" file can be run (executed) by the microprocessor.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
37
ASSEMBLE, LINK, AND RUN A PROGRAM
 The following figure shows how an executable program is created & run by
following the steps outlined above.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


38
ASSEMBLE, LINK, AND RUN A PROGRAM
 The following figure shows how an executable program is created & run by
following the steps outlined above.
 Cont……

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


39
ASSEMBLE, LINK, AND RUN A PROGRAM
 MASM produces another optional file, the crossreference, which has the
extension ".crf".
 An alphabetical list of all symbols & labels in the program.
 Also program line numbers in which they are referenced.
 The assembler (MASM) creates the opcodes, operands & offset addresses
under the ".obj" file.
 The LINK program produces the ready-to-run program with the ".exe"
(Executable) extension.
 The LINK program sets up the file so it can be loaded
by the OS and executed.
 The program can be run at the OS level, using the following command:
C>myfile
 When the program name is typed in at the OS level, the OS loads the
program in memory.
 Referred to as mapping, which means that the program is mapped into
the physical memory of the PC.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
40
DATA TYPES AND DATA DEFINITION
 The 8088/86 processor supports many data types.
 Data types can be 8- or 16-bit, positive or negative.
 A number less than 8 bits wide must be coded as an 8-bit register
with the higher digits as zero.
 A number is less than 16 bits wide must use all 16 bits.
 ORG is used to indicate the beginning of the offset address.
 The number after ORG can be either in hex or in decimal.
 If the number is not followed by H, it is decimal and the
assembler will convert it to hex.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


41
DATA TYPES AND DATA DEFINITION
 DB Define Byte:
 One of the most widely used data directives, it allows allocation
of memory in byte-sized chunks.
 This is the smallest allocation unit permitted.
 DB can define numbers in decimal, binary, hex, & ASCII.
• D after the decimal number is optional.
• B (binary) and H (hexadecimal) is required.
• To indicate ASCII, place the string in single quotation marks.
 DB is the only directive that can be used to define ASCII strings
larger than two characters.
 It should be used for all ASCII data definitions.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


42
DATA TYPES AND DATA DEFINITION
 Some examples:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


43
DATA TYPES AND DATA DEFINITION
 List file for DB examples

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


44
DATA TYPES AND DATA DEFINITION
 DUP duplicate:
 DUP will duplicate a given number of characters.
 Two methods of filling six memory locations with FFH.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


45
DATA TYPES AND DATA DEFINITION
 List file of DUP example:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


46
DATA TYPES AND DATA DEFINITION
 DW Define Word:
 DW is used to allocate memory 2 bytes (one word) at a time:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


47
DATA TYPES AND DATA DEFINITION
 DW Define Word:
 List file for DW examples.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


48
DATA TYPES AND DATA DEFINITION
 EQU equate: EQU associates a constant value with a data label.
 When the label appears in the program, its constant value will be
substituted for the label.
 When EQU is used for the counter constant: COUNT EQU 25,
and when executing the instructions "MOV CX, COUNT", the
register CX will be loaded with the value 25, it will be in the
immediate addressing mode.
 In contrast to using DB: COUNT DB 25, and when executing the
same instruction "MOV CX, COUNT" it will be in the direct
addressing mode.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


49
DATA TYPES AND DATA DEFINITION
 EQU can also be used in the data segment:

 Assume a constant (a fixed value) used in many different places


in the data and code segments.
 By use of EQU, one can change it once and the assembler will
change all of them.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


50
DATA TYPES AND DATA DEFINITION
 DD define doubleword:
 The DD directive is used to allocate memory locations that are 4
bytes (two words) in size.
 Data is converted to hex & placed in memory locations
 Low byte to low address and high byte to high address.

 List file for DD examples.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


51
DATA TYPES AND DATA DEFINITION
 DQ define quadword:
 DQ is used to allocate memory 8 bytes (four words) in size, to
represent any variable up to 64 bits wide:

 List file for DQ examples.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


52
DATA TYPES AND DATA DEFINITION
 DT define ten bytes:
 DT is used for memory allocation of packed BCD numbers.
 This directive allocates 10 bytes.
 The "H" after the data is not needed.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


53
DATA TYPES AND DATA DEFINITION
 DT define ten bytes:
 List file for DT examples.

 DT can also be used to allocate 10-byte integers by using the "D"


option:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


54
FULL SEGMENT DEFINITION
 SEGMENT DEFINITION:
 The SEGMENT and ENDS directives indicate the beginning &
ending of a segment, in this format:

 The label, or name, must follow naming conventions and be


unique.
 The [options] field gives important information to the assembler
for organizing the segment, but is not required.
 The ENDS label must be the same label as in the SEGMENT
directive.
 In full segment definition, the ".MODEL" directive is not used.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
55
FULL SEGMENT DEFINITION
 The directives ".STACK", ".DATA", and ".CODE" are replaced by SEGMENT and
ENDS directives that surround each segment.
 The following Figure shows the full segment definition and simplified format, side
by side, and followed by programs.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


56
EXAMPLE OF FULL SEGMENT DEFINITION

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


57
EXAMPLE OF FULL SEGMENT DEFINITION

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


58
FULL SEGMENT DEFINITION
 Stack Segment Definition:
 The stack segment shown contains the line "DB 64 DUP (?)" to reserve 64
bytes of memory for the stack.
 The following three lines in full segment definition are comparable to
".STACK 64" in simple definition:

 Data Segment Definition


 In full segment definition, the SEGMENT directive names the data segment
and must appear before the data.
 The ENDS segment marks the end of the data segment:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


59
FULL SEGMENT DEFINITION
 Code Segment Definition:
 The code segment also begins and ends with SEGMENT and ENDS
directives:

 Immediately after PROC, the ASSUME directive, associates segments with specific
registers.
 By assuming the segment register is equal to the segment labels used in the
program.
 If an extra segment had been used, ES would also be included in the ASSUME
statement.
 ASSUME tells the assembler which of the segments, defined by SEGMENT,
should be used.
 Also helps the assembler to calculate the offset addresses from the beginning
of that segment.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
60

FULL SEGMENT DEFINITION
 In "MOV AL, [BX] " the BX register is the offset of the data segment.
 On transfer of control from OS to the program, of the three segment
registers, only CS and SS have the proper values.
 The DS value (and ES) must be initialized by the program

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


61
MORE SAMPLE PROGRAMS
 Example1: Write, run, and analyze a program that adds 5 bytes of
data and saves the result. The data should be the following hex
numbers: 25, 12, 15, 1F, and 2B.
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


62
MORE SAMPLE PROGRAMS
 Example2:- Write and run a program that adds four words of data and
saves the result. The values will be 234DH, 1DE6H, 3BC7H, and
566AH. Use DEBUG to verify the sum is D364.

 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


63
MORE SAMPLE PROGRAMS
 Example3:- Write and run a program that transfers 6 bytes of data
from memory locations with offset of 0010H to memory locations
with offset of 0028H.
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


64
DEBUG program instruction set
 DEBUG is a program included in the MS-DOS and PC-DOS
operating systems that allows the programmer to monitor a program’s
execution closely for debugging purposes.
 Specifically, it can be used:
 To examine and alter the contents of memory,
 To enter and run programs, and
 To stop programs at certain points in order to check or even
change data.
 You will learn:
 How to enter and exit DEBUG,
 How to enter, run, and debug programs,
 How to examine and alter the contents of registers and memory.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


65
DEBUG program instruction set
 Debug instructions
 List of commands
 A: Assemble [address] you can type in code this way
 D: [range] ; DUMP
 E: address [list] ;
 G: Go [=address] addresses runs the program
 R: Show & change registers Appears to show the same thing as
T, but doesn't cause any code to be executed.
 T: start address Trace either from the starting address or current
location.
 U: start address UnAssemble

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


66
DEBUG program instruction set
1. Entering and exiting debug:
 To enter the DEBUG program, simply type its name :
A>DEBUG filename <enter>
 To exit the DEBUG program, the quit command, Q, may be
typed: -Q <enter>
2. R The register command:
 Allows you to examine and/or alter the contents of the internal
registers of the CPU.
 -R will display all registers
 -R <register name > only the register named will be display.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


67
Example : Using the R Command to Display/Modify Register

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


68
DEBUG program instruction set
3. A, the assemble command:
 The assemble command is used to enter Assembly language instructions into
memory.
 Syntax: A <starting address>
 The starting address may be given as an offset number, in which case it is
assumed to be an offset into the code segment, or the segment register can be
specified explicitly.
 In other words, “-A 0100" and “-A CS:0100" will achieve the same results.
 When this command is entered at the command prompt DEBUG will begin
prompting you to enter Assembly language instructions.
 After an instruction is typed in and followed by <enter>, DEBUG will prompt
for the next instruction. This process is repeated until you type a <enter>.
 be aware that one important difference between DEBUG programming and
Assembly language programming is that DEBUG assumes that all numbers
are in hex, whereas most
assemblers assume that numbers are in decimal unless they are followed by
"H“.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


69
DEBUG program instruction set
4. U, the unassemble command: looking at machine code.
 The unassembled command displays the machine code in
memory along with their equivalent Assembly language
instructions.
 The command can be given in either format shown below.
-U <starting address > <ending address>
-U <starting address > < L number of bytes>
 The assemble instruction takes Assembly language instructions
from the keyboard and converts them to machine code, which it
stores in memory,
 The unassemble instruction does the opposite. Unassemble
takes machine code stored in memory and converts it back to
Assembly language instructions to be displayed on the monitor.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


70
DEBUG program instruction set
4. G, the go command:
 The go command instructs DEBUG to execute the instructions
found between the two given addresses.
 Its format is:
G < = starting address> <stop address>
 If no addresses are given, DEBUG begins executing instructions
at CS;IP until a breakpoint is reached.
 Before the instructions were executed, the R command is used to
check the values of the registers.
 Since CS:IP pointed to the first instruction, the G command was
entered,

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


71
Example: Assemble, Unassemble, and Go Commands

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


72
Example: Various Forms of the Go Command

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


73
Example: Various Forms of the Go Command

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


74
DEBUG program instruction set
5. T, the trace command: a powerful debugging tool
 The trace command allows you to trace through the execution of your
programs one or more instructions at a time to verify the effect of the
programs on registers and/or data.
 Syntax: T <= starting address> <number of instructions>
 This tells DEBUG to begin executing instructions at the starting address.
 DEBUG will execute however many instructions have been requested in
the second field. The default value is 1 if no second field is given.
 The trace command functions similarly to the go command in that if no
starting address is specified, it starts at CS:IP.
 The difference between this command and the go command is that trace
will display the register contents after each instruction, whereas the go
command does not display them until after termination of the program.
 Another difference is that the last field of the go command is the stop
address, whereas the last field of the trace command is the number of
instructions to execute.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


75
Example : Trace Command

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


76
Example: Moving Data into 8- and 16-bit Registers

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


77
Example : Assembling and Unassembling a Program

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


78
DEBUG INSTRUCTION SET
6. F, the fill command: filling memory with data
 The fill command is used to fill an area of memory with a data item.
 The syntax of the F command is as follows:
F <starting address > <ending address> <data>
F <starting address > < L number of bytes > <data>
 This command is useful for filling a block of memory with data, for
example to initialize an area of memory with zeros.
 Normally, you will want to use this command to fill areas of the data
segment, in which case the starting and ending addresses would be
offset addresses into the data segment.
 To fill another segment, the register should precede the offset.
 For example, the first command below would fill 16 bytes, from
DS:100 to DS:10F with FF.
 The second command would fill a 256-byte block of the code segment,
from CS:100 to CS:IFF with ASCII 20 (space).

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


79
Introduction to Assembly Language Programming
7. D, the dump command: examining the contents of memory
 The dump command is used to examine the contents of memory.
 The syntax of the D command is as follows:
D <start address > <end address>
D <start address > < L number of bytes>
 The D command can be entered with a starting and ending address, in
which case it will display all the bytes between those locations.
 It can also be entered with a starting address and a number of bytes (in
hex), in which case it will display from the starting address for that
number of bytes.
 If the address is an offset, DS is assumed.
 The D command can also be entered by itself, in which case DEBUG
will display 128 consecutive bytes beginning at DS:100.
 The next time "D" is entered by itself, DEBUG will display 128 bytes
beginning at wherever the last display command left off.
 In this way, one can easily look through a large area of memory,
128 bytes at a time.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


80
Example: Filling and Dumping a Block of Memory

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


81
Example: Using the Dump Command to Examine Machine Code

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


82
DEBUG INSTRUCTION SET
8. E, the enter command: entering data into memory.
 The fill command was used to fill a block with the same data item.
 The enter command can be used to enter a list of data into a certain portion of
memory.
 The syntax of the E command is as follows:
E <address > <data list>
E <address>
 The following example showed how to enter ASCII data, which can be enclosed in
either single or double quotes.
 The E command has another powerful feature: the ability to examine and alter
memory byte by byte.
 If the E command is entered with a specific address and no data list, DEBUG
assumes that you wish to examine that byte of memory and possibly alter it.
 After that byte is displayed, you have four options:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


83
Example : Using the E Command to Enter Data into Memory

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


84
EXAMINING THE STACK IN DEBUG
 PUSHING ONTO THE STACK
 First the assemble command is used to enter instructions that load three
registers with 16-bit data, initialize the stack pointer to 1236H, and push the
three registers onto the stack.
 Then the instructions are executed with the go command and the contents of
the stack examined with the dump command.
 Notice that the stack grows "upward" from higher memory locations toward
lower memory locations.
 After each push, the stack pointer is decremented by 2.
 POPPING THE STACK
 The following Example demonstrates the effect of pop instructions on the
stack.
 The trace shows that after each pop is executed, the stack pointer SP is
incremented by 2.
 As the stack is popped, it shrinks "downward" toward the higher memory
addresses.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


85
Example : Pushing Onto the Stack

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


86
Example : Popping the Stack Contents into Registers

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


87
INSTRUCTION SET OF 8086
 The instruction set of the 8086 microprocessor is divided into seven
different groups:
A. Data transfer instruction
B. Strings instruction
C. Loops and jumps instruction
D. Arithmetic instruction
E. Bit manipulation instruction
F. Subroutine and interrupt instruction
G. Processor control instruction
 The instructional groups are organized in such a way that the more
commonly used instructions are presented first, followed by less
frequently used instructions.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
88
DATA TRANSFER INSTRUCTIONS:
 This group of instructions makes it possible to move (copy) data
around inside the processor and between the processor and its
memory.
A. MOV DESTINATION, SOURCE:
 Transfer can be from register to register, register to memory or from
memory to register but not from memory to memory.
 The source and destination must be of same type i.e. either both must
be byte or word.
 In this instruction, the assembler will look at the size of the specified
register in the operand field to determine if the immediate data is a 1-,
or 2-byte number.
 MOV instruction does not affect any flags.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


89
DATA TRANSFER INSTRUCTIONS:
 EXAMPLE: MOV AL, 30H
MOV AX, 30H
 In the first instruction, the 30H is coded as a byte value because it is
being MOVed into AL.
 In the second instruction, the 30H is coded as a word value because it
is being MOVed into AX.
 This is clearly shown by the resulting code for both instructions.
 MOV AL, 30H is coded as B0 30 and MOV AX, 30H is coded as B8
30 00.
 Note that the second two bytes represent the byte-swapped value
0030H.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


90
Cont..
.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


91
Cont.…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


92
DATA TRANSFER INSTRUCTIONS:
 Some times, For example, in MOV [SI], 0 the processor does not
know if the operand should be coded as a byte value, or as word
value.
 For cases like this, we use BYTE PTR and WORD PTR directives to
indicate the size of data.
 If you wish to MOV a byte value into memory, use:
 MOV BYTE PTR [SI], 0
 MOV WORD PTR [SI], 0
 The byte ptr, and word ptr assembler directives stand for "byte
pointer," and "word pointer."

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


93
DATA TRANSFER INSTRUCTIONS(cont..)
PUSH and POP instructions
 The stack is a collection of memory locations pointed to by the stack
pointer register and the stack segment register(SS:SP).
 PUSH and POP instructions are used to load to or receive data from
the stack memory.
 Storing a CPU register in the stack is called a push.
 Loading the contents of the stack into the CPU register is called a
pop.
 The SP points at the current memory location used as the top of the
stack.
 No flags are affected by this instruction(PUSH and POP).

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


94
DATA TRANSFER INSTRUCTIONS(cont..)
PUSH SOURCE
 When we wish to write data into the stack area, we use the PUSH
instruction.
 The source of the word can be a general-purpose register, a
segment register, or memory.
 As data is pushed onto the stack it is decremented by 2.
 As data is popped off the stack into the CPU, it is incremented by 2.
 When an instruction pushes or pops a general purpose register, it
must be the entire 16-bit register.
 One must code "PUSH AX".
 There are no instructions such as "PUSH AL" or "PUSH AH".
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
95
DATA TRANSFER INSTRUCTIONS(cont..)
 As each PUSH is executed, the register contents are saved on the
stack and SP is decremented by 2.
 EXAMPLES:
PUSH BX ; Decrement SP by 2, copy BX to stack
PUSH DS ; Decrement SP by 2, copy DS to stack
PUSH table [BX] ; Decrement SP by 2, copy word from
; memory in DS at EA = table + [BX]
; to stack
PUSH AL ; Illegal, must push a word

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


96
DATA TRANSFER INSTRUCTIONS(cont..)
 EXAMPLES:- The stack segment register has been loaded with
4000H and the stack pointer register with FFFFH. If register CX
contains 1234H and AX contains 4455H, what is the result of :
PUSH AX
PUSH CX
 Solution:
 The stack pointer points to a location referred to as the top of the
stack.
 Whenever we push an item onto the stack, the SP is decremented
by 2.
 This is necessary because all pushes involve 2 bytes of data
 Figure 3-3 shows the new contents of memory after PUSH AX
and PUSH CX have executed.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
97
Instruction Set of 8086

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


98
DATA TRANSFER INSTRUCTIONS(cont..)
 The data contained in memory locations 4FFFEH and 4FFFDH is
replaced by the contents of register AX and data contained in memory
locations 4FFFCH and 4FFFBH is replaced by the contents of register
CX.
 Notice that the new stack pointer value is 4FFFBH.
 Remember that the stack builds toward 0.
 Also notice that the contents of registers CX and AX remain
unchanged.
 When the SP register is pushed, the value written to the stack is
the value of SP before the push.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


99
DATA TRANSFER INSTRUCTIONS(cont..)
 PUSHA ;
 Save all 16-bit registers onto the stack in the following order:
AX, CX, DX, BX, SP, BP, SI, DI.
 The value of the SP is that before the PUSHA instruction.

 PUSHF; copies the contents of the flag register to the stack.


Hu, Institute Of Technology Department Of Electrical And Computer Engineering
100
DATA TRANSFER INSTRUCTIONS(cont..)
POP DESTINATION:
 The POP instruction is used to perform the reverse of a PUSH.
 Copies a word from the stack location pointed to by the stack pointer
to a destination specified in the Instruction.
 The stack pointer is used to read 2 bytes of data and copy them into
the location specified in the operand field.
 The destination can be :
 A general-purpose register,
 A segment register except CS and IP
 A memory location.
 The data in the stack is not changed.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


101
DATA TRANSFER INSTRUCTIONS(cont..)
 After the word is copied to the specified destination, the stack pointer
is automatically incremented by 2 to point to the next word on the
stack.
 No flags are affected by the POP instruction.
EXAMPLES:
POP DX ; Copy a word from top of stack to DX
; Increment SP by 2
POP DS ; Copy a word from top of stack to DS
; Increment SP by 2
POP TABLE [BX] ; Copy a word from top of
; stack to memory in
;DS:TABLE +BX and DS:TABLE + BX + 1
 NOTE: POP CS Is illegal.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
102
DATA TRANSFER INSTRUCTIONS(cont..)
 EXAMPLES: Assume the contents of the stack segment register and
the stack pointer are 4000H and FFFBH, respectively. What is the
result of POP DX followed by POP BX? USE FIGURE 3.4 NEXT
PAGE.
 SOLUTION:
 The contents of location 4FFFBH (34H) are copied into the lower
byte of DX, and the contents of location 4FFFCH (12H) are
copied into the upper half of DX.
 Similarly, the contents of location 4FFFDH (55H) are copied into
the lower byte of BX, and the contents of location 4FFFEH (44H)
are copied into the upper half of BX.
 The stack pointer is then incremented a second time and points to
4FFFFH.
 Fig. 3-4 shows a snap shot of memory contents in the stack area.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
103
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


104
DATA TRANSFER INSTRUCTIONS(cont..)
POPA Destination (Pop All Registers).
 All general purpose registers are popped from the stack in the order
indicated in Table 3-6.
 Note that the contents of the SP are not loaded with the data popped
off the stack.
 This is necessary to prevent the stack from changing locations
halfway through the execution.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


105
DATA TRANSFER INSTRUCTIONS(cont..)
Overflow and Underflow of Stack:
 PUSH instruction decrements SP by 2.
 At some point, if SP=0000H and if there is an attempt to PUSH
data on the stack, Stack overflow will result.
 On the other hand, POP instruction increments SP by 2.
 At some point, if SP=FFFFH and if there is an attempt to POP
data from the stack, Stack Underflow will result.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


106
DATA TRANSFER INSTRUCTIONS(cont..)
IN ACCUMULATOR, PORT
 Input byte or word from port to accumulator register(AL or AX).
 Data read from an input port always ends up in the accumulator.
 IN: COPY DATA FROM A PORT TO ACCUMULATOR.
 The input port is actually a hardware device connected to the
processor's data bus.
 When executing the IN instruction, the processor will output the
address of the input port on the address bus.
 The selected input port will then place its data onto the data bus to be
read by the processor.
 The processor allows two different forms of the IN instruction:
 Direct and Indirect.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


107
DATA TRANSFER INSTRUCTIONS(cont..)
 Direct: If the port number is between 00 and FFH, we would use:
 IN AL,80H or
 IN AX,80H.
 Using AL in the operand field causes 8 bits of data to be read.
 Two bytes can be input by using AX in the operand field.
 Example:
 IN AL, 0F8H ; Copy a byte from port 0F8H to AL
 IN AX, 95H ; Copy a word from port 95H to AX.
 Example: What is the result of IN AL, 80H if the data at input port
80H is 22H?
 Solution: The byte value 22H is copied into register AL(AL = 22H)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


108
DATA TRANSFER INSTRUCTIONS(cont..)
 Indirect: If a full 16-bit port address must be specified, the port
address is loaded(MOVED) into register DX, and
 IN AL, DX ; To copy a byte from 8-bits port DX to AL.
 IN AX, DX ; To copy a Word from 16-bits port DX to Ax.
 Example:
 MOV DX, 30F8H ; Load 16-bit address of the port in DX.
 IN AL, DX ; Copy a byte from 8bit port 30F8H to AL.
 IN AX, DX ; Copy a word from 16-bit port 30F8H to AX.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


109
DATA TRANSFER INSTRUCTIONS(cont..)
OUT PORT, ACCUMULATOR
 Output byte or word from accumulator to port.
 If the output port is 16-bit, then this port address loaded to DX and ,
 OUT DX, AL or OUT DX, AX
 Example:
 MOV DX, 30F8H ; Load 16-bit address of the port in DX.
 OUT DX, AL ; Copy the contents of AL to pert 30F8H
 OUT DX, AX ; Copy the contents of AX to port 30F8H.
 When the port address is in range of 00H to FFH, then:
 OUT 80H, AL or OUT 80H, AX.
 Examples:
 OUT 0F8H, AL ; Copy content of AL to 8 bit port 0F8H.
 OUT 0F8H, AX ; Copy contents of AX to 16-bit port 0F8H.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


110
DATA TRANSFER INSTRUCTIONS(cont..)
 Example: What happens during execution of OUT DX, AL if AL
contains 7CH and DX contains 3000H?
 Solution:
 The port address stored in register DX is output on the address
bus, along with the 7C from AL on the data bus.
 The output port circuitry must recognize address 3000H and store
the data.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


111
DATA TRANSFER INSTRUCTIONS(cont..)
LEA DESTINATION, SOURCE (Load effective address):
 This instruction is used to load the offset of the source memory
operand into one of the processor's registers.
 The memory operand may be specified by any number of addressing
modes. The destination may not be a segment register.
 Determines the offset of the variable or memory location named as
the source and loads this address in the specified 16-bit register.
 Flags are not affected by LEA instruction.
 Example:
 LEA CX, TOTAL ; Load CX with offset of TOTAL in DS.
 LEA AX, [BX] [DI] ; Load AX with EA = BX + DI
 LEA DX, 10 [SI] ; LOAD DX with EA = SI + 10
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
112
DATA TRANSFER INSTRUCTIONS(cont..)
 Example: What is the difference between:
 MOV AX, [40H] and LEA AX, [40H]?
 Solution:
 In MOV AX, [40H] ; Places 2 bytes of data from locations 40H
; and 41H into register AX.
 In LEA AX, [40H] ; Places 40H into register AX.
 Example: What does LEA AX, [SI] do?
 Solution: The value of SI at execution time is loaded into AX.
 Example:
 MOV BX, 35H
 MOV DI, 12H
 LEA SI, [BX+DI] ; SI = 35H + 12H = 47H
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
113
DATA TRANSFER INSTRUCTIONS(cont..)
XCHG DESTINATION, SOURCE (Exchange data):
 Used to swap the contents of two 8-, or 16-bit operands.
 One operand must be a processor register (excluding the segment
registers).
 The other operand may be a register or a memory location.
 If a memory location is used as an operand it is assumed to be within
a data segment.
 Example: Registers AL and BL contain 30H and 40H, respectively.
What is the result of XCHG AL, BL?
 Solution: After execution,
 AL = BL = 40H and
 BL =AL = 30H.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
114
DATA TRANSFER INSTRUCTIONS(cont..)
Reading Assignment:
 XLAT
 LDS
 LES
 LAHF and
 SAHF

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


115
LOOPS AND JUMPS INSTRUCTION
 When there is a need to change the path of program execution by
forcing the processor to fetch its next instruction from a new location,
we can use Jump instructions.
 When there is a need to execute some portion of the program more
than one times we can use loop instructions.
 A jump alters the contents of the processor's instruction pointer.
 Remember that the CS and IP registers are combined to determine the
address of the next instruction fetch.
 Jump instructions are classified as
 Unconditional Jump (JMP)
 Conditional Jump (J cond)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


116
UNCONDITIONAL JUMPS
JMP Target
 Unconditional jump to Specified Destination(Target)
 This instruction will always cause the 8086 to fetch its next
instruction from the location specified in the instruction without any
precondition.
 If control is transferred to a memory location within the current code
segment, it is NEAR jump.
 This is sometimes called intra-segment jump (within segment).
 If control is transferred outside the current code segment, it is a FAR
or intersegment jump(between segments).
 Since the CS:IP registers always point to the address of the next
instruction to be executed, they must be updated when a control
transfer instruction is executed.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
117
UNCONDITIONAL JUMPS INSTRUCTIONS
 The Unconditional Jump Can Take The Following Forms:
A. SHORT JUMP:
 Which is specified by: "JMP SHORT label".
 This is a jump in which the address of the target location is within
-128 to +127 bytes of memory relative to the address of the
current IP.
B. FAR JUMP:
 Which has the format "JMP FAR PTR label".
 This is a jump out of the current code segment, meaning that not
only the IP but also the CS is replaced with new values.
C. NEAR JUMP:
 Which is the default, has the format "JMP label".
 This is a near jump (within the current code segment)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


118
UNCONDITIONAL JUMPS INSTRUCTIONS
 The target address can be any of the addressing modes of direct,
register, register indirect, or memory indirect:
 Direct JUMP:- the target address can be anywhere in the segment
within the range +32767 to -32768
 For example, in “JMP AGAIN”
 Register indirect JUMP; the target address is in a register.
 For example, in "JMP BX”, IP = BX.
 Memory indirect JMP; the target address is the contents of two
memory locations pointed at by the register.
 For Example: "JMP [DI]" will replace the IP with the contents
of memory locations pointed at by DI and DI + 1.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


119
CONDITIONAL JUMP INSTRUCTIONS
 Conditional jumps are always short jumps in the 8086.
 These instructions will cause a jump to a label given in the instruction
if the desired conditions occurs in the program.
 If the jump is not taken (jump condition is not fulfilled), execution
simply go to the next instruction.
 In the conditional jump, control is transferred to a new location if a
certain condition is met.
 The flag register is the one that indicates the current condition.
 For example, with "JNZ label", the processor looks at the zero flag.
 If ZF = 0, it will jump to label to fetch the instruction.
 If ZF = 1, it will not jump but will execute the next instruction
below the JNZ.
 See the summery of conditional jump instruction.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
120
8086 CONDITIONAL JUMP INSTRUCTIONS

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


121
CONDITIONAL JUMP INSTRUCTIONS
 When signed numbers are compared, use the JG, JL, JGE,
JLE, JE, and JNE instructions.
 The terms greater than and less than refer to signed
numbers.
 When unsigned numbers are compared, use the JA, JB,
JAB, JBE, JE, and JNE instructions.
 The terms above and below refer to unsigned numbers.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


122
CONDITIONAL JUMP INSTRUCTIONS
 EXAMPLE: Write an assembly
language program that adds two numbers
from memory in data segment at offsets of
1100H and 1101H and stores the result at an
offset of (1102H if it is positive, 1103H if it
is negative and 1104H if it is zero.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


123
LOOP INSTRUCTIONS
LOOP Short-Label (Loop).
 This instruction is used to repeat a series of instruction for specified
number of times.
 The number is specified in the CX register.
 The CX register is automatically decremented by one, each time after
execution of LOOP instruction.
 Until CX = 0, execution will jump to a destination specified by a label
in the instructions.
 Example: write a program to add five words and save the result in
SUM memory location, using LOOP instruction.
 Solution: see the following program.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


124
LOOP INSTRUCTIONS
.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


125
LOOPE/LOOPZ
LOOPE/LOOPZ Short-Label (Loop if Equal, Loop if Zero).
 This instruction is similar to LOOP except for a secondary
condition that must be met for the jump to take place.
 In addition to decrementing CX, LOOPZ also examines the state
of the zero flag.
 If the zero flag is set and CX does not equal 0, LOOPZ will
jump to the target.
 If CX equals 0, or if the zero flag gets cleared within the loop,
the loop will terminate.
 As always, when a conditional instruction does not have the
correct flag condition, execution continues with the next
instruction.
 LOOPE is an alternate name for this instruction.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
126
LOOPE/LOOPZ
LOOPNE/LOOPNZ Short-Label (Loop if not Equal, Loop if
not Zero).
 LOOPNZ is the opposite of LOOPZ.
 The zero flag must be cleared to allow further looping.
 LOOPNE has the same function.
 For LOOPE/LOOPZ and LOOPNE/LOOPNZ instructions there
is one more condition for exit from loop, which is given below.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


127
Example of conditional, unconditional and loop.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


128
STRING INSTRUCTIONS
 A particularly nice feature of the 8086 is its ability to handle strings.
 A string is a collection of bytes, or words that can be up to 64KB in
length.
 An example of a string might be a sequence of ASCII character codes
that constitute a password, or the ASCII codes for “Good Morning!.“
 They are capable of performing operations on a series of operands
located in consecutive memory locations.
 For example, while the CMP instruction can compare only 2 bytes (or
words) of data, the CMPS (compare string) instruction is capable of
comparing two arrays of data located in memory locations pointed at
by the SI and DI registers.
 The common operations that we can perform on any string are
copying, comparing and scanning.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


129
Use of SI and DI, DS and ES in string Instructions
 In 8088/86 microprocessors, the SI and DI registers always point to
the source and destination operands, respectively.
 Now the question is:
 Which segments are they combined with to generate the 20-bit
physical address?
 To generate the physical address, the 8088/86 always uses SI as the
offset of the DS (data segment) register and DI as the offset of ES
(extra segment).
 That means, the source is resides in DS:SI and destination is reside in
ES:DI memory location.
 This is the default mode.
 It must be noted that the ES register must be initialized for the string
operation to work.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


130
Byte and word operands in string instructions
 In each of the string instructions, the operand can be a byte or a word.
 They are distinguished by the letters B (byte) and W (word) in the
instruction mnemonic.
 Table below provides a summary of all the string instructions.
 Each one will be discussed separately in the context of examples.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


131
SUMMERY OF STRING OPERATION TABLE

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


132
DF, the direction flag
 To process operands located in consecutive memory locations requires
that the pointer be incremented or decremented.
 In string operations this is achieved by the direction flag (DF).
 It is the job of the programmer to specify the choice of increment or
decrement by setting the direction flag to high or low.
 The are two instruction to set the direction flags.
 CLD (clear direction flag), DF = 0. and
 STD (set direction flag), DF = 1,
 If DF = 0, indicating that the string instruction should increment the
pointers(SI and DI) automatically.
 If DF = 1, indicating that the string instruction should decrement the
pointers(SI and DI) automatically.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
133
REP prefix
 The REP (repeat) prefix allows a string instruction to perform the
operation repeatedly.
 Now the question is:
 How many times is it repeated?
 REP assumes that CX holds the number of times that the instruction
should be repeated.
 In other words, the REP prefix tells the CPU to perform the string
operation and then decrements the CX register automatically.
 This process is repeated until CX becomes zero.
 To understand some of the concepts discussed so far, look at Example
below.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


134
REP prefix
 Example: Using string instructions, write a program that transfers a
block of 19 bytes of data from DATA1 to DATA2.
 Assume data1 = ‘HAWASSA UNIVERSITY!’
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


135
Cont…
 In above Example 6, after the transfer of every byte by the MOVSB
instruction, both the SI and DI registers are incremented automatically
once only (notice CLD).
 The REP prefix causes the CX counter to be decremented and
MOVSB is repeated until CX becomes zero.
 Notice in Example above that both DS and ES are set to the same
value.
 Example :- What instructions are necessary to make a copy of the
SHOPPER string? The index registers should auto-increment during
the string operation. Write a program.
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


136
STOS and LODS instructions
 The STOSB instruction stores the byte in the AL register into
memory locations pointed at by ES:DI and increments DI once(if
DF=0). If DF =1, then DI is decremented.
 The STOSW instruction stores the contents of AX in memory
locations ES:DI and ES:DI+1 (AL into ES:DI and AH into ES:DI+1),
then increments DI twice (if DF = 0). If DF = 1, DI is decremented
twice.
 The LODSB instruction loads the contents of memory locations
pointed at by DS:SI into AL and increments (or decrements) SI once
if DF = 0 (or DF = 1).
 LODSW loads the contents of memory locations pointed at by DS:SI
into AL and DS:SI+1 into AH. The SI is incremented twice if DF = 0.
Otherwise, it is decremented twice.
 LODS is never used with a REP prefix.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
137
STOS and LODS instructions
 EXAMPLE: Write a program that:
 Uses STOSB to store byte AAH into l00 memory locations.
 Uses LODS to test the contents of each location to see if AAH
was there.
 If the test fails, the system should display the message “bad
memory”.
 If the test fails, the system should display the message “Good
memory”.
 Solution: see the following
 In the data code segment, the expression could be:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


138
STOS and LODS instructions
.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


139
STOS and LODS instructions
 In the program in Example above, first AAH is written into 100
locations by using word-sized operand AAAAH and a count of 50.
 In the test part, LODS brings in the contents of memory locations into
AL one by one, and each time it is exclusive-ORed with AAH (the
AH register has the hex value of AA).
 If they are the same, ZF = 1 and the process is continued,
 Otherwise, the pattern written there by the previous routine is not
there and the program will exit.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


140
The REPZ and REPNZ prefixes
 These prefixes can be used with the CMPS and SCAS instructions for
testing purposes.
 They are explained below.
 REPZ (repeat zero), which is the same as REPE (repeat
equal), will repeat the string operation as long as the source and
destination operands are equal (ZF = 1) or until CX becomes
zero.
 REPNZ (repeat not zero), which is the same as REPNE (repeat
not equal), will repeat the string operation as long as the source
and destination operands are not equal (ZF = 0) or until CX
become zero.
 These two prefixes will be used in the context of applications
after the explanation of the CMPS and SCANS instructions.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


141
CMPS (compare string)
 CMPS (compare string) allows the comparison of two arrays of data
pointed at by the SI and DI registers.
 One can test for the equality or inequality of data by use of the REPE
or REPNE prefixes, respectively.
 The comparison can be performed a byte at a time or a word at time
by using CMPSB or CMPSW.
 For example, if comparing “Euorop” and “Europe” for equality, the
comparison will continue using the REPE CMPS as long as the two
arrays are the same.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


142
CMPS (compare string)
 Example:
 Assuming that there is a spelling of “Europe” in an electronic
dictionary and a user types in “Euorope”, write a program that
compares these two and displays the following message,
depending on the result:
1. If they are equal, display “The spelling is correct”.
2. If they are not equal, display “Wrong spelling”.
 Solution: see the following program.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


143
CMPS (compare string)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


144
SCAS (scan string)
 The SCASB string instruction compares each byte of the array pointed
at by ES:DI with the contents of the AL register, and depending on
which prefix of REPE or REPNE is used, a decision is made for
equality or inequality.
 For example, in the array “Mr. Gones”, one can scan for the letter
“G” by loading the AL register with the character “G” and then using
the “REPNE SCASB” operation to look for that letter.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


145
SCAS (scan string)
 EXAMPLE:- Write a program to scan for the letter ‘N’, in
‘SHOPPING’ strings.
 If the letter is found, display on screen ‘THE LETTER IS
FOUND! ’
 If the letter is not found, , display on screen ‘THE LETTER IS
NOT FOUND! ’
 Solution :
 Note that, for SCAS instruction, destination is, ES:DI, while
source is, AL or AX.
 So that, the string must be in destination of DE:DI

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


146
SCAS (scan string)
.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


147
USING STRING INSTRUCTION
 Example:- Assuming that there is a spelling of “teff" in an electronic
dictionary and a user types in “teaf", write a program that compares
these two and displays the following message, depending on the
result:
 1 . If they are equal, display "The spelling is correct".
 2. If they are not equal, display "Wrong spelling".
.MODEL SMALL
.DATA
DATA_DICT DB "teff"
DATA_TYPED DB "teef"
MESSAGE1 DB "The spelling is correct",'$'
MESSAGE2 DB "Wrong spelling",'$'
.CODE
;write the code here
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
148

You might also like