Module-2 Complete Notes
Module-2 Complete Notes
EX:
MOV A, #25H ;load 25H into A
MOV R4, #62 ;load 62 into R4
MOV B, #40H ;load 40H into B
MOV DPTR, #4521H ;DPTR=4512H
MOV DPL, #21H ;This is the same
MOV DPH, #45H ;as above
Movement of data between Rr (r=0,1,….,7) registers is not allowed. i.e., MOV R4,R7 is invalid.
The SFR (Special Function Register) can be accessed by their names or by their addresses
The register bank locations are accessed by the register names as well as by their RAM addresses.
Internal RAM and External RAM can be accessed using this register indirect addressing mode.
The advantage of giving an address using a register is that we can increment the address in a
loop, by simply incrementing the register, and hence access a series of locations.
Internal RAM is accessed by 8-bit addresses given by R0 Or R1 and external RAM can be accessed
using the following instructions.
5. Indexed Addressing Mode:
This mode is used to access data from the Code memory (Internal ROM or External ROM).
In this addressing mode, address is indirectly specified as a “SUM” of (A and DPTR) or (A and
PC).
This is very useful because ROM contains permanent data which is stored in the form of Look Up
tables. To access a Look Up table, address is given as a SUM or two registers, where one acts as the
base and the other acts as the index within the table.
A "C" is present in such instructions, to indicate Code Memory.
Figure below shows the instructions used in accessing external RAM and ROM memory
Example Program:
Write a program to copy the value 45H into RAM memory locations 40H to 41H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop
Solution:
(a)
MOV A,#45H ;load A with value 45H
MOV 40H,A ;copy A to RAM location 40H
MOV 41H,A ;copy A to RAM location 41H
(b)
MOV A,#45H ;load A with value 45H
MOV R0,#40H ;load the pointer. R0=40H
MOV @R0,A ;copy A to RAM R0 points to
INC R0 ;increment pointer. Now R0=41h
MOV @R0,A ;copy A to RAM R0 points to
(c)
MOV A,#45H ;A=45H
MOV R0,#40H ;load pointer.R0=40H,
MOV R2,#02 ;load counter, R2=3
AGAIN: MOV @R0,A ;copy 55 to RAM R0 points to
INC R0 ;increment R0 pointer
DJNZ R2,AGAIN ;loop until counter = zero
Data Transfer Instructions of 8051:
MOV
MOVX
MOVC
Exchange
Exchange Digit
PUSH
POP
1. MOV :
Syntax: MOV DST,SRC
1. MOV A, #n
Example:
MOV A, #25H ;Register ‘A’ gets the 25H Immediate data.
2. MOV A, Rr |
Example:
MOV A, R0 ; Register ‘A’ gets the value of a RAM register.
;The value remains in the RAM register and is also copied into A ;register.
3. MOV A, addr |
Example:
MOV A, 25H ; Register ‘A’ gets the contents of the memory location whose
;address is 25H.
4. MOV A, @Rp |
Example:
MOV A, @R0
Operation:
Register ‘A’ gets the contents of the location pointed by the register R0.
If R0 = 20H and Location 20H contains value 35H, then 35H will be copied into to A
register.
4. XCH: (Exchange)
5. XCHD: (Exchange Digit)
Arithmetic Instructions of 8051:
I. Addition of Unsigned numbers:
Instruction - ADD:
The instruction ADD is used to add two operands. The syntax of the ADD instruction is
The destination operand is always in register A while the source operand can be a register, immediate data
or in memory (direct and indirect address).
ADD instruction can change any of the AF, CF, or PF bits of PSW register.
Following are list of possible add instructions.
Example of Addition Program CY =1, since there is a
Show how the flag register is affected by the following instruction. carry out from D7.
MOV A,#0F5H ;A=F5 hex PF =1, because the
ADD A,#0BH ;A=F5+0B=00 number of 1s in
Solution: Accumulator is zero
F5H 1111 0101 (an even number),
+ 0BH + 0000 1011 PF is set to 1.
100H CY=1 0000 0000 AC =1, since there is a
carry from D3 to D4
ADDC and Addition of 16-Bit Numbers:When adding two 16-bit data operands, the propagation of a carry
from lower byte to higher byte is concerned. The instruction ADDC (Add with Carry) is used in such
occasions. Following are the instructions with different addressing modes.
Example for ADDC
Write a program to add two 16-bit numbers. Place the sum in R4 and R5;
R4 should have the lower byte.
Solution:
CLR C ;make CY=0
MOV A, #0E7H ;load the low byte now A=E7H
ADD A, #8DH ;add the low byte
MOV R4, A ;save the low byte sum in R4
MOV A, #3CH ;load the higher byte
ADDC A, #3BH ;add with the carry
MOV R5, A ;save the higher byte
sum
In many microprocessor there are two different instructions for subtraction: SUB and SUBB (subtract with
borrow). In the 8051 we have only SUBB. The 8051 uses adder circuitry to perform the subtraction.
Syntax:
SUBB A, SOURCE ; A = A – Source – CY
This instruction subtracts the source bye and carry flag from the accumulator and puts the result in
accumulator.
The destination operand is always in acummulator register A while the source operand can be a register,
immediate data or in memory (direct and indirect address). SUBB instruction can change any of the AF, CF,
OV and PF bits of PSW register.
To make SUB out of SUBB, we have to make CY=0 prior to the execution of the instruction.
Notice that we use the CY flag for the borrow.
Ex:
1. SUBB A, #25H ; A = A - 25H - CY
Operation:
Performs ‘A’ Register – Immediate data – CY Flag (Carry Flag holds the borrow of the previous
subtraction). Stores the result in A Register.
2. SUBB A, Rn ; A = A - Rn - CY (where, n=0,1,..,7)
3. SUBB A, addr
Example:
SUBB A, @R0 ; A = A - [R0] - CY
Operation:
Performs A Register – Contents of the memory location pointed by the register R0 – CY Flag.
Result is stored in A Register.
Example Program:
MOV A,#45H
CLR C
SUBB A,#23H ; A=45H-23H=22H
SUBB instruction is used for multi-byte numbers and will take care of the borrow of the lower operand.
Example program is given below,
The result in the above example is positive number since the CY=0.
III. Multiplication of Unsigned Numbers:
The 8051 supports byte by byte multiplication only.
The byte are assumed to be unsigned data.
Syntax:
MUL AB ; A x B, 16-bit result in B, A
Example program:
Syntax:
DIV A B ;Divide A by B, A/B,
;Result, A = Quotient, B = Remainder
Example Program:
Syntax:
VI. Decrement:
Syntax:
BCD Number System:
The binary representation of the digits 0 to 9 is called BCD (Binary Coded Decimal).
Unpacked BCD:
In unpacked BCD, the lower 4 bits of the number represent the BCD number, and the rest of the bits are 0
Ex. 00001001 and 00000101 are unpacked BCD for 9 and 5
Packed BCD:
In packed BCD, a single byte has two BCD number in it, one in the lower 4 bits, and one in the upper 4 bits
Ex. 0101 1001 is packed BCD for 59H.
Digit BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Summary of DA instruction:
After an ADD or ADDC instruction
If the lower nibble (4 bits) is greater than 9, or if AC=1, add 0110 to the lower 4 bits
If the upper nibble is greater than 9, or if CY=1, add 0110 to the upper 4 bits
1. AND:
This instruction will perform a logic AND on the two operands and place the result in the
destination
• The destination is normally the accumulator
• The source operand can be a register, in memory, or immediate data.
This instruction will perform a logic OR on the two operands and place the result in the
destination
• The destination is normally the accumulator
• The source operand can be a register, in memory, or immediate data.
3. XOR:
This instruction will perform a logic XOR on the two operands and place the result in the
destination
• The destination is normally the accumulator
• The source operand can be a register, in memory, or immediate
The XRL instruction can be used to clear the contents of a register by XORing it with
itself.
Show how XRL A, A clears A, assuming that A = 45H.
45H 0100 0101
45H 0100 0101
00H 0000 0000
4. Complement:
CPL A
complements the register A. This is called 1’s complement. Source and destination both will be the
accumulator. Content of A will 1’s complemented and stored back in A itself.
MOV A, #56H
CPL A ;now A=A9H
;0101 0110(56H)
;becomes 1010 1001(A9H)
To get the 2’s complement, all we have to do is to add 1 to the 1’s complement
Rotate Instructions:
RR A ; rotate right A
In rotate right
The 8 bits of the accumulator are rotated right one bit position, and
Bit D0 exits from the LSB and enters into MSB, D7
RL A ; rotate left A
In rotate left
The 8 bits of the accumulator are rotated left one bit position, and
Bit D7 exits from the MSB and enters into LSB, D0
CLR C ;make CY = 0
MOV A,#26H;A = 0010 0110
RRC A ;A = 0001 0011 CY = 0
RRC A ;A = 0000 1001 CY = 1
RRC A ;A = 1000 0100 CY = 1
RLC A ; rotate left A, through carry
Bits are rotated from left to right
They exit the MSB to the carry flag, and the carry flag enters the LSB
Example 2- 14
Write a program that finds the number of 1s in a given byte.
MOV R1,#0
MOV R7,#8 ;count=08
MOV A,#97H
AGAIN: RLC A
JNC NEXT ;check for CY
INC R1 ;if CY=1 add to count
NEXT: DJNZ R7,AGAIN
SWAP A
It swaps the lower nibble and the higher nibble
In other words, the lower 4 bits are put into the higher 4 bits and the higher 4 bits are put into
the lower 4 bits
SWAP works only on the accumulator (A)
Example 2- 15
(a) Find the contents of register A in the following code.
(b) In the absence of a SWAP instruction, how would you exchange the nibbles?
Write a simple program to show the process.
Solution:
(a)
MOV A,#72H ;A = 72H
SWAP A ;A = 27H
(b)
MOV A,#72H ;A = 0111 0010
RL A ;A = 1110 0100
RL A ;A = 1100 1001
RL A ;A = 1001 0011
RL A ;A = 0010 0111
CJNE destination, source, Label
The actions of comparing and jumping are combined into a single instruction called CJNE
(compare and jump if not equal)
The CJNE instruction compares two operands, and jumps if they are not equal.
The destination operand can be in the accumulator or in one of the Rn registers.The source operand
can be in a register, in memory, or immediate data. The operands themselves remain unchanged
after execution.
It changes the CY flag as shown in in below table, to indicate if the destination operand is larger or
smaller.
Write code to determine if register A contains the value 99H. If so make R1 = FFH
otherwise, make Rl = 00H.
Solution:
CJNE A, #99H, NEXT ; if A!=99H, then jump to label NEXT
MOV R1,#0FFH ;Load R1=FFH
NEXT: MOV R1,#00H ; Load R1=00H
CJNE R5,#80H, NOT_EQUAL ; Check R5 = 80H, if not equal jump to label ‘NOT-EQUAL’
….…… ; If R5 = 80H execute immediate next instruction
NOT_EQUAL: JNC DOWN ; Jump if CY=0 to label ‘DOWN’ (if R5 >= 80 then CY=0)
….…. ; R5 < 80
DOWN: .…..
Notice in the CJNE instruction that any Rn register can be compared with an immediate value
There is no need for register A to be involved.
The compare instruction is really a subtraction, except that the operands remain unchanged
Flags are changed according to the execution of the SUBB instruction.
Types of CJNE Instructions are,
Branch instructions:
Repeating a sequence of instructions a certain number of times is called a loop.
Loop action is performed by instruction, DJNZ reg, Label, DJNZ addr, Label
• The register is decremented by one value
• If it is not zero, then it jumps to the target address referred to by the label
• Prior to the start of loop the register is loaded with the count value for the number of
repetitions
• Counter can be R0 – R7 or RAM location
Example for DJNZ reg, Label:
DJNZ R7, Back ; Decrement R7 and if not equal to “0”, go to label “Back”
Example Program:
Write a program to
a. load the accumulator with the value 55H, and
b. complement the ACC 900 times
Determine if R5 contains the value 0. If so, put 00H in it else put FFH.
Example:
JNC Down ; If Carry flag is “0”, jump to label “Down”
Example Program:
Determine if the given number 09H is even or odd. If even, put 00H , else put FFH in R5
register.
6. JNB bit, Label ; Jump if bit is Zero (means not set) to label.
Here bit is, an address of a single bit in bit addressable RAM or a bit of a Special
function registers.
Example:
JNB 30H, NEXT ; if bit at 30H is zero , jump to label “NEXT” (Here
;30H is bit address in bit addressable area of RAM )
JNB P1.0, DOWN ; If P1.0 = “0”, jump to location “DOWN”
Unconditional Jumps:
The unconditional jump is a jump in which control is transferred
unconditionally to the target location.
Unconditional jumps do not test any bit or byte to determine whether the jump
should be taken.
Syntax: LJMP ladd ;Long Jump using the long (full) 16 bit address
3-byte instruction
First byte is the opcode
Second and third bytes represent the 16-bit target address (Any memory
location from 0000 to FFFFH)
Syntax: SJMP radd ; Short Jump to label using the relative address
2-byte instruction
First byte is the opcode
Second byte is the relative target address
00 to FFh (forward +127 and backward -128 bytes from the current PC)
To calculate the target address of a short jump (SJMP, JNC, JZ, DJNZ,
etc.)
The second byte is added to the PC of the instruction immediately below
the jump
If the target address is more than -128 to +127 bytes from the address
below the short jump instruction
The assembler will generate an error stating the jump is out of range
Code above shows the how SJMP works.
Note: Here bit is an address of a single bit in bit addressable area of RAM or bit of a Special function
registers SFR’s.
Examples:
SETB P1.0 ; Set the port bit P1.0=1.
CLR PSW.7 ;MSB bit of PSW register is cleared to zero (same as CLR C)
CPL ACC.1 ;Complement the bit position 1 in Accumulator,
;Suppose if A=02H after CPL ACC.1 , A = 00H
There are several instructions by which the CY flag can be manipulated directly.
MOV C,40H ; Copy the bit B present at location 40H into CY flag
ANL C, 30H ; “CY” ANDed with bit at location “30H” and store the
;result in CY (Here ‘CY = B’ and bit at 30H is ‘A’)
ORL C,50H ; “CY” ORed with bit at “50H” (Here bit C is present at bit
;address 50H)