Unit 2 FMPMCCSD
Unit 2 FMPMCCSD
programming - introduction
from Microprocessors and Interfacing by Douglas Hall
Outline
• Program Development Steps • Writing programs for the use
with assemblers
• Representing program operations
• Program format
• Finding the right instructions
• Assembler Directives
• Writing a program
• Types of numbers used in data
• Constructing the machine codes for 8086 statements
instructions
• Assembly Language Program
• Instruction templates development Tools
• MOD and R/M Bit patterns in 8086
• Tips for hand coding
Program Development Steps
• Just ask yourself question that “What do I really want this program
to do?”
Representing program operations
• The Intel literature shows two different formats for coding 8086
instructions.
• Instruction templates helps you to code the instruction properly.
• If the other operand in the instruction is also one of the eight register
then put in 11 for MOD bits in the instruction code.
• If the other operand is memory location, there are 24 ways of
specifying how the execution unit should compute the effective
address of the operand in the main memory.
• If the effective address specified in the instruction contains
displacement less than 256 along with the reference to the contents
of the register then put in 01 as the MOD bits.
• If the expression for the effective address contains a displacement
which is too large to fit in 8 bits then out in 10 in MOD bits.
MOV instruction coding format(contd.)
• MOV SP, BX: this instruction will copy a word from BX register to SP
register.
MOV instruction coding format(contd.)
• MOV 43H [SI], DH: copy a byte from DH register to memory location.
MOV instruction coding format
• MOV CX, [437AH]: copy the contents of the two memory locations to
the register CX.
Tips for Hand Coding 8086 Programs
• Check your algorithm very carefully to make sure that it really does
what it supposed to do.
• Initially write down assembly language statements and comments for
your program.
• Recheck each statement to make sure that you have right instructions
to implement your algorithm.
• Finally work out the binary code needed by each instruction and
execute you program.
Writing programs for use with and assembler
• Allows you to refer the data items by names rather than their offsets.
• How to write the programs so that it can be used by the assemblers.
Program Format
Sample
formatted
program
Assembler Directives(contd.)
• There are different directives available for assembler each with their
unique purpose.
• SEGMENT and ENDS Directives
• Naming data and Addresses – EQU, DB, DW and DD Directives
• The ASSUME Directive
• The END Directive
Assembler Directives(contd.)
• It is a program used to join several object files into large object files.
• While writing large programs it is good to divide them into modules
so that each modules can be written, tested, debugged independently
and then use linker to combine the modules to form the actual
program.
• It produces two files link file which contains the binary codes of all
the combined modules and a link map file which contains the address
information about the linked files.
Locators
• A debugger is the program which allows you to load your object code
program in to system memory.
• It allows you to look at the contents of the registers and memory
locations after your program runs.
• It also allows you to set breakpoints at any points in the program.
• It simply allows you to find the source of the problem into the
program.
• There are lots of debuggers available like Borland Turbo Debugger,
Microsoft’s Code view debugger etc.
Emulators
OPERAND:
The CPU executes the instruction using the information resides in these fields .
There are six general formats of instructions in 8086 instruction set.
The instruction of 8086 vary from 1to 6 bytes length
ONE BYTE INSTRUCTION:
It is only one byte long and may have implied data or register operands.
The least three significant 3 bits of the opcode are used for specifying register
operand if any otherwise all the 8 bits form an opcode and the operands are implied.
REGISTER TO REGISTER
The format is 2 byte long
The first byte of the code specifies the opcode and width
The second byte of the code shows the register operand and R/M field
The Register represented by REG is one of the operands . The R/M field specifies
another register or memory location .ie the other operand
The first two bytes contains the information regarding OPCODE,MOD and R/M fields
The remaining 4 bytes contains 2 bytes of displacement and 2 bytes of data
ADDRESSING MODES OF 8086
According two the flow of instructions may be categorized as
1. Sequential Control flow instructions
2. Control transfer instructions
Sequential control flow instructions are the instructions which after execution
transfer control to the next instruction appearing immediately. The control transfer
instructions transfer control to some predefined address or the address somehow
specified in the instruction after their execution.
What is addressing mode?
The different ways in which a source operand is denoted in an instruction are known
as addressing mode the addressing modes for sequential control flow instructions
are
Example
MOV DL, 08H
The 8-bit data (08H) given in the instruction is moved to DL
(DL) 08H
MOV AX, 0A9FH
The 16-bit data (0A9FH) given in the instruction is moved to AX register
(AX) 0A9FH
The instruction will specify the name of the register which holds the data to be operated by the
instruction. All registers except IP may be used in this mode
Example:
MOV CL, DH
The content of 8-bit register DH is moved to another 8-bit register CL
(CL) (DH)
Example
MOV AX, [BX]; suppose the register BX contains 4895H, then the contents
; 4895H are moved to AX
ADD CX, {BX}
In this addressing mode, the operands offset address is found by adding the contents of
SI or DI register and 8-bit/16-bit displacements. DS and ES are the default segments for
index registers SI and DI respectively. This is the special case of the of register indirect
addressing mode.
Example
OUT − Used to send out a byte or word from the accumulator to the provided
port.
LDS − Used to load DS register and other provided register from the memory
LES − Used to load ES register and other provided register from the memory.
2. ARITHMETIC INSTRUCTIONS
These instructions are used to perform arithmetic operations like addition,
subtraction, multiplication, division, etc.
Following is the list of instructions under this group −
3. LOGICAL INSTRUCTIONS
These instructions are used to perform operations where data bits are involved,
i.e. operations like logical, shift, etc.
Following is the list of instructions under this group −
AND − Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/word.
TEST − Used to add operands to update flags, without affecting operands.
SHR − Used to shift bits of a byte/word towards the right and put zero(S) in
MSBs.
SAR − Used to shift bits of a byte/word towards the right and copy the old MSB
into the new MSB.
4. STRING INSTRUCTIONS
String is a group of bytes/words and their memory is always allocated in a
sequential order.
Following is the list of instructions under this group −
REP − Used to repeat the given instruction till CX ≠ 0.
REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF
= 1.
MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided
memory location to the I/O port.
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in
AL or string word with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string word into
AX.
8. INTERRUPT INSTRUCTIONS
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service
specified.
INTO − Used to interrupt the program during execution if OF = 1
IRET − Used to return from interrupt service to the main program
ASSEMBLER DIRECTIVES
Assembler directives are the Instructions to the Assembler, linker and loader
regarding the program being executed. also called ‘pseudo instructions. Control the
generation of machine codes and organization of the program; but no machine codes
are generated for assembler directives.
They are used to
› specify the start and end of a program
› attach value to variables
› allocate storage locations to input/ output data
› define start and end of segments, procedures, macros etc..
ASSUME
Used to tell the assembler the name of the logical segment it should use for a
specified segment. You must tell the assembler that what to assume for any segment
you use in the program.
Example
ASSUME: CODE
Tells the assembler that the instructions for the program are in segment named CODE.
DB – Defined Byte
Used to declare a byte type variable or to set aside one or more locations of type byte in
memory.
Example
PRICES DB 49H, 98H, 29H:
Declare array of 3 bytes named PRICES and initialize 3 bytes as shown.
DD – Define Double Word
Used to declare a variable of type doubleword or to reserve a memory location which
can be accessed as doubleword.
DQ – Define Quadword
Used to tell the assembler to declare the variable as 4 words of storage in memory.
DT – Define Ten Bytes
Used to tell the assembler to declare the variable which is 10 bytes in length or reserve
10 bytes of storage in memory.
DW – Define Word
Used to tell the assembler to define a variable type as word or reserve word in memory.
DUP: used to initialize several locations and to assign values to location
END – End the Program
To tell the assembler to stop fetching the instruction and end the program execution.
ENDP – it is used to end the procedure.
ENDS – used to end the segment.
EQU – EQUATE
Used to give name to some value or symbol.
EVEN – Align On Even Memory Address
Tells the assembler to increment the location counter to the next even address if it is not
already at an even address.
EXTRN
Used to tell the assembler that the name or labels following the directive are in some
other assembly module.
GLOBAL – Declares Symbols As Public Or Extrn
Used to make the symbol available to other modules.It can be used in place of EXTRN or
PUBLIC keyword.
GROUP – Group related segment
Used to tell the assembler to group the logical segments named after the
directive into one logical segment. This allows the content of all the segments to be
accessed from the same group.
INCLUDE – include source code from file
Used to tell the assembler to insert a block of source code from the named file
into the current source module. This shortens the source code.
LABEL
Used to give the name to the current value in the location counter. The LABEL directive
must be followed by a term which specifies the type you want associated with that
name.
LENGTH
Used to determine the number of items in some data such as string or array.
NAME
Used to give a specific name to a module when the programs consisting of several
modules.
OFFSET
It is an operator which tells the assembler to determine the offset or displacement of
named data item or procedure from the start of the segment which contains it.
ORG – Originate
Tells the assembler to set the location counter value.
Example, ORG 7000H sets the location counter value to point to 7000H location in
memory.
$ is often used to symbolically represent the value of the location counter. It is
used with ORG to tell the assembler to change the location according to the current
value in the location counter. E.g. ORG $+100.
Arithematic Operation 8086 microprocessor
Aim:
To write an assembly language program to perform addition of two 16-bit signed and unsigned numbers.
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
ADD AX, OPR2
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 4269H
OPR2 DW 1000H
RES DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
UNSIGNED NUMBERS
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.
Input:
OPR1 = 4269H
OPR2 = 1000H
Output:
RES = 5269H
SIGNED NUMBERS
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.
Input:
OPR1 = 9763H
OPR2 = A973H
Output:
RES = 40D6H
2. Subtraction of two 16-bit numbers
Aim:
To write an assembly language program to perform subtraction of two 16-bit signed and unsigned numbers.
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MOV BX, OPR2
SUB AX, BX
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 4269H
OPR2 DW 1000H
RES DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
UNSIGNED NUMBERS
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.
Input:
OPR1 = 4269H
OPR2 = 1000H
Output:
RES = 3269H
SIGNED NUMBERS
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.
Input:
OPR1 = 9763H
OPR2 = 8973H
Output:
RES = 0DF0H
3. Multiplication of two 16-bit unsigned numbers
Aim:
To write an assembly language program to perform multiplication of two 16-bit unsigned numbers.
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MUL OPR2
MOV RESLW, AX
MOV RESHW, DX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 2000H
OPR2 DW 4000H
RESLW DW ? // reserving uninitialized space
RESHW DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.
Input:
OPR1 = 2000H
OPR2 = 4000H
Output:
Aim:
To write an assembly language program to perform multiplication of two 16-bit signed numbers.
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
IMUL OPR2
MOV RESLW, AX
MOV RESHW, DX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 7593H
OPR2 DW 6845H
RESLW DW ? // reserving uninitialized space
RESHW DW ? // reserving uninitialized space
DATA ENDS
END
List File:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.
Input:
OPR1 = 7593H
OPR2 = 6845H
Output:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.
Input:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.
Input:
Output:
Aim:
To write an assembly language program to perform division of 16-bit unsigned number by 8-bit unsigned
number.
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
DIV OPR2
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 2C58H
OPR2 DB 56H
RESQ DB ? // reserving uninitialized space
RESR DB ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.
Input:
Output:
Aim:
To write an assembly language program to perform division of 16-bit signed number by 8-bit signed
number.
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
IDIV OPR2
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 26F8H
OPR2 DB 0AAH
RESQ DW ? // reserving uninitialized space
RESR DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.
Input:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.
Input:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.
Input:
OPR1 = 26F8H
OPR2 = AAH <- 2’s Complement of (-56H)
Output:
Aim:
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 00H
MOV AL, CHAR
ADD AL, CHAR1
AAA
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
CHAR DB '8'
CHAR1 DB '6'
RES DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=0, p=1, a=1, i=1, d=0.
Input:
CHAR = '8'
CHAR1 = '6'
Output:
Aim:
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 00H
MOV AL, CHAR
SUB AL, CHAR1
AAS
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
CHAR DB '9'
CHAR1 DB '5'
RES DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
Case 1:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
Input:
CHAR = ‘9’
CHAR1 = ‘5’
Output:
Case 2:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=1, z=0, o=0, p=1, a=1, i=1, d=0.
Input:
CHAR = ‘5’
CHAR1 = ‘9’
Output:
Aim:
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 00
MOV AL, NUM1
MUL NUM2
AAM
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
NUM1 DB 09
NUM2 DB 05
RES DW ? // reserving uninitialized space
DATA ENDS
END
List file:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.
Input:
NUM1 = 09
NUM2 = 05
Output:
Aim:
Tools:
Program:
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, DIVIDEND
AAD
MOV CH, DIVISOR
DIV CH
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
DIVIDEND DW 0607H
DIVISOR DB 09H
RESQ DB ? // reserving uninitialized space
RESR DB ? // reserving uninitialized space
DATA ENDS
END
List file:
Flow Chart:
Result:
Flags:
Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
Input:
Output:
RESQ = 07 (AL)
RESR = 04 (AH)