Вы находитесь на странице: 1из 10

PROGRAMMING IN 8085

DEVELOPMENT OF PROGRAM

A program is a sequence of instructions written to tell a computer to perform a specific


function. The instructions are selected from the instruction set of the microprocessor. The
first step in writing a program is to think very carefully about the problem that you want
the program to solve. In other words, ask yourself many times, “What do I really want
this program to do?” If you don’t do this, you may write a program that works great but
does not do what you need it to do. As you think about the problem, it is a good idea to
write down exactly what you want the program to do and the order in which you want the
program to do it. At this point, you do not write down the instructions, you just divide the
given problem in small steps in terms of the operations the 8085 can perform. This
sequence of operations used to solve a programming problem is often called the
algorithm. For more complex problems, however, we develop a more extensive outline
before translating the algorithm into instructions. The most common way of representing
the algorithm for a program is flowchart. The steps necessary to write the program can be
represented in a pictorial format, called a flowchart. Generally, a flowchart is used for
two purposes: to assist and clarify the thinking process and to communicate the
programmer’s thoughts and logic to others. The below figure shows some of the common
flowchart symbols:

Arrow indicates the direction of the program execution.

Rectangle represents a process or an operation.

Diamond represents a decision making block.

Oval indicates the beginning or the end of the operation.

Double-sided represents a predefined process such as a sub-routine.


Rectangle

Circle with an represents a continuation (an entry or an exit) to a


Arrow different page.

Fig. 1 Flow Charting Symbols


Writing and Executing a program in a Single Board Microcomputer

Writing a simple program of adding two hexadecimal numbers 23H and 84H and saving
the result in a register in assembly language is illustrated below:

Algorithm

1. Load the 1st number 23H in one register.


2. Load the 2nd number 84H in another register.
3. Add the contents of the two registers.
4. Save the result in any register.
5. End the program.

Flow Chart

The above steps can be represented in a pictorial format with the help of flowchart.

START

Load 23H in
Register A

Load 84H in
Register B

Add Register
B to A

Save sum in
Register C

STOP

Fig.2 Flow Chart for a Program


Assembly Language

The steps described in the flowchart are translated into an Assembly Language as
follows:

Opcode Operand
1. MVI A, 23H
2. MVI B, 84H
3. ADD B
4. MOV C, A
5. HLT

Machine Language

Now, the above mnemonics should be converted into machine language. By looking up
the machine code for each instruction in the instruction set, we can translate the program
into machine language as follows:

Mnemonics Machine Code (Hex)


1. MVI A, 23H 3E}
2. MVI 23}
3. ADD
4. MOV
5. HLT

INSTRUCTION CYCLE:

An instruction is a command given to the computer to perform a specified operation on the given data.
Sequence of instructions written for a computer to perform a particular task is called program.
Program & data are stored in the memory. The microprocessor fetches one instruction from the
memory at a time & executes it. It executes all the instructions of the program one by one to produce
the final result. The necessary steps that a microprocessor carries out to fetch an instruction &
necessary data from the memory & to execute it constitute an instruction cycle.

In other words, an instruction cycle is defined as the time required completing the execution of an
instruction.

An instruction cycle consists of a fetch cycle & an execute cycle. The time required to fetch an opcode
(fetch cycle) is a fixed slot of time while the time required to execute an instruction (execute cycle) is
variable which depends on the type of instruction to be executed.
FETCH OPERATION:

The first byte of an instruction is its opcode. An instruction may be more than one byte long. The other
bytes are data of operand address. The program counter (PC) keeps the memory address of the next
instruction to be executed. In the beginning of a fetch cycle the content of the program counter, which
is the address of the memory location where opcode is available, is sent to the memory. The memory
places the opcode on the data bus so as to transfer it to the microprocessor. The entire operation of
fetching an opcode takes three clock cycles.

EXECUTE OPERATION:

The opcode fetched from the memory goes to the instruction register (IR). From the instruction
register it goes to the decoder circuitry which decodes the instruction. After the instruction is decoded,
execution begins. If the operand is in general purpose registers execution is immediately performed.

The time taken for decoding and execution is one clock cycle. If an instruction contains data or
operand and address which are still in the memory, the microprocessor has to perform some read
operations to get the desired data. After receiving the data it performs execute operation. A read cycle
is similar to a fetch cycle. In case of a read cycle the quantity received from the memory are data or
operand address instead of an opcode. In some instructions write operation is performed. In write cycle
data are sent from the microprocessor to the memory or an output device. Thus we see that in some
cases an execute cycle may involve one or more read or write cycles or both.

MACHINE CYCLE:

Machine cycle is defined as the time required completing the operation of accessing either memory or
I/O. In the 8085, the machine cycle may consist of three to six T states.
T-State:

T-State is defined as one sub-division of the operation performed in one clock period. These sub-
divisions are internal states synchronized with the system clock.

PROGRAMMING TECHNIQUES:

Microprocessor is very fast and accurate in processing the data. It is more efficient than human beings
when it is required to perform the repeated tasks. To perform repetitions tasks programmer must use
different programming techniques such as looping, indexing etc.

Looping:

Looping is the programming technique which is used to tell the processor to repeat the task. A loop
can be constructed by using jump instructions. Continuous loops can be constructed by using
unconditional jump instructions. Unless you reset the system continuous loop does not stop repeating
the tasks. For example: modulo ten counter which will be discussed later.
Conditional loops can be constructed by using conditional jump instructions. The specified tasks will
be repeated only when the conditions are met. For example: the delay loop, which will be discussed in
the next section.

INDEXING:

Indexing is the programming technique in which objects will be sorted in a sequential manner. In this
data bytes are stores in memory locations sequentially & those data bytes are referred to by their
memory address.

PROGRAMS

1.Write an Assembly Language Program to add 2- 16 bit numbers:

LHLD 9501H
XCHG
LHLD 9503H
MVI C, 00
DAD D
JNC LOOP1
INR C
LOOP1 SHLD 9505H
MOV A, C
STA 9507H
HLT

2.Write an ALP to add n- 8 bit numbers:

MVI D, 00
MVI C, 05
LXI H, 8030
MOV A, M
DCR C
AHEAD INX H
ADD M
JNC LOOP-1
INR D
LOOP-1 DCR C
JNZ AHEAD
STA 8056
MOV A, D
STA 8051
HLT

3.Write an ALP to perform 32-bit addition:

LXI H, 8500
MOV C, M
INX H
LXI D, 8600
XRA A
LOOP-1 LDAX D
ADC M
MOV M, A
INX H
INX D
DCR C
JNZ LOOP-1
MVI A, 00
RAL
MOV M, A
RST 5
4.Write an ALP to subtract 2-8 bit numbers:

LXI H, 8501
MOV A, M
INX H
SUB M
JNC LOOP-1
INR C
LOOP-1 INX H
MOV M, A
MOV A, C
INX H
MOV M, C
RST5

5.Write an ALP to ADD 2-BCD numbers:

LXI H, 8A00
MVI D, 00
MOV A, M
INX H
ADD M
DAA
JNC LOOP-1
INR D
LOOP-1 STA 8A03
MOV A, D
STA 8A04
RST5

6.Write an ALP to SUBTRACT 2-16 BIT NUMBERS:

LHLD 8100
XCHG
LHLD 8102
MOV A, E
SUB L
STA 8104
JNC LOOP
DCR D
LOOP MOV A, D
SUB H
STA 8105
RST5
7.Write an ALP to multiply 2-8bit numbers:

LXI H, 8A00
MOV B, M
XRA A
MOV C, A
INX H
AHEAD ADD M
JNC LOOP-1
INR C
LOOP-1 DCR C
JNZ AHEAD
INX H
MOV M, A
INX H
MOV M, C
RST5

8.Write an ALP to perform division of 1-8 bit number by another 8-bit number:

LXI H, 8900H
MOV A, M
INX H
MOV B, M
MVI C, FF
LOOP INR C
SUB B
JNC LOOP
ADD B
STA 8902H
MOV A, C
STA 8903H
HLT

TIME DELAYS:

Counters are constructed using software instructions to keep track of the events. Since the
counting is performed at such high speed, only the last count can be observed. To observe all the
counts, there must be an appropriate time delay between counts.
Designing a delay is very simple. A register is loaded with a number, depending on the delay
required, and then the register is decremented until it reaches zero by setting up a loop with a
conditional JUMP instruction. The loop causes the delay, depending upon the clock period of the
system.

Single Register Delay:

A count is loaded in a register and the loop is executed until the count reaches zero. The set of
instructions necessary to set up a delay loop are:

LOOPMVI B, FF
DCR B
JNZ LOOP

To calculate the time delay we must consider the T-states required for each instruction, and for the
number of times the instruction are executed in the loop. The clock frequency of 8085 is 3MHZ.

Clock period T= 1/f = 1/3*106 = 0.33*10-6

Register B is loaded with FFH (25510) therefore the loop is repeated 255 times.
The time delay can be calculated as follows:

TL=(T*Loop T states*N10)
Where:
TL=Time Delay in loop
T= System Clock Period.
N10=Equivalent decimal number of Hexadecimal count loaded in the delay register.

DCR & JNZ forms a 0 loop with a total of 14 (4+10) T- states.


Therefore:
TL= (0.33*10-6*14*255)
TL=1.1781 *10-3

Eg:
Write an ALP to display FF and 00 alternatively with a delay:

MVI A, FF
BACK PUSH PSW
STA 8FF1
CALL UPDDT
CALL DELAY
POP PSW
CMA
JMP BACK
HLT