Unit 2 MPMC Notes
Unit 2 MPMC Notes
LECTURE NOTES
ON
1
UNIT - 2
Internal Circuits
Working Registers
Internal RAM ROM
Program Counter Stack Pointer Stack Pointer Clock
It has many instructions to move data between It has few instructions to move data between memory
memory and CPU and CPU
Less number of pins are multifunctional More number of pins are multifunctional
Single memory map for data and code Separate memory map for data and code (program)
(program)
Access time for memory and IO are more Less access time for built in memory and IO.
More flexible in the design point of view Less flexible since the additional circuits which is
residing inside the microcontroller is fixed for a
particular microcontroller
Large number of instructions with flexible Limited number of instructions with few
addressing modes addressing modes
2
2.2. RISC AND CISC CPU ARCHITECTURES
Microcontrollers with small instruction set are called reduced instruction set computer (RISC) machines
and those with complex instruction set are called complex instruction set computer (CISC). Intel 8051 is
an example of CISC machine whereas microchip PIC 18F87X is an example of RISC machine.
RISC CISC
Only load/store instructions are used to access In additions to load and store instructions, memory
memory access is possible with other instructions also.
3
2.3. HARVARD & VON- NEUMANN CPU ARCHITECTURE
Von-Neumann (Princeton architecture) Harvard architecture
Data Program
Data
Memory Memory
Address Bus
Address Bus
It uses single memory space for both It has separate program memory and data memory
instructions and data.
It is not possible to fetch instruction code and data Instruction code and data can be fetched
simultaneously
Execution of instruction takes more machine cycle Execution of instruction takes less machine cycle
Also known as control flow or control driven Also known as data flow or data driven
computers computers
Simplifies the chip design because of single memory Chip design is complex due to separate memory
space space
Eg. 8085, 8086, MC6800 Eg. General purpose microcontrollers, special DSP
chips etc.
4
2.4 INTRODUCTION
A set of instructions written in a specific sequence for the computer to solve a specific task is called a
program and software is a collection of such programs.
The program stored in the computer memory in the form of binary numbers is called machine
instructions. The machine language program is called object code.
An assembly language is a mnemonic representation of machine language. Machine language and assembly
language are low level languages and are processor specific.
The assembly language program the programmer enters is called source code. The source code (assembly
language) is translated to object code (machine language) using assembler.
Programs can be written in high level languages such as C, C++ etc. High level language will be
converted to machine language using compiler or interpreter. Compiler reads the entire program and
translate into the object code and then it is executed by the processor. Interpreter takes one statement of
the high level language as input and translate it into object code and then executes.
5
The 8051 architecture.
I/O
A0-A7
ALU PSW Port 0
SFR D0-D7
General
A B Purpose I/O
RAM Port 1
I/O
A8-
Port 2 A15
ROM
DPTR
PC I/O
DPH
INT
DPL
Port 3 CNTR
SERIAL
RD/WR
E IE
ALE System General IP
Timing purpose PCON
PSEN
area SBUF
XTAL1 System SCON
interrupt TCON
XTAL2 timers
Bit addressible
area TMOD
RESET TL0
Register Bank 3
Data
buffers Register Bank 2 TH0
Register Bank 1 TL1
Register Bank 0 TH1
VCC Memory
GND control SFR and
General Purpose RAM
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the program
size is more than 4 K Bytes 8051 will fetch the code automatically from external memory.
Accumulator is an 8 bit register widely used for all arithmetic and logical operations.
Accumulator is also used to transfer data between external memory. B register is used along with
Accumulator for multiplication and division. A and B registers together is also called MATH
registers.
6
PSW (Program Status Word). This is an 8 bit register which contains the arithmetic status of ALU
and the bank select bits of register banks.
CY AC F0 RS1 RS0 OV - P
CY - carry flag
AC - auxiliary carry flag
F0 - available to the user for general purpose
RS1,RS0 - register bank select bits
OV - overflow
P - parity
Stack Pointer (SP) – it contains the address of the data item on the top of the stack. Stack may
reside anywhere on the internal RAM. On reset, SP is initialized to 07 so that the default stack will
start from address 08 onwards.
Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer lower byte). This is a 16
bit register which is used to furnish address information for internal and external program
memory and for external data memory.
Program Counter (PC) – 16 bit PC contains the address of next instruction to be executed. On reset
PC will set to 0000. After fetching every instruction PC will increment by one.
2.6
7
2.6 PIN DIAGRAM
Pinout Description
Pins 1-8 PORT 1. Each of these pins can be configured as an input or an output.
Pin 9 RESET. A logic one on this pin disables the microcontroller and clears the contents of most
registers. In other words, the positive voltage on this pin resets the microcontroller. By
applying logic zero to this pin, the program starts execution from the beginning.
Pins10-17 PORT 3. Similar to port 1, each of these pins can serve as general input or output. Besides,
all of them have alternative functions
Pin 10 RXD. Serial asynchronous communication input or Serial synchronous communication output.
8
2.7 MEMORY ORGANIZATION
Internal RAM organization
R7 1F
R6 1E
R5 1D
BANK 3
R4 1C 2F
7F 78 7F
R3 1B 77 70 7E
2E
R2 1A
6F 68 .
R1 19 2D
R0 18 67 60 .
2C
R7 17 5F 58 .
2B
R6 16
R5 15
57 50 .
BANK 2
2A
R4 14 29
4F 48 .
R3 13 47 40 .
28
R2 12
R1 11 27
3F 38 .
R0 10 26
37 30 .
R7 0F 2F 28
R6 0E 25 32
R5 0D 27 20
24 31
BANK 1
R4 0C 1F 18
R3 0B
23 30
17 10
R2 0A 22
R1 09 21 0F 08
R0 08 07 00
R7 07
20 General purpose memory
R6 06
R5 05
BANK 0
Working Registers
Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1, R2, R3,
R4, R5, R6, and R7). There are four such register banks. Selection of register bank can be done through
RS1,RS0 bits of PSW. On reset, the default Register Bank 0 will be selected.
Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows access to bit variables.
This is where individual memory bits in Internal RAM can be set or cleared. In all there are 128 bits
numbered 00h to 7Fh. Being bit variables any one variable can have a value 0 or 1. A bit variable can be set
with a command such as SETB and cleared with a command such as CLR. Example instructions are:
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
Note, bit 25h is actually bit 5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and 2Fh.
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available for general-
purpose data storage, user should take care while using the memory location from 00 -2Fh
9
since these locations are also the default register space, stack space, and bit addressable space. It is a good
practice to use general purpose memory from 30 – 7Fh. The general purpose RAM can be accessed using
direct or indirect addressing modes.
Number of address lines required for 16 Kbyte memory is 14 lines and that of 32Kbytes of memory is 15
lines.
PSEN PSEN
A14 A14
A13 A13
A13
A12 A12
A12
… .. 32 Kbyte
A9 .
A8
A8 WE
. RAM
WR 16 Kbyte .
RD OE
RAM A3
AL A7
LE A2
..
8051 A0-A7 A0-A7 A1
A1
A0
A0
LOWER BYTE
EA ADDRESS
GND [AD0 – AD7] DAT DAT
A A
O/P O/P
AD0
- DATA BUS [AD0 – AD7]
AD7
The lower order address and data bus are multiplexed. De-multiplexing is done by the latch. Initially the
address will appear in the bus and this latched at the output of latch using ALE signal. The output of the
latch is directly connected to the lower byte address lines of the memory. Later data will be available in
this bus. Still the latch output is address it self. The higher byte of address bus is directly connected to the
memory. The number of lines connected depends on the memory size.
The RD and WR (both active low) signals are connected to RAM for reading and writing the data.
PSEN of microcontroller is connected to the output enable of the ROM to read the data from the memory.
EA (active low) pin is always grounded if we use only external memory. Otherwise, once the program
size exceeds internal memory the microcontroller will automatically switch to external memory.
10
STACK
A stack is a last in first out memory. In 8051 internal RAM space can be used as stack. The address of the
stack is contained in a register called stack pointer. Instructions PUSH and POP are used for stack
operations. When a data is to be placed on the stack, the stack pointer increments before storing the data
on the stack so that the stack grows up as data is stored (pre-increment). As the data is retrieved from the
stack the byte is read from the stack, and then SP decrements to point the next available byte of stored data
(post decrement). The stack pointer is set to 07 when the 8051 resets. So that default stack memory starts
from address location 08 onwards (to avoid overwriting the default register bank ie., bank 0).
11
2.8 INSTRUCTION SYNTAX.
General syntax for 8051 assembly language is as follows.
LABEL : (THIS IS NOT NECESSARY UNLESS THAT SPECIFIC LINE HAS TO BE ADDRESSED). The label is a symbolic
address for the instruction. When the program is assembled, the label will be given specific address in which
that instruction is stored. Unless that specific line of instruction is needed by a branching instruction in the
program, it is not necessary to label that line.
OPCODE: Opcode is the symbolic representation of the operation. The assembler converts the opcode to
a unique binary code (machine language).
OPERAND: While opcode specifies what operation to perform, operand specifies where to perform that
action. The operand field generally contains the source and destination of the data. In some cases only
source or destination will be available instead of both. The operand will be either address of the data, or
data itself.
COMMENT: Always comment will begin with ; or // symbol. To improve the program quality,
programmer may always use comments in the program.
12
2.9 ADDRESSING MODES
Various methods of accessing the data are called addressing modes. 8051
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Relative addressing.
6. Absolute addressing.
7. Long addressing.
8. Indexed addressing.
9. Bit inherent addressing.
10. Bit direct addressing.
1. Immediate addressing.
In this addressing mode the data is provided as a part of instruction itself. In other words data
immediately follows the instruction.
Eg. MOV A,#30H
ADD A, #83 # Symbol indicates the data is immediate.
2. Register addressing.
In this addressing mode the register will hold the data. One of the eight general registers (R0 to
R7) can be used and specified as the operand.
Eg. MOV A,R0
ADD A,R6
R0 – R7 will be selected from the current selection of register bank. The default register bank will be bank 0.
3. Direct addressing
There are two ways to access the internal memory. Using direct address and indirect address. Using direct
addressing mode we can not only address the internal memory but SFRs also. In direct addressing, an 8 bit internal data
memory address is specified as part of the instruction and hence, it can specify the address only in the range of 00H to
FFH. In this addressing mode, data is obtained directly from the memory.
Eg. MOV A,60h
ADD A,30h
4. Indirect addressing
The indirect addressing mode uses a register to hold the actual address that will be used in data movement.
Registers R0 and R1 and DPTR are the only registers that can be used as data pointers. Indirect addressing cannot be
used to refer to SFR registers. Both R0 and R1 can hold 8 bit address and DPTR can hold 16 bit address.
Eg. MOV A,@R0
ADD A,@R1
MOVX A,@DPTR
5. Indexed addressing.
In indexed addressing, either the program counter (PC), or the data pointer (DTPR)—is used to
hold the base address, and the A is used to hold the offset address. Adding the value of the base address to
13
the value of the offset address forms the effective address. Indexed addressing is used with JMP or MOVC
instructions. Look up tables are easily implemented with the help of index addressing.
Eg. MOVC A, @A+DPTR // copies the contents of memory location pointed by the sum of the accumulator
A and the DPTR into accumulator A.
MOVC A, @A+PC // copies the contents of memory location pointed by the sum of the
accumulator A and the program counter into accumulator A.
6. Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address, (offset), is
an 8 bit signed number, which is automatically added to the PC to make the address of the next instruction.
The 8 bit signed offset value gives an address range of +127 to —128 locations. The jump destination is
usually specified using a label and the assembler calculates the jump offset accordingly. The advantage of
relative addressing is that the program code is easy to relocate and the address is relative to position in the
memory.
Eg. SJMP LOOP1
JC BACK
7. Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes instructions. The absolute addressing mode specifies the lowest 11 bit of the
memory address as part of the instruction. The upper 5 bit of the destination address are the upper 5 bit of the
current program counter. Hence, absolute addressing allows branching only within the current 2 Kbyte page
of the program memory.
Eg. AJMP LOOP1
ACALL LOOP2
8. Long Addressing
The long addressing mode is used with the instructions LJMP and LCALL. These are 3 byte instructions.
The address specifies a full 16 bit destination address so that a jump or a call can be made to a location within
a 64 Kbyte code memory space.
Eg. LJMP FINISH
LCALL
DELAY
14
2.9 INSTRUCTION SET.
1. Instruction Timings
The 8051 internal operations and external read/write operations are controlled by the oscillator clock.
T-state, Machine cycle and Instruction cycle are terms used in instruction timings.
T-state is defined as one subdivision of the operation performed in one clock period. The terms 'T- state' and
'clock period' are often used synonymously.
Machine cycle is defined as 12 oscillator periods. A machine cycle consists of six states and each state lasts for
two oscillator periods. An instruction takes one to four machine cycles to execute an instruction. Instruction
cycle is defined as the time required for completing the execution of an instruction. The 8051 instruction cycle
consists of one to four machine cycles.
Eg. If 8051 microcontroller is operated with 12 MHz oscillator, find the execution time for the following
four instructions.
1. ADD A, 45H
2. SUBB A, #55H
3. MOV DPTR, #2000H
4. MUL AB
Since the oscillator frequency is 12 MHz, the clock period is, Clock period = 1/12 MHz = 0.08333 µS. Time for 1
machine cycle = 0.08333 µS x 12 =1 µS.
Instruction No. of machine cycles Execution time
1. ADD A, 45H 1 1 µs
2. SUBB A, #55H 2 2 µs
3. MOV DPTR, #2000H 2 2 µs
4. MUL AB 4 4 µs
2. 8051 Instructions
The instructions of 8051 can be broadly classified under the following headings.
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
6. Bit manipulation instructions
15
c. Move an immediate 8 bit data to register A or to Rn or to a memory location(direct or
indirect)
i. MOV @R0, #0E8H
ii. MOV DPTR, #0F5A2H
iii. MOV DPTR, #5467H
iv. Move the contents of a memory location to A or A to a memory location using direct
and indirect addressing MOV A, 65H
v. MOV A, @R0
vi. MOV 45H, A
vii. MOV @R1, A
d. Move the contents of a memory location to Rn or Rn to a memory location using direct
addressing
i. MOV R3, 65H
ii. MOV 45H, R2
e. Move the contents of memory location to another memory location using direct and indirect
addressing
i. MOV 47H, 65H
ii. MOV 45H, @R0
f. Move the contents of an external memory to A or A to an external memory
i. MOVX A,@R1
ii. MOVX @R0,A
g. Move the contents of program memory to A
i. MOVC A, @A+PC
ii. MOVC A, @A+DPTR
iii. MOVX A,@DPTR
iv. MOVX@DPTR,A
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
i. ADD A, #45H
ii. ADDC A, #OB4H
ii. Add the contents of A with register Rn with or without carry.
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry using direct and indirect
addressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A, @R1
iv. ADDC A, @R0
MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored in A and B register.
After multiplication the lower byte of the result will be stored in accumulator and higher byte of result will
be stored in B register.
Eg. MOV A,#45H ;[A]=45H
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209
;[A]=09H, [B]=42H
Division
DIV AB. This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned
number which is stored in B register. After division the result will be stored in accumulator and remainder
will be stored in B register.
Eg. MOV A,#45H ;[A]=0E8H
MOV B,#0F5H ;[B]=1BH
DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H
;[A] = 08H, [B]=10H
When two BCD numbers are added, the answer is a non-BCD number. To get the result in BCD, we use DA
A instruction after the addition. DA A works as follows.
If lower nibble is greater than 9 or auxiliary carry is 1, 6 is added to lower nibble.
If upper nibble is greater than 9 or carry is 1, 6 is added to upper nibble.
Eg 1: MOV A,#23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after da a
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABh
DA A // [A]=11, C=1 . ANSWER IS 111. Accumulator data is changed after DA A
INC increments the value of source by 1. If the initial value of register is FFh, incrementing the value will
cause it to reset to 0. The Carry Flag is not set when the value "rolls over" from 255 to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is incremented. If the initial
value of DPTR is FFFFh, incrementing the value will cause it to reset to 0.
18
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value will cause it to
reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to FFh.
Logical Instructions
Logical AND
ANL destination, source: ANL does a bitwise "AND" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. "AND" instruction logically
AND the bits of source and destination.
ANL A,#DATA ANL A, Rn
ANL A,DIRECT ANL A,@Ri
ANL DIRECT,A ANL DIRECT, #DATA
Logical OR
ORL destination, source: ORL does a bitwise "OR" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. " OR " instruction logically
OR the bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source: XRL does a bitwise "EX-OR" operation between source and
destination, leaving the resulting value in destination. The value in source is not affected. " XRL "
instruction logically EX-OR the bits of source and destination.
XRL A,#DATA XRL A,Rn
XRL A,DIRECT XRL A,@Ri
XRL DIRECT,A XRL DIRECT, #DATA
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit then the state of the bit
will be reversed. If operand is the Accumulator then all the bits in the Accumulator will be reversed.
Rotate Instructions RR
A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is shifted one
location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
19
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going into the carry bit in the PSW,
while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into the carry bit in the PSW,
while the carry goes into bit 0.
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than (the address
following the jump instruction by 127 or less) or less than (the address following the jump by 128 or less) is
called a relative jump. Schematically, the relative jump can be shown as follows: -
20
The remaining relative jumps are conditional jumps
JC <relative address> JNC
<relative address> JB bit,
<relative address>
JNB bit, <relative address> JBC
bit, <relative address>
CJNE <destination byte>, <source byte>, <relative address> DJNZ
<byte>, <relative address>
JZ <relative address> JNZ
<relative address>
00 0000 - 07FF
01 0800 - 0FFF
02 1000 - 17FF
03 1800 - 1FFF
.
.
1E F000 - F7FF
1F F800 - FFFF
It can be seen that the upper 5bits of the program counter (PC) hold the page number and the lower 11bits of
the PC hold the address within that page. Thus, an absolute address is formed by taking page numbers of
the instruction (from the program counter) following the jump and attaching the specified 11bits to it to
form the 16-bit address.
Applications that need to access the entire program memory from 0000H to FFFFH use long absolute
jump. Since the absolute address has to be specified in the op-code, the instruction length is 3 bytes (except
for JMP @ A+DPTR). This jump is not re-locatable.
Example: -
21
Another classification of jump instructions is
1. Unconditional Jump
2. Conditional Jump
1. The unconditional jump is a jump in which control is transferred unconditionally to the target location.
a. LJMP (long jump). This is a 3-byte instruction. First byte is the op-code and second and third bytes
represent the 16-bit target address which is any memory location from 0000 to FFFFH
eg: LJMP 3000H
b. AJMP: this causes unconditional branch to the indicated address, by loading the 11 bit address to 0 -10 bits
of the program counter. The destination must be therefore within the same 2K blocks.
c. SJMP (short jump). This is a 2-byte instruction. First byte is the op-code and second byte is the relative
target address, 00 to FFH (forward +127 and backward -128 bytes from the current PC value). To
calculate the target address of a short jump, the second byte is added to the PC value which is address of
the instruction immediately below the jump.
2. Conditional Jump instructions.
JBC Jump if bit = 1 and clear bit
JNB Jump if bit = 0
JB Jump if bit = 1
JNC Jump if CY = 0
JC Jump if CY = 1
CJNE reg,#data Jump if byte ≠ #data
CJNE A,byte Jump if A ≠ byte
DJNZ Decrement and Jump if A ≠ 0
JNZ Jump if A ≠ 0
JZ Jump if A = 0
Bit level JUMP instructions will check the conditions of the bit and if condition is true, it jumps to the address
specified in the instruction. All the bit jumps are relative jumps.
JB bit, rel ; jump if the direct bit is set to the relative address specified. JNB
bit, rel ; jump if the direct bit is clear to the relative address specified.
JBC bit, rel ; jump if the direct bit is set to the relative address specified and then clear the bit.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
g. [PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of PC.
h. [SP]=[SP]-1; (SP decrements)
i. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC.
j. [SP]=[SP]-1; (SP decrements again)
8051 has 128 bit addressable memory. Bit addressable SFRs and bit addressable PORT pins. It is possible to perform
following bit wise operations for these bit addressable locations.
1. LOGICAL AND
a. ANL C,BIT(BIT ADDRESS) ; ‘LOGICALLY AND’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ANL C, /BIT; ; ‘LOGICALLY AND’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
2. LOGICAL OR
a. ORL C,BIT(BIT ADDRESS) ; ‘LOGICALLY OR’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ORL C, /BIT; ; ‘LOGICALLY OR’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
3. CLR bit
a. CLR bit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BE CLEARED.
b. CLR C ; CONTENT OF CARRY WILL BE CLEARED.
4. CPL bit
a. CPL bit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BE COMPLEMENTED.
b. CPL C ; CONTENT OF CARRY WILL BE COMPLEMENTED.
23
2.10 ASSEMBLER DIRECTIVES.
Assembler directives tell the assembler to do something other than creating the machine code for an
instruction. In assembly language programming, the assembler directives instruct the assembler to
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables
ORG (origin)
The ORG directive is used to indicate the starting address. It can be used only when the program
counter needs to be changed. The number that comes after ORG can be either in hex or in decimal.
Eg: ORG 0000H ;Set PC to 0000.
24
2.11 ASSEMBLY LANGUAGE PROGRAMS.
1. Write a program to add the values of locations 50H and 51H and store the result in locations in 52h and 53H.
2. Write a program to store data FFH into RAM memory locations 50H to 58H using direct addressing
mode
3. Write a program to subtract a 16 bit number stored at locations 51H-52H from 55H-56H and store the result in
locations 40H and 41H. Assume that the least significant byte of data or the result is stored in low address. If the
result is positive, then store 00H, else store 01H in 42H. ORG 0000H ; Set program counter 0000H
MOV A, 55H ; Load the contents of memory location 55 into A
CLR C ; Clear the borrow flag
SUBB A,51H ; Sub the contents of memory 51H from contents of A
MOV 40H, A ; Save the LSByte of the result in location 40H
MOV A, 56H ; Load the contents of memory location 56H into A
SUBB A, 52H ; Subtract the content of memory 52H from the content A
MOV 41H, ; Save the MSbyte of the result in location 415.
MOV A, #00 ; Load 005 into A
ADDC A, #00 ; Add the immediate data and the carry flag to A MOV
42H, A ; If result is positive, store00H, else store 0lH in 42H
END
25
4. Write a program to add two 16 bit numbers stored at locations 51H -52H and 55H-56H and store the result in
locations 40H, 41H and 42H. Assume that the least significant byte of data and the result is stored in low
address and the most significant byte of data or the result is stored in high address.
5. Write a program to store data FFH into RAM memory locations 50H to 58H using indirect addressing
mode.
ORG 0000H ; Set program counter 0000H
MOV A, #0FF H ; Load FFH into A
MOV RO, #50H ; Load pointer, R0-50H
MOV R5, #08H ; Load counter, R5-08H
Start:MOV @RO, A ; Copy contents of A to RAM pointed by R0
INC RO ; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END
6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60H and 61H and
store the result in BCD at memory locations 52H and 53H. Assume that the least significant byte of the
result is stored in low address.
26
INC DPTR ;Increment DPTR
DJNZ R6, again ;Loop until counter R6=0
END
8. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H
O R G 0 00 0 H ; Set program counter 0000H
N EQU 15
M O V R 0 , #0 0 ; Clear R0
C LR A ; Clear A
a ga i n: INC R 0 ; Increment R0
A D D A , R0 ; Add the contents of R0 with A C
J NE R 0, # N, aga i n ; L o o p u n t il c ou n t er , R0, N
M O V 70 H,A ; Save the result in location 70H END
9. Write a program to multiply two 8 bit numbers stored at locations 70H and 71H and store the result at memory
locations 52H and 53H. Assume that the least significant byte of the result is stored in low address.
ORG 0000H ; Set program counter 00 OH
MOV A, 70H ; Load the contents of memory location 70h into A
MOV B, 71H ; Load the contents of memory location 71H into B
MUL AB ; Perform multiplication
MOV 52H,A ; Save the least significant byte of the result in location 52H MOV 53H,B ; Save the most
significant byte of the result in location 53
END
10. Ten 8 bit numbers are stored in internal data memory from location 5oH. Write a program to
increment the data.
Assume that ten 8 bit numbers are stored in internal data memory from location 50H, hence R0 or R1
must be used as a pointer.
The program is as follows.
OPT 0000H MOV
R0,#50H MOV
R3,#0AH
Loopl: INC @R0
INC RO
DJNZ R3, loopl END
END
11. Write a program to find the average of five 8 bit numbers. Store the result in H. (Assume that
after adding five 8 bit numbers, the result is 8 bit only).
ORG 0000H
MOV 40H,#05H
MOV 41H,#55H
MOV 42H,#06H
MOV 43H,#1AH
MOV 44H,#09H
MOV R0,#40H
MOV R5,#05H
MOV B,R5
CLR A
Loop: ADD A,@RO
INC RO
27
DJNZ R5,Loop
DIV AB
MOV 55H,A END
12. Write a program to find the cube of an 8 bit number program is as follows
ORG 0000H
MOV R1,#N
MOV A,R1
MOV B,R1
MUL AB //SQUARE IS COMPUTED
MOV R2, B
MOV B, R1
MUL AB
MOV 50,A
MOV 51,B
MOV A,R2
MOV B, R1
MUL AB
ADD A, 51H
MOV 51H, A
MOV 52H, B
MOV A, # 00H
ADDC A, 52H
MOV 52H, A //CUBE IS STORED IN 52H,51H,50H
END
13. Write a program to exchange the lower nibble of data present in external memory 6000H and 6001H
ORG 0000H ; S e t p r o g r a m c o u n t e r 0 0h M
O V D P T R , # 6 0 0 0 H ; C o p y a d d r es s 6 0 0 0 H t o D P TR
MOVX A, @DPTR ; Copy c on t en t s of 60008 t o A
MOV R0, #45H ; L o a d p oin t er , R 0 = 4 5 H
MOV @RO, A ; Copy cont of A to RA M pointed by 8 0
INC DPL ; In cr em en t p oin ter
MOVX A, @DPTR ; C op y con t en t s of 60 0 18 to A
XCHD A, @R0 ; E xc ha n ge l o we r ni bbl e o f A wi t h RAM poi nt e d b y R O
14. Write a program to count the number of and o's of 8 bit data stored in location 6000H.
ORG 00008 ; Set program counter 00008
MOV DPTR, #6000h ; Copy address 6000H to DPTR
MOVX A, @DPTR ; C opy n um be r t o A
MOV R0,#08 ; Copy 0 8 in RO
MOV R2,#00 ; C opy 0 0 in R 2
MOV R3,#00 ; Copy 00 in R3
CLR C ; Clear carry flag
BACK: RLC A ; Rota te A th r ough carr y flag
28
JC NEXT ;IfCF=1,branchtonext
INC R2 ; I f C F = 0 , i n c r e m e n t R 2 AJMP NEXT2
NEXT: INC R3 ; I f C F = 1, in cr em en t R3
NEXT2: DJNZ RO,BACK ; Repeat until RO is zero
END
15. Write a program to shift a 24 bit number stored at 57H-55H to the left logically four places. Assume that the
least significant byte of data is stored in lower address.
ORG 0000H ; Set program counter 0000h
MOV R1,#04 ; Set up loop count to 4
again: MOV A,55H ; Place the least significant byte of data in A
CLR C ; Clear tne carry flag
RLC A ; Rotate contents of A (55h) left through carry
MOV 55H,A
MOV A,56H
RLC A ; Rotate contents of A (56H) left through carry
MOV 56H,A
MOV A,57H
RLC A ; Rotate contents of A (57H) left through carry
MOV 57H,A
DJNZ R1,again ; Repeat until R1 is zero
END
16. Two 8 bit numbers are stored in location 1000h and 1001h of external data memory. Write a
program to find the GCD of the numbers and store the result in 2000h. ALGORITHM
Step 1 :Initialize external data memory with data and DPTR with address
Step 2 :Load A and TEMP with the operands
Step 3 :Are the two operands equal? If yes, go to step 9
Step 4 :Is (A) greater than (TEMP) ? If yes, go to step 6
Step 5 :Exchange (A) with (TEMP) such that A contains the bigger number
Step 6 :Perform division operation (contents of A with contents of TEMP)
Step 7 :If the remainder is zero, go to step 9
Step 8 :Move the remainder into A and go to step 4
Step 9 :Save the contents 'of TEMP in memory and terminate the program
ORG 0000H ; Set program counter 0000H
TEMP EQU 70H
TEMPI EQU 71H
MOV DPTR, #1000H ; Copy address 100011 to DPTR
MOVX A, @DPTR ; Copy First number to A
MOV TEMP, A ; Copy First number to temp INC DPTR
MOVX A, @DPTR ; Copy Second number to A
LOOPS: CJNE A, TEMP, LOOP1 ; (A) /= (TEMP) branch to LOOP1
AJMP LOOP2 ; (A) = (TEMP) branch to L00P2
LOOP1: JNC LOOP3 ; (A) > (TEMP) branch to LOOP3
NOV TEMPI, A ; (A) < (TEMP) exchange (A) with (TEMP)
MOV A, TEMP
MOV TEMP, TEMPI
LOOP3: MOV B, TEMP
DIV AB ; Divide (A) by (TEMP)
MOV A, B ; Move remainder to A
CJNE A,#00, LOOPS ; (A)/=00 branch to LOOPS
LOOP2: MOV A, TEMP
MOV DPTR, #2000H
MOVX @DPTR, A ; Store the result in 2000H
END
29
2.12 BASICS OF INTERRUPTS.
During program execution if peripheral devices needs service from microcontroller, device will generate
interrupt and gets the service from microcontroller. When peripheral device activate the interrupt signal,
the processor branches to a program called interrupt service routine. After executing the interrupt service
routine the processor returns to the main program.
ISR will always ends with RETI instruction. The execution of RETI instruction results in the following.
Classification of interrupts.
30
2.13 8051 INTERRUPT STRUCTURE.
8051 has five interrupts. They are maskable and vectored interrupts. Out of these five, two are external
interrupt and three are internal interrupts.
1. IE Register
This is an 8 bit register used for enabling or disabling the interrupts. The structure of IE register is
shown below.
2. IP Register.
This is an 8 bit register used for setting the priority of the interrupts.
31
TIMERS AND COUNTERS
Timers/Counters are used generally for
Time reference
Creating delay
Wave form properties measurement
Periodic interrupt generation
Waveform generation
8051 has two timers, Timer 0 and Timer 1.
Timer in 8051 is used as timer, counter and baud rate generator. Timer always counts up irrespective of
whether it is used as timer, counter, or baud rate generator: Timer is always incremented by the
microcontroller. The time taken to count one digit up is based on master clock frequency.
If Master CLK=12 MHz,
Timer Clock frequency = Master CLK/12 = 1 MHz
Timer Clock Period = 1micro second
This indicates that one increment in count will take 1 micro second.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the timers, and each timer also has
two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).
32
TMOD Register
TCON Register
33
TIMER MODES
Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit mode.
Timer Mode-2: (Auto-Reload Mode): This is a 8 bit counter/timer operation. Counting is performed in
TLX while THX stores a constant value. In this mode when the timer overflows i.e. TLX becomes FFH, it is
fed with the value stored in THX. For example if we load THX with 50H then the
34
timer in mode 2 will count from 50H to FFH. After that 50H is again reloaded. This mode is useful in
applications like fixed time sampling.
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and TF0 are
available to Timer-0 lower 8 bits(TL0).
35
2.14 PROGRAMMING 8051 TIMERS IN ASSEMBLY
In order to program 8051 timers, it is important to know the calculation of initial count value to be stored in
the timer register. The calculations are as follows.
In any mode, Timer Clock period = 1/Timer Clock Frequency.
= 1/(Master Clock Frequency/12)
Mode 1:
Load the TMOD value register indicating which timer (0 or 1) is to be used and which
timer mode is selected.
Load registers TL and TH with initial count values.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
Keep monitoring the timer flag (TF) with the “JNB TFx,target” instruction to see if it is
raised. Get out of the loop when TF becomes high.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer 1,
respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for timer
0 and timer 1, respectively.
Go back to step 2 to load TH and TL again.
Mode 0:
The programming techniques mentioned here are also applicable to counter/timer mode 0.
The only difference is in the number of bits of the initialization value.
Mode 2:
Load the TMOD value register indicating which timer (0 or 1) is to be used; select timer
mode 2.
Load TH register with the initial count value. As it is an 8-bit timer, the valid range is from
00 to FFH.
Start the timer.
36
Keep monitoring the timer flag (TFx) with the “JNB TFx,target” instruction to see if
it is raised. Get out of the loop when TFx goes high.
Clear the TFx flag.
Go back to step 4, since mode 2 is auto-reload.
1. Write a program to continuously generate a square wave of 2 kHz frequency on pin P1.5 using
timer 1. Assume the crystal oscillator frequency to be 12 MHz.
The period of the square wave is T = 1/(2 kHz) = 500 s. Each half pulse = 250 s.
The value n for 250 s is: 250 s /1 s = 250
65536 - 250 = FF06H.
TL = 06H and TH = 0FFH.