8051 Programming Sec B Elect M&M
8051 Programming Sec B Elect M&M
Microcontrollers
8051 Assembly Language Programming/
Instruction Sets
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING
ADDRESSING MODES
The instructions of 8051 may be classified based on the source or destination type
Register addressing.
Direct addressing.
Register Indirect addressing.
Immediate addressing.
Base register + Index register.
PUSH <direct>
This instruction copies the data from the source address onto the stack.
Operation
[SP] [SP] + 1, [[SP]] direct
Addressing mode
Direct addressing
Example
Let SP =0AH and data pointer = 1234H
PUSH DPL
PUSH DPH
The first instruction PUSH DPL will set the SP = 0BH and store 34H in internal RAM
location 0BH. The second instruction PUSH DPH will set the SP = 0CH and store 12H in
internal RAM location 0BH. The stack pointer will remain at 0CH.
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING
POP <direct>
This instruction copies data from the stack to the destination location.
Operation
(direct) [[SP]], [SP] [SP] -1
Addressing mode
Direct addressing.
Example
Let SP = 35 H and data at internal RAM locations 34H be 52H, then
instruction POP DPL will level the stack pointer to value 34H and DPL =
52H.
ARITHMETIC INSTRUCTIONS:
ADD A, <src-byte>
Operation
[A] [A] + <src-byte>
This instruction adds the byte variable indicated to the accumulator. The result is
contained in the accumulator.
All the addressing modes can be used for source: an immediate number, a register,
direct address, and indirect address.
Mnemonic Example Description
ADD A, Rn ADD A, R0 This instruction will add the byte in register Rn
of the selected register bank with the byte in
accumulator. The result is contained in the
accumulator
ADD A, direct ADD A, 20H This instruction will add the contents of the
memory location whose direct address is
specified in the instruction with the accumulator
contents. The result of addition will he stored in
the accumulator.
ADD A, @ Ri ADD A, @ R0 This instruction will add the contents of memory
location whose address is pointed by register Ri
of the selected register bank with contents of the
accumulator. The result of addition is stored in
the accumulator
ADD A, # data ADD A, # 30H This instruction will add the immediate 8 bit
data with data in the accumulator. The result of
addition is stored in the accumulator.
ADDC A, <src-byte>
Operation
[A] [A] + < src-byte> + carry
This instruction will add the byte variable indicated, the carry flag and the
accumulator contents. The result of addition is stored in the accumulator.
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING
All the addressing modes can be used for source : an immediate number, a register,
direct address, indirect address.
Mnemonics Addressing Example Description
mode
ADDC A, Rn Register ADDC A, Rl This instruction will add the
addressing contents of accumulator with
the contents of register Rn of
the selected register bank and
carry flag. The result of
addition is stored in
accumulator.
ADDC A, Direct ADDC A, 10H This instruction will add the
direct addressing contents of memory location
whose direct address is
specified in the instruction with
the contents of accumulator and
carry. The result of addition is
stored in the accumulator.
ADDC A, @Ri Register Indirect ADDC A, This instruction will add the
@R0 contents of memory location
pointed by register Ri of
selected register bank with the
accumulator and earn' flag. The
result is stored in accumulator.
ADDC A, #data Immediate ADDC A, This instruction will add the
addressing #40H contents of accumulator with
immediate data specified in the
instruction along with carry.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 11/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
Mnemonics Addressing Example Description
mode
INC <byte>
Operation
byte byte +1
This instruction will increment the indicated variable by 1.
If the byte value is FFH and if it is incremented, then the result will overflow to 00H.
It supports three addressing modes. Register, direct and register-indirect.
Mnemonics Addressing Example Description
mode
INC Rn Register addressing INC R5 This instruction will increment
the contents of register Rn of
selected register bank by 1.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 12/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
INC direct Direct addressing INC 10H This instruction will increment
the contents of memory location
whose address is specified in the
instruction by 1
INC @Ri Register Indirect INC, @R0 This instruction will increment
the contents of memory location
that is pointed by register Ri by
1.
INC DPTR
Operation
[DPTR] [DPTR] + 1
Addressing mode: Register addressing mode.
This instruction will increment the contents of Data Pointer by 1.
A 16-bit increment is performed. An overflow of the low-order byte of the data
pointer (DPL) from 0FFH to 00H will increment the high order byte (DPH).
DPTR is the only 16-bit register that is incremented.
Example: let the contents of DPH = 12 H and the contents of DPL = FFH. The
instruction ING DPTR will cause DPL = 13 H and DPL = 00 H.
DEC <byte>
Operation
<byte> <byte> -1
• This instruction will decrement the indicated variable by 1.
• An original value of 00H will underflow FFH.
• Three operand-addressing modes are allowed: register, direct and register indirect.
o
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 13/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
DEC @Ri Register Indirect DEC @R0 This instruction will decrement the
contents of memory location that is
pointed by register Ri by 1.
MUL AB
Operation
[A]7-0 [A] x [B] . [B]15-8
This instruction multiplies an eight bit unsigned integer in the Accumulator and the 13
register. The low-order byte of the sixteen-bit product is left in the accumulator, and
the high-order byte in B.
Addressing mode : Register addressing mode.
Example: Let A = 50 H, B = A0 H after execution of MUL AB the content of register
B = 32 H and register A = 00 H (as (50H) X (A0 M) = (3200H)).
DIV AB
Operation
[A] (Quotient) <- A + B. [BJ (Remainder)
This instruction divides the unsigned number in accumulator with the unsigned
number in register B.
Accumulator contents the quotient of the result and register B contains the remainder.
The contents of A and B, when division by 0 is attempted, are undefined.
Addressing mode. Register addressing mode.
Example; Let [A] = FB H and [B] = 12 H then DIV AB will result [A] = 0DH
(quotient), [B] = 11 H (remainder)
DAA
Decimal Adjust Accumulator for addition.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 14/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
ADDC A, Rn Add R Register with Carry bit to Accumulator 1 1
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 15/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
LOGICAL INSTRUCTIONS
ANL <dest-byte>, <src-byte>
This instruction performs bit wise logical AND operation between the destination and
the source byte. The result is stored at the destination byte.
The source and destination support four addressing modes: register, direct, register-
indirect, and immediate addressing modes.
Mnemonics Addressing Example Description
mode
ANL A, Rn Register ANL A, This instruction will perform bit wise logical AND
addressing R5 operation between the contents of accumulator and
register Rn of the selected register bank. The result
will be stored in the accumulator
ANL A, Direct ANL A, This instruction will bit wise logically AND the
direct addressing 70H contents of accumulator with the contents of memory
location whose direct address is specified in the
instruction. The result will be stored in the
accumulator
ANL direct, Direct AXL This instruction will bit wise logically AND the
A addressing 30H, A contents of memory location whose direct address is
specified in the instruction with the contents of
accumulator. The result will be stored in the memory
location whose direct address is specified in the
instruction.
ANL A, ®Ri Register ANL A, This instruction will bit wise logically AND the
Indirect @R1 contents of accumulator with the contents of memory
Addressing location pointed by register Ri of the selected
register bank. The result will be stored
ANL A, Immediate ANL A, This instruction will bit wise logically AND the
#data addressing #57H contents of accumulator with the immediate data
specified in the instruction. The result will be stored
in the accumulator.
ANL direct, Immediate ANL This instruction will bit wise logically AND the
#data addressing 54H, contents of memory location whose direct address is
#33H specified in the instruction with the contents of with
the immediate data specified in the instruction. The
result will be stored in the memory location whose
direct address is specified in the instruction.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 16/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
ORL <dest-byte>, <src-byte>
This instruction performs bit wise logical OR operation between the destination and
the source byte. The result is stored at the destination byte.
The source and destination support four addressing modes: register, direct, register-
indirect, and immediate addressing modes.
CLRA
This instruction will clear all the bits of accumulator to zero.
CPLA
This instruction will complements all the bits (l's complement) of the accumulator.
RLA
This instruction will rotate the eight bits in the accumulator by one bit to the left.
Addressing mode: Register Specific addressing mode.
RLCA
This instruction will rotate the eight bits in the accumulator and the carry flag together
by one bit to the left
Addressing mode: Register Specific addressing mode.
RRA
This instruction will rotate the eight bits in the accumulator by one bit to the right.
Addressing mode: Register Specific addressing mode.
RRCA
This instruction will rotate the eight bits in the accumulator and the carry flag together
by one bit to the right.
Addressing mode: Register Specific addressing mode.
SWAP A
This instruction interchanges the low order and high order nibbles of the accumulator.
Operation:
[A3-0] [A7-4]
Addressing mode: Register Specific addressing mode.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 17/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
TIMER/COUNTER PROGRAMS
To Generate a Square Wave On The Port 1.
MOV SP, #7H ; Initialize stack pointer since we are using
; subroutine program
BACK: MOV P1, #00H ; Send 00H on port 1 to generate low level
; of square wave
ACALL DELAY ; Wait for sometime
MOV P1, #0FFH ; Send FFH on port 1 high level of square wave
ACALL DELAY ; Wait for sometime
SJMP BACK ; Repeat the sequence
DELAY: MOV R1, #0FFH ; Load Count
AGAIN: DJNZ R1, AGAIN ; Decrement count and repeat the process
; until count is zero
RET ; Return to main page
ORG 8100H
MOV TMOD, #01H ; 16 bit timer mode
LOOP: MOV TH0, #0FEH ; -500 (high byte)
MOV TL0, #0CH ; -500 (low byte)
SETB TR0 ; start timer
WAIT: JNB TF0, WAIT ; wait for overflow flag
CLR P1.0 ; toggle port bit
SJMP LOOP ; repeat
END
A 1 kHz square wave requires a high-time of 500 μs and a low-time of 500 μs. Since the
interval is longer than 256 μs, mode 2 cannot be used. Full 16-bit timer mode, mode 1, is
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 19/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
required. The main difference in the software is that the timer registers, TL0 and TH0, are
reinitialized after each overflow.
There is a slight discrepancy in the output frequency in the program above. This results from
the extra instructions inserted after the timer overflow to reinitialize the timer. If exactly 1
kHz is required, the reload value for TL0/TH0 must be adjusted somewhat. Such errors do
not occur in auto-reload mode, since the timer is never stopped - it overflows at a consistent
rate set by the reload value in TH0.
PROGRAMMING EXAMPLES
List the programming steps needed to receive data serially using 8051 microcontroller and
explain.
Solution
The following steps are taken to program 8051 to receive data serially.
1. The TMOD register is loaded with value 20 H indicating timer 1 is used mode 2 (8 bit
auto reload mode). This timer is used to set band rate.
2. TH1 is loaded with a suitable value to set proper band rate. If crystal frequency is
11.0592 MHz and if we load TH1 with FD we get a band rate of 9600.
3. The SCON register is loaded with the value 50 H, indicating serial mode 1, where 8
bit data is framed with start and stop bits and receive enable is turned on.
4. TR1 flag is set to 1 to start Timer
5. R1 is cleared with the CLR R1 instruction.
6. The R1 flag bit is monitored with the use of the instruction JNB R1, XX to see if an
entire character has been received yet.
7. When Ri is raised SBUF has the serial data byte move the
8. contents to desired memory location.
9. To receive the next character go to step 5.
Example
Write program to receive bytes of data serially and put them in P1. Set the baud rate at
2400, 8-bit data, and 1 stop bit. Assuming crystal frequency 11.0592 MHz.
Solution
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 20/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
SETB TR1 ; start timer 1
NEXT: JNB R1, NEXT ; wait for char to come in
MOV A, SBUF ; save incoming byte in A
MOV P1, A ; send to port 1
CLR R1 ; get ready to receive next byte
SJMP NEXT ; keep getting data
Example
Write program to send 44H to ports P1 and P2, using (a) their addresses (b) their names.
Solution
Example
Write program to copy a block of 8 bytes of data to RAM locations starting at 50H from RAM
locations 30H.
Solution
Example
Write a program for 8051 to transfer serially letter N at 9600 baud, continuously. Assuming
crystal frequency 11.0592 MHz.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 21/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
Solution
Example
Add the contents of RAM locations 60H, 61H and 62H. Store the result in RAM locations
41H (MSB) and 40H (LSB).
Solution
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 22/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
ASSIGNMENT
Q.1. (AMIE S14, 10 marks): Explain the instruction sets of 8051 in detail.
Q.2. (AMIE S10, 20 marks): Explain the following instructions of 8051 microcontroller:
(i) DJNZ R2, THERE
(ii) CJNE A, DIRECT, REL
(iii) PUSH 0
(iv) MOVX A, @DPTR
(v) MOVCA, @ A+DPTR
(vi) LJMP 4100H
(vii) MOV @ R0, # data
(viii) ORL A, @R1
(ix) XCH A, @R0
(x) SETB C
Q.3. (AMIE W10, 10 marks): Explain the following instructions of 8051 microcontrollers: (i) INCA (ii) INC
add (iii) ADD @ RP (iv) ADD CA, #n (v) MOL AB
Q.4. (AMIE S11, 8 marks): Explain the following instructions:
(i) MOV A, # 56H
(ii) MOVC A, @A+DPTR
(iii) DEC @R1
(iv) DJNZ R0 BACK
Q.5. (AMIE W12, 10 marks): Explain the operation of the following instructions of 8051 microcontroller: (i)
SWAPA (ii) MOV A @R1 (iii) MOVx A#R1 (iv) DJNZ R0 TABLE (v) SETB P1.3
Q.6. (AMIE W13, 10 marks): Explain the operation of the following instructions with examples: (i) MOVC A
@A + DPTR (ii) XCHD A, @RO (iii) MOV C, P3.1 (iv) SWAPA (vv) RR A
Q.7. (AMIE W14, 10 marks): Explain the operations of following instructions: (i) CJNE A, # data, LABEL 1
(ii) DJNZ R0, LABEL 2 (iii) RL A (iv) DA A (v) MOVX @ DPTR, A
Q.8. (AMIE S15, 6 marks): What are the significance of DPTR and EA pin?
Q.9. (AMIE S15, 6 marks): Explain why SJMP instruction is used in place of HLT in 8051.
Q.10. (AMIE S15, 6 marks): Explain the operation of CJNE and DJNZ instructions.
Q.11. (AMIE S10, 5 marks): List and explain the logical group of instructions of 8051 microcontroller with
examples.
Q.12. (AMIE W11, 5 marks): Explain any five arithmetic instructions of 8051.
Q.13. (AMIE S12, 16 marks): Write four instructions for each data transfer group, arithmetic group, logical
group and branch group.
Q.14. (AMIE S10, 12, 5 marks): Write a delay program using registers of 8051 microcontroller.
Q.15. (AMIE W10, 10 marks): Write a program to double the number in register R1 and store the result in R2
and R3. The microcontroller used is 8051.
Q.16. (AMIE W10, 10 marks): Illustrate the logical OR operation in 8051 using a program.
Q.17. (AMIE S15, 8 marks): Explain addressing modes of 8051 microcontroller.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 23/24
AMIE(I) STUDY CIRCLE(REGD.)
MICROPROCESSORS & MICROCONTROLLERS
8051 ASSEMBLY LANGUAGE PROGRAMMING A Focused Approach
Q.18. (AMIE W10, 10 marks): Explain how data is stored and received in 8051 using cell and stack
instructions.
Q.19. (AMIE S11, 12, W12, 8 marks): Write a program to generate a square wave of frequency 2 kHz through
port P1.0 by timer 0 of 8051 microcontroller.
Q.20. (AMIE W13, 10 marks): Write an assembly language program using 8051 microcontroller instructions
to generate a 50 Hz square wave at port 0, pin 6 (i.e. p0.6).
Q.21. (AMIE W11, 10 marks): Write an assembly language program using 8051 to generate a sawtooth and
square waveform using the general purpose ports of 8051.
Q.22. (AMIE S11, 4 marks): Explain the difference between forward jump and backward jump.
Q.23. (AMIE W11, 4 marks): Explain the instructions to access external RAM and external ROM.
Q.24. (AMIE W11, 10 marks): Write an assembly language program using 8051 to access the 7 segment code
of a number which is stored in ROM. Store 7 segment codes of 0-9.
Q.25. (AMIE S13, 10 marks): Explain different types of instructions groups with the help of at least two
assembly language instructions for 8051 microcontroller.
Q.26. (AMIE W13, 10 marks): An array of 10 numbers is stored in the internal data RAM starting from
location 30H. Write an assembly language program to move the array starting from location 40H.
Q.26. (AMIE S15, 8 marks): Write a program using 8051 assembly language to change the data 55H stored in
the lower byte of the data pointer register to AAH using rotate instruction.
Q.27. (AMIE W14, 10 marks): Write an assembly language program of 8051 for producing binary image of a
8 bit binary data.
Q.28. (AMIE S15, 10 marks): Justify why the crystal oscillator frequency in 8051 is chosen as 11.0592 MHz.
(For online support such as eBooks, video lectures, audio lectures, unsolved papers, quiz, test series and course updates,
visit www.amiestudycircle.com)
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 24/24