Sie sind auf Seite 1von 14

24-Oct-11

1
Programming of Microprocessors
Slides Prepared By
Hardeep Singh
Lecturer/ECE
SBBSIET, Padhiana
Introduction
A Computer can do only what the programmer asks
to do.
To perform a particular task the programmer
prepares the sequence of instruction.
A set of programs for a particular computer is called
software of the computer.
Program written in the form of 0s and 1s called
machine language program.
Problems related with machine language
Difficult to understand or debug a program
Programs are long
Program writing is difficult and tiresome
Chances of careless errors in writing the program
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 2
24-Oct-11
2
To facilitate programmer machine codes can
be written in hexadecimal system.
Hexadecimal can easily be written in
hexadecimal system compared to binary
system.
Microprocessor kit converts hexadecimal into
binary as the microcomputer understands a
program only in binary system
The hexadecimal is also not convenient to
understand so we develop mnemonics for
each hexadecimal code which is in user
friendly language
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 3
Assembly Language
A programmer easily write a program in
alphanumeric symbols instead of binary or
hexadecimal.
Meaningful and easily rememberable symbols are
chosen for the purpose.
Example: ADD for addition
SUB for subtraction, etc.
Such symbols are called mnemonics.
Program written in mnemonics are called
Assembly language program
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 4
24-Oct-11
3
A microprocessor specific language is called low level
language.
A program which translate as assembly language
program into machine language program is called
assembler
Self Assembler
present on microcomputer which translates the code
Disadvantage is that it requires lot of memory space and time to
translates the code
Cross Assembler
Installed on desktop computer
Converts code into machine level objects and transfer to
microcomputer
Disassembler
Converts Machine code into assembly language program
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 5
Addition of two numbers placed in two consecutive memory
locations 2501H and 2502H
Mnemonics Operands Memory
Address
Machine Codes Comments
LXI H, 2501H 2000 21, 01, 25 Getthe address of the 1
st
number in HL pair
MOV A,M 2003 7E Get the 1
st
number in
accumulator
INX H 2004 23 Increment the contentof
HL pair
ADD M 2005 86 Add the 1
st
and 2
nd
number
STA 2503H 2006 23, 03, 25 Store sum in 2503
HLT 2009 76 Stop
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 6
24-Oct-11
4
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 7
Stack
During the execution of a program sometimes it is
necessary to save the contents of the certain registers
because the registers are required for some other
operationinsubsequent steps.
These contents moved to certain memory location to
certainmemorylocationsbyPUSHoperations
After completing these operations those contents which
were saved in the memory are transferred back to the
registersbyPOPoperations.
Memory locations for this purpose is set aside by the
programmer inthebeginning.
Theset of thesememorylocationsarecalledstack
The last memory location of the occupied portion of the
stackiscalledstacktop.
A16-bit special register (StackPointer) holdstheaddressof
stacktop
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 8
24-Oct-11
5
The stack pointer is initialized in the beginning
of the program by LXI SP or SPHL instructions.
Any of RAM can be used by stack.
There is no restriction on the location of the
stack in the memory.
Data stored in the stack on last-in-first-out
(LIFO) principle
Stack access is faster than memory access
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 9
Stack Related Instructions
PUSH Rp
POP Rp
SPHL
XTHL
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 10
24-Oct-11
6
PUSH Rp
Push the contents of register pair on to stack
This instruction is used to write 16 bit data on the stack
When this instruction is executed the contents of
specified register pair are copied on the stack in the
following sequence.
The stack pointer in decremented by one and the contents
of higher order register of the specified register pair are
copied to the memory location pointed the stack pointer.
The stack pointer is again decremented by 1 and the
contents of lower order of register pair are copied to
memory location pointed by the stack pointer.
Example: PUSH B
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 11
POP Rp
POP off stack contents to register pair
When this instruction is executed, the contents of
memory location pointed by the stack pointer register
are copied to the lower order byte of register pair
The stack pointer in incremented by one and the
contents of that memory location are copied to the
higher order byte of register pair.
The stack pointer is again incremented by one
The process of POP is exactly opposite to PUSH
operation. So the contents stored by PUSH are taken
back using POP instructions.
Example POP B
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 12
24-Oct-11
7
SPHL
Load stack pointer with HL register pair
contents
When this instruction is executed, the
contents of HL pair are transferred to the stack
pointer
The contents of H register is copied to higher
order byte of stack pointer and the contents
of L register are copied to lower byte of stack
pointer
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 13
XTHL
Exchange HL with top of stack
When this instruction is executed the contents
of L register are exchanged with stack location
pointed by the stack pointer. The contents of
H register are exchanged with the next stack
location
The contents of the stack pointer are not
altered.
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 14
24-Oct-11
8
LXI SP
This instruction initialized the stack pointer
with 16 bit address
The stack point can be intializedby two
methods
Direct method: LXI SP, Data 16 Bit
Indirect method : LXI H, Data 16 Bit SPHL
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 15
Some other Commands
INX SP
This instruction increments the Stack Pointer by 1.
DAD SP
This instruction adds the contents of SP with the
contents of HL pair. The result of addition is stored
into the HL pair
DCX SP
This instruction decrements the stack pointer by 1
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 16
24-Oct-11
9
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 17
2604
2605
2606
2607
SP
2608
6B
2608
2609
2610
Subroutines
While writing a program certain operations may occur
several times and they are not available as individual
instructions.
Example is multiplication of numbers, as this program
is used again & again
The concept of subroutines is developed to avoid the
repetition of smaller programs.
The small program for a particular task is called
subroutine.
Subroutine written separately & stored in the memory
They are called at various points of the main program
by CALL instruction where they required.
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 18
24-Oct-11
10
The last instruction of subroutine is RET
After completion of subroutine, the main program
begins from the instruction immediately following
the CALL instruction
When program jumps from the main program to
subroutine , the counter of the program counter is
saved in the stack so that the program execution
may come back to the main program as the proper
point after the completion of the subroutine.
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 19
Introduction to Programming
Step 1: Define the problem
Step 2: Solution Plan
Step 3: Flowchart
Step 4: Program
Step 5: Check the result
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 20
24-Oct-11
11
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 21
Direction Arrow
Input & Output Unit
Process
Operation
Decision Unit
Predefined
Process/
Subrotines
Examples of Assembly Programming
Example 1: Place 05 in register B
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 22
Memory
Address
Machine
Codes
Mnemonics Operands Comments
FC00 06, 05 MVI B, 05 GET 05 IN
REGISTER B
FC02 76 HLT STOP
24-Oct-11
12
Example 2: Get 05 in register A and
then move it to register B
Memory
Address
Machine Codes Mnemonics Operands Comments
FC00 3E, 05 MVI A, 05 Get 05 in
register A
FC01 47 MOV B, A Transfer 05
from register A
to register B
FC02 76 HLT Stop
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 23
Place 05 in the accumulator. Increment it by one and
store the results in the memory location FC50 H
Memory
Address
Machine Codes Mnemonics Operands Comments
FC00 3E, 05 MVI A, 05 Get 05 inthe
accumulator
FC01 3C INR A Increment the
contentof the
accumulator by
one
FC03 32, 50, FC STA FC05 H Store result in
FC05H
FC06 76 HLT Halt
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 24
24-Oct-11
13
8 Bit Subtraction
Memory
Address
Machine
Codes
Mnemonics Operands Comments
2000 21, 01, 25 LXI H, 2501 H Get address of 1
st
number in H-
L pair
2003 7E MOV A, M 1
st
number in accumulator
2004 23 INX H Content of H-L pair increases
from 2501 H to 2501 H
2005 96 SUB M 1
st
Number 2
nd
Number
2006 23 INX H Content of H-L pair becomes
2503 H
2007 77 MOV M, A Store result in 2503H
2008 76 HLT Halt
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 25
Problem
Write a program to provide the given traffic lights
(Green, Yellow, Red) and two pedestrian (Walk
and Dont Walk)
The signal lights and signs turned on/off by the
data bits of an output port as shown below
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 26
Lights Data Bits On Time (Seconds)
Green D
O
15
Yellow D
1
5
Red D
2
20
Walk D
3
15
Dont Walk D
4
25
24-Oct-11
14
Problem Analysis
The problem is primarily concerned with providing various
time delays for a complete sequence of 40 seconds.
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 27
Timein
Sequence
Dont
Walk
Walk Red Yellow Green Hex
Code
D7 D6 D5 D4 D3 D2 D1 D0
0
15 0 1 0 0 0 0 0 1 41H
20 1 0 0 0 0 1 0 0 86H
40 1 0 0 1 0 0 0 0 90H
24-Oct-11 HardeepSingh/Lecturer/SBBSIET 28
START
Turn on Green Light