0% found this document useful (0 votes)
22 views216 pages

معالجات دقيقة

The document provides an overview of microprocessors, detailing their definitions, components, and functions. It explains the differences between microcomputers, microprocessors, and microcontrollers, as well as the internal architecture of a microprocessor, including the ALU, control unit, and registers. Additionally, it covers instruction execution, memory organization, and the importance of machine and assembly languages in programming microprocessors.

Uploaded by

gghshdhdgdghdhdh
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)
22 views216 pages

معالجات دقيقة

The document provides an overview of microprocessors, detailing their definitions, components, and functions. It explains the differences between microcomputers, microprocessors, and microcontrollers, as well as the internal architecture of a microprocessor, including the ALU, control unit, and registers. Additionally, it covers instruction execution, memory organization, and the importance of machine and assembly languages in programming microprocessors.

Uploaded by

gghshdhdgdghdhdh
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/ 216

Basic Concepts of Microprocessors

• Differences between:
– Microcomputer – a computer with a
microprocessor as its CPU. Includes memory, I/O
etc.
– Microprocessor – silicon chip which includes
ALU, register circuits & control circuits
– Microcontroller – silicon chip which includes
microprocessor, memory & I/O in a single
package.
What is a Microprocessor?
• The word comes from the combination micro and
processor.
– Processor means a device that processes whatever. In
this context processor means a device that processes
numbers, specifically binary numbers, 0’s and 1’s.
• To process means to manipulate. It is a general term that
describes all manipulation. Again in this content, it means to
perform certain operations on the numbers that depend on the
microprocessor’s design.
What about micro?
• Micro is a new addition.
– In the late 1960’s, processors were built using discrete
elements.
• These devices performed the required operation, but were too
large and too slow.

– In the early 1970’s the microchip was invented. All of


the components that made up the processor were now
placed on a single piece of silicon. The size became
several thousand times smaller and the speed became
several hundred times faster. The “Micro”Processor
was born.
Definition of the Microprocessor

The microprocessor is a programmable device


that takes in numbers, performs on them
arithmetic or logical operations according to
the program stored in memory and then
produces other numbers as a result.
Definition (Contd.)
• Lets expand each of the underlined words:
– Programmable device: The microprocessor can perform
different sets of operations on the data it receives depending
on the sequence of instructions supplied in the given
program.
By changing the program, the microprocessor manipulates
the data in different ways.

– Instructions: Each microprocessor is designed to execute a


specific group of operations. This group of operations is
called an instruction set. This instruction set defines what the
microprocessor can and cannot do.
Definition (Contd.)
– Takes in: The data that the microprocessor
manipulates must come from somewhere.
• It comes from what is called “input devices”.
• These are devices that bring data into the system
from the outside world.
• These represent devices such as a keyboard, a
mouse, switches, and the like.
Definition (Contd.)
– Numbers: The microprocessor has a very narrow view on life. It
only understands binary numbers.

A binary digit is called a bit (which comes from binary digit).

The microprocessor recognizes and processes a group of bits


together. This group of bits is called a “word”.

The number of bits in a Microprocessor’s word, is a measure of its


“abilities”.
Definition (Contd.)
– Words, Bytes, etc.
• The earliest microprocessor (the Intel 8088 and Motorola’s
6800) recognized 8-bit words.
– They processed information 8-bits at a time. That’s why they are
called “8-bit processors”. They can handle large numbers, but in
order to process these numbers, they broke them into 8-bit pieces
and processed each group of 8-bits separately.

• Later microprocessors (8086 and 68000) were designed with


16-bit words.
– A group of 8-bits were referred to as a “half-word” or “byte”.
– A group of 4 bits is called a “nibble”.
– Also, 32 bit groups were given the name “long word”.

• Today, all processors manipulate at least 32 bits at a time and


there exists microprocessors that can process 64, 80, 128 bits
Definition (Contd.)
– Arithmetic and Logic Operations:
• Every microprocessor has arithmetic operations such as add
and subtract as part of its instruction set.
– Most microprocessors will have operations such as multiply and
divide.
– Some of the newer ones will have complex operations such as
square root.

• In addition, microprocessors have logic operations as well.


Such as AND, OR, XOR, shift left, shift right, etc.

• Again, the number and types of operations define the


microprocessor’s instruction set and depends on the specific
microprocessor.
Definition (Contd.)
– Stored in memory :
• First, what is memory?
– Memory is the location where information is kept while not in
current use.
– Memory is a collection of storage devices. Usually, each storage
device holds one bit. Also, in most kinds of memory, these
storage devices are grouped into groups of 8. These 8 storage
locations can only be accessed together. So, one can only read or
write in terms of bytes to and form memory.
– Memory is usually measured by the number of bytes it can hold.
It is measured in Kilos, Megas and lately Gigas. A Kilo in
computer language is 210 =1024. So, a KB (KiloByte) is 1024
bytes. Mega is 1024 Kilos and Giga is 1024 Mega.
Definition (Contd.)
– Stored in memory:
• When a program is entered into a computer, it is
stored in memory. Then as the microprocessor starts
to execute the instructions, it brings the instructions
from memory one at a time.

• Memory is also used to hold the data.


– The microprocessor reads (brings in) the data from
memory when it needs it and writes (stores) the results
into memory when it is done.
Definition (Contd.)
– Produces: For the user to see the result of the
execution of the program, the results must be
presented in a human readable form.
• The results must be presented on an output device.

• This can be the monitor, a paper from the printer, a


simple LED or many other forms.
A Microprocessor-based system
From the above description, we can draw the
following block diagram to represent a
microprocessor-based system:

Input Output

Memory
Inside The Microprocessor
• Internally, the microprocessor is made up of
3 main units.
– The Arithmetic/Logic Unit (ALU)
– The Control Unit.
– An array of registers for holding data while it is
being manipulated.
Organization of a microprocessor-
based system
• Let’s expand the picture a bit.
I/O
Input / Output

ALU Register
Array
System Bus

Control Memory

ROM RAM
Memory
• Memory stores information such as instructions
and data in binary format (0 and 1). It provides
this information to the microprocessor whenever
it is needed.

• Usually, there is a memory “sub-system” in a


microprocessor-based system. This sub-system
includes:
– The registers inside the microprocessor
– Read Only Memory (ROM)
• used to store information that does not change.
– Random Access Memory (RAM) (also known as
Read/Write Memory).
• used to store information supplied by the user. Such as
programs and data.
Memory Map and Addresses
• The memory map is a picture representation
of the address range and shows where the
different memory chips are located within
the address range.
0000 0000
EPROM Address Range of EPROM Chip
3FFF
4400
RAM 1 Address Range of 1st RAM Chip
Address Range

5FFF
6000
RAM 2 Address Range of 2nd RAM Chip
8FFF
9000
RAM 3 Address Range of 3rd RAM Chip
A3FF
A400

RAM 4 Address Range of 4th RAM Chip

F7FF
FFFF
Memory
• To execute a program:
– the user enters its instructions in binary format into the
memory.
– The microprocessor then reads these instructions and
whatever data is needed from memory, executes the
instructions and places the results either in memory or
produces it on an output device.
The three cycle instruction
execution model
• To execute a program, the microprocessor “reads”
each instruction from memory, “interprets” it, then
“executes” it.

• To use the right names for the cycles:


– The microprocessor fetches each instruction,
– decodes it,
– Then executes it.

• This sequence is continued until all instructions


are performed.
Machine Language
• The number of bits that form the “word” of a
microprocessor is fixed for that particular
processor.
– These bits define a maximum number of combinations.
• For example an 8-bit microprocessor can have at most 28 = 256
different combinations.

• However, in most microprocessors, not all of these


combinations are used.
– Certain patterns are chosen and assigned specific
meanings.
– Each of these patterns forms an instruction for the
microprocessor.
– The complete set of patterns makes up the
microprocessor’s machine language.
The 8085 Machine Language
• The 8085 (from Intel) is an 8-bit microprocessor.
– The 8085 uses a total of 246 bit patterns to form its
instruction set.
– These 246 patterns represent only 74 instructions.
• The reason for the difference is that some (actually most)
instructions have multiple different formats.

– Because it is very difficult to enter the bit patterns


correctly, they are usually entered in hexadecimal
instead of binary.
• For example, the combination 0011 1100 which translates into
“increment the number in the register called the accumulator”,
is usually entered as 3C.
Assembly Language
• Entering the instructions using hexadecimal is quite
easier than entering the binary combinations.
– However, it still is difficult to understand what a program
written in hexadecimal does.
– So, each company defines a symbolic code for the
instructions.
– These codes are called “mnemonics”.
– The mnemonic for each instruction is usually a group of
letters that suggest the operation performed.
Assembly Language
• Using the same example from before,
– 00111100 translates to 3C in hexadecimal (OPCODE)
– Its mnemonic is: “INR A”.
– INR stands for “increment register” and A is short for
accumulator.

• Another example is: 1000 0000,


– Which translates to 80 in hexadecimal.
– Its mnemonic is “ADD B”.
– “Add register B to the accumulator and keep the result in the
accumulator”.
Assembly Language
• It is important to remember that a machine
language and its associated assembly language are
completely machine dependent.
– In other words, they are not transferable from one
microprocessor to a different one.

• For example, Motorolla has an 8-bit


microprocessor called the 6800.
– The 8085 machine language is very different from that
of the 6800. So is the assembly language.
– A program written for the 8085 cannot be executed on
the 6800 and vice versa.
“Assembling” The Program
• How does assembly language get translated into
machine language?
– There are two ways:
– 1st there is “hand assembly”.
• The programmer translates each assembly language instruction
into its equivalent hexadecimal code (machine language). Then
the hexadecimal code is entered into memory.
– The other possibility is a program called an
“assembler”, which does the translation automatically.
• System Bus – wires connecting memory & I/O to
microprocessor
– Address Bus
• Unidirectional
• Identifying peripheral or memory location
– Data Bus
• Bidirectional
• Transferring data
– Control Bus
• Synchronization signals
• Timing signals
• Control signal
Intel 8085 Microprocessor
• Microprocessor consists of:
– Control unit: control microprocessor operations.
– ALU: performs data processing function.
– Registers: provide storage internal to CPU.
– Interrupts
– Internal data bus
The ALU
• In addition to the arithmetic & logic circuits, the
ALU includes the accumulator, which is part of
every arithmetic & logic operation.

• Also, the ALU includes a temporary register used


for holding data temporarily during the execution
of the operation. This temporary register is not
accessible by the programmer.
• Registers
– General Purpose Registers
• B, C, D, E, H & L (8 bit registers)
• Can be used singly
• Or can be used as 16 bit register pairs
– BC, DE, HL
• H & L can be used as a data pointer (holds memory
address)
Accumulator Flags
– Special Purpose Registers B
D
C
E
• Accumulator (8 bit register) H L
Program Counter
– Store 8 bit data Stack Pointer
– Store the result of an operation
– Store 8 bit data during I/O transfer Address 16 8 Data
• Flag Register
– 8 bit register – shows the status of the microprocessor before/after an
operation
– S (sign flag), Z (zero flag), AC (auxillary carry flag), P (parity flag) &
CY (carry flag)

D7 D6 D5 D4 D3 D2 D1 D0

S Z X AC X P X CY

– Sign Flag
• Used for indicating the sign of the data in the accumulator
• The sign flag is set if negative (1 – negative)
• The sign flag is reset if positive (0 –positive)
• Zero Flag
– Is set if result obtained after an operation is 0
– Is set following an increment or decrement operation of that register
10110011
+ 01001101
---------------
1 00000000
• Carry Flag
– Is set if there is a carry or borrow from arithmetic operation

1011 0101 1011 0101


+ 0110 1100 - 1100 1100
--------------- ---------------
Carry 1 0010 0001 Borrow 1 1110 1001
• Auxillary Carry Flag
– Is set if there is a carry out of bit 3
• Parity Flag
– Is set if parity is even
– Is cleared if parity is odd
The Internal Architecture
• We have already discussed the general purpose
registers, the Accumulator, and the flags.

• The Program Counter (PC)


– This is a register that is used to control the sequencing
of the execution of instructions.
– This register always holds the address of the next
instruction.
– Since it holds an address, it must be 16 bits wide.
The Internal Architecture
• The Stack pointer
– The stack pointer is also a 16-bit register that is
used to point into memory.
– The memory this register points to is a special
area called the stack.
– The stack is an area of memory used to hold
data that will be retreived soon.
– The stack is usually accessed in a Last In First
Out (LIFO) fashion.
Non Programmable Registers
• Instruction Register & Decoder
– Instruction is stored in IR after fetched by processor
– Decoder decodes instruction in IR

Internal Clock generator


– 3.125 MHz internally
– 6.25 MHz externally
The Address and Data Busses
• The address bus has 8 signal lines A8 – A15
which are unidirectional.
• The other 8 address bits are multiplexed (time
shared) with the 8 data bits.
– So, the bits AD0 – AD7 are bi-directional and serve as
A0 – A7 and D0 – D7 at the same time.
• During the execution of the instruction, these lines carry the
address bits during the early part, then during the late parts of
the execution, they carry the 8 data bits.
– In order to separate the address from the data, we can
use a latch to save the value before the function of the
bits changes.
Demultiplexing AD7-AD0
8085
A15-A8

ALE

AD7-AD0 Latch
A7- A0

D7- D0

– Given that ALE operates as a pulse during T1, we will


be able to latch the address. Then when ALE goes low,
the address is saved and the AD7– AD0 lines can be
used for their purpose as the bi-directional data lines.
Demultiplexing the Bus AD7 – AD0
• The high order address is placed on the address bus and hold for 3 clk
periods,
• The low order address is lost after the first clk period, this address
needs to be hold however we need to use latch
• The address AD7 – AD0 is connected as inputs to the latch 74LS373.
• The ALE signal is connected to the enable (G) pin of the latch and the
OC – Output control – of the latch is grounded
The Overall Picture
• Putting all of the concepts together, we get:
Chip Selection
A15- A10 Circuit

8085
CS
A15-A8

ALE
A9- A0 1K Byte
AD7-AD0 Latch Memory
A7- A0 Chip

WR RD IO/M D7- D0
RD WR
Introduction to 8085 Instructions
The 8085 Instructions
– Since the 8085 is an 8-bit device it can have up to 28
(256) instructions.
• However, the 8085 only uses 246 combinations that represent a
total of 74 instructions.
– Most of the instructions have more than one format.

– These instructions can be grouped into five different


groups:
• Data Transfer Operations
• Arithmetic Operations
• Logic Operations
• Branch Operations
• Machine Control Operations
Instruction and Data Formats
• Each instruction has two parts.
– The first part is the task or operation to be
performed.
• This part is called the “opcode” (operation code).

– The second part is the data to be operated on


• Called the “operand”.
Data Transfer Operations
– These operations simply COPY the data from the
source to the destination.
– MOV, MVI, LDA, and STA

– They transfer:
• Data between registers.
• Data Byte to a register or memory location.
• Data between a memory location and a register.
• Data between an I\O Device and the accumulator.

– The data in the source is not changed.


The LXI instruction
• The 8085 provides an instruction to place
the 16-bit data into the register pair in one
step.
• LXI Rp, <16-bit address> (Load eXtended Immediate)

– The instruction LXI B 4000H will place the


16-bit number 4000 into the register pair B, C.
• The upper two digits are placed in the 1st register of
the pair and the lower two digits in the 2nd .

LXI B 40 00H B 40 00 C
The Memory “Register”
• Most of the instructions of the 8085 can use a
memory location in place of a register.
– The memory location will become the “memory” register M.
• MOV M B
– copy the data from register B into a memory location.
– Which memory location?

• The memory location is identified by the contents


of the HL register pair.
– The 16-bit contents of the HL register pair are treated
as a 16-bit address and used to identify the memory
location.
Using the Other Register Pairs
– There is also an instruction for moving data from
memory to the accumulator without disturbing the
contents of the H and L register.

• LDAX Rp (LoaD Accumulator eXtended)

– Copy the 8-bit contents of the memory location identified by the


Rp register pair into the Accumulator.
– This instruction only uses the BC or DE pair.
– It does not accept the HL pair.
Indirect Addressing Mode
• Using data in memory directly (without loading
first into a Microprocessor’s register) is called
Indirect Addressing.

• Indirect addressing uses the data in a register pair


as a 16-bit address to identify the memory location
being accessed.
– The HL register pair is always used in conjunction with
the memory register “M”.
– The BC and DE register pairs can be used to load data
into the Accumultor using indirect addressing.
Arithmetic Operations
– Addition (ADD, ADI):
– Any 8-bit number.
– The contents of a register.
– The contents of a memory location.
• Can be added to the contents of the accumulator and the result
is stored in the accumulator.

– Subtraction (SUB, SUI):


– Any 8-bit number
– The contents of a register
– The contents of a memory location
• Can be subtracted from the contents of the accumulator. The
result is stored in the accumulator.
Arithmetic Operations Related to
Memory
• These instructions perform an arithmetic operation
using the contents of a memory location while
they are still in memory.
– ADD M
• Add the contents of M to the Accumulator
– SUB M
• Sub the contents of M from the Accumulator
– INR M / DCR M
• Increment/decrement the contents of the memory location in
place.

– All of these use the contents of the HL register pair to


identify the memory location being used.
Arithmetic Operations
– Increment (INR) and Decrement (DCR):
• The 8-bit contents of any memory location or any
register can be directly incremented or decremented
by 1.
• No need to disturb the contents of the accumulator.
Manipulating Addresses
• Now that we have a 16-bit address in a register
pair, how do we manipulate it?
– It is possible to manipulate a 16-bit address stored in a
register pair as one entity using some special
instructions.
• INX Rp (Increment the 16-bit number in the register pair)
• DCX Rp (Decrement the 16-bit number in the register pair)

– The register pair is incremented or decremented as one


entity. No need to worry about a carry from the lower
8-bits to the upper. It is taken care of automatically.
Logic Operations
• These instructions perform logic operations on the
contents of the accumulator.
– ANA, ANI, ORA, ORI, XRA and XRI
• Source: Accumulator and
– An 8-bit number
– The contents of a register
– The contents of a memory location
• Destination: Accumulator
ANA R/M AND Accumulator With Reg/Mem
ANI # AND Accumulator With an 8-bit number

ORA R/M OR Accumulator With Reg/Mem


ORI # OR Accumulator With an 8-bit number

XRA R/M XOR Accumulator With Reg/Mem


XRI # XOR Accumulator With an 8-bit number
Logic Operations
– Complement:
• 1’s complement of the contents of the accumulator.
CMA No operand
Additional Logic Operations
• Rotate
– Rotate the contents of the accumulator one
position to the left or right.
– RLC Rotate the accumulator left.
Bit 7 goes to bit 0 AND the Carry flag.
– RAL Rotate the accumulator left through the carry.
Bit 7 goes to the carry and carry goes to bit 0.
– RRC Rotate the accumulator right.
Bit 0 goes to bit 7 AND the Carry flag.
– RAR Rotate the accumulator right through the carry.
Bit 0 goes to the carry and carry goes to bit 7.
RLC vs. RLA
Carry Flag

• RLC
7 6 5 4 3 2 1 0

Accumulator

Carry Flag

• RAL 7 6 5 4 3 2 1 0

Accumulator
Logical Operations
• Compare
• Compare the contents of a register or memory location with the
contents of the accumulator.
– CMP R/M Compare the contents of the register
or memory location to the contents of
the accumulator.
– CPI # Compare the 8-bit number to the
contents of the accumulator.
• The compare instruction sets the flags (Z, Cy, and S).

• The compare is done using an internal subtraction that does not


change the contents of the accumulator.
A – (R / M / #)
Branch Operations
• Two types:
– Unconditional branch.
• Go to a new location no matter what.
– Conditional branch.
• Go to a new location if the condition is true.
Unconditional Branch
– JMP Address
• Jump to the address specified (Go to).

– CALL Address
• Jump to the address specified but treat it as a subroutine.

– RET
• Return from a subroutine.

– The addresses supplied to all branch operations must be


16-bits.
Conditional Branch
– Go to new location if a specified condition is met.
• JZ Address (Jump on Zero)
– Go to address specified if the Zero flag is set.
• JNZ Address (Jump on NOT Zero)
– Go to address specified if the Zero flag is not set.
• JC Address (Jump on Carry)
– Go to the address specified if the Carry flag is set.
• JNC Address (Jump on No Carry)
– Go to the address specified if the Carry flag is not set.
• JP Address (Jump on Plus)
– Go to the address specified if the Sign flag is not set
• JM Address (Jump on Minus)
– Go to the address specified if the Sign flag is set.
Machine Control
– HLT
• Stop executing the program.
– NOP
• No operation
• Exactly as it says, do nothing.
• Usually used for delay or to replace instructions
during debugging.
Operand Types
• There are different ways for specifying the
operand:
– There may not be an operand (implied operand)
• CMA
– The operand may be an 8-bit number (immediate data)
• ADI 4FH
– The operand may be an internal register (register)
• SUB B
– The operand may be a 16-bit address (memory address)
• LDA 4000H
Instruction Size
• Depending on the operand type, the instruction
may have different sizes. It will occupy a different
number of memory bytes.
– Typically, all instructions occupy one byte only.
– The exception is any instruction that contains
immediate data or a memory address.
• Instructions that include immediate data use two bytes.
– One for the opcode and the other for the 8-bit data.
• Instructions that include a memory address occupy three bytes.
– One for the opcode, and the other two for the 16-bit address.
Instruction with Immediate Date
• Operation: Load an 8-bit number into the
accumulator.

– MVI A, 32
• Operation: MVI A
• Operand: The number 32
• Binary Code:
0011 1110 3E 1st byte.
0011 0010 32 2nd byte.
Instruction with a Memory
Address
• Operation: go to address 2085.

– Instruction: JMP 2085


• Opcode: JMP
• Operand: 2085
• Binary code:
1100 0011 C3 1st byte.
1000 0101 85 2nd byte
0010 0000 20 3rd byte
Addressing Modes
• The microprocessor has different ways of
specifying the data for the instruction. These are
called “addressing modes”.

• The 8085 has four addressing modes:


– Implied CMA
– Immediate MVI B, 45
– Direct LDA 4000
– Indirect LDAX B
• Load the accumulator with the contents of the memory location
whose address is stored in the register pair BC).
Data Formats
• In an 8-bit microprocessor, data can be
represented in one of four formats:
• ASCII
• BCD
• Signed Integer
• Unsigned Integer.

– It is important to recognize that the microprocessor


deals with 0’s and 1’s.
• It deals with values as strings of bits.
• It is the job of the user to add a meaning to these strings.
Data Formats
• Assume the accumulator contains the following
value: 0100 0001.
– There are four ways of reading this value:
• It is an unsigned integer expressed in binary, the equivalent
decimal number would be 65.
• It is a number expressed in BCD (Binary Coded Decimal)
format. That would make it, 41.
• It is an ASCII representation of a letter. That would make it the
letter A.
• It is a string of 0’s and 1’s where the 0th and the 6th bits are set
to 1 while all other bits are set to 0.

ASCII stands for American Standard Code for Information Interchange.


Counters & Time Delays
Counters
• A loop counter is set up by loading a register with
a certain value
• Then using the DCR (to decrement) and INR (to
increment) the contents of the register are updated.
• A loop is set up with a conditional jump
instruction that loops back or not depending on
whether the count has reached the termination
count.
Counters
• The operation of a loop counter can be
described using the following flowchart.

Initialize

Body of loop

Update the count

No Is this
Final
Count?

Yes
Sample ALP for implementing a loop
Using DCR instruction
MVI C, 15H
LOOP DCR C
JNZ LOOP
Using a Register Pair as a Loop
Counter
• Using a single register, one can repeat a loop for a
maximum count of 255 times.

• It is possible to increase this count by using a


register pair for the loop counter instead of the
single register.
– A minor problem arises in how to test for the final
count since DCX and INX do not modify the flags.
– However, if the loop is looking for when the count
becomes zero, we can use a small trick by ORing the
two registers in the pair and then checking the zero flag.
Using a Register Pair as a Loop
Counter
• The following is an example of a loop set
up with a register pair as the loop counter.

LXI B, 1000H
LOOP DCX B
MOV A, C
ORA B
JNZ LOOP
Delays
• It was shown in Chapter 2 that each instruction
passes through different combinations of Fetch,
Memory Read, and Memory Write cycles.
• Knowing the combinations of cycles, one can
calculate how long such an instruction would
require to complete.
• The table in Appendix F of the book contains a
column with the title B/M/T.
– B for Number of Bytes
– M for Number of Machine Cycles
– T for Number of T-State.
Delays
• Knowing how many T-States an instruction
requires, and keeping in mind that a T-State is one
clock cycle long, we can calculate the time using
the following formula:

Delay = No. of T-States / Frequency

• For example a “MVI” instruction uses 7 T-States.


Therefore, if the Microprocessor is running at 2
MHz, the instruction would require 3.5 µSeconds
to complete.
Delay loops
• We can use a loop to produce a certain
amount of time delay in a program.

• The following is an example of a delay


loop:
MVI C, FFH 7 T-States
LOOP DCR C 4 T-States
JNZ LOOP 10 T-States

• The first instruction initializes the loop counter and is


executed only once requiring only 7 T-States.
• The following two instructions form a loop that
requires 14 T-States to execute and is repeated 255
times until C becomes 0.
Delay Loops (Contd.)
• We need to keep in mind though that in the last
iteration of the loop, the JNZ instruction will fail and
require only 7 T-States rather than the 10.
• Therefore, we must deduct 3 T-States from the total
delay to get an accurate delay calculation.
• To calculate the delay, we use the following formula:
Tdelay = TO + TL
– Tdelay = total delay
– TO = delay outside the loop
– TL = delay of the loop

• TO is the sum of all delays outside the loop.


Delay Loops (Contd.)
• Using these formulas, we can calculate the
time delay for the previous example:

• TO = 7 T-States
– Delay of the MVI instruction

• TL = (14 X 255) - 3 = 3567 T-States


– 14 T-States for the 2 instructions repeated 255 times
(FF16 = 25510) reduced by the 3 T-States for the final
JNZ.
Using a Register Pair as a Loop
Counter
• Using a single register, one can repeat a loop for a
maximum count of 255 times.

• It is possible to increase this count by using a


register pair for the loop counter instead of the
single register.
– A minor problem arises in how to test for the final
count since DCX and INX do not modify the flags.
– However, if the loop is looking for when the count
becomes zero, we can use a small trick by ORing the
two registers in the pair and then checking the zero flag.
Using a Register Pair as a Loop
Counter
• The following is an example of a delay loop
set up with a register pair as the loop
counter.

LXI B, 1000H 10 T-States


LOOP DCX B 6 T-States
MOV A, C 4 T-States
ORA B 4 T-States
JNZ LOOP 10 T-States
Using a Register Pair as a Loop
Counter
• Using the same formula from before, we can
calculate:

• TO = 10 T-States
– The delay for the LXI instruction

• TL = (24 X 4096) - 3 = 98301 T- States


– 24 T-States for the 4 instructions in the loop repeated
4096 times (100016 = 409610) reduced by the 3 T-
States for the JNZ in the last iteration.
Nested Loops Initialize loop 2

Body of loop 2

• Nested loops can be Initialize loop 1

easily setup in Body of loop 1

Assembly language by Update the count1

using two registers for


No Is this
the two loop counters Final
Count?
and updating the right Yes
register in the right Update the count 2

loop. Is this
No
– In the figure, the body of Final
Count?
loop2 can be before or
Yes
after loop1.
Nested Loops for Delay
• Instead (or in conjunction with) Register Pairs, a
nested loop structure can be used to increase the
total delay produced.

MVI B, 10H 7 T-States


LOOP2 MVI C, FFH 7 T-States
LOOP1 DCR C 4 T-States
JNZ LOOP1 10 T-States
DCR B 4 T-States
JNZ LOOP2 10 T-States
Delay Calculation of Nested
Loops
• The calculation remains the same except
that it the formula must be applied
recursively to each loop.
– Start with the inner loop, then plug that delay in
the calculation of the outer loop.

• Delay of inner loop


– TO1 = 7 T-States
• MVI C, FFH instruction
– TL1 = (255 X 14) - 3 = 3567 T-States
• 14 T-States for the DCR C and JNZ instructions repeated 255
Delay Calculation of Nested
Loops
• Delay of outer loop
– TO2 = 7 T-States
• MVI B, 10H instruction
– TL1 = (16 X (14 + 3574)) - 3 = 57405 T-States
• 14 T-States for the DCR B and JNZ instructions and 3574
T-States for loop1 repeated 16 times (1016 = 1610) minus 3 for the
final JNZ.
– TDelay = 7 + 57405 = 57412 T-States

• Total Delay
– TDelay = 57412 X 0.5 µSec = 28.706 mSec
Increasing the delay
• The delay can be further increased by using
register pairs for each of the loop counters
in the nested loops setup.
• It can also be increased by adding dummy
instructions (like NOP) in the body of the
loop.
Timing Diagram
Representation of Various Control signals generated during
Execution of an Instruction.
Following Buses and Control Signals must be shown in a
Timing Diagram:
•Higher Order Address Bus.
•Lower Address/Data bus
•ALE
•RD
•WR
•IO/M
Timing Diagram
Instruction:
A000h MOV A,B

Corresponding Coding:
A000h 78
Timing Diagram
Instruction:
A000h MOV A,B
Corresponding Coding:
A000h 78

OFC

8085 Memory
Timing Diagram
Instruction: T1 T2 T3 T4

A000h MOV A,B A0h


A15- A8 (Higher Order Address bus)
Corresponding Coding: 00h 78h

A000h 78

ALE

RD

OFC WR

IO/M
8085 Memory
Op-code fetch Cycle
Timing Diagram
Instruction:
A000h MVI A,45h
Corresponding Coding:
A000h 3E
A001h 45
Timing Diagram
Instruction:
A000h MVI A,45h
Corresponding Coding:
OFC
A000h 3E
MEMR
A001h 45
8085 Memory
Timing Diagram
T1 T2 T3 T4 T5 T6 T7

A0h A0h
A15- A8 (Higher Order Address bus)
00h 3Eh 01h 45h
DA7-DA0 (Lower order address/data Bus)
Instruction:
A000h MVI A,45h
ALE
Corresponding Coding:
RD
A000h 3E
A001h 45
WR

IO/M

Op-Code Fetch Cycle Memory Read Cycle


Timing Diagram
Instruction:
A000h LXI A,FO45h
Corresponding Coding:
A000h 21
A001h 45
A002h F0
Timing Diagram

Instruction:
A000h LXI A,FO45h
Corresponding Coding: OFC

A000h 21 MEMR

MEMR
A001h 45
A002h F0 8085 Memory
Timing Diagram

Op-Code Fetch Cycle Memory Read Cycle Memory Read Cycle

T1 T2 T3 T4 T5 T6 T7 T8 T9 T10

A0h A0h A0h


A15- A8 (Higher Order Address bus)
00h 21h 01h 45h 02h F0h
DA7-DA0 (Lower order address/data Bus)

ALE

RD

WR

IO/M
Timing Diagram
Instruction:
A000h MOV A,M
Corresponding Coding:
A000h 7E
Timing Diagram

Instruction:
A000h MOV A,M
OFC
Corresponding Coding: MEMR

A000h 7E
8085 Memory
Timing Diagram

T1 T2 T3 T4 T5 T6 T7
A0h Content Of Reg H
A15- A8 (Higher Order Address bus)
00h 7Eh L Reg Content Of M
Instruction: DA7-DA0 (Lower order address/data Bus)

A000h MOV A,M


Corresponding Coding: ALE
A000h 7E
RD

WR

IO/M

Op-Code Fetch Cycle Memory Read Cycle


Timing Diagram

Instruction:
A000h MOV M,A
Corresponding Coding:
A000h 77
Timing Diagram

Instruction:
A000h MOV M,A
OFC
Corresponding Coding: MEMW

A000h 77
8085 Memory
Timing Diagram

T1 T2 T3 T4 T5 T6 T7
A0h Content Of Reg H
A15- A8 (Higher Order Address bus)
00h 7Eh L Reg Content of Reg A
Instruction: DA7-DA0 (Lower order address/data Bus)

A000h MOV M,A


Corresponding Coding: ALE
A000h 77
RD

WR

IO/M

Op-Code Fetch Cycle Memory Write Cycle


Chapter 9
Stack and Subroutines
The Stack
• The stack is an area of memory identified by the
programmer for temporary storage of information.
• The stack is a LIFO structure.
– Last In First Out.
Memory
• The stack normally grows backwards into
memory.
– In other words, the programmer
defines the bottom of the stack The Stack
grows
and the stack grows up into backwards
Bottom
reducing address range. into memory
of the
Stack
The Stack
• Given that the stack grows backwards into
memory, it is customary to place the bottom of the
stack at the end of memory to keep it as far away
from user programs as possible.
• In the 8085, the stack is defined by setting the SP
(Stack Pointer) register.

LXI SP, FFFFH

• This sets the Stack Pointer to location FFFFH (end


of memory for the 8085).
Saving Information on the Stack
• Information is saved on the stack by PUSHing it
on.
– It is retrieved from the stack by POPing it off.

• The 8085 provides two instructions: PUSH and


POP for storing information on the stack and
retrieving it back.
– Both PUSH and POP work with register pairs ONLY.
The PUSH Instruction
• PUSH B
– Decrement SP
– Copy the contents of register B to the memory
location pointed to by SP
– Decrement BSP C
12 F3
– Copy the contents of register C to the memory
FFFB

location pointed to by SP F3
FFFC
FFFD
FFFE 12
FFFF SP
The POP Instruction
• POP D
– Copy the contents of the memory location
pointed to by the SP to register E
– Increment SP
– Copy the contents
D E
of the memory location
pointed to by
12 the
F3 SP to register D
FFFB

– Increment SP FFFC
FFFD F3 SP
FFFE 12
FFFF
Operation of the Stack
• During pushing, the stack operates in a
“decrement then store” style.
– The stack pointer is decremented first, then the
information is placed on the stack.

• During poping, the stack operates in a “use then


increment” style.
– The information is retrieved from the top of the the
stack and then the pointer is incremented.

• The SP pointer always points to “the top of the


stack”.
LIFO
• The order of PUSHs and POPs must be opposite
of each other in order to retrieve information back
into its original location.

PUSH B
PUSH D
...
POP D
POP B
The PSW Register Pair
• The 8085 recognizes one additional register pair
called the PSW (Program Status Word).
– This register pair is made up of the Accumulator and
the Flags registers.

• It is possible to push the PSW onto the stack, do


whatever operations are needed, then POP it off of
the stack.
– The result is that the contents of the Accumulator and
the status of the Flags are returned to what they were
before the operations were executed.
Subroutines
• A subroutine is a group of instructions that will be
used repeatedly in different locations of the
program.
– Rather than repeat the same instructions several times,
they can be grouped into a subroutine that is called
from the different locations.

• In Assembly language, a subroutine can exist


anywhere in the code.
– However, it is customary to place subroutines
separately from the main program.
Subroutines
• The 8085 has two instructions for dealing
with subroutines.
– The CALL instruction is used to redirect
program execution to the subroutine.
– The RTE insutruction is used to return the
execution to the calling routine.
The CALL Instruction
• CALL 4000H
– Push the address of the instruction
immediately following the CALL onto the
2000 stack
CALL 4000
2003
– Load the program PC
counter
2 0 0 3with the 16-bit

address supplied with the CALL instruction.


FFFB
FFFC
FFFD 03
FFFE 20
FFFF SP
The RTE Instruction
• RTE
– Retrieve the return address from the top of
the stack
– Load the program counter with the return
address. PC 2003
FFFB
4014 ... FFFC
4015 RTE FFFD 03 SP
FFFE 20
FFFF
Cautions
• The CALL instruction places the return address at
the two memory locations immediately before
where the Stack Pointer is pointing.
– You must set the SP correctly BEFORE using the
CALL instruction.

• The RTE instruction takes the contents of the two


memory locations at the top of the stack and uses
these as the return address.
– Do not modify the stack pointer in a subroutine. You
will loose the return address.
Passing Data to a Subroutine
• In Assembly Language data is passed to a
subroutine through registers.
– The data is stored in one of the registers by the calling
program and the subroutine uses the value from the
register.

• The other possibility is to use agreed upon


memory locations.
– The calling program stores the data in the memory
location and the subroutine retrieves the data from the
location and uses it.
Call by Reference and Call by
Value
• If the subroutine performs operations on the
contents of the registers, then these modifications
will be transferred back to the calling program
upon returning from a subroutine.
– Call by reference

• If this is not desired, the subroutine should PUSH


all the registers it needs on the stack on entry and
POP them on return.
– The original values are restored before execution
returns to the calling program.
Cautions with PUSH and POP
• PUSH and POP should be used in opposite order.

• There has to be as many POP’s as there are


PUSH’s.
– If not, the RET statement will pick up the wrong
information from the top of the stack and the program
will fail.

• It is not advisable to place PUSH or POP inside a


loop.
Conditional CALL and RTE
Instructions
• The 8085 supports conditional CALL and
conditional RTE instructions.
– The same conditions used with conditional JUMP
instructions can be used.

– CC, call subroutine if Carry flag is set.


– CNC, call subroutine if Carry flag is not set
– RC, return from subroutine if Carry flag is set
– RNC, return from subroutine if Carry flag is not set
– Etc.
A Proper Subroutine
• According to Software Engineering practices, a
proper subroutine:
– Is only entered with a CALL and exited with an RTE
– Has a single entry point
• Do not use a CALL statement to jump into different points of
the same subroutine.
– Has a single exit point
• There should be one return statement from any subroutine.

• Following these rules, there should not be any


confusion with PUSH and POP usage.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ES85 MICROPROCESSOR KIT
PROCEDURE OF EXECUTION
Commands and Keys
Reset
This key is located in the main 85 board. On depressing this key the program starts executing
from the beginning or reset address 0000. On power on reset it. Display ES - 85 in local LCD
display.
H (Help Menu)
This key is used go PS-85L help menu and it will display the following commands. Depressing
the space bar key increments to display the next command
KEY FUNCTIONS
A <BEG> ASSEMBLE
B BAUD RATE
D <BEG> DISASSEMBLE
E <BEG><END> EXAMINE
G <BEG> EXECUTE
H HELP COMMANDS
L <OFFSET> DOWN LOAD
M <ADDR> MODIFY
N NORMAL MODE
Q QUIT
R <REG> REGISTER DISPLAY
S SERIAL MODE
T <START><END><DS> BLOCK TRANSFER
U <BEG><END> UP LOAD

17
Steps to Execute the Program on 8085 Microprocessor Kit
Follow the steps given below in order to execute the program on 8085 Microprocessor Kit:
1. Enter Program
2. Enter Data
3. Execute Program
4. Check Result
Procedure:
Press RESET
Press A and Enter starting address of the program ex: 8000 and Press Enter
Ex: A8000
ES85 ------- 8000:
-A8000
Type the Program
 Press RESET
Enter G8000, 8000 which will set the program counter to the address
Give the inputs as example
PC: 8000
S8501H, 21-Value

Press spacebar to enter the next input values and Press enter after last input.
Press N and then press comma till restart is triggered (till change of address)
Check the output as
Press Output
S8503H, 21- value

18
PROGRAM NO. 1

ADDITION OF TWO 8-BIT NUMBERS


Aim: Program for addition of two 8-bit numbers and sum is 8 bit.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
Consider the first number 26H is stored in memory location 8501H and the second
number 62H is stored in memory location 8502H.The result after addition of two
numbers is to be stored in the memory location 8503 H. Assume program starts from
memory location 8000H.
Algorithm:
 Initialize the memory location of first number in HL register pair.
 Move first number/data into accumulator
 Increment the content of HL register pair to initialize the memory location of second data
 Add the second data with accumulator
 Store the result in memory location 8503H
Flow chart:

19
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Address of first number
8000 21 LXI H, 8501 H
in H-L register pair.
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Transfer first number in
8003 7E MOV A,M
accumulator.
Increment content of H-L
8004 23 INX H
register pair
Add first number and
8005 86 ADD M
second number
Increment content of H-L
8006 23 INX H
register pair
8007 77 MOV M,A Store sum in 8503 H
Terminate program.
8008 EF RST.5 (Memory points to
0028h)

Result:
INPUT DATA RESULT
Memory location Data Memory Data
location
8501 26H 8503 88H
8502 62H 8504 00H

Viva-Voice Questions:

1. What is the function of LXI H, 8000 H instruction?

2. How you can store a data in a memory location?

3. How you can read a data from a memory location?

4. What are flags available in 8085?

5. What is the function of RESET key of a 8085 microprocessor kit

20
PROGRAM NO. 2

SUBTRACTION OF TWO 8-BIT NUMBERS


Aim: Program for Subtraction of two 8-bit numbers and sum is 8 bit.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
Consider the first number 07H is stored in memory location 8501H and the second
number 03H is stored in memory location 8502H.The result after Subtraction of two
numbers is to be stored in the memory location 8503 H. Assume program starts from
memory location 8000H.
Algorithm:
 Initialize the memory location of first number in HL register pair.
 Move first number/data into accumulator
 Increment the content of HL register pair to initialize the memory location of second data
 Subtract the second data with accumulator
 Store the result in memory location 8503H
Flow chart:

21
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Address of first number
8000 21 LXI H, 8501 H
in H-L register pair.
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Transfer first number in
8003 7E MOV A,M
accumulator.
Increment content of H-L
8004 23 INX H
register pair
subtract first number and
8005 86 SUB M
second number
Increment content of H-L
8006 23 INX H
register pair
8007 77 MOV M,A Store result in 8503 H
Terminate program.
8008 EF RST.5 (Memory points to
0028h)

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8501 07H 8503 04H
8502 03H 8504 00H

Viva-Voice Questions:
1. What is the function of LXI H, 8000 H instruction?

2. How you can store a data in a memory location?

3. How you can read a data from a memory location?

4. What are flags available in 8085?

5. What is the function of RESET key of a 8085 microprocessor kit

22
PROGRAM NO. 3

ADDITION OF TWO 8 BIT NUMBERS AND SUM IS 16-BIT


Aim: Program for Addition of Two 8-Bit Numbers and Sum is 16 Bit.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The first number 34H is stored in memory location 8501H and the second number 26H is
stored in memory location 8502H. The result after addition will be stored in the memory
location 8503 H and 8504H. Consider program is written from memory location 8000H.
Algorithm:
st
 Initialize the memory location of 1 data in HL register pair.
 Store first data in the memory location
 Increment the content of HL register pair for entering next data in the next memory
location
 Store second data in the memory location
 Move second number in accumulator
 Decrease the content of HL register pair
 Add the content of memory (first data) with accumulator
 Store the results in memory location 8503H and 8504H.
Flow chart:

23
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Address of first number
8000 21 LXI H, 8501 H
in H-L register pair.
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Sum of msb’s & register
8003 0E MVI C,00H
value in 00h
8004 00
Transfer first number in
8005 7E MOV A,M
accumulator.
Increment content of H-L
8006 23 INX H
register pair
Add first number and
8007 86 ADD M
second number
Jump if no carry to
8008 D2 JNC 800CH
800Ch location
Lower byte data is stored
8009 0C
in memory
Higher byte data is stored
800A 80
in memory
800B 0C INR C Increment register C
Data of accumulator is
800C 32 AHEAD STA 8503H
stored into 8503h address
Lower byte data is stored
800D 03
in memory
Higher byte data is stored
800E 85
in memory
800F 79 MOV A,C MSB’S of sum in A
MSB’S of sum in A is
8010 32 STA 8504H transferred to 8504h
location
Lower byte data is stored
8011 04
in memory
Higher byte data is stored
8012 85
in memory
8013 EF RST.5 Terminate program

24
Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8501 34H 8503 5AH
8502 26H 8504 00H

Viva-Voice Questions:
1. What is the function of JNC instruction?
2. What is the difference between conditional and unconditional jump instruction?
3. What is the function of STA 2500$ instruction?
4. What is multi byte?

25
PROGRAM NO. 4

DECIMAL ADDITION OF TWO 8 BIT NUMBERS AND SUM IS 16-BIT


Aim: Program for Decimal Addition of Two 8-Bit Numbers and Sum is 16 Bit.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
Two decimal numbers are stored in 8501H and 8502H. The result is to be stored in
8503H location. Consider program starts from memory location 8000H
Algorithm:
 Initialize the memory location of first number in HL register pair.
 Load the first number in accumulator
 Increment the content of HL register pair to initialize the memory location of second data
 Addition of the content of second memory location with first data
 Decimal adjustment of result
 Store the result in memory location 8002H

26
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Address of first number
8000 21 LXI H, 8501 H
in H-L register pair.
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Sum of msb’s & register
8003 0E MVI C,00H
value in 00h
8004 00
Transfer first number in
8005 7E MOV A,M
accumulator.
Increment content of H-L
8006 23 INX H
register pair
Add first number and
8007 86 ADD M
second number
8008 27 DAA
Jump if no carry to
8009 D2 JNC 800CH
800Ch location
Lower byte data is stored
800A 0C
in memory
Higher byte data is stored
800B 80
in memory
800C 0C INR C Increment register C
Data of accumulator is
800D 32 AHEAD STA 8503H
stored into 8503h address
Lower byte data is stored
800E 03
in memory
Higher byte data is stored
800F 85
in memory
8010 79 MOV A,C MSB’S of sum in A
MSB’S of sum in A is
8011 32 STA 8504H transferred to 8504h
location
Lower byte data is stored
8012 04
in memory
Higher byte data is stored
8013 85
in memory
EF RST.5 Terminate program

27
Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8501 34H 8503 5AH
8502 26H 8504 00H

Viva-Voice Questions:
1. What is the function of DAA instruction?
2. What is the difference between the MOV & MVI opcodes?
3. What is the function of JNC instruction?

28
PROGRAM NO. 5

DECIMAL SUBTRACTION OF TWO 8 BIT NUMBERS


Aim: Program for Decimal Subtraction of Two 8-Bit Numbers
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
Two decimal numbers are stored in 8501H and 8502H. The result is to be stored in
8503H location. Consider program starts from memory location 8000H
Algorithm:
 Initialize the memory location of first number in HL register pair.
 Load the 99h number in accumulator
 Perform 9’s complement of the first number
 Increment the content in Accumulator to perform 10’s complement
 decrement the content of HL register pair to initialize the memory location of second data
 Addition of the content of second memory location with first data
 Decimal adjustment of result
 Store the result in memory location 8503H

Viva-Voice Questions:
1. What is the function of DAA instruction?
2. What is the difference between the MOV & MVI opcodes?
3. What is the function of JNC instruction?

29
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Address of first number
8000 21 LXI H, 8501 H
in H-L register pair.
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Copy immediate data 99
8003 3E MVI A,99H
in A
8004 99
8005 96 SUB M 9’s complement
Increment content of A
8006 3C INR A
register
Decrement content of H-
8007 2B DCX H
L register pair
Addition of
8008 86 ADD M complemented data and
the second number
Decimal Accumulator
8009 27 DAA
Adjust
Data of accumulator is
800A 32 STA 8503H
stored into 8503h address
800B 03
800C 85
800D EF RST.5 Terminate program

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8501 34H 8503 5AH
8502 26H 8504 00H

30
PROGRAM NO. 6

ADDITION OF TWO 16-BIT NUMBERS


Aim: Program for addition of two 16-bit numbers.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
Consider the first 16-bit number 02H & 03H is stored in memory location 8501H &
8502h, the second 16-bit number 05H & 04H is stored in memory location 8503H &
8504h.The result after addition of two numbers is to be stored in the memory location
8505 H(LSB’s sum) & 8506H(MSB’s sum). Assume program starts from memory
location 8000H.
Algorithm:
 Start the program by loading HL register pair with address of 1st number.
 Copy the data to DE register pair.
 Load the second number to HL pair.
 Add the two register pair contents and check for carry.
 Store the value of sum and carry in memory locations.
 Terminate the program.
Flow chart:

31
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Get first 16-bit number in
8000 2A LHLD 8501 H
HL
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Save first 16-bit number
8003 EB XCHG
in de
Get second 16-bit
8004 2A LHLD 8503 H
number in HL
Lower byte data is stored
8005 03
in memory
Higher byte data is stored
8006 85
in memory
Get lower byte of the
8007 7B MOV A,E
first number
Add lower byte of the
8008 95 ADD L second number

Store the result in l


8009 6F MOV L,A
register
Get higher byte of the
800A 7A MOV A,D
first number
Add higher byte of
800B 9C ADD H
second number
Store l6-bit result in
800C 67 MOV H,A
memory locations
Store l6-bit result in
800D 22 SHLD 8505H
memory locations
800E 05

800F 85

8010 76 HLT Terminate program

32
Result:
INPUT DATA RESULT
Memory location Data Memory Data
location
8501 02H 8505 07H
8502 03H 8506 07H
8503 05H
8504 04H

Viva-Voice Questions:

1. What is the function of LHLD 8501H instruction?

2. How you can store a data in a memory location?

3. How you can read a data from a memory location?

4. What are flags available in 8085?

5. What is the function of HLT key of a 8085 microprocessor kit

33
PROGRAM NO. 7

SUBTRACTION OF TWO 16
16-BIT NUMBERS
Aim: Program for Subtraction of two 16
16-bit numbers.
Apparatus required: 8085 Microprocessor Kit
Kit, +5V Power supply, keyboard
Theory:
Consider the first 16-bit
bit number 09H & 05H is stored in memory location 8501H
8 &
8502h, the second 16-bit
bit number 05H & 04H is stored in memory location 8503H
8 &
8504h.The result after Subtraction of two numbers is to be stored in the memory location
8505 H(LSB’s difference
difference) & 8506H(MSB’s difference).. Assume program
progra starts from
memory location 8000H.
Algorithm:
 Start the program by loading HL register pair with address of 1st number.
 Copy the data to DE register pair.
 Load the second number to HL pair.
 Subtract the two register pair contents and check for carry.
 Store the value of difference and borrow in memory locations.
 Terminate the program.
Flow chart:

34
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Get first 16-bit number in
8000 2A LHLD 8501 H
HL
Lower byte data is stored
8001 01
in memory
Higher byte data is stored
8002 85
in memory
Save first 16-bit number
8003 EB XCHG
in de
Get second 16-bit
8004 2A LHLD 8503 H
number in HL
Lower byte data is stored
8005 03
in memory
Higher byte data is stored
8006 85
in memory
Get lower byte of the
8007 7B MOV A,E
first number
Subtract lower byte of
8008 95 SUB L the second number

Store the result in l


8009 6F MOV L,A
register
Get higher byte of the
800A 7A MOV A,D
first number
Subtract higher byte of
800B 9C SBB H second number with
borrow
Store l6-bit result in
800C 67 MOV H,A
memory locations
Store l6-bit result in
800D 22 SHLD 8505H
memory locations
800E 05
800F 85
8010 76 HLT Terminate program

35
Result:
INPUT DATA RESULT
Memory location Data Memory Data
location
8501 09H 8505 04H
8502 05H 8506 01H
8503 05H
8504 04H

Viva-Voice Questions:

1. What is the function of LXI H, 8000 H instruction?

2. How you can store a data in a memory location?

3. How you can read a data from a memory location?

4. What are flags available in 8085?

5. What is the function of RESET key of a 8085 microprocessor kit

36
PROGRAM NO. 8

ONE’S COMPLEMENT OF AN 8-BIT NUMBER


Aim: program for one’s complement of an 8-bit numbers
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in memory location 8100H and one’s complement of number will
be stored in location 8101H. Assume the program memory starts from 8000H.
Algorithm:
 Load memory location of data 8100H in H-L registers pair.
 Move data into accumulator
 Complement accumulator
 Store the result in memory location 8101H
Flow chart:

37
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Load Address Of
8000 3A LDA 8100 H Number In H-L Register
Pair
Lower Byte Data Is
8001 00
Stored In Memory
8002 81
Complement
8003 2F CMA
Accumulator
8004 32 STA 8101H Store The Result

8005 01

8006 81

8007 76 HLT Terminate Program

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8100 03 8101 FC

Viva-Voice Questions:
1. What is the function of LDA instruction?
2. What is the function of CMA?
3. What is the function of STA?

38
PROGRAM NO. 9

2’S COMPLEMENT OF AN 8-BIT NUMBER


Aim: program for 2’s complement of an 8-bit numbers
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in memory location 8100H and 2’s complement of number will be
stored in location 8101H. Assume the program memory starts from 8000H.
Algorithm:
 Load memory location of data 8100H in H-L registers pair.
 Move data into accumulator
 Complement accumulator
 Store the result in memory location 8101H
Flow chart:

39
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Load Address Of
8000 3A LDA 8100 H Number In H-L Register
Pair
Lower Byte Data Is
8001 00
Stored In Memory
8002 81
Complement
8003 2F CMA
Accumulator
8004 3C INR A Increment

8005 32 STA 8101H Store The Result

8006 01

8007 81

8008 76 HLT Terminate Program

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8100 02 8101 FE

Viva-Voice Questions:
1. What is the function of LDA instruction?
2. What is the function of CMA?
3. What is the function of STA?
4. Explain the working of 2’s complement
5. What is the function of INR & INX opcodes?

40
PROGRAM NO. 10

ONE’S COMPLEMENT OF A 16-BIT NUMBER


Aim: program for one’s complement of a 16-bit numbers
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in memory location 8100H and one’s complement of number will
be stored in location 8103H. Assume the program memory starts from 8000H.
Algorithm:
 Load memory location of data 8100H in H-L registers pair.
 Move lower data into accumulator
 Complement accumulator
 Store the result in memory location 8102H
 Increment H-L registers pair.
 Move higher data into accumulator
 Complement accumulator
 Store the result in memory location 8103H
Flow chart:

41
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Load Address Of
8000 3A LDA 8100 H Number In H-L Register
Pair
8001 00

8002 81
Move The Number Into
8003 7E MOV A,M
Accumulator
Complement
8004 2F CMA
Accumulator
8005 32 STA 8102H Store The Result

8006 02

8007 81

8008 23 INX H Increment HL Reg.Pair


Move The Number Into
8009 7E MOV A,M
Accumulator
Complement
800A 2F CMA
Accumulator
800B 32 STA 8103H Store The Result

800C 03

800D 81

800E 76 HLT Terminate

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8100 11 8102 6E
8101 11 8103 EE

42
PROGRAM NO. 11

2’S COMPLEMENT OF AN 16-BIT NUMBER


Aim: program for 2’s complement of an 8-bit numbers
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in memory location 8100H and 2’s complement of number will be
stored in location 8103H. Assume the program memory starts from 8000H.
Algorithm:
 Load memory location of data 8100H in H-L registers pair.
 Move data into accumulator
 Complement accumulator
 Store the result in memory location 8101H
Flow chart:

43
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8100 H Load Accumulator
8001 00
8002 81
Move The Number Into
8003 7E MOV A,M
Accumulator
Complement
8004 2F CMA
Accumulator
8005 3C INR A Increment Accumulator
8006 32 STA 8102H Store The Result
8007 02
8008 81
8009 23 INX H Increment HL Reg.Pair
Move The Number Into
800A 7E MOV A,M
Accumulator
Complement
800B 2F CMA
Accumulator
800C 3C INR A Increment Accumulator
800D 32 STA 8103H Store The Result
800E 03
800F 81
8010 76 HLT Terminate

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8100 19 8102 E7
8101 23 8103 DD

44
PROGRAM NO. 12

EXCHANGE CONTENTS OF MEMORY LOCATIONS


Aim: program for exchanging contents of memory locations
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The first number is stored in memory location 8501H and second number will be stored
in location 8502H. The two memory locations will exchange the data. Assume the
program memory starts from 8000H.
Algorithm:
 Load memory location of data 8501H in accumulator.
 Move data of accumulator into B register
 Load memory location of data 8502H in accumulator.
 Store the contents of accumulator at address 8501H
 Move data of B register into accumulator
 Store the contents of accumulator at address 8502H

Flow chart:
START

Load Data into


Accumulator

Copy Data from


A to B Register

Store Data

Copy Data from


B to A Register

Store Data

STOP
45
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8501 H Load Accumulator
8001 01
8002 85
Move Accumulator Data
8003 47 MOV B,A
Into A
8004 3A LDA 8502 H Load Accumulator
8005 02
8006 85
8007 32
8008 01
8009 85
Move Data Into
800A 78 MOV A, B
Accumulator
800B 32 STA 8502H Store The Result
800C 02
800D 85
800E 76 HLT

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8501 12 8501 13
8502 13 8502 12

Viva-Voice Questions:
1. Write A Simple Code To Exchange Data Using XCHG Opcode
2. Difference between LDA & STA opcodes

46
PROGRAM NO. 13

MULTIPLICATION OF TWO 8-BIT NUMBERS


Aim: program to perform 8-bit multiplication of two 8-bit data and store the result in memory.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The HL register pair is first initialized to the start address of memory at which the data is
stored. Then data is brought to accumulator A and the other one is multiplied from
memory itself. The result from A is then stored into memory again using the HL register.
Algorithm:
 Load Accumulator.
 Move the data to a register (E register).
 Get the second data and load into Accumulator.
 Add the two register contents
 Check for carry.
 Increment the value of carry.
 Check whether repeated addition is over and store the value of product and carry in
memory location.
 Terminate the program.
Flow chart:

47
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8500 H Load Accumulator
8001 00
8002 85
Move Accumulator into
8003 5F MOV E, A
E
8004 3A LDA 8501 H Load Accumulator
8005 01
8006 85
Move Accumulator into
8007 4F MOV C, A
C
8008 21 LXI H,0000H Load Data Into HL
8009
800A
Data Of D Register Pair
800B 19 DAD D
With HL Register Pair
800C 0D DCR C Decrement C register
800D C2 JNZ
800E 0B
800F 80
Store HL Register Pair
8010 22 SHLD 8600H Data Into Memory
Location
8011 00
8012 86
8013 76 HLT Terminate

48
Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8500 03 8600 03
8501 01

Viva-Voice Questions:
1. Explain the opcode DAD
2. What is the operands of DAD opcode
3. Explain the SHLD opcode

49
PROGRAM NO. 14
ADDITION OF 5 NUMBERS IN ARRAY
Aim: program to perform addition of 5 numbers in array.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The counter is set for 5 values. The HL register pair is loaded with the 5 numbers
simultaneously by incrementing the HL register pair and decrementing the counter till
zero. The result is stored in the last memory address. Assume that program starts at
8000h address location.
Algorithm:
 C register is used as counter and loaded with 05h and accumulator with 00h numbers.
 Load HL register pair with 8200h
 The accumulator 00h value and the first number is added and stored in A
 HL register pair is incremented and the counter is decremented till counter =00h
 The result is stored in memory
Flow chart:

50
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 0E MVI C,05H Counter Set to 05h
8001 05
8002 3E MVI A,00H Accumulator Set to 00h
8003 00
Load HL register pair
8004 LXI H,8200H
with immediate data
8005 00
8006 82
8007 86 BACK ADD M Add to memory
Increment HL Register
8008 23 INX H
Pair
8009 0D DCR C Decrement Counter
800A C2 JNZ BACK Jump if no zero
800B 07
800C 80
Copy Data Of A To
800D 77 MOV M,A
Memory
800E 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 05
8201 10
8202 15 8205 75
8203 20
8204 25
Viva Questions:
1. What is the function of JNZ opcode
2. Difference between HALT and RST instructions

51
PROGRAM NO. 15
LARGEST BETWEEN TWO NUMBERS
Aim: Program to find largest between two numbers.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The first number is stored in 8200h and the HL reg.pair is incremented and the second
number is compared with the first. Depending on the sign flag (if positive) first number is
stored in memory.
Algorithm:
 Load HL register pair with 8200h
 Copy first number into A.
 HL register pair is incremented.
 First and second number are compared and result is stored in memory
Flow chart:

52
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Load data into HL
8000 21 LXI H, 8200 H
register pair
8001 00
8002 82
8003 7E MOV A, M Copy data into A
Increment HL register
8004 D3 INX H
pair
8005 BE CMP M Compare
8006 F2 JP 800AH Jump on Plus
8007 0A
8008 80
8009 7E MOV A, M Copy data into A
Store The Result In
800A 32 STA 8400H
Memory
800B 00
800C 84
800D 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 03 8400 03
8201 01

Viva-Voice Questions:
1. Explain the 8085 FLAGS.
2. Explain the instruction JP.
3. Explain about CMP instruction.

53
PROGRAM NO. 16
SMALLEST BETWEEN TWO NUMBERS
Aim: Program to find smallest between two numbers.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The first number is stored in 8200h and the HL reg.pair is incremented and the second
number is compared with the first. Depending on the sign flag number is stored in
memory.
Algorithm:
 Load HL register pair with 8200h
 Copy first number into A.
 HL register pair is incremented.
 First and second number are compared and result is stored in memory
Flow chart:

54
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
Load data into HL
8000 21 LXI H, 8200 H
register pair
8001 00
8002 82
8003 7E MOV A, M Copy data into A
Increment HL register
8004 D3 INX H
pair
8005 BE CMP M Compare
8006 F2 JM 800AH Jump on Minus
8007 0A
8008 80
8009 7E MOV A, M Copy data into A
Store The Result In
800A 32 FW STA 8400H
Memory
800B 00
800C 84
800D 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 01 8400 01
8201 03

Viva-Voice Questions:
1. Explain the 8085 FLAGS.
2. Explain the instruction JM.
3. Explain about CMP instruction.

55
PROGRAM NO. 17
MULTIPLYING A NUMBER BY 2
Aim: Program to Perform Multiplication.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in accumulator and rotated left is performed and the result is stored
in memory.
Algorithm:
 Load A with 8200h.
 The Number Is Rotated Left
 Result Is Stored In Memory
Flow chart:

START

LOAD NUMBER
INTO
ACCUMULATOR

ROTATE LEFT

STORE
RESULT
FROM A TO
MEMORY

STOP

56
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8200H Load Accumulator
8001 00
8002 82
8003 07 RLC Rotate Left
Store The Result In
8004 32 STA 8400H
Memory
8005 00
8006 84
8007 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 06 8400 0C

Viva-Voice Questions:
1. Explain rotate left with example.
2. Differentiate different rotate operations.

57
PROGRAM NO. 18
MULTIPLYING A NUMBER BY 4
Aim: Program to Perform Multiplication.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in accumulator and rotate left is performed twice and the result is
stored in memory.
Algorithm:
 Load A with 8200h.
 The Number Is Rotated Left twice.
 Result Is Stored In Memory
Flow chart:

START

LOAD NUMBER
INTO
ACCUMULATOR

ROTATE LEFT

STORE
RESULT
FROM A TO
MEMORY

STOP

58
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8200H Load Accumulator
8001 00
8002 82
8003 07 RLC Rotate Left
8004 07 RLC Rotate Left
Store The Result In
8005 32 STA 8400H
Memory
8006 00
8007 84
8008 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 02 8400 08

Viva-Voice Questions:
1. Explain rotate left with example.
2. Differentiate different rotate operations.

59
PROGRAM NO. 19
DIVIDING A NUMBER BY 2
Aim: Program to Perform Division.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in accumulator and rotate right is performed and the result is stored
in memory.
Algorithm:
 Load A with 8200h.
 The Number Is Rotate right
 Result Is Stored In Memory
Flow chart:

START

LOAD NUMBER
INTO
ACCUMULATOR

ROTATE RIGHT

STORE
RESULT
FROM A TO
MEMORY

STOP

60
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8200H Load Accumulator
8001 00
8002 82
8003 07 RRC Rotate Right
Store The Result In
8004 32 STA 8201H
Memory
8005 00
8006 84
8007 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 03 8201 FC

Viva-Voice Questions:
1. Explain rotate right with example.
2. Differentiate different rotate operations.

61
PROGRAM NO. 20
FIND ODD OR EVEN NUMBER
Aim: Program to find even or odd number.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The number is stored in accumulator and rotated right is performed. If the number is even
EE is printed and if the number is odd 00 is printed.
Algorithm:
 Store the input data bytes in consecutive memory locations.
 Rotate right.
 Fetch each data byte and check it D0 bit.
 If D0 bit=0 then even, else odd.
 Store odd in memory locations. Store even in some other consecutive memory location.
Flow chart:

START

LOAD NUMBER
INTO
ACCUMULATOR

ROTATE right and


if even print EEH
and if odd print 00h

STORE
RESULT
FROM A TO
MEMORY

STOP

62
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 3A LDA 8200H Load Accumulator
8001 00
8002 82
8003 07 RRC Rotate Right
8004 32 MVI A,00H Print 00 if odd
8005 00
8006 0A JC FN Jump on carry
8007 0B
8008 80
8009 3E MVI A,EEH Print EE if EVEN
800A EE
Store The Result In
800B 32 FN STA 8400H
Memory
800C 00
800D 84
800E 76 HLT Halt

Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8200 03 8400 00

Viva-Voice Questions:
1. Explain the load operations.
2. Explain jump instructions.
3. Explain store operations.

63
PROGRAM NO. 21

DIVISION OF TWO 8-BIT NUMBERS


Aim: program to perform division of two 8-bit data and store the result in memory.
Apparatus required: 8085 Microprocessor Kit, +5V Power supply, keyboard
Theory:
The HL register pair is first initialized to the start address of memory at which the data is
stored. Then data is brought to accumulator A. DAD & subtraction is used to perform
division. The result from A is then stored into memory again using the HL register.
Algorithm:
 Start the program by loading HL register pair with address of memory location.
 Move the data to a register (B register).
 Get the second data and load into Accumulator.
 Compare the two numbers to check for carry.
 Subtract the two numbers.
 Increment the value of carry.
 Check whether repeated subtraction is over and store the value of product and carry in
memory location.
 Terminate the program.

64
Flow chart:

65
Program:
Machine
Memory
Codes Labels Mnemonics Operands Comments
Address
(Data)
8000 2A LHLD 8501H Load into HL
8001 01
8002 85
8003 3A LDA 8503 H Load Accumulator
8004 03
8005 85
Move Accumulator into
8006 47 MOV B, A
B
8007 0E MVI C,08H Initialize Counter
8008 08
Data Of D Register Pair
8009 29 LOOP DAD D
With HL Register Pair
800A 7C MOV A,H Move H data into A
800B 90 SUB B Subtract B
800C 0A JC AHEAD Jump on carry
800D 11
800E 80
800F 67 MOV H,A Move A data into H
8010 2C INR L Increment L
8011 0D DCR C Decrement C
8012 C2 JNZ LOOP Jump on no zero
8013 09
8014 80
8015 22 SHLD 8504H Store Result
8016 04
8017 85
8018 EF HLT Terminate

66
Result:
INPUT DATA RESULT
Memory location Data Memory location Data
8501 54 8504 09
8502 00 8505 03
8503 09

Viva-Voice Questions:
1. Explain Branch Instructions.
2. Explain jump instructions.
3. Explain DAD instruction.

67
PROGRAM NO. 22

TRAFFIC LIGHT INTERFACE


Aim: Program for Interfacing Traffic Light Controller with 8085 Microprocessor trainer kit and
simulating the sequence of traffic light states.
Apparatus Required: 8085 Microprocessor Kit, +5v Power Supply, Keyboard, Traffic Light
Controller, Flat Ribbon Cable.
Theory:
Combination of Red, Amber and Green LEDs are provided to indicate Halt, Wait and Go
states for vehicles. Combination of Red and Green LEDs are provided for pedestrian
crossing. 36 LEDs are arranged in the form of an intersection. At the left corner of each
road, a group of 5 LEDs (Red, Amber and Green) are arranged in the form of a T section
to control the traffic of that road. Each road is named as North N, South S, East E and
West W. L1,L10, L19 and L28 (Red) are for stop signal for the vehicles on the road
N,S,W and E respectively. L2, L11, L20 and L29 (Amber) indicate wait state for the
vehicles on the road N, S, E and W respectively.
L3, L4 and L5 (Green) are for left, straight and right turn for the vehicles on the road S.
Similarly L12 - L13 - L14, L23 - L22 - L21 and L32 - L31 - L30 simulates same function
for the Roads E, N & W respectively. A total of 16 LEDs (2 Red & 2 Green at each road)
are provided for pedestrian crossing. L7 - L9, L16 - L18, L25 - L27 & L34 - L36 (Green)
when on allows pedestrians to cross and L6 - L8, L15 - L17, L24 - L26 & L33 - L35
(Red) when on alarms the pedestrians to wait. To minimize the hardware pedestrians
indicator LEDs (both Green and Red) are connected to some port lines (PC4 to PC7) with
Red inverted. Red LED’s L10 and L28 are connected to port lines PC2 to PC3 while L1
and L19 are connected to lines PC0 and PC1 after inversion. All other LEDs (Amber and
Green) are connected to Port A and port B.

68
69
Program:

70
CYCLE-2
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

Introduction to MASM
The Microsoft macro assembler is an x86 high level assembler for DOS and Microsoft
windows. It supports wide varieties of macro facilities and structured programming
idioms including high level functions for looping and procedures
A program called assembler used to convert the mnemonics of instructions along with
the data into the equivalent object code modules, these object code may further
converted into executable code using linked and loader programs. This type of program
is called as ASSEMBLY LANGUAGE PROGRAMMING. The assembler converts and
Assembly language source file to machine code the binary equivalent of the assembly
language program. In this respect, the assembler reads an ASCII source file from the
disk and program as output. The major different between compilers for a high level
language like PASCAL and an Assembler is that the compiler usually emits several
machine instructions for each PASCAL statement. The assembler generally emits a
single machine instruction for each assembler language statement.
Attempting to write a program in machine language is not particularly bright. This
process is very tedious, mistakes, and offers almost no advantages over programming in
assembly language. The major disadvantages over programming in assembly language
over pure machine code are that you must first assemble and link a program before you
can execute it. However attempting to assemble the code by hand would take for longer
than the small amount of time that the assembler takes the perform conversion for you.
An assembler like Microsoft Macro Assembler (MASM) provides a large number of
features for assembly language programmers. Although learning about these features
take a fair amount of time. They are so useful that it is well worth the effort.

Microsoft MASM version 6.11 contains updated software capable of processing


printing instructions. Machine codes and instruction cycle counts are generated by
MASM for all instructions on each processor beginning with 8086. To assemble the
file PROG.ASM use this command: (better to use DOS command line)

MASM PROG.ASM
The MASM program will assemble the PROG.ASM file. (To create PROG.OBJ
from PROG.ASM)

To create PROG.EXE from PROG.OBJ, use this LINK command:


LINK PROG.OBJ
It converts the contents of PROG.OBJ into PROG.EXE.

To link more than one object file use + signs between their file names as in:
LINK PROGA+PROGB+PROGC

71
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

The following is a list of MASM reserved words:

ASSUME assume definition


CODE begin code segment
DATA begin data segment DB define byte
DD define double word
DQ define quad word
DS define storage
DUP duplicate
DW define word
ELSE else statement
END end program
ENDM end macro
ENDIF end if statement
ENDP end procedure
ENDS end segment
EQU equate
IF if statement
FAR far reference
MACRO define macro
.MODEL model type
NEAR near reference
OFFSET offset
ORQ origin
PARA paragraph
PROC define procedure
.EXIT generate exit code
PUBLIC public reference
SEG locate segment
SEGMENT define segment
PTR pointer

USING DEBUG TO EXECUTE THE 80x86 PROGRAM:


DEBUG is a utility program that allows a user to load an 80x 86 programs into memory and execute it
step by step. DEBUG displays the contents of all processor registers after each instruction execute,
allowing the user to determine if the code is performing the desired task. DEBUG only displays the 16-
bit portion of the general purpose registers. Code view is capable of displaying the entire 32 bits.
DEBUG is a very useful debugging tool. We will use DEBUG to step through a number of simple
programs, gaining familiarity with Debug’s commands as we do so. DEBUG contains commands that
can display and modify memory, assemble instructions, disassemble code already placed into memory,
trace single or multiple instructions, load registers with data and do much more.

72
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
DEBUG loads into memory like any other program, in the first available slot. The memory space used by
www.foxitsoftware.com/shopping

DEBUG for the user program begins after the end of Debug’s code. If an .EXE or .COM file were
specified, DEBUG would load the program according to accepted DOS conventions.

To execute the program file PROG.EXE use this command


DEBUG PROG.EXE

DEBUG uses a minus sign as its command prompt, so should see a “-“ appear on display.

To get a list of some commands available with DEBUG is :


T -- trace (step by step execution)
U -- un assemble
D -- dump
G -- go (complete execution)
H -- Hex

To execute the program file PROG.ASM use the following procedure:


.MASM PROG.ASM
.LINK PROG.OBJ
.DEBUG PROG.EXE

ASSEMBLER DIRECTIVES: The limits are given to the assembler using some pre defined alphabetical
strings called Assembler Directives which help assembler to correctly understand. The assembly
language programs to prepare the codes.

DB GROUP EXTRN
DW LABEL TYPE
DQ LENGTH EVEN
DT LOCAL SEGMENT
ASSUME NAME
END OFFSET
ENDP ORG
ENDS PROC
EQU PTR
73
DB-Define Byte: The DB drive is used to reserve byte of memory locations in the available on memory.
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

USING DEBUG TO EXECUTE THE 80x86 PROGRAM:


DEBUG is a utility program that allows a user to load an 80x 86 programs into memory and execute
it step by step. DEBUG displays the contents of all processor registers after each instruction execute,
allowing the user to determine if the code is performing the desired task. DEBUG only displays the
16-bit portion of the general purpose registers. Code view is capable of displaying the entire 32 bits.
DEBUG is a very useful debugging tool. We will use DEBUG to step through a number of simple
programs, gaining familiarity with Debug’s commands as we do so. DEBUG contains commands
that can display and modify memory, assemble instructions, disassemble code already placed into
memory, trace single or multiple instructions, load registers with data and do much more.

DEBUG loads into memory like any other program, in the first available slot. The memory space
used by DEBUG for the user program begins after the end of Debug’s code. If an .EXE or .COM file
were specified, DEBUG would load the program according to accepted DOS conventions.

To execute the program file PROG.EXE use this command


DEBUG PROG.EXE

DEBUG uses a minus sign as its command prompt, so should see a “-“ appear on display.
To get a list of some commands available with DEBUG is:
T -- trace (step by step execution)
U -- un assemble
D -- dump
G -- go (complete execution)
H -- Hex

To execute the program file PROG.ASM use the following procedure:


.MASM PROG.ASM
.LINK PROG.OBJ
.DEBUG PROG.EXE

74
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping
ASSEMBLER DIRECTIVES: The limits are given to the assembler using some pre defined alphabet-
ical strings called Assembler Directives which help assembler to correctly understand. The assembly
language programs to prepare the codes.

DB GROUP EXTRN
DW LABEL TYPE
DQ LENGTH EVEN
DT LOCAL SEGMENT
ASSUME NAME
END OFFSET
ENDP ORG
ENDS PROC
EQU PTR

DB-Define Byte: The DB drive is used to reserve byte of memory locations in the available on
memory.

DW-Define Word: The DW drive is used to reserve 16 byte of memory location available on memory.

DQ-Define Quad Word (4 words): The DB directives is used to reserve 8 bytes of memory locations
in the memory available.

DT-Define Ten Byte: The DT directive is used to reserve 10 byte of memory locations in the available
memory.

ASSUME: Assume local segment name the Assume directive is used to inform the assembler. The
name of the logical segments to be assumed for different segment used in programs.

END: End of the program the END directive marks the end of an ALP.

ENDP: End of the procedure.

ENDS: End of the segment.

EQU: The directive is used to assign a label with a variable or symbol. The directive is just to reduce
recurrence of the numerical values or constants in the program.

OFFSET: Specifies offset address.

SEGMENT: The segment directive marks the starting of the logical segment.

EXECUTION OF ASSEMBLY LANGUAGE PROGRAMMING IN MASM SOFTWARE:


Assembly language programming has 4 steps.
1. Entering Program
2. Compile Program
3. Linking a Program
4. Debugging a Program

75
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

76
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

1. To implement assembly language program for addition of two 16-bit numbers.

PROGRAM:
DATA SEGMENT
N1 DW 0005H
N2 DW 0002H
RES DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA START: MOV AX,
DATA
MOV DS, AX
MOV AX, N1
MOV BX, N2
ADD AX, BX
MOV RES, AX
INT 21H
CODE ENDS
END START

RESULT:
AX = 0007h

2. To implement assembly language program for subtraction of two 16-bit numbers.

PROGRAM:
DATA SEGMENT
N1 DW 0005H
N2 DW 0003H
RES DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AX,N1
MOV BX,N2
SUB AX,BX
MOV RES,AX
INT 21H
CODE ENDS
END START

RESULT:
AX = 0002h

77
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

3. To implement ALP to find average of 8-bit numbers in array.

ALGORITHM:
1. Start.
2. Initialize counter = 10.
3. Initialize array pointer.
4. Sum = 0.
5. Get the array element pointed by array pointer.
6. Add array element in the Sum.
7. Increment array pointer decrement counter.
8. Repeat steps 4, 5 & 6 until counter = 0.
9. Display Sum.
10. Stop.

PROGRAM:
DATA SEGMENT
ARRAY DB 12H, 24H, 26H, 63H, 25H, 86H, 2FH, 33H, 10H, 35H
SUM DW 0
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA

START: MOV AX, DATA


MOV DS, AX
MOV CL, 10
XOR DI, DI
LEA BX, ARRAY
BACK: MOV AL, [BX+DI]
MOV AH, 00H
MOV SUM, AX
INC DI
DEC CL
JNZ BACK
INT 21
CODE ENDS
END START

RESULT:
AX = 0211h

78
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
4 implement ALP to find the maximum number in the array. www.foxitsoftware.com/shopping

ALGORITHM:
1. Start.
2. Initialize data segment.
3. Initialize the pointer.
4. Initialize counter = 0.
5. Initialize the array base pointer.
6. Get the maximum number.
7. Compare the number with maximum number.
8. If num> MAX, Max = num & increment pointer.
9. Decrement the counter.
10. If count = 0 stop or else repeat steps 6, 7, 8, 9.
11. Store maximum number.
12. Stop.
PROGRAM:
DATA SEGMENT
N DB 05H
LIST DB 12H,34H,41H,
10H,15H
DATA ENDS
CODE SEGMENT

ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV AX,0000H
LEA SI,LIST
MOV AL,[SI]
INC SI
MOV CL,N
DEC CL
BACK:
CMP AL,[SI]
JNC MAX
MOV AL,[SI]
MAX:
INC SI
DEC CL
JNZ BACK
INT 03H

CODE ENDS
END START

RESULT:
79
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping
5. To implement ALP to search a number in an array.

PROGARM:
DATA SEGMENT
N DB 05H
LIST DB 12H,34H,41H,10H,15H
ITEM DB 34H
DATA ENDS CODE
SEGMENT

ASSUME CS: CODE, DS: DATA


START:
MOV DS,AX
MOV AX,0000H
LEA SI,LIST
MOV CL,05
MOV AL,[SI]
INC SI
BACK:
CMP AL,ITEM
JZ DISPLAY
MOV AL,[SI]
INC SI
DEC CL
JNZ BACK MOV AX,
0000H DISPLAY:
INT 03H
CODE ENDS
END START

80
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

6. program to perform multiplication of two 16bits numbers

DATA SEGMENT
N1 DW 0005H
N2 DW 0002H
RES1 DW ?
RES2 DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS: DATA
START:
MOV AX,DATA
MOV DS,AX
MOV DX , 0000H
MOV AX ,N1
MOV BX, N2
MUL BX
MOV RES1 , AX
MOV RES2 , BX
INT 03H
CODE ENDS
END START
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

7. WRITE AN ALP TO PERFORM DIVISION OF TWO 16BIT NUMBERS USING 8086

DATA SEGMENT
N1 DW 4444H
N2 DW 2222H
RES1 DW ?
RES2 DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV AX,N1
MOV DX,0000H
MOV BX,N2
DIV BX
MOV RES1,AX
MOV RES2,DX
INT 03H
CODE ENDS
END START
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

8. ADDITION OF (N numbers) USING 8086

DATA SEGMENT
ARRAY DB 12H,14H,21H,10H,15H
SUM DW 0
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV CL,05H
XOR DI,DI
LEA BX,ARRAY
BACK:
MOV AL,[BX+DI]
MOV AH,00
ADD SUM,AX
MOV AX,SUM
INC DI
JNZ BACK
INT 03H
CODE ENDS
END START
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping

9. WRITE AN ALP TO FIND SMALLEST OF (N numbers) USING 8086

DATA SEGMENT
N DB 05H
LIST DB 12H,34H,41H,10H,15H
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE , DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV AX,0000H
LEA SI,LIST
MOV AL,[SI]
INC SI
MOV CL,N
DEC CL
BACK:
CMP AL,[SI]
JC MIN
MOV AL,[SI]
MAX:
INC SI
DEC CL
JNZ BACK
INT 03H
CODE ENDS
END START
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping
10. program to perform ASCENDING SORT numbers

DATA SEGMENT
STRING1 DB 99H,12H,56H,45H,36H
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV CH,04H

UP2:
MOV CL,04H
LEA SI,STRING1

UP1:
MOV AL,[SI]
MOV BL,[SI+1]
CMP AL,BL
JC DOWN
MOV DL,[SI+1]
XCHG [SI],DL
MOV [SI+1],DL
DOWN:
INC SI
DRC CL
JNZ UP1
DEC CH
JNZ UP2
INT 03
CODE ENDS
END START

You might also like