Programming of Microprocessor
Programming of Microprocessor
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 low signal (may be voltage
0V) and 1 indicates a high signal (may be voltage 5V).
On the other hand all the computers work on Von Neuman stored program principle. We
have to store the computer program, which is nothing but the set of instruction and
computer executes them one other another as per the program requirement.
We have to store the program (i.e. set of instructions) in computer memory. The starting
address of the program must be specified which executing the program and it is loaded
into the program counter. After that program counter keeps track of the execution of the
program till the end of the program.
Every program must be end with a stop or terminating instruction, otherwise the control
unit will keep on fetching information from memory. Once it encounters a halt or stop
instruction, execution stops.
Simple Example:
Consider that we want to add two numbers 75 and 97.
The first requirement is to keep this two numbers in memory.
Next we have to write a program to fetch this two numbers from memory to CPU and add
this two numbers. Finally the result has to be stored in memory.
Again we have to load the program in memory. We must know the memory address of the
first instruction where we have stored the program.
While executing the program counter must be loaded with the starting address of the
program. The control unit will generate appropriate signal to perform the required task.
Consider that we are using Intel 8085 microprocessor to solve this problem. The task
required to perform this operation:
Get the two numbers from memory to general purpose register.
Perform the addition operation
Store the result back into the memory.
Finer details with respect to 8085:
Assume that we store number 75 in memory location 1000H . The result will be stored in
memory location 1002H .
One possible solution:
The format of this instruction is opcode Low-order address high order address
opcode of LDA is 00 11 10 10 i.e. 3 AH
Step 2: Move the contents of accumulator to register B.
MOV B A
The format of this instruction is
01
DDD
SSS
Destination Source
register
register
i.e. 0 1
000
111
1001H
Step : Add the content of register B to accumulator and store the result in accumulator.
ADD B
The format of this instruction is
10000
i.e.
S S S
Source register
80 H (Machine instruction in hexadecimal)
10000000
Step 5: Store the result that is present in accumulator to memory location 1002H
STA 10 0 2 H
3 2 0 2 1 0 (Machine instruction in hexadecimal)
opcode Lower order address higher order address
opcode of STA is 0 0 1 1 0 0 1 0 i.e. 32 H
Step 6 : HALT : to indicate the end of program
76H
Opcode of HALT 0 1 1 1 0 1 1 0
Therefore to carry out this addition, we have to perform these five operation.
In this example we assume that data are available in memory and storing the result in
memory.
But if we want to take the input from some input device (like key board), first we have to
accept the input from keyboard and stored in some memory location. Similarly to display
the result in monitor, we have to get the result from memory and display in monitor with
the help of some o instruction.
The complete program is :
Memory location
0100H
3A 00 10
0103H
47
0104H
3A 01 10
0107H
80
0108H
32 02 10
010BH
76
LDA
MOVBA
LDA 1001H
ADD B
STA 1002H
HLT
If we store this program from memory location 100H , then the contents of memory is
shown below:
Memory Address
in Hex
in Hex
Fig
While executing the program, the program counter (PC) will be loaded with the starting
memory address of this program, i.e. 0100H.
The processor will start execution this program starting from memory location 0100H
and it keeps on doing the execution job till it encounters a halt instruction.
If we program the microprocessor in this way, i.e writing the machine code directly, it is
known as machine language programming.
The main advantage of machine language programming is that the memory control is
directly in the hands of the programmer, so that, he/she may able to manage the memory
of the system more efficiently.
The disadvantages of machine language programming are more prominent.
The programming, coding and resource management techniques are tedious. The
programmer has to take care of al these functions.
The programs are difficult to understand unless one has a thorough technical knowledge
of the processor architecture and the instruction set.
Also it is difficult to remember the machine code of each and every instruction,.
Assembly Language programming:
Te content of register B is added with the content of accumulator and store the result in
accumulator. It is done in fifth instruction.
The content of accumulator is stored in memory location 1002H. It is done in sixth
instruction.
Seventh instruction is to halt the processor, i.e, to stop the program execution.
The first two instructions can be replaced
LXI
H ,1000 H
Assembly language instruction sequence for the same program for 8086 microprocessor.
Consider that data segment starts from 20000H and code segment starts from 1000H.
MOV CX , 2000 H // Initialize DS at 2000H
MOV DS , CX //
MOV AX , 1000 H // Get first operand in AX
// Load the register pair H-L by 0200H which is address of the first
element.
MOV
BACK: INX H // Increment the register pair H-L to get the next element
CMP M PThe content of the memory location whose address is in H-L pair is subtracted
from the accumulator. The accumulator remain unchanged. CY 1 if
A H L
JNC NEXT
// Jump to the level NEXT if the carry is not set, i.e, content of
accumulator is biggest so far.
MOV A SI
// If the next number is bigger, replace the previous one with the next
element
LOOP:
MOV A, M
STAX D
INX H
INX D
DCR C
JNZ LOOP // Jump on not zero, i.e. the result of decrement is not zero.
HLT
Assembly language instruction sequence for 8086 microprocessor.
In 8086, we have to define the data segment by setting the DS register. Assume that DS is
set appropriately
MOV SI , 1000 H
MOV DI , 2000 H
MOV CX , FFH
LOOP:
MOV AX , SI
MOV DI , AX
INC SI
INC DI
DEC CX
JNZ LOOP
HLT
The program listing is similar to the program of 8085, every instruction of 8085 is
replaced by an equivalent instruction of 8086.
Therefore, the above program listing is correct. But this is not an efficient implementation
for 8086, because we have not used any advance feature of 8086.
Alternate program listing for 8086 microprocessor
Assume that the data segment register and extra segment registers are set appropriately.
REP
MOV SI , 1000 H
MOV DI , 2000 H
MOV CX , FFH
CLD
MOVSB