You are on page 1of 9

Programming of microprocessor

Module-11
Lecture-5
Computer Programming:
From the discussion of microprocessor 8085 and 8086, it is clear that all the instructions
are nothing but the combination of 0s and 1s. 0 indicates a lo! signal "ma# be $oltage
0%& and 1 indicates a high signal "ma# be $oltage 5%&.
'n the other hand all the computers !or( on %on )euman stored program principle. *e
ha$e to store the computer program, !hich is nothing but the set of instruction and
computer e+ecutes them one other another as per the program re,uirement.
*e ha$e to store the program "i.e. set of instructions& in computer memor#. -he starting
address of the program must be specified !hich e+ecuting the program and it is loaded
into the program counter. .fter that program counter (eeps trac( of the e+ecution of the
program till the end of the program.
/$er# program must be end !ith a stop or terminating instruction, other!ise the control
unit !ill (eep on fetching information from memor#. 'nce it encounters a halt or stop
instruction, e+ecution stops.
0imple /+ample1
2onsider that !e !ant to add t!o numbers 35 and 43.
-he first re,uirement is to (eep this t!o numbers in memor#.
)e+t !e ha$e to !rite a program to fetch this t!o numbers from memor# to 256 and add
this t!o numbers. Finall# the result has to be stored in memor#.
.gain !e ha$e to load the program in memor#. *e must (no! the memor# address of the
first instruction !here !e ha$e stored the program.
*hile e+ecuting the program counter must be loaded !ith the starting address of the
program. -he control unit !ill generate appropriate signal to perform the re,uired tas(.
2onsider that !e are using 7ntel 8085 microprocessor to sol$e this problem. -he tas(
re,uired to perform this operation1
8et the t!o numbers from memor# to general purpose register.
5erform the addition operation
0tore the result bac( into the memor#.
Finer details !ith respect to 80851
.ssume that !e store number 35 in memor# location 1000H . -he result !ill be stored in
memor# location 1009H .
'ne possible solution1
0tep 11 First load the content of memor# location 1000H to accumulator.
1000 : 00 10 LDA H A
"machine instruction in he+adecimal&
-he format of this instruction is opcode Lo!-order address high order address
opcode of L;. is 00 11 10 10 i.e.
:
H
A
0tep 91 Mo$e the contents of accumulator to register B.
MOV B A
-he format of this instruction is
0 1 D D D S S S
i.e. 0 1 0 0 0 1 1 1
<3
H
"Machine instruction in he+adecimal&
0tep :1 Load the content of memor# location 1001H to accumulator
1001 : 01 10 LDA H A
"Machine instruction in he+adecimal&
0tep <1 .dd the content of register B to accumulator and store the result in accumulator.
ADD B
-he format of this instruction is
1 0 0 0 0 0 0 0
i.e. 1 0 0 0 0 0 0 0
80
H
"Machine instruction in he+adecimal&
0tep 51 0tore the result that is present in accumulator to memor# location 1009H
1009 STA H
: 9 0 9 1 0 "Machine instruction in he+adecimal&
opcode Lo!er order address higher order address
opcode of STA is 0 0 1 1 0 0 1 0 i.e.
:9
H
0tep 6 1 HALT 1 to indicate the end of program
'pcode of =.L- 0 1 1 1 0 1 1 0 36H
;estination
register
0ource
register
0ource register
-herefore to carr# out this addition, !e ha$e to perform these fi$e operation.
7n this e+ample !e assume that data are a$ailable in memor# and storing the result in
memor#.
>ut if !e !ant to ta(e the input from some input de$ice "li(e (e# board&, first !e ha$e to
accept the input from (e#board and stored in some memor# location. 0imilarl# to displa#
the result in monitor, !e ha$e to get the result from memor# and displa# in monitor !ith
the help of some
o
instruction.
-he complete program is 1
Memor# location
0100H :A 00 10 LDA
010:H <3 MOVBA
010<H :A 01 10 LDA 1001H
0103H 80 ADD B
0108H :9 09 10 STA 1002H
010BH 36 HLT
7f !e store this program from memor# location 100H , then the contents of memor# is
sho!n belo!1
Memor# .ddress in =e+
in =e+
Fig
*hile e+ecuting the program, the program counter "52& !ill be loaded !ith the starting
memor# address of this program, i.e. 0100H.
-he processor !ill start e+ecution this program starting from memor# location 0100H
and it (eeps on doing the e+ecution ?ob till it encounters a halt instruction.
7f !e program the microprocessor in this !a#, i.e !riting the machine code directl#, it is
(no!n as machine language programming.
-he main ad$antage of machine language programming is that the memor# control is
directl# in the hands of the programmer, so that, he@she ma# able to manage the memor#
of the s#stem more efficientl#.
-he disad$antages of machine language programming are more prominent.
-he programming, coding and resource management techni,ues are tedious. -he
programmer has to ta(e care of al these functions.
-he programs are difficult to understand unless one has a thorough technical (no!ledge
of the processor architecture and the instruction set.
.lso it is difficult to remember the machine code of each and e$er# instruction,.
.ssembl# Language programming1
-he assembl# language programming is simpler as compared to the machine language
programming.
-he instruction mnemonics are directl# used in the assembl# language programming.
-here is an one-to Aone correspondence bet!een instruction mnemonics and machine
instruction.
.n assembler is used to translate the assembl# language programming to machine code.
-he program !ritten in assembl# language programming is more readable than machine
language programming.
-he main impro$ement in assembl# language o$er machine language is that the address
$alue and the contents can be identified b# labels.
-he assembl# language instruction se,uence for 8085 microprocessor of the pre$iousl#
discussed program !ill loo( li(e1
1000 LDA H
@@ Load accumulator from memor# address 1000H
, MOV B A
@@ Mo$e the content from accumulator to B
1001 LDA H
@@ Load accumulator from memor# address 1001H
ADD B
@@ .dd the content of > to accumulator and store the result in accumulator
1009 STA H
@@ 0tore the content of accumulator in memor# location 1009H
HLT @@ =alt
.nother assembl# language instruction se,uence to sol$e the same problem.
, 00 MVI L H
@@ Load lo!er order b#te of address 00H to register L
,10 MVI H H
@@ Load higher order b#te of address 10H to register H
, MVI B M
@@ Mo$e the contents of memor# location addressed b# H L pair to B
01 10 LDA H H
@@ Load the accumulator from memor# location 1001H
ADD B
@@ .dd the content of B to accumulator
09 10 STA H H
@@ 0tore the content of accumulator to memor# location 1009H
HLT @@ =alt.
-he H-L register pair is loaded !ith 1000H !hich is the address of first input. -his is
done in first t!o instruction.
Mo$ing the contents of memor# location 1000H to the register B. 7t is done in third
instruction.
)e+t !e are loading the accumulator from the memor# location 1001H, !hich is the
second input. -his is done in fourth instruction.
-e content of register B is added !ith the content of accumulator and store the result in
accumulator. 7t is done in fifth instruction.
-he content of accumulator is stored in memor# location 1009H. 7t is done in si+th
instruction.
0e$enth instruction is to halt the processor, i.e, to stop the program e+ecution.
-he first t!o instructions can be replaced
,1000 LXI H H
.ssembl# language instruction se,uence for the same program for 8086 microprocessor.
2onsider that data segment starts from 90000H and code segment starts from 1000H.
, 9000 MOV CX H
@@ 7nitialiBe DS at 9000H
, MOV DS CX
@@
[ ]
, 1000 MOV AX H
@@ 8et first operand in AX
[ ]
, 1001 MOV BX H
@@ 8et second operand in BX
, ADD AX BX
@@ 5erform addition
[ ]
1009 , MOV H AX
@@ 0tore the result in location 1009H
HLT @@ 0top
0ince the immediate data cannot be loaded into a segment register, the data is transferred
to one of the general purpose register, i.e. CX, and then the register content is mo$ed to
the segment register DS.
-he data segment register contains 9000H.
-he effecti$e address of the operands are
900011001
i.e. 91000H and 91001H
-he result is stored in memor# location 91009H .
5roblem1 Find out the largest number from an unordered arra# of si+teen 8 bit numbers
stored se,uentiall# in the memor# locations starting at 0900H .
.ssembl# language instruction se,uence for 8085 microprocessor.
, MVI B OFH
@@ put 'F= in register > to count the number of input elements
, 0900 LXI H H
@@ Load the register pair =-L b# 0900H !hich is address of the first
element.
, MOV A M
@@ -he content of memor# location pointed b# =-L register pair is mo$ed
to accumulator.
BACK1
INX H
@@ 7ncrement the register pair =-L to get the ne+t element
CMP M P
-he content of the memor# location !hose address is in =-L pair is subtracted
from the accumulator. -he accumulator remain unchanged. 1 CY = if
( ) ( ) ( )
A H L <
JNC NEXT
@@ Cump to the le$el )/D- if the carr# is not set, i.e, content of
accumulator is biggest so far.
, MOV A M
@@ if carr# is set, the content of the memor# addressed b# ( ) H L
pair is
biggest so far, so putting it in accumulator.
NEXT1
DCR B
@@ ;ecrement the content B, to indicate !e ha$e chec(ed one more
element
JNZ BACK
@@ ?ump on not Bero, content of B indicates numbers of elements to read,
and repeat the process.
HLT @@ FinaliBe the input and store.
.ssembl# language instruction se,uence for 8086 microprocessor
=ere assume that the data segment stands from memor# location 10000H
, MOV CX OFH
@@ initialiBe counter for number of iteration
,1000 MOV AX H
@@ initialiBe data segment
, MOV DS AX
@@
0900 MOV SI H
@@ initialiBe source pointer
[ ]
, MOV AX SI
@@ ta(e first number in AX
BACK1
INC SI
@@ increment source pointer.
[ ]
, CMP AX SI
@@ compare ne+t number !ith the pre$ious
JNC NEXT
@@ 7f the ne+t number is smaller, ?ump to NEXT.
[ ]
MOV A SI
@@ 7f the ne+t number is bigger, replace the pre$ious one !ith the ne+t
element
NEXT1 LOOP BACK // Eepeat the procedure for 15 times.
HLT
CMP operation subtract the source operand from the destination operand, but does not
store the result an# !here. -he carr# flag is set if the source operand is greater than the
destination operand.
LOOP1 -his instruction e+ecutes the part of the program from the label in the instruction
to the loop instruction, CX number of times. .t each iteration CX is decremented
automaticall#. -his instruction basicall# implements decrement counter and ?ump if not
Bero structure.
/+ample1 *rite a program to mo$e a string of data b#tes from offset 1000H to 9000H .
-he length of the string is FFH .
.ssembl# language instruction se,uence for 8085 microprocessor1
, 1000
, 9000
,
L I H H
L I D H
MVI C FFH

L''51
, MOV A M

STAX D
@@ -he content of register . is mo$ed to the memor# location
!hose address is in register pair D-E

INX H


INX D

DCR C


JNZ LOOP
@@ Cump on not Bero, i.e. the result of decrement is not Bero.
HLT
.ssembl# language instruction se,uence for 8086 microprocessor.
7n 8086, !e ha$e to define the data segment b# setting the DS register. .ssume that DS is
set appropriatel#
, 1000
, 9000
,
MOV SI H
MOV DI H
MOV CX FFH
L''51 [ ]
, MOV AX SI
[ ]
, MOV DI AX


INC SI

INC DI

DEC CX

JNZ LOOP
HLT
-he program listing is similar to the program of 8085, e$er# instruction of 8085 is
replaced b# an e,ui$alent instruction of 8086.
-herefore, the abo$e program listing is correct. >ut this is not an efficient implementation
for 8086, because !e ha$e not used an# ad$ance feature of 8086.
.lternate program listing for 8086 microprocessor
.ssume that the data segment register and e+tra segment registers are set appropriatel#.
, 1000
, 9000
,
MOV SI H
MOV DI H
MOV CX FFH
CLD
REP MOVSB
CLD instruction clears the direction flag.
7f the direction flag bit is 0, the string is processed in auto increment mode.
REP 1 -his instruction is used as a prefi+ to other instructions. -he instruction to !hich
the REP prefi+ is pro$ided, is e+ecuted repeatedl# until the CX register becomes
Bero. .t each iteration CX is decremented b# one automaticall#. *hen CX
becomes Bero, the e+ecution proceeds to the ne+t instruction in se,uence.
MOVSB 1 . string of b#tes stored in a set of consecuti$e memor# locations is mo$ed to
another set of destination locations.

You might also like