Dept.
of EEE, Sir MVIT, Bangalore
In This Module . . .
Introduction to 8051 assembly programming,
Assembling and running an 8051 program,
Data types
Assembler directives,
Arithmetic, logic instructions and programs,
Jump, loop and call instructions,
IO port programming
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Introduction to 8051 Assembly Programming
Machine Language
Program with 0s and 1s
Assembly Language
Program written using the instruction set provided by the manufacturer
Knowledge of internal architecture is necessary
Can be easily converted to machine language using assembler
High Level Language
Program written in languages like C, C++ and JAVA
Knowledge of internal architecture is not necessary
Can be easily converted to machine language using compiler
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Introduction to 8051 Assembly Programming
Assembly language instruction consists of four fields
Label: Mnemonic Operand ;Comment
Label
Used to reference a line in the program
Should not exceed a certain number of characters
Should not same as any of the keywords
Mnemonic or Instruction
Programs written using codes and abbreviations
Easy to remember
Code signifies the operation being performed
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Introduction to 8051 Assembly Programming
Assembly language instruction consists of four fields
Label: Mnemonic Operand ;Comment
Operand
The data upon which the operation is performed
Comment
Any statement following a semicolon is considered as a comment
It is not executed by the assembler
Used to describe the program or make it more meaningful
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Assembling and Running an 8051 Program
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Assembler Directives
They appear just like instructions in the ALP, but they tell the assembler to do
something other than creating the machine code for the instruction.
They instruct the assembler to do the following
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Widely used 8051 assembler directives
ORG
EQU
SET
DB
END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
ORG (Origin)
Used to indicate the starting address
Eg: ORG 2000h ; set the PC to 2000
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
EQU and SET
Used to assign numerical value or register
name to the specified symbol name.
Eg:
Pointer SET R1 ; use R1 as pointer
Counter EQU R3 ; use R3 as counter
N EQU 35h
MOV R3, #N ; 35h is stored in R3
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
EQU and SET
EQU is used to define a constant without
storing information in the memory.
The symbol defined with EQU should not be
redefined
SET directive allows redefinition of symbols
at a later stage.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
DB (define byte)
Initializes memory with 8 bit values.
Eg:
DB 40H ; hex
DB 01011100B ; binary
DB 48 ; decimal
DB ‘HELLO’ ; ASCII
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
END
It indicates the end of the program to the
assembler.
Any text that appears after the END directive is
ignored.
If END directive is missing, assembler will
generate an error message.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Types of Instructions
Data Transfer Instructions
Arithmetic Instructions
Logical Instructions
Bit Level Manipulation instructions
Program and Machine control instructions
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Data Transfer Instructions
Internal Data Transfer MOV
External Data Transfer MOVX
Code memory Read only Data Transfer
MOVC
Push and Pop PUSH and POP
Data Exchange XCH
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Internal Data Transfer
Moving data from one location to another
MOV Destination,Source
Data stored at a source address is moved (actually
copied) to a destination address
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Addressing Modes
Immediate Addressing MOV A,#20h
Register Addressing MOV R5,A
Direct Addressing MOV A,30h
Indirect Addressing MOV A,@R0
• ***Moving data between registers Rn is not
allowed
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
3. Put the number 34h in registers R5, R6 and R7.
Solution:
MOV R5,#34h MOV A,#34h
MOV R6,#34h MOV R5,A
MOV R7,#34h MOV R6,A
MOV R7,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
External Data Transfer
• Data transfer between the microcontroller and the
external memory
MOVX A,@DPTR
MOVX A,@Ri
MOVX @DPTR,A
MOVX @Ri,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
External Data Transfer
• Example
MOV DPTR,#8050h
MOVX A,@DPTR
8050 2D
DPTR 8050
A 2D
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
Copy the data in external RAM location 0123h to 45h.
Solution:
MOV DPTR,#0123h
MOVX A,@DPTR
MOV 45h,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Code Memory Read Only Data Transfer
• Used for accessing predefined bytes of data stored in
the ROM
• Done using indirect addressing and the A register in
conjunction with the DPTR or the PC
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
•MOVC A,@A+DPTR
•MOVC A,@A+PC
• The number in register A is added to the pointing
register to form the address in ROM where the desired
data is found
• The data is then fetched from that address and placed
in the Accumulator
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Example:
MOV DPTR,#1234h
MOV A,#56h
MOVC A, @A+DPTR
• The contents of the address 128Ah in ROM will be
copied to the Accumulator
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
EXAMPLE
MOV A, #58h
MOVC A,@A+PC
The PC is incremented by 1, before it is added to A to form the final
address of the code byte.
If the PC content is 4000h, the program will copy the content of 4059
to A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
PUSH and POP
• Enables data transfer between an area of internal RAM
known as Stack and the specified direct address
• The instruction format is given as
PUSH address
POP address
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
• The PUSH opcode copies data from the
source address to the address in the
Stack Pointer incremented by 1
• Example
If the content of SP is 30h
PUSH 80h
• Moves the content of the address 80h to
the address 31h
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
• The POP opcode copies the data from the
address in SP to the address specified in
the instruction and then decrements the
SP by 1
If the content of SP is 30h
POP 80h
• Moves the content of the address 30h to
the address 80h and then decrements
the SP by 1
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
Set the SP register to 07h and PUSH the SP register on the stack;
Predict what number is pushed to the address 08h.
Solution:
MOV SP,#07h
PUSH SP
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Excercise
Put the number 8Dh in RAM locations 30h to 34h.
Use the concept of stack.
MOV 30h, #8Dh
MOV SP,#30h
PUSH 30h
PUSH 30h
PUSH 30h
PUSH 30h
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Data Exchanges
• Moves data in two directions, from source
to destination and from destination to
source
XCH A,Rn XCH A,R3
XCH A,direct XCH A,30h
XCH A,@Ri XCH A,@R0
XCHD A,@Ri XCHD A,@R1
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
Exchange the contents of the SP and the PSW
Solution:
MOV A,SP
XCH A,PSW
MOV SP,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
•Swap the contents of registers R7 and
R6 using the following methods
•1. Register addressing
•2. Direct Addressing
•3. Stack
•4. Exchange instruction
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
• Using Register Addressing
• MOV A,R6
• MOV R5,A
• MOV A,R7
• MOV R6,A
• MOV A,R5
• MOV R7,A
• 6 Bytes AND 6 Lines
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
•Using Direct Addressing
•MOV 10h,06h
•MOV 06h,07h
•MOV 07h,10h
•9 Bytes and 3 Lines
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
•Using Stack
•PUSH 07h
•PUSH 06h
•POP 07h
•POP 06h
•8 Bytes and 4 Lines
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
•Using Exchange Instruction
•XCH A,R6
•XCH A,R7
•XCH A,R6
•3 Bytes and 3 Lines
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Arithmetic Instructions
INC destination
Increment destination by 1
DEC destination
Decrement destination by 1
ADD/ADDC destination, source
Add source with destination without
or with carry
SUBB destination, Source
Subtract with borrow source from
destination
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MUL AB
Multiply the content of Registers A and B
DIV AB
Divide the content of register A by content of B
DA A
Decimal adjust Accumulator.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Add Group of Instructions
• Adds the contents of destination with the sources
• CY, AC and OV flags are affected
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
1. Add the numbers in the internal RAM locations 30h and
31h and put the result in the RAM location 40h(LSB) and
41h(MSB).
MOV 41h,#00h
MOV A,30h
ADD A,31h
JNC loop
INC 41h
loop: MOV 40h,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
2. Multiply the number in register R3 by the number on port2
and put the result in the external RAM locations 30h(MSB)
and 31h(LSB).
MOV A,0A0h
MOV OFOh,R3
MUL AB
MOV R0,31h
MOVX @R0,A
DEC R0
MOV A,0F0h
MOVX @R0,A Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Logical Instructions
• Byte level AND, OR, XOR and NOT Boolean instructions
• Rotate instructions Rotate right/left with/without carry
• Swap accumulator
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Byte Level Boolean Instructions
Boolean Operator Mnemonic
AND ANL
OR ORL
XOR XRL
NOT CPL
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Rotate and Swap Instructions
RL A
Rotate the A register one bit position to the left
RLC A
Rotate the A register and the carry flag one bit
position to the left
RR A
Rotate the A register one bit position to the right
RRC A
Rotate the A register and the carry flag one bit
position to the right
SWAP A
Interchanges the nibbles of the Accumulator
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
1. OR the contents of ports 1 and 2; Put the result in external RAM
location 0100h.
Solution:
MOV A,90h
ORL A,0A0h
MOV DPTR,#0100h
MOVX @DPTR,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
2. Compliment the lower nibble of RAM location 2Ah.
Solution:
MOV A,2Ah
CPL A
MOV R0,#2Ah
XCHD A,@R0
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Bit Level Manipulation Instructions
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
1. Clear bit 3 of RAM location 22h without affecting any other bit.
Solution:
CLR 13h
OR
ANL 22h,#F7h
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
2. Move bit 4 of RAM location 30h to bit 2 of A.
Solution:
MOV A,30h MOV A,30h
RR A RLC
RR A RLC
RLC
RLC
MOV 0E2,C
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Program and Machine Control
Instructions
• Conditional Branching
• Direct Jump
• Direct Call and Return from Routines
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Conditional Branching
• JZ (Jump on Zero)
• JNZ (Jump on No Zero)
• JC (Jump on Carry)
• JNC (Jump on No Carry)
• JB (Jump if Bit Set)
• JNB (Jump if Bit Not Set)
• JBC (Jump if Bit is Set and Clear Bit)
• CJNE (Compare and Jump if Not Equal)
• DJNZ (Decrement and Jump if Not Zero)
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Direct jumps
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
• The SJMP command can only jump to an address within +/- 128 bytes of the
SJMP command.
• The AJMP command can only jump to an address that is in the same 2k block
of memory as the AJMP command
• The LJMP command can jump to any address in the memory.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Direct Calls and Returns
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
• When the 8051 executes an LCALL or an ACALL instruction it immediately pushes
the current Program Counter onto the stack and then continues executing code
at the address indicated by the LCALL instruction.
• The RET instruction, when executed, returns to the address following the
instruction that called the given subroutine. More accurately, it returns to the
address that is stored on the stack.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Addressing Modes
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
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Indexed Addressing
MOVC A,@A+DPTR
MOVC A,@A+PC
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Relative Addressing
JZ rel
SJMP rel
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Absolute Addressing
AJMP addr11
ACALL addr11
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Long Addressing
LJMP addr16
LCALL addr16
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Bit Inherent Addressing
CLR C
The flag which contains the operand is implied in the opcode
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Bit Direct Addressing
CLR 07H
SETB 07H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to exchange the contents of
external data memory 8100H with contents of
internal data memory 40H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV DPTR,#8100H
MOVX A,@DPTR
XCH A,40H
MOVX @DPTR,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to exchange the contents of A
register and B register using stack
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
PUSH A
PUSH B
POP A
POP B
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to copy the most significant nibble
of A in both nibbles of RAM address 3CH. Also
write the algorithm for example if A=36H and
3CH=33H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV 3CH,A
SWAP A
MOV R0,#3CH
XCHD A,@RO
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to add the unsigned numbers
found in internal RAM locations 25H and 26H and
27H together and the put the result in RAM
locations 31H (MSB) and 30H(LSB)
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV 31H,#00
MOV A,25H
ADD A,26H
ADDC A,27H
JNC LAST
MOV 31H,#00H
LAST: MOV 30H,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to add the N numbers found in
internal RAM locations starting from the
address 30H and put the result in RAM
locations 61H (MSB) and 60H(LSB)
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV 61H,#00
MOV R1,#N
MOV R0,30H
MOV A,@R0
LOOP: INC R0
ADD A,@R0
DJNZ R1,LOOP
JNC LAST
MOV 61H,#01H
LAST: MOV 60H,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to add 5 nos..Numbers
are stored between internal RAM 60H to
64H. Store the result in R0 and A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R0H,#00
MOV R2,#05
MOV R1,30H
MOV A,@R1
LOOP: INC R1
ADD A,@R1
DJNZ R2,LOOP
JNC LAST
MOV R0,#01H
LAST: END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to find the average of N
numbers found in internal RAM locations
starting from the address 40H. Store the
result after the last byte in the memory
block. Assume that the sum is within 8 bits.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R1,#N
MOV R0,40H
MOV A,@R0
LOOP: INC R0
ADD A,@R0
DJNZ R1,LOOP
MOV B,#N
DIV AB
INC R0
MOV @R0,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to find if the number stored in
40H is even or odd. If even store 00 in 41H.
Else store FF.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV 41H,#00H
MOV A,40H
MOV B,#02H
DIV AB
CJNE B, #01, LAST
MOV 41H,#FFH
LAST: END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP which checks whether the 10
nos. Stored from external RAM memory address
2000H are odd or even. The program should
store accordingly 00H or FFH from internal
locations 30H onwards
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV DPTR,#2000H
MOV R0,#30H
LOOP: MOVX A,@DPTR
MOV B,#02H
DIV AB
MOV @R0,#00H
CJNE B, #01, NEXT
MOV @R0,#FFH
NEXT: INC R0
INC DPTR
DJNZ R1,LOOP
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write a subroutine which checks the
content of location 20H if it is a positive
number the subroutine finds its 2’s
complement and stores it in same location
and returns
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV A,20H
RLC A
JC LAST
MOV A,20H
CPL A
ADD A,#01
MOV 20H,A
LAST: RET
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP in 8051 to count the number of
positive and negative numbers present in internal
memory block starting with the address 20H
containing N bytes store the counts after the last
data byte in the memory block
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R0,#20H
MOV R1,#00H; keeps count of positive numbers
MOV R2,#00H; keeps count of negative numbers
MOV R3,#N
LOOP: MOV A,@R0
RLC A
JC NEG
INC R1
SJMP NEXT
NEG: INC R2
NEXT: INC R0
DJNZ R3, LOOP
INC R0
MOV A,R1
MOV @R0,A
INC R0
MOV A,R2
MOV @R0,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to find the Factorial
of a given number N stored in
external RAM location 9000H. Store
the result in 9001H. Assume N! is
maximum 8 bit value
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV DPTR,#9000H
MOVX A,@DPTR
MOV R1,A
DEC R1
LOOP: MOV B,R1
MUL AB
DJNZ R1,LOOP
INC DPTR
MOVX @DPTR,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to find the value of N!/R!
Using a subroutine that calculates the
factorial of a number. Assume the values
of N and R are stored in locations 10H
and 11H. Store the value of N!/R! In 12H.
Assume N!, R! And N!/R! are all
maximum 8 bit values
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV A,10H
LCALL FACT
MOV R0,A
MOV A,11H
LCALL FACT
MOV B,A
MOV A,R0
DIV AB
MOV 12H,A
FACT: MOV B,A
DEC B
LOOP: MOV B,R1
MUL AB
DJNZ B,LOOP
RET
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to perform 16x8 bit
multiplication.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV A,30H
MOV B,40H
MUL AB
MOV 50H,A
MOV R1,B
MOV A,31H
MOV B,40H
MUL AB
ADD A,R1
MOV 51H,A
CLR A
ADDC A,B
MOV 52H,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to find the largest
number among the 14D 8 bit number
stored in internal RAM starting from the
address 30H. Store the result in 40H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R2,#14D
MOV R0,#30H
CLR C
MOV A,#00H
LOOP: SUBB A,@RO
JNC NEXT
MOV A,@R0
NEXT: INC R0
DJNZ R2,LOOP
MOV 40h,A
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to find the fibonacci
terms less than FFH. Store the series
in the internal RAM starting from the
address 40H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R0,#40H
MOV B, #00H
MOV A, #01H
MOV @R0, #00H
INC R0
MOV @R0, #01H
BACK: MOV R1,A
ADD A, B
JC LAST
INC R0
MOV @R0, A
MOV B, R1
SJMP BACK
LAST: NOP
END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write 8051 ALP to find first two
internal RAM locations between 20H
and 30H which contains consecutive
numbers. If so set the carry flag to 1
else clear the carry flag. Store the
address of consecutive numbers in
40h and 41h.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R0,#30H
LOOP: MOV A,@R0
DEC R0
CLR C
SUBB A,@R0
CJNE A,#01H,NEXT
SETB C
SJMP LAST
NEXT: CJNE R0,#20H,LOOP
CLR C
LAST: MOV 40h,R0
INC R0
MOV 41h,R0
END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to find if the number in
30H is a palindrome. If yes store FFH
in 40H. Else store 00.
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R0,#FFh
MOV A,30h
ANL A,#81H
JB PSW.0 LAST
MOV A,30H
ANL A,#42H
JB PSW.0 LAST
MOV A,30H
ANL A,#24H
JB PSW.0 LAST
MOV A,30H
ANL A,#18H
JB PSW.0 LAST
SJMP RESULT
LAST: MOV R0,#00h
RESULT: MOV 40h,R0
END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Write an ALP to count the number of
one’s and zero’s in the number
stored in 30H. Store the count in 40H
and 41H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV R0,#00H
MOV R1,#00H
MOV A,30H
MOV R2,#08H
Repeat: RLC A
JC Ones
INC R0
SJMP Count
Ones: INC R1
Count: DJNZ R2,Repeat
MOV 40H,R0
MOV 41H,R1
END
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Additional programs to be learnt
1. Block data transfer
2. Block exchange
3. Sorting
4. All code conversion programs
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Machine Cycle
Oscillator Frequency = 11.0592 MHz
1 M/C Cycle = 12 Oscillator Pulses
= 12 x 1/f
= 1.085 micro sec
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Simple Delay Subroutine
No. of M/C Cycles
MOV R1,#FFh 1 1
Loop: DJNZ R1,Loop 2 255
RET 2 1
Delay Time = [1+2*255+2]*1.085 µs
=0.556ms
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Count Calculation
No. of M/C Cycles
MOV R1,#count 1 1
Loop: DJNZ R1,Loop 2 count
RET 2 1
Delay Time = [1+2*count+2]*1.085 µs
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Excercise
Write a delay subroutine to generate a delay of 0.5 ms. Assume the oscillator
frequency as 11.0592 MHz.
Time Delay = [1+2*count+2]*1.085 µs = 0.5ms
count=228.9 = 229d = E5h
MOV R1,#E5h
Loop: DJNZ R1,Loop
RET
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Nested Loop Delay Subroutine
No. of M/C Cycles
MOV R1,#FFh 1 1
Loop2: MOV R2,#FFh 1 255
Loop1: DJNZ R2,Loop1 2 255*255
DJNZ R1,Loop2 2 255
RET 2 1
Delay Time
=[1+255+2*255*255+2*255+2]*1.085 µs
=0.142s
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Count Calculation
No. of M/C Cycles
MOV R1,#count1 1 1
Loop2: MOV R2,#count2 1 count1
Loop1: DJNZ R1,Loop1 2 count1*count2
DJNZ R1,Loop2 2 count1
RET 2 1
Delay Time
=[1+count1+2*count1*count2+2*count1+2]*1
.085 µs
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Excercise
Write a delay subroutine to generate a delay of 0.1s.
Assume the oscillator frequency as 11.0592 MHz.
Time Delay
=[1+count1+2*count1*255+2*count1+2]*
1.085 µs = 0.1s
count=179.6 = 180d = B4h
MOV R1,#B4h
Loop2: MOV R2,#FFh
Loop1: DJNZ R1,Loop1
DJNZ R1,Loop2
RET Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
For a machine cycle of 1.085 µSec, find the time
delay in the following subroutine.
DELAY: MOV R2,#200
AGAIN: MOV R3,#250
NOP
NOP
HERE: DJNZ R3,HERE2
DJNZ R2,AGAIN
RET
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
If the XTAL frequency of 8051 is 8 MHz, find the
time taken to execute the following program:
MOV R2,#04
MOV R1,#06
WAIT: DJNZ R2,WAIT
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Show the stack contents, SP contents and
contents of any registers affected after each
step of the following sequence of operation
MOV SP,#70H
MOV R5,#30H
MOV A,#44H
ADD A,R5
MOV R4,A
PUSH 4
PUSH 5
POP 4
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
SP STACK A R5 R4
MOV SP,#70H 70
MOV R5,#30H 30
MOV A,#44H 44
ADD A,R5 74
MOV R4,A 74
PUSH 4 71 74
PUSH 5 72 30
POP 4 71 30
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
After reset, the contents of internal memory
of 8051 with address 0AH and 0BH contains
data 22H and 33H respectively. Sketch the
contents of internal memory from address
07H to 0BH and the value of register SP, after
executing the following code:
PUSH 0AH
.
.
.
MOV 81H,#0BH
POP 09H
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
SP 07H 08H 09H 0AH 0BH
07 22 33
PUSH 0A H 08 22
MOV 81H,#0BH 0B
POP 09H 0A 33
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Signed Number representation in 8051
• Signed numbers are of two types:
• Positive
• Negative
• When dealing with signed numbers the MSB (bit D7) is used to
represent the sign of the number
• For positive numbers D7=0
• For negative numbers D7=1
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Storing negative numbers
• Stored in 2’s complement form
• Write the number in binary form
• Complement each bit
• Add 1
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Data Types
Unsigned char : 0 to 255 (8 Bit)
Signed char : -128 to 127 (8 Bit)
sign
0 000 0000 00
0 000 0001 01
0 000 0010 02
0 111 1111 127
1 111 1111 -1
1 111 1110 -2
1 000 0001 -127
1 000 0000 -128
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Examine the following code and analyse the
result with flag register content
• MOV A, #-30D
• MOV R2, #-50D
• ADD A , R2
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Flags affected during arithmetic operations
1. AC : Set when carry is generated from D3 bit
2. C : Set when carry is generated from D7 bit
C is to be ignored in signed arithmetic operations
3. OV: Set when there is a carry either from D6 or D7 bit but not both. (if
OV=1 the result is wrong. In signed number manipulation OV is used to
indicate the validity of the answer)
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
MOV A, #-30D A 11100010
MOV R2, #-50D R2 11001110
ADD A , R2 A 1 10110000
FLAGS SET: AC, C
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Moving data to a port changes the port latch
Moving data from a port gets data from the port pins
To configure a port pin as an input pin write a 1 to the port pin latch
To configure a port pin as an output pin write a 0 to the port pin latch
By default a port is an output port
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Excercise
Copy the data on pins of port 2 to the port 2 latch
MOV R0,P2
MOV P2,R0
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Excercise
Configure all the pins of port 1 as an input pins and get the port data
to register R7
MOV P1,#FF
MOV R7,P1
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
Configure pin 5 of Port 0 as input pin
MOV P0,#00100000b
OR
MOV P0,#20h
OR
SETB P0.5
OR
SETB 85
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore
Exercise
Write a program to transfer the value 41h serially (one bit at a time) via pin
P2.1
MOV A,#41H
MOV R1,#08H
REPEAT: RRC A
MOV P2.1,C
DJNZ R1,REPEAT
Prepared by Dr. R Subha, Dept. of EEE, Sir MVIT, Bangalore