Programming the 8051 Microcontroller
Programming the 8051 Microcontroller
LEARNING OBJECTIVES
As you learned in the basic 8085/8088 microprocessor class, an Instruction consists of two parts namely;
Opcode and Operand(s). The opcode tells the processor what to do on executing an instruction. The
operand(s) is the parameter(s) required by opcode to complete the action. The term Instruction Set refers to
the set of instructions supported by a processor/controller architecture. The instruction set of 8051 family
microcontroller is broadly classified into five categories namely; Data transfer instructions, Arithmetic
Programming the 8051 Microcontroller 169
instructions, Logical instructions, Boolean instructions and Program control transfer instructions. Before
going to the details of each type of instructions, it is essential to understand the different addressing modes
supported by the 8051 microcontroller.
A Memory
address
B
R0
07 H 255 (FFH)
R7
A Memory
address
B
R0 55H
55H 255 (F FH )
R7
Fig. 6.2 Illustration of indirect memory addressing with 8bit indirect register (E.g. MOV A, @R0)
Among the scratchpad registers R0 to R7, only R0 and R1 can be used for indirect addressing. For 16bit
external data memory/memory mapped register operations, 16bit register DPTR is used as the indirect
addressing register. The whole 64K bytes of the external memory (if present physically on chip) can be
accessed by indirect addressing.
E.g. MOV DPTR, #0055H ; Load DPTR register with 0055H, The-
; address of the memory location
MOVX A,@DPTR ; Load Accumulator with the contents-
; of the memory location pointed by DPTR
Executing these two instructions moves the content of external data memory at address 0055H to the
Accumulator (Fig. 6.3).
A Memory
address
B
DPH 00H
DPL 55H 0055H 255 (FFH)
Fig. 6.3 Illustration of Indirect memory addressing with 16bit Indirect Register (E.g. MOVX A, @DPTR)
Indirect addressing is same as the pointer concept in C Programming. Indirect addressing is commonly
used for operations similar to pointer based operations in C Programming like array, external memory access,
etc.
offset address register, accumulator, is an 8bit register, the maximum size of the look up table is 256 bytes
(Base address + 0 to Base address + 255).
Another register, which is 16bit wide, used for indexed addressing is Program Counter (PC). The only
difference in using PC for indexed addressing when compared to DPTR is that the PC is not available to user
for direct manipulation and user cannot directly load the desired base address to the PC register by a MOV
instruction as in the case of DPTR. Instead the desired address is loaded to the PC by diverting the program
flow to the location where the Lookup table is held in the code memory by using a CALL instruction.
First the desired table index is loaded to the accumulator register and a call is made to the location where
the lookup table is stored. For this type of tables, the index should be within 1 to 255 (including both). 0
cannot be used as an index since the execution of MOVC A,@A+DPTR increments the PC to the address of
the RET instruction’s memory location. If you use a 0 index along with the PC, the contents retrieved will
be the binary data corresponding to the RET instruction. Code memory lookup tables are usually used for
storing string constants and other configuration data. Another use of indexed addressing is the ‘case jump’
instruction in 8051. Here the jump instruction’s destination address is calculated as the sum of the base
pointer (PC) and the Accumulator data.
Example 1
Write an assembly program to display the numbers from 0 to 9 on a 7-segment common-anode LED display
which is connected to Port 2 of the 8051. The seven segment codes for displaying the numerals 0 to 9 on the
common anode LED is stored as lookup table in the code memory starting at 0050H. Use DPTR register for
holding the base address of the table.
Please refer to the section on 7-segment LED Display given in Chapter 2 for more details on 7-segment
LED Display. Figure 6.4 illustrates the interfacing of common anode LED Display with Port 2 of 8051.
VCC Vcc
AT89C51
Common anode
R
DP G F E D C B A
P2.7
P2.6
P2.5
P2.4
P2.3
P2.2
P2.1
GND P2.0
Fig. 6.4 Circuit for interfacing 7-segment LED display with 8051
Programming the 8051 Microcontroller 173
Now we need to design the different bit patterns (code) to be outputted to the Port 2 pins to display the
numbers from 0 to 9 on the LED Display. The following table explains the same. Refer to the LED segment
arrangement diagram given earlier for clarification.
Digit DP G F E D C B A Code
P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 (Hex)
0 1 1 0 0 0 0 0 0 C0
1 1 1 1 1 1 0 0 1 F9
2 1 0 1 0 0 1 0 0 A4
3 1 0 1 1 0 0 0 0 B0
4 1 0 0 1 1 0 0 1 99
5 0 0 0 1 0 0 1 0 12
6 0 0 0 0 0 0 1 0 02
7 1 1 1 1 1 0 0 0 F8
8 1 0 0 0 0 0 0 0 80
9 1 0 0 1 0 0 0 0 90
Now the codes for displaying the digits and the port interfacing for the LED display is complete. The next
step required for setting up the decimal counter for counting from 0 to 9 is the development of firmware. The
flow chart given in Fig. 6.5 illustrates the firmware requirements for building the decimal counter.
Start
1. Initialise Port 2
2. Initialise Stack Pointer
3. De-select ADC chip
4. Load DPTR with the base address of look-up
table stored in code memory
Index Register = 9?
Fig. 6.5 Flowchart for displaying digits using 7-Segment LED Display
174 Introduc on to Embedded Systems
location pointed by the SP register. POP instruction retrieves the data, which is stored on the stack memory
using PUSH instruction. POP instruction copies the data stored in memory location pointed by SP register to
the variable given along with POP instruction and decrements the SP by one. In 8051 architecture the stack
memory grows upward in memory and follows Last In First Out (LIFO) method. PUSH and POP instructions
use only direct addressing mode and hence the instructions PUSH R0, PUSH R1, PUSH A, etc. are not
valid. They are valid only if the arguments are used with absolute addressing. Hence the valid instructions
corresponding to them are PUSH AR0, PUSH AR1, and PUSH ACC, etc. The operation of PUSH and POP
instructions are illustrated in Fig. 6.6 and Fig. 6.7.
The upper 128 bytes of RAM are not implemented physically in the basic 8051 architecture and its
ROMless counterpart 8031. With these devices, if the SP is set to a location in the upper 128 byte area, the
PUSHed bytes will be lost and POPed bytes will be indeterminate.
[Link] Data Exchange Instruc ons
The data exchange instructions exchange data between a memory location and the accumulator register.
Data exchange instructions modify both memory location and accumulator register. 8051 supports two data
exchange instructions, namely, XCH A, <memloc> and XCHD A, @Ri.
The XCH A,<memloc> instruction performs the exchange of the data at memory location ‘memloc’ with
the accumulator. By using MOV instructions the XCH instruction can be implemented as
MOV R0, A
MOV A, memloc ; ‘memloc’ is a memory in the range
; 00H to 7FH
MOV memloc, R0
It requires three instructions and 4 machine cycles and a temporary variable R0 to achieve this. Whereas
the XCH A, <memloc> accomplishes this task with a single instruction and one machine cycle. The XCHD
178 Introduc on to Embedded Systems
A,@Ri (where i = 0 or 1) instruction exchanges the low nibbles between the accumulator and the data memory
pointed by the indirect memory register R0 or R1. Both XCH and XCHD are accumulator specific instructions.
The operation of XCH A, <memloc> and XCHD A,@Ri instructions are illustrated in Figs 6.8 and 6.9.
the memory pointer register, Port 0 emits the content of DPL register (Lower order 8bit address) and Port
2 emits the content of DPH register. If R0 or R1 is used as the memory pointer register, Port 0 emits the
contents of R0 or R1 register (Lower order 8bit address) and Port 2 emits the contents of its SFR register (P2
SFR). The instruction mnemonic used for external data transfer operation is MOVX and depending on the
memory pointer register the operand will be R0, R1 or DPTR. The accumulator is the implicit operand in
MOVX instruction. The direction of external data memory operation (Read or write operation) is determined
by the use of the accumulator register. If the accumulator register is used as the source register, the external
data memory operation is a Write operation and the WR\ signal is also asserted. The external data memory
operation is a Read operation if the accumulator is used as destination register. This also generates the RD\
signal.
External data memory related instructions are listed in the table given below.
MOVX A,@Ri Read the content of external data memory (8bit 2 2* (fOSC/12)
address) pointed by R0 or R1 to accumulator
MOVX @Ri,A Write accumulator content to external data memo- 2 2* (fOSC/12)
ry (8bit address) pointed by R0 or R1
MOVX A,@DPTR Read the content of external data memory (16 bit 2 2* (fOSC/12)
address) pointed by DPTR register to accumulator
MOVX @DPTR,A Write accumulator content to external data memo- 2 2* (fOSC/12)
ry (16bit address) pointed by DPTR register
The following sample code illustrates how to read and write from and to an 8bit external memory.
; Method-1
; Using Indirect register R0
The following sample code illustrates how to read and write data from and to a 16bit external memory.
MOV DPTR, #2055H ; Let 2055H be the external data memory
; address
MOVX A,@DPTR ; Reads the content of 2055H to Accumulator
MOV A, #01H ; Load Accumulator with 1
MOVX @DPTR, A ; Writes 1 to external memory 2055H
180 Introduc on to Embedded Systems
Figure 6.10 is given the Instruction fetch and execute sequence for the MOVX instruction.
ALE
PSEN
RD
P0 ADDR
INST PCL INST Data PCL INST PCL
(DPL/Ri)
in out in in out in out
out
Fig. 6.10 Timing diagram for MOVX instruction when the program memory is external
MOVX is a two machine cycle instruction. The opcode fetching starts at state-1 (S1) of the first machine
cycle and it is latched to the instruction register. A second program memory fetch occurs at State 4 (S4) of the
same machine cycle. Program memory fetches are skipped during the second machine cycle. This is the only
time program memory fetches are skipped. The instruction-fetch and execute sequence and timing remains
the same regardless the physical location of program memory (it can be either internal or external). If the
program memory is external, the Program Strobe Enable (PSEN) is asserted low on each program memory
fetch and it is activated twice per machine cycle (Fig. 6.10). Since there is no program fetch associated with
the second machine cycle of MOVX instruction, PSEN is not asserted during this (two PSEN signals are
skipped). During the second machine cycle of MOVX instruction, the address and data bus are used for data
memory access.
The lower order address bus is multiplexed with the data bus in 8051 and Port 0 outputs the lower order
address bus during external memory operations and it is available at S5-S6 states of the first machine cycle.
Since it is available only for a short duration of 1 to 1.5 states, it should be latched. The trigger for latch is
provided by the signal Address Latch Enable (ALE). ALE is asserted twice in each machine cycle (during S1
Phase 2 (S1P2) and S4 Phase 2 (S4P2)). Since data is outputted to or read from the external memory at State
1 (S1) to State 3 (S3) of the second machine cycle, during a MOVX instruction, ALE is not emitted at S1P2
of the second machine cycle. Without a MOVX instruction ALE is emitted twice per each machine cycle and
it can be used as a clock signal to any device.
Programming the 8051 Microcontroller 181
The control signal PSEN is not asserted if the program memory is internal to the chip. However, ALE
signal is asserted even if the program memory is internal.
The external data memory timing for external data memory write is similar to the Read operation except
that the control signal used is WR\ instead of RD\ and the data for writing is available in the place of ‘Data
in’ in the above timing diagram . The timing for all signals remains the same.
[Link] Code Memory Read Instruc ons
Code memory read instruction is used for reading from the code memory. There is only one instruction for
code memory read operation and it is MOVC. Detailed description of MOVC instruction is given in an earlier
section with subtitle “Indexed Addressing”. Please refer back.
MUL AB A=AxB Multiplies accumulator with B register and stores the 4 4*(fOSC/12)
result in accumulator & B (Lower order byte of result
in accumulator and higher order byte in B register)
DIV AB A = integer Divides accumulator with B register and stores the result 4 4*(fOSC/12)
part of [A/B] in accumulator and remainder in B register
B=Remainder
of [A/B]
DA A Decimal adjust Accumulator. Used in BCD arithme- 1 fOSC/12
tic
contains 0FEH and borrow (carry) flag is 0, executing the instruction SUBB A, #0FFH results in –1 which is
represented in the accumulator by the 2’s complement form of 1. Hence the content of accumulator becomes
FFH (2’s complement form of 1) and the borrow (carry) flag is also set). The carry flag is reset when the result
of a subtraction is +ve (‘A’ is greater than (<loc> + Carry)). The SUBB A, <loc> instruction is accumulator
specific, meaning accumulator is an implicit operand for this instruction.
Example 1
The accumulator register contains 80H and B register contains 8FH. Add accumulator content with B register.
Explain the output of the summation and the status of the carry flag after the addition operation.
Adding 80H with 8FH results in 10FH. Since 10FH requires more than 8 bits to represent, the accumulator
holds only the least significant 8 bits of the result (Here 0FH). The carry bit CY present in the Program Status
Word (PSW) register is set to indicate that the output of the addition operation resulted in a number greater
than FFH. Figure 6.11 illustrates the addition operation and the involvement of Carry bit (CY).
D7 D6 D5 D4 D3 D2 D1 D0
A CY 1 0 0 0 0 0 0 0 80H
ADD A,B +
B 1 1 0 0 0 1 1 1 1 8FH
CY 0 0 0 0 1 1 1 1 0FH
A
=1
Example 2
Register R0 contains 0BH. Add the contents of register R0 with the sum obtained in the previous example
using ADDC instruction. Explain the output of the summation and the status of the carry flag after the
addition operation.
The instruction ADDC A, R0 adds accumulator content with contents of register R0 and the carry flag
(CY). Before executing this instruction, the accumulator contains the sum of previous addition operation,
which is 0FH and the carry flag (CY) is in the set state. Register R0 contains 0BH. Executing the instruction
ADDC A, R0 adds 0FH with 0BH and 1. The resulting output is 1BH which is less than FFH. This resets the
carry flag. Accumulator register holds the sum. It should be noted that each addition operation sets or resets
the carry flag based on the result of addition, regardless of the previous condition of the carry flag. Figure
6.12 explains the ADDC operation.
D7 D6 D5 D4 D3 D2 D1 D0
A 0 0 0 0 1 1 1 1 0FH
CY
ADDC A,R0 + =1
R0 0 0 0 0 1 0 1 1 0BH
1 1 1 1
A CY 0 0 0 1 1 0 1 1 1BH
=0
Example 3
The accumulator register contains 8FH and B register contains 0FH. The borrow Flag (CY) is in the set state.
Subtract the contents of accumulator with borrow and B register. Explain what is the output of the subtraction
and the status of the borrow flag after the subtraction operation.
8051 supports only SUBB instruction for subtraction. The SUBB instruction subtracts the borrow flag
(CY) and the number to be subtracted, from accumulator. The subtraction operation is implemented by
184 Introduc on to Embedded Systems
adding accumulator with the 2’s complement of borrow flag and the 2’s complement of the number to be
subtracted.
The 2’s complement of borrow flag (CY=1) results in FFH. The 2’s complement of B register content
(0FH) yields F1H. The accumulator content is added with the 2’s complement of borrow flag (8FH + FFH)
and the result (8E) is added with the 2’s complement of B register (8EH + F1H). This results in 7FH with the
borrow flag (CY) in the set state. The final step in the subtraction operation is complementing the borrow flag
(Carry Flag). A value of 1 for borrow flag after complementing indicates that the result is negative whereas
a value of 0 indicates that the result of subtraction is positive. If the result is negative, accumulator contains
the 2’s complement of the negative number. Figure 6.13 illustrates subtract with borrow operation and the
involvement of borrow bit (CY).
CY 0 0 0 0 0 0 0 1 01H
=1
B 0 0 0 0 1 1 1 1 0FH
2's Complement of B
= 1's Complement of 1 1 1 1 0 0 0 1 F1H
B+1
A 1 0 0 0 1 1 1 1 8FH
+
SUBB B
CY 1 1 1 1 1 1 1 1 FFH
1 1 1 1 1 1 1 1
A CY 1 0 0 0 1 1 1 0 8EH
=1
+
1 1 1 1 0 0 0 1 F1H
1
A CY 0 1 1 1 1 1 1 1 7FH
=1
Complement
CY 0 1 1 1 1 1 1 1 A=
=0 7FH
Example 1
Convert the binary number FFH present in the accumulator to unpacked BCD and store the resultant 3 digit
BCD number in consecutive memory locations starting from 20H
BCD numbers are numbers with base 10. BCD numbers use digits 0 to 9 for representing a number. They
are normally used for representing a meaningful decimal number. Each digit in the BCD number represents
the consecutive powers of 10. For example, the BCD number 255 is interpreted as 2 × 102 + 5 × 101 + 5
× 100. Binary numbers follow the base 2 numbering system. Regardless of the numbering system, digital
systems internally represent the numbers in binary format. For an 8bit word length controller/processor,
the maximum number value that can be represented in binary is 11111111, which is equivalent to FFH in
hexadecimal and 255 in decimal. For retrieving the digits of a decimal number, the number is divided with 10
repeatedly until the quotient is zero. The remainder from each division gives the successive LSB digits of the
corresponding BCD number. For example, for 255, the first division gives the quotient as 25 and remainder
as 5, hence the LSB digit for the BCD number is 5. The next division of the quotient by 10 gives 2 as quotient
and 5 as remainder. The remainder 5 forms the next LSB digit for the BCD. Performing one more division
with 10 on the quotient (2) gives 0 as quotient and 2 as remainder. This remainder acts as the MSB digit
for the corresponding BCD number. In fact for an 8bit binary number, only two divisions are required. The
remainders from the two consecutive divisions form the consecutive LSB digits for the BCD number and the
quotient after the second division becomes the MSB digit for the corresponding BCD number. The following
assembly code implements this principle. The DIVAB instruction performs the successive divisions with 10.
;####################################################################
;binary_unpacked_bcd.src
;Firmware for converting binary number to unpacked BCD
;The binary number to be converted is present in the accumulator
;The BCD corresponding to the converted binary is stored in-
;memory location starting from 20H with the least significant digit
;of BCD stored in memory location 20H
;Written & Compiled for A51 Assembler
;Written by Shibu K V. Copyright (C) 2008
;####################################################################
ORG 0000H ; Reset vector
186 Introduc on to Embedded Systems
Example 2
Convert the packed BCD number ‘98’ stored in the accumulator to corresponding binary number and store
the result in accumulator.
The packed BCD number is stored using a single byte. The higher 4 bits of the byte store the most significant
digit of the packed BCD number and the lower 4 bits store the least significant digit of the BCD number.
Hence the accumulator representation of packed BCD is 10001000b (98H). The binary value corresponding
to this packed BCD is the binary representation of 9 × 101 + 8 × 100 = 9 × 10 + 8 × 1 = 90 + 8.
;####################################################################
;packedbcd_to_binary.src
;Firmware for converting packed BCD to binary
;The BCD number to be converted is present in accumulator
;The resultant binary number is held in Accumulator register
;Written & assembled for A51 Assembler
;Written by Shibu K V. Copyright (C) 2008
;####################################################################
ORG 0000H ; Reset vector
JMP 0100H ; Jump to start of main program
ORG 0003H ; External Interrupt 0 ISR location
RETI ; Simply return. Do nothing
Programming the 8051 Microcontroller 187
MOV 00H,#80H ; Load Data memory location 00H (R0) with 80H
MOV A,#0FFH ; Load Accumulator with FFH
MOV DPTR,#00FFH ; Load DPTR with 00FFH
INC 00H ; Increment the contents of Data memory-
; location 00H. It becomes 81H
INC A ; Increment the content of Accumulator. It-
; becomes 00H
INC DPTR ; Increment the content of DPTR Register. It-
; becomes 0100H
The decrement instruction (DEC) decrements the content of the location pointed by the operand. The DEC
A instruction is an accumulator specific instruction which decrements the content of accumulator. Executing
the instruction DEC A will not modify the carry flag. The DEC <loc> instruction decrements the content
188 Introduc on to Embedded Systems
pointed by <loc>. If the content pointed by the operand of DEC instruction is 00H, executing the DEC
instruction simply rolls over it to FFH. None of the status bits are modified to indicate this. The following
code snippet illustrates the usage of DEC instruction.
MOV 00H,#80H ; Load Data memory location 00H (R0) with 80H
MOV A,#0FFH ; Load Accumulator with FFH
DEC 00H ; Decrement the contents of Data memory-
; location 00H. It becomes 7FH
DEC A ; Decrement the content of Accumulator. It-
; becomes FEH
There is no specific instruction to decrement the DPTR register content by one in 8051 instruction set.
However decrementing the DPTR can be achieved by using other instructions in the optimal way.
[Link] Decimal Adjust
The DA A instruction adjusts the accumulator content to give a meaningful BCD result in BCD arithmetic.
Binary Coded Decimal (BCD) is a number representation in numeric systems. In the BCD number system,
numerals from 0 to 9 are used for representing a number. Based on the number of BCD digits represented in a
single byte, the BCD numbers are known as packed and unpacked BCDs. In the unpacked BCD representation,
a single BCD digit (0 to 9) is represented by a single byte whereas in the packed BCD representation two
BCD digits (00 to 99) are represented using a single byte. ADD and ADDC instructions used for BCD
addition should follow a DA A instruction to bring the Accumulator content to a meaningful BCD result. It
should be noted that the DA A instruction will not convert a binary number to a BCD. The DA A instruction
always follow the ADD instruction.
Example 1
Write an assembly program to add two packed BCD numbers ‘28’ and ‘12’ stored in consecutive memory
locations starting from data memory address 50H. The result of the addition should be adjusted for BCD.
The Assembly code snippet shown below illustrates the implementation.
;####################################################################
;BCD_addition.src
;Firmware for adding two packed BCD numbers
;The BCD numbers to be added are present in the memlocation 50H & 51H
;The resultant BCD number is held in Accumulator register
;After addition the result is adjusted for BCD
;Written & assembled for A51 Assembler
;Written by Shibu K V. Copyright (C) 2008
;####################################################################
ORG 0000H ; Reset vector
JMP 0100H ; Jump start of main program
ORG 0003H ; External Interrupt 0 ISR location
RETI ; Simply return. Do nothing
ORG 000BH ; Timer 0 Interrupt ISR location
Programming the 8051 Microcontroller 189
If the accumulator is not adjusted for decimal, after the addition, the accumulator content will be 3AH.
Adjusting the accumulator for decimal, after the addition operation, produces a meaningful result, 40H
(Which is the sum of the BCDs 28 and 12).
ANL A,<loc> A=A&<loc> Bitwise logical AND the content of the accu- 1 fOSC/12
mulator with the content of <loc> and stores
the result in Accumulator. <loc> can be B,
R0, R1, …R7, or any SFR, an internal data
memory or an immediate constant or an inter-
nal data memory pointed indirectly by indirect
addressing register R0 or R1 and @
ANL <loc>,A <loc> = A&<loc> Bitwise logical AND the content of the accu- 1 fOSC/12
mulator with the content of <loc> and stores
the result in memory location <loc>. <loc> can
be any SFR or an internal data memory
ANL <loc>,#const <loc> = Bitwise logical AND the content of <loc> with 2 2*(fOSC/12)
<loc>&const an immediate constant and stores the result in
memory location <loc>. <loc> can be any SFR
or an internal data memory
XRL A,<loc> A = A^<loc> Bitwise logical XOR the content of the accu- 1 fOSC/12
mulator with the content of <loc> and stores
the result in Accumulator. <loc> can be B,
R0, R1, …R7, or any SFR, an internal data
memory or an immediate constant or an inter-
nal data memory pointed indirectly by indirect
addressing register R0 or R1 and @
XRL <loc>,A <loc> = A^<loc> Logical XOR the content of the accumulator 1 fOSC/12
with the content of <loc> and stores the result
in memory location <loc>. <loc> can be any
SFR or an internal data memory
XRL <loc>,#const <loc> = <loc> ^ Logical XOR the content of <loc> with an 2 2*(fOSC/12)
const immediate constant and stores the result in
memory location <loc>. <loc> can be any SFR
or an internal data memory
CLR A A = 00H Clear the content of accumulator (Loads 0 to 1 fOSC/12
Accumulator)
CPL A A = ~A Complement the content of the accumulator. 1 fOSC/12
1s are replaced by 0s and 0s by 1s.
RL A A = A<1 Rotate the Accumulator content 1 bit to the 1 fOSC/12
left. The MSB of the accumulator is shifted out
to the LSB position of the accumulator
RLC A A = A<1 Rotate the accumulator content 1 bit to the 1 fOSC/12
left through carry bit. The MSB of the accu-
mulator is shifted out to carry bit and content
of carry bit enters at the LSB position of the
Accumulator
RR A A = A>1 Rotate the accumulator content 1 bit to the 1 fOSC/12
right. The LSB of the Accumulator is shifted
out to the MSB position of the Accumulator
Programming the 8051 Microcontroller 191
The ANL instruction performs bitwise ANDing operation. The operation of ANL instruction is illustrated
below. Suppose the Accumulator contains 80H and if it is ANDed with an immediate constant 80H, the
following actions shown in Fig. 6.14 take place.
Accumulator 1 0 0 0 0 0 0 0 80H
AND operation & & & & & & & &
Constant 1 0 0 0 0 0 0 0 80H
Result in 1 0 0 0 0 0 0 0 80H
accumulator
Fig. 6.14 Illustration of ANL instruction operation
Similarly the ORL instruction performs the bitwise ORing operation. For example, let us assume that the
accumulator contains 01H and it is ORed with an immediate data 80H. The result will be 81H and it is stored
in the accumulator. It is explained in Fig. 6.15.
Accumulator 0 0 0 0 0 0 0 1 01H
‘OR’ operation | | | | | | | |
Constant 1 0 0 0 0 0 0 0 80H
Result in 1 0 0 0 0 0 0 1 81H
accumulator
Fig. 6.15 Illustration of ORL instruction operation
The XRL instruction performs the bitwise XORing operation. The principle of XOR is that if both the
XORing bits are same (both are either 0 or 1) the output bit is 0 and if the XORing bits are complement
to each other the output bit is 1. For example, if the accumulator content is 81H and an XOR operation of
the accumulator with an immediate constant 80H yields 01H as output. The XRL instruction operation is
explained in Fig. 6.16.
192 Introduc on to Embedded Systems
Accumulator 1 0 0 0 0 0 0 1 81H
‘XOR’ operation ^ ^ ^ ^ ^ ^ ^ ^
Constant 1 0 0 0 0 0 0 0 80H
Result in 0 0 0 0 0 0 0 1 01H
accumulator
Fig. 6.16 Illustration of XRL instruction operation
The CLR A and CPL A instructions are accumulator specific instructions. The CLR A instruction clears
the content of accumulator (loads accumulator with 00H), whereas the CPL A instruction complements
the accumulator content (negates the accumulator content). Both CLR A and CPL A instructions are single
machine cycle instructions.
Assuming accumulator contains F0H, Fig. 6.17 illustrates the operation of CLR A and CPL A
instructions.
Accumulator 1 1 1 1 0 0 0 0 F0H
Accumulator 1 1 1 1 0 0 0 0 F0H
‘CPL’ operation
0 0 0 0 1 1 1 1 0FH
Rotate instructions rotates the bits of the Accumulator. All Rotate instructions are Accumulator specific
instructions and Accumulator is the implicit register for rotate operations. Accumulator bits can be either
rotated to left or right.
The RL A instruction rotates the accumulator bits to the left by one position and the MSB of the accumulator
comes in place of the LSB. For example, the accumulator contains 80H, after executing an RL A instruction,
the contents of the accumulator will become 01H. It is illustrated in Fig. 6.18.
The RLC A instruction is similar in operation to RL A instruction except that in RLC A instruction the MSB
of the Accumulator is shifted to the Carry bit (CY) and the content of carry flag at the moment of execution of
the RLC A instruction is shifted to the LSB of Accumulator. The operation of RLC A instruction is illustrated
in Fig. 6.19. Assuming the Accumulator contains 80H and the carry bit is in the cleared state (0). Executing
the RLC A instruction sets the carry flag and modifies the Accumulator content to 00H.
One more execution of the RLC A instruction will change the accumulator content to 01H and resets the
carry flag.
Programming the 8051 Microcontroller 193
Accumulator 1 0 0 0 0 0 0 0 80H
Result in Accumulator
after RLA instruction 0 0 0 0 0 0 0 1 01H
execution
Fig. 6.18 Illustration of RL A instruction operation
CY
0
Accumulator 1 0 0 0 0 0 0 0 80H
CY
1
Result in Accumulator
after RLC A instruction 0 0 0 0 0 0 0 0 00H
execution
Fig. 6.19 Illustration of RLC A instruction operation
The RR A instruction rotates the accumulator bits to the right by one position and the LSB of the
accumulator comes in place of the MSB. For example if the accumulator contains 01H, after executing the
RR A instruction, the contents of the accumulator will become 80H (Fig. 6.20).
Accumulator 0 0 0 0 0 0 0 1 01H
Result in Accumulator
after RR A instruction 1 0 0 0 0 0 0 0 80H
execution
Fig. 6.20 Illustration of RR A instruction operation
The RRC A instruction is similar to RLC A instruction except that in RRC A instruction the LSB of the
accumulator is shifted to the carry bit (CY) and the content of carry flag at the moment of execution of
RRC A instruction is shifted to the MSB of the accumulator. Assuming that the accumulator contains 01H
and the carry bit is in the set state (1). Executing the RRC A instruction sets the carry flag and modifies the
accumulator content to 80H (Fig. 6.21).
The SWAP A instruction interchanges the lower and higher order nibbles of the Accumulator. Assuming
the accumulator contains F5H, executing SWAP A instruction modifies the accumulator content to 5FH.
The SWAP A instruction shown in Fig. 6.22 is very helpful in BCD to binary conversion.
194 Introduc on to Embedded Systems
CY
1
Accumulator 0 0 0 0 0 0 0 1 01H
Accumulator 1 1 1 1 0 1 0 1 F5H
Result in Accumulator
after SWAP A instruction 0 1 0 1 1 1 1 1 5FH
execution
Higher Nibble Lower Nibble
Fig. 6.22 Illustration of SWAP A instruction operation
The Boolean instructions are very useful in bit manipulation operation for bit-addressable SFRs.
Manipulation of port status registers (P0 to P3) is a typical example. The usage of Boolean instructions is
illustrated below.
MOV C, P2.0 ; Load carry flag with Port pin 2.0’s status.
MOV P2.0, C ; Load Port 2.0 latch with content of carry flag
SETB C ; Set the carry flag
CLR C ; Clear carry flag
ANL C, P2.0 ; Logical AND P2.0 Latch bit with Carry flag and
; load Carry bit with the result
†
The actual state of the bit remains unchanged. If bit 0H is in the cleared state, executing the instruction ANL C, /0H will not change
the state of bit 0H to set.
††
The actual state of the Bit remains unchanged. If bit 0H is in the cleared state, executing the instruction ORL C, /0H will not change
the state of bit 0H to set.
196 Introduc on to Embedded Systems
The JC rel and JNC rel instructions has got special significance in comparison operation. The 8051
supports only one compare instruction, CJNE, which checks only for the equality of the register/data pair
compared. There is no built in instruction for checking above or below condition (Compare and jump if above
and Compare and jump if below). These conditions can be checked by using the JC and JNC instructions in
combination with the CJNE instruction. The CJNE instruction followed by the JNC instruction implements
the Compare and jump if above condition and the CJNE instruction followed by the JC instruction implements
the Compare and jump if below condition. The following code snippet illustrates the same. Assume that the
accumulator content is 50H and it is compared against 51H.
Jump Instruc ons The instruction JMP represents three type of jumps. They are SJMP, LJMP and AJMP
and they are used in appropriate contexts. If the programmer is not interested in analysing the contexts, he/
she can simply use the JMP instruction.
SJMP instruction stands for Short Jump. The destination address is given as an offset to the current address
held by the Program Counter (PC). The SJMP instruction is a two byte instruction consisting of the opcode
for SJMP and the relative offset address which is one byte. The jump distance for the SJMP instruction is
limited to the range of –128 to +127 bytes relative to the instruction following the jump.
LJMP instruction stands for Long Jump. The destination address for LJMP is given as the real physical
address of the code memory location to which the jump is intended. The LJMP instruction is a three byte
instruction consisting of the opcode for LJMP and the two byte physical address of the location to which the
program flow is to be diverted. The jump location can be anywhere within the 64K program memory.
The third type of jump instruction AJMP stands for Absolute Jump. The AJMP instruction encodes the
destination address as 11-bit constant. The instruction is two byte long consisting of the opcode, which itself
contains higher 3 bits of the 11-bit constant. Rest 8 bits of the destination address is held by the second byte
of the instruction. When an AJMP instruction is executed, these 11 bits are substituted for the lower 11 bits in
the Program Counter (PC). The higher 5 bits of the Program Counter remains the same. Hence the destination
will be within the same 2K block of the current instruction. In general AJMP is used for jumping within a
memory block.
The programmer need not bother about handling these three jumps, what he/she can do is simply provide
the destination address as label or a 16 bit constant. The assembler converts the JMP instruction to any of the
three jump instruction depending on the context.
Case jumps for diverting program execution flow dynamically can be implemented using JMP
@A+DPTR instruction. The destination address is computed during run time as the sum of the DPTR register
content and accumulator. Normally DPTR is loaded with the address of a jump table which holds jump
instructions to the memory location corresponding to a ‘case’. For an n-way branching requirement, the
accumulator is loaded with 0 through n–1. The following code snippet illustrates the implementation of a
switch case statement using JMP @ A+DPTR.
//switch case in ‘Embedded C’
switch (var)
{
Case0:
// Action…
Case1:
// Action…
Case2:
// Action…
}
Corresponding switch case implementation in 8051 Assembly using JMP @ A+DPTR
var EQU 50H ;Assume memory location 50H is holding variable ‘var’
MOV DPTR, #CASE_TABLE ;Load the base address of jump table
MOV A,var; Load Accumulator with the case index
RL A ; Convert the case table index to corresponding offset
JMP @A+DPTR ; Jump to the case condition
;###################################################################
198 Introduc on to Embedded Systems
CASE_TABLE:
AJMP CASE0 ; Jump to location implementing code for CASE0
AJMP CASE1 ; Jump to location implementing code for CASE1
AJMP CASE2 ; Jump to location implementing code for CASE2
CASE0: ; Action corresponding to case0
CASE1: ; Action corresponding to case1
CASE2: ; Action corresponding to case2
Locations CASE0, CASE1 and CASE2 should contain the action item to be implemented for each case
statement.
The subroutine calling instruction CALL is of two types; namely LCALL and ACALL. LCALL is a three
byte instruction of which the first byte represents the opcode and the second and third bytes hold the physical
address of the program memory where the subroutine, which is to be executed, is located. For LCALL the
subroutine can be anywhere within the 64K byte of program memory. ACALL is similar to AJMP. ACALL is
a two byte instruction of which the first byte holds the opcode as well as the higher 3 bits of the 11-bit address
of the location where the subroutine is located. The second byte holds the remaining 8 bits of the address
location. ACALL is used for calling a subroutine which is within the 2K block of the instruction, calling the
subroutine.
RET instruction performs return from a subroutine call. Executing the RET instruction brings the program
flow back to the instruction following the CALL to the subroutine. RETI performs the return from an interrupt
routine. RETI instruction is similar to the RET instruction in all respect except that the RETI informs the
interrupt system that the interrupt in progress is serviced.
The NOP instruction does not perform anything specific. It simply forces the CPU to wait for one machine
cycle. It is very useful for generating short waits. It simply eats up the processor time. For example, if you
need a short delay between the toggling of a control line/device select line or port pin, simply add some NOP
instructions. The following code snippet illustrates the usage of NOP instruction in programming.
SETB P2.0 ; Pull P2.0 line HIGH
NOP ; Hold P2.0 line HIGH for 2 machine cycles
NOP
CLR P2.0 ; Release P2.0 line
All the conditional branching instructions specify the destination address by relative offset method and
so the jump locations are limited to –128 to +127 bytes from the instruction following the conditional jump
instruction. Even if the user specifies the actual address, the assembler converts it to an offset at the time of
assembling the code.
Unlike the 8085 and 8086 CPU architecture, there is no zero flag for 8051. The instructions JZ and
JNZ instructions implement the zero flag check functionality by checking the accumulator content for zero.
DJNZ instruction is used for setting up loop control and generating delays. CJNE instruction compares two
variables and check whether they are equal. CJNE instruction in combination with the carry flag can be used
for testing the conditions ‘greater than’, ‘greater than or equal’ and ‘less than’. The two bytes in the operand
field of CJNE are taken as unsigned integers. If the first is less than the second, the Carry bit is set (1). If
the first is greater than or equal to the second, the carry bit is cleared. Boolean instructions are also used for
conditional program control transfer. Please refer to the “Program Control Transfer based on bit status” under
the Boolean Instructions section for more details.
Please refer to the Online Learning Centre for the Complete 8051 Instruction Set reference
Summary
� An Instruction consists of two parts namely; Opcode and Operand(s). The Opcode tells the processor
what to do on executing an instruction. Operand(s) is the parameter(s) required by opcode LO1
to complete the action
� Addressing Mode refers to the way in which an operand is specified in an instruction along with the
opcode LO1
� Direct Addressing, Indirect Addressing, Register Addressing, Immediate Addressing and Indexed
Addressing are the addressing modes supported by 8051 LO1
� Instructions in which the register operand is implicitly specified by some bits of the opcode referred
as register Instructions, whereas instructions which implicitly work on certain specific
registers are known as Register Specific Instructions LO2
� The instruction set of 8051 family microcontroller is broadly classified into five categories, namely;
Data transfer instructions, Arithmetic instructions, Logical instructions, Boolean
LO2
instructions and Program Control Transfer instructions
� ‘Stack’ is an internal memory for storing variables temporarily. The instruction PUSH saves data
to the stack and the instruction POP retrieves the pushed data from the stack memory LO2
� Data transfer instructions transfer data between a source and destination. The data transfer can be
between register or memory (internal or external). Arithmetic instructions perform arithmetical
operations like addition, subtraction, multiplication, division, increment and decrement LO2
200 Introduc on to Embedded Systems
Keywords
Opcode: The command that tells the processor what to do on execu ng an instruc on [LO 1]
Operand(s): The parameter(s) required by an opcode to complete the ac on [LO 1]
Addressing Mode: The way in which an operand is specified in an instruc on along with opcode [LO 1]
Direct Addressing: The addressing mode in which the operand is specified as direct memory address [LO 1]
Indirect Addressing: The addressing mode in which the operand is specified as memory address indirectly,
using indirect addressing registers [LO 1]
Register Addressing: Addressing mode in which the operand is specified as Registers [LO 1]
Register Instruc ons: Instruc ons in which the register operand is implicitly specified by some bits of the
opcode [LO 1]
Register Specific Instruc ons: Instruc ons which implicitly work on certain specific registers only [LO 1]
Immediate Addressing: Addressing mode in which the operand is specified as immediate constants [LO 1]
Indexed Addressing: Addressing mode in which the operand is specified through an index register [LO 1]
Data Transfer Instruc ons: Instruc ons for transferring data between a source and des na on [LO 2]
Stack: Internal memory for storing variables temporarily [LO 2]
PUSH: Instruc on for pushing data to the stack memory [LO 2]
POP: Instruc on for retrieving the pushed data bytes from stack memory [LO 2]
Programming the 8051 Microcontroller 201
Data Exchange Instruc ons: Instruc ons for exchanging data between a memory loca on and the accumulator
register in 8051 architecture [LO 2]
External Data Memory Instruc on: Instruc on for transferring data between external memory and processor
[LO 2]
Arithme c Instruc ons: Instruc ons for performing basic arithme c opera ons including addi on, subtrac on,
mul plica on, division, increment and decrement [LO 2]
2’s Complement: A binary data representa on used in subtrac on opera on [LO 2]
Binary Coded Decimal (BCD): Numbers with base 10. Represented using digits 0 to 9 [LO 2]
Unpacked BCD: A single BCD digit (0 to 9) represented in a single byte [LO 2]
Packed BCD: Two BCD digits (00 to 99) represented using a single byte [LO 2]
Decimal Adjust Accumulator (DAA): An instruc on for adjus ng the accumulator content to give a meaningful
BCD, a er BCD arithme c [LO 2]
Logical Instruc ons: Instruc ons for performing logical opera ons such as ‘ORing’, ‘ANDing’, ‘XORing’,
complemen ng, clearing, bit rota on and swapping nibbles of a byte, etc [LO 2]
Boolean Instruc ons: Instruc ons for performing various opera ons like bit transfer, bit manipula on, logical
opera ons on bits, program control transfer based on bit state, etc [LO 2]
7. Register R0 contains 50H and Accumulator contains 01H. What will be the contents of R0 and A after
executing the instruction MOV A,R0
(a) R0 = 01H; A = 01H (b) R0 = 50H; A = 01H
(c) R0 = 01H; A = 50H (d) R0 = 50H; A = 50H
(e) None of these
8. Data memory location 00H contains F0H and Stack Pointer (SP) contains 07H. What will be the contents of
memory location 00H and SP after executing the instruction PUSH 00H
(a) Data memory location 00H = 07H; SP = 07H
(b) Data memory location 00H = F0H; SP = 07H
(c) Data memory location 00H = F0H; SP = 08H
(d) Data memory location 00H = F0H; SP = 06H
(e) None of these
9. Data memory location 00H contains F0H and Stack Pointer (SP) contains 08H. The memory location 08H
contains 0FH. What will be the contents of memory location 00H, 08H and SP after executing the instruction
POP 00H
(a) Memory location 00H = 0FH; Memory location 08H = F0H; SP = 08H
(b) Memory location 00H = F0H; Memory location 08H = F0H; SP = 07H
(c) Memory location 00H = 0FH; Memory location 08H = 0FH; SP = 07H
(d) Memory location 00H = 0FH; Memory location 08H = 0FH; SP = 09H
(e) None of these
10. Data memory location 0FH contains 00H and the accumulator contains FFH. What will be the contents of
data memory location 0FH and the accumulator after executing the instruction XCH A, 0FH
(a) Memory location 0FH = 00H; Accumulator = FFH
(b) Memory location 0FH = 00H; Accumulator = 00H
(c) Memory location 0FH = FFH; Accumulator = 00H
(d) Memory location 0FH = FFH; Accumulator = FFH
(e) None of these
11. Data memory location 0FH contains A5H, Accumulator contains 5AH and register R0 contains 0FH.
What will be the contents of data memory location 0FH, Register R0 and accumulator after executing the
instruction XCHD A, @R0
(a) Memory location 0FH = A5H; Accumulator = 5AH; R0 = 0FH
(b) Memory location 0FH = 55H; Accumulator = AAH; R0 = 0FH
(c) Memory location 0FH = AAH; Accumulator = 55H; R0 = 0FH
(d) Memory location 0FH = A5H; Accumulator = 55H; R0 = AAH
(e) None of these
12. Register DPTR holds 2050H. Explain the result of executing the instruction MOVX @DPTR, A
(a) P2 SFR = 20H; P1 SFR = 50H during the first machine cycle; RD\ signal is asserted once
(b) P2 SFR = 50H; P1 SFR = 20H during the first machine cycle; RD\ signal is asserted once
(c) P2 SFR = 20H; P1 SFR = 50H during the first machine cycle; WR\ signal is asserted once
(d) P2 SFR = 50H; P1 SFR = 20H during the first machine cycle; WR\ signal is asserted once
(e) None of these
13. The Program Strobe Enable (PSEN) signal is asserted during program fetching if
(a) The program memory is external to the controller
(b) The Program memory is internal to the controller
(c) The Program memory is either internal or external to the controller
14. How many program fetches occur per machine cycle?
(a) 1 (b) 2 (c) 3 (d) 4
Programming the 8051 Microcontroller 203
15. How many ‘program memory fetches’ are skipped during the execution of MOVX instruction?
(a) 1 (b) 2 (c) 3 (d) 4
16. The Address Latch Enable (ALE) signal is asserted how many times in a machine cycle?
(a) 1 (b) 2 (c) 3 (d) 4
17. How many times the ALE signal is skipped during the execution of a MOVX instruction?
(a) 1 (b) 2 (c) 3 (d) 4
18. Which of the following is true about MOVC instruction
(a) Used for reading from Program memory (b) Uses Indexed Addressing technique
(c) Both a & b (d) None of these
19. The content of Accumulator is FFH and the Carry Flag is in the cleared state. What will be the contents of
Accumulator and carry flag after executing the instruction ADD A,#1
(a) Accumulator = 01H; Carry flag = 1 (b) Accumulator = 01H; Carry flag = 0
(c) Accumulator = 00H; Carry flag = 0 (d) Accumulator = 00H; Carry flag = 1
20. Accumulator register contains 0FH and the Carry flag CY is in the set state. What will be the state of Carry
flag after executing the instruction ADD A,#0F0H
(a) 1 (b) 0 (c) Indeterminate
21. The content of the accumulator is FFH and the Carry flag is in the cleared state. What will be the contents of
the accumulator and carry flag after executing the instruction ADDC A,#1
(a) Accumulator = 01H; Carry flag = 1 (b) Accumulator = 01H; Carry flag = 0
(c) Accumulator = 00H; Carry flag = 0 (d) Accumulator = 00H; Carry flag = 1
22. Accumulator register contains 0FH and the Carry flag CY is in the cleared state. What will be the contents
of Carry flag and Accumulator after executing the instruction SUBB A,#0F0H
(a) Accumulator = E1H; Carry flag = 1 (b) Accumulator = E1H; Carry flag = 0
(c) Accumulator = 1FH; Carry flag = 0 (d) Accumulator = 1FH; Carry flag = 1
23. Accumulator register contains F0H and the Carry flag CY is in the cleared state. What will be the contents
of the Carry flag and the accumulator after executing the instruction SUBB A,#0FH
(a) Accumulator = E1H; Carry flag = 1 (b) Accumulator = E1H; Carry flag = 0
(c) Accumulator = 1FH; Carry flag = 0 (d) Accumulator = 1FH; Carry flag = 1
24. Accumulator register contains 0FFH and the B register contains 02H. What will be the contents of the
Accumulator and B register after executing the instruction MUL AB
(a) Accumulator = 0FEH; B = 01H (b) Accumulator = 00H; B = 0FEH
(c) Accumulator = 0FEH; B = 00H (d) Accumulator = 01H; B = 0FEH
25. Accumulator register contains 0FFH, B register contains 02H and the Carry flag is in the cleared state. What
will be the contents of the Carry flag and Overflow flag after executing the instruction MUL AB
(a) Carry flag = 1; Overflow flag = 0
(b) Carry flag = 0; Overflow flag = Remains same as the previous value
(c) Carry flag = 1; Overflow flag = 1
(d) Carry flag = 0; Overflow flag = 1
26. Accumulator register contains 0FFH, B register contains 02H and the Overflow flag is in the cleared state.
What will be the contents of the Carry flag and Overflow flag after executing the instruction MUL AB
(a) Carry flag = remains same as the previous value; Overflow flag = 0
(b) Carry flag = remains same as the previous value; Overflow flag = 1
(c) Carry flag = 1; Overflow flag = 1
(d) Carry flag = 0; Overflow flag = 1
27. Accumulator contains 0FFH and the B Register contains 02H. What will be the contents of the accumulator
and B register after executing the instruction DIV AB
204 Introduc on to Embedded Systems
(a) Accumulator = 7FH; Carry flag = 0 (b) Accumulator = 7FH; Carry flag = 1
(c) Accumulator = 00H; Carry flag = 0 (d) Accumulator = 00H; Carry flag = 1
39. What changes will happen on executing the instruction CLR 00H
(a) The code memory location 00H becomes 00H
(b) The data memory location 00H becomes 00H
(c) The external data memory location 00H becomes 00H
(d) The LS Bit of the data held by data memory location 20H becomes 0
40. Accumulator register contains 0FH and carry flag is in the set state. What will be the contents of the
Accumulator and carry flag after executing the instruction CPL A
(a) Accumulator = 0FH; Carry flag = 0 (b) Accumulator = 0FH; Carry flag = 1
(c) Accumulator = F0H; Carry flag = 0 (d) Accumulator = F0H; Carry flag = 1
41. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of Accumulator
and carry flag after executing the instruction RL A
(a) Accumulator = 02H; Carry flag = 0 (b) Accumulator = 02H; Carry flag = 1
(c) Accumulator = 80H; Carry flag = 0 (d) Accumulator = 80H; Carry flag = 1
42. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of Accumulator
and carry flag after executing the instruction RLC A
(a) Accumulator = 02H; Carry flag = 0 (b) Accumulator = 02H; Carry flag = 1
(c) Accumulator = 03H; Carry flag = 0 (d) Accumulator = 03H; Carry flag = 1
43. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of the
accumulator and carry flag after executing the instruction RR A
(a) Accumulator = 02H; Carry flag = 0 (b) Accumulator = 02H; Carry flag = 1
(c) Accumulator = 80H; Carry flag = 0 (d) Accumulator = 80H; Carry flag = 1
44. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of the
accumulator and carry flag after executing the instruction RRC A
(a) Accumulator = 80H; Carry flag = 0 (b) Accumulator = 80H; Carry flag = 1
(c) Accumulator = 03H; Carry flag = 0 (d) Accumulator = 03H; Carry flag = 1
45. Accumulator register contains 5FH. What will be the content of the accumulator after executing the instruction
SWAP A
(a) 00H (b) F5H (c) 5FH (d) 00H
46. What changes will happen on executing the instruction CPL 00H
(a) The code memory location 00H becomes 00H
(b) The data memory location 00H becomes 00H
(c) The external data memory location 00H becomes 00H
(d) The LS Bit of the data held by data memory location 20H is complemented
47. The carry bit is in the set state and the port status bit P1.0 is in the cleared state. What will be the values of
Carry bit and P1.0 after executing the instruction ANL C, /P1.0
(a) Carry flag = 0; P1.0 = 0 (b) Carry flag = 0; P1.0 = 1
(c) Carry flag = 1; P1.0 = 0 (d) Carry flag = 1; P1.0 = 1
48. The Carry bit is in the cleared state and the Port status bit P1.0 is in the cleared state. What will be the values
of Carry bit and P1.0 after executing the instruction ORL C, /P1.0
(a) Carry flag = 0; P1.0 = 0 (b) Carry flag = 0; P1.0 = 1
(c) Carry Flag = 1; P1.0 = 0 (d) Carry flag = 1; P1.0 = 1
49. Which of the following Jump instruction is the optimal instruction if the offset (relative displacement of
jump location from the current instruction location) of the jump location is greater than 127 and less than
–128 and is within the same 2K block of the current instruction
206 Introduc on to Embedded Systems
12. Explain the difference between ADD, ADDC and DAA instructions. Explain the significance of DAA
instruction? [LO 1, LO 2]
13. Explain the instruction for division operation. How is a divide by zero condition handled in the 8051
architecture? [LO 1, LO 2]
14. Explain the different logical operations supported by 8051 and the corresponding instruction in detail.
[LO 1, LO 2]
15. Explain the different Boolean instructions supported by 8051. [LO 1, LO 2]
16. Explain the different unconditional program control transfer instructions supported by 8051.[LO 1, LO 2]
17. Explain the different conditional program control transfer instructions supported by 8051. [LO 1, LO 2]
18. Explain the implementation of switch() case statement using jump tables. [LO 1, LO 2]
19. The 8051 status register doesn’t contain a zero flag. Explain how the 8051 architecture implements the Jump
on zero and Jump on non-zero conditions. [LO 1, LO 2]
20. Explain the difference between LCALL and ACALL for subroutine invocation? Which one is faster in
execution and why? [LO 1, LO 2]
21. Explain the difference between RET and RETI instructions. [LO 1, LO 2]
22. Explain the different types of jumps supported by 8051 architecture. Which one is faster in execution and
why? [LO 1, LO 2]
Lab Assignments
1. A lookup table with 6 bytes is stored in code memory location starting from 8000H. Write a small 8051
assembly program to read the lookup table
2. Implement a BCD counter to count from 00 to 99 with a delay of 1 second between the successive counts.
Display the count using two 7-Segment LED displays in multiplexed configuration
3. Optimise the following piece of code (Rewrite the code with instructions/operations giving the same
functionality) for memory size and performance
ORG 0000H
JB P1.0, PORT_SET
MOV A, #50H
LJMP SKIP
PORT_SET: CLR P1.0
SKIP: LCALL DELAY
ORG 0050H
DELAY: NOP
NOP
RET
4. Write a 8051 assembly language program using timer interrupt for generating a 50 Hz square wave at the
port pin P1.0. The oscillator frequency applied to the microcontroller is 12.00MHz
5. Write a 8051 assembly language program for generating a 5 kHz square wave at the port pin P1.0. The
oscillator frequency applied to the microcontroller is 12.00 MHz.
208 Introduc on to Embedded Systems
6. Write a 8051 Assembly language program to generate the Fibonacci series of a given number. Assume the
number whose Fibonacci number is to be calculated is received from the hyper terminal application
running on a PC to which the microcontroller is interfaced. Upon receiving the number from the hyper
terminal application, the Fibonacci series for the number is generated and it is sent to the hyper terminal
application running on the PC. The program also inserts the character ‘,’ to separate the two consecutive
numbers in the series while sending it. The numbers are sent with their corresponding ASCII value (e.g.
0 is sent as 30H). The serial communication parameter settings for both hyper terminal application and
microcontroller program are: baudrate = 9600, 1 start bit, 8 data bits, 1 stop bit, No parity. Use a crystal
resonator with frequency 11.0592MHz for designing the microcontroller hardware.
7. Write a 8051 assembly language program to find the largest number from an array of 10 numbers. The
array is located in the data memory and the start address of the array is 20H.
8. Write a 8051 assembly language program to generate a PWM signal of ON time 100 microseconds and
duty cycle of 39.06%, at port pin P1.0
9. Explain with code snippet how the 8051 timer can be used for the measurement of the width of an
unknown pulse
10. It is required by an experimental setup to count the number of pulses arrived during a time period of 50
milliseconds. Explain with code snippets how it can be implemented using the 8051 timers.