Module 2
Module 2
Module 2
------------------------------------------------------------------------------------------------------------------
8051 Instruction Set: Addressing Modes, Data Transfer instructions,
Arithmetic
instructions, Logical instructions, Branch instructions, Bit manipulation
instructions.
Simple Assembly language program examples (without loops) to use these
instructions
The 8051 microcontroller has only one data type. It is 8 bits, and the size of each
register is also 8 bits. It is the job of the programmer to break down data larger
than 8 bits (00 to FFH, or 0 to 255 in decimal) to be processed by the CPU. The
data types used by the 8051 can be positive or negative.
DB (define byte)
The DB directive is the most widely used data directive in the assembler. It is used
to define the 8-bit data. When DB is used to define data, the numbers can be in
decimal, binary, hex, or ASCII formats. For decimal, the “D” after the decimal
number is optional, but using “B” (binary) and “H” (hexadecimal) for the others is
required. Regardless of which is used, the assembler will convert the numbers into
hex. To indicate ASCII, simply place the characters in quotation marks („like this‟).
The assembler will assign the ASCII code for the numbers or characters
automatically. The DB directive is the only directive that can be used to define
ASCII strings larger than two characters; therefore, it should be used for all ASCII
data definitions. Following are some DB examples:
Either single or double quotes can be used around ASCII strings. This can be
useful for strings, which contain a single quote such as “O‟Leary”. DB is also used
to allocate memory in byte-sized chunks.
Assembler directives
The following are some more widely used directives of the 8051.
ORG (origin)
The ORG directive is used to indicate the beginning of the address. The number
that comes after ORG can be either in hex or in decimal. If the number is not
EQU (equate)
This is used to define a constant without occupying a memory location. The EQU
directive does not set aside storage for a data item but associates a constant value
with a data label so that when the label appears in the program, its constant value
will be substituted for the label. The following uses EQU for the counter constant
and then the constant is used to load the R3 register.
When executing the instruction “MOV R3, #COUNT”, the register R3 will be loaded
with the value 25 (notice the # sign). Assume that there is a constant (a fixed value)
used in many different places in the program, and the programmer wants to
change its value throughout. By the use of EQU, the programmer can change it
once and the assembler will change all of its occurrences, rather than search the
entire program trying to find every occurrence.
END directive
Another important pseudocode is the END directive. This indicates to the
assembler the end of the source (asm) file. The END directive is the last line of an
8051 program, meaning that in the source code anything after the END directive is
ignored by the assembler. Some assemblers use “. END” (notice the dot) instead of
“END”.
The way in which the data operands are accessed by different instructions is
known as the addressing modes. There are various methods of denoting the data
operands in the instruction. The 8051 microcontroller supports mainly 5
addressing modes. They are
1. Immediate addressing mode
2. Direct Addressing mode
3. Register addressing mode
4. Register Indirect addressing mode
5. Indexed addressing mode
The EQU directive can be used to access immediate data as shown below
Ex:
Count EQU 30h
… …
Mov R1, # Count ; R1 = 30h
The addressing mode in which the data operands are accessed from the internal
RAM location (00 -7FH) and the address of the data operand is given in the
instruction is known as direct addressing mode. The direct addressing mode most
often uses the address range 30h-7Fh of Internal RAM and the SFRs
Ex:
MOV R1, 42H ; Move the contents of RAM location 42h into R1
register
MOV 49H, A ; Move the contents of the accumulator into the RAM
location 49.
ADD A, 56H ; Add the contents of the RAM location 56h to the
accumulator
MOV 0E0h, R0 ; Move the data from the register R0 to the register A
Another major use of direct addressing mode is the data access from stack. Only
direct addressing mode is allowed for pushing (writing) and popping (reading) the
data into/from the stack.
Ex
PUSH 03h ; Push the contents of the address 03h to the stack
memory
PUSH A ; Invalid
The addressing mode in which a register is used as a pointer to the data memory
block is known as Register indirect addressing mode. Only registers R0 and R1 are
used as pointers and they must be preceded by @ sign.
MOV A,@ R0 ;Move the contents of RAM location whose address is in R0 into A
(accumulator)
MOV @ R1 , B ;Move the contents of B into RAM location whose address is
held by R1
This addressing mode is used in accessing the data elements of lookup table
entries located in program ROM space of 8051.
Ex : MOVC A,@ A+DPTR
The 16-bit register DPTR and register A are used to form the address of the data
element stored in on-chip ROM. Here C denotes to code memory .In this instruction
the contents of A are added to the 16-bit DPTR register to form the 16-bit address
of the data operand.
Instruction format
ii. MOV A, @R0 ;Copies the data from memory location pointed by register
R0 to Register A.
iii. MOV 45H, A ;Copies the data from Register A to memory location 45h
iv. MOV @R1, A ;Copies the data from register A to memory address pointed
by Register R1
PUSH 6
[SP]=08[08h]=[06h]=25H //CONTENT OF 08 IS 25H
[SP]=09 [09]=[01]=12H //CONTENT OF 09 IS 12H
PUSH 1 [SP]=0A[0A]=[04]=F3H //CONTENT OF 0A IS F3H
PUSH 4
11. Exchange digit. Exchange the lower order nibble of Accumulator with
lower order nibble of the internal RAM location which is indirectly
addressed by the register.
i. XCHD A,@R1
Arithmetic Instructions
The 8051 can perform addition, subtraction. Multiplication and division operations
on 8 bit numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
i. ADD A, #45H ; Add the contents of Accumulator with the data 45h
ii. ADDC A, #OB4H ; Add the contents of Accumulator with the data B4h
with carry flag
ii. Add the contents of A with register Rn with or without carry.
i. ADD A, R5 ;Add the contents of Accumulator with the data in register R5
ii. ADDC A, R2 ;Add the contents of Accumulator with the data in register
R2 and carry flag.
iii. Add the contents of A with contents of memory with or without carry using
direct and indirect addressing
i. ADD A, 51H ;Add the contents of Accumulator with the data in memory
location 51h
ii. ADDC A, 75H ;Add the contents of Accumulator with the data in memory
location 75h and carry flag.
iii. ADD A, @R1 ; Add the contents of Accumulator with the data in memory
location pointed by register R1
iv. ADDC A, @R0 ; ; Add the contents of Accumulator with the data in
memory location pointed by register R0 and the carry flag.
CY,AC and OV flags will be affected by this operation.
Subtraction
In this group, we have instructions to
i. Subtract the immediate data from the contents of A with carry.
i. SUBB A, #45H ;Subtract the data 45h and the carry flag from the data in
Accumulator
ii. SUBB A, #OB4H
ii. Subtract the data in register Rn from contents of A with carry.
i. SUBB A, R5 ;Subtract the data in register R5 and the carry flag from the
data in Accumulator
Logical OR
ORL destination, source :ORL does a bitwise "OR" operation between source
and destination,leaving the resulting value in destination. The value in
source is not affected. " OR " instruction logically OR the bits of source and
destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source: XRL does a bitwise "EX-OR" operation
between source and destination, leaving the resulting value in
destination. The value in source is not affected. " XRL " instruction
logically EX-OR the bits of source and destination.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7
going to bit 0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit
0 going into the carry bit in the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit
7 going into the carry bit in the PSW, while the carry goes into bit 0.
2. Specifying only one byte reduces the size of the instruction and speeds up
program execution.
1. Short jump range ((i.e.)-128 to 127 from the instruction following the
jump instruction) Instructions that use Relative Jump
Short absolute jump instruction is also a 2 byte instruction. This AJMP instruction
can be used when the jump address is located in the same page of the memory in
the page in which the instruction AJMP is located.
In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte
each. The hexadecimal addresses of the pages are given as follows:-
00 0000 - 07FF
01 0800 - 0FFF
02 1000 - 17FF
3 1800 - 1FFF
.
.
1E F000 - F7FF
1F F800 – FFFF
It can be seen that the upper 5bits of all the addresses in a page remain same and
hold the page number and the lower 11bits of all the addresses vary from all 0‟s to
all 1‟s(i.e) 000h-7FFh. Branch address of the AJMP instruction can be calculated in
the following manner. Upper 5 bits of the branch address is taken from the upper 5
bytes of the PC and the remaining 11 bits are obtained by concatenating the upper
3 bits of first byte of instruction with the second byte of instruction to form the
16bit branch address.
Advantage: The instruction length becomes 2 bytes.
JB Jump if bit = 1
JNC Jump if CY = 0
JC Jump if CY = 1
CJNE reg,#data Jump if [reg] ≠ #data
CJNE A,byte address Jump if [A ]≠ [byte address]
Decrement and Jump
DJNZ if A ≠ 0
JNZ Jump if A ≠ 0
JZ Jump if A = 0
Bit level JUMP instructions will check the conditions of the bit and if
condition is true, it jumps to the address specified in the instruction. All the
bit jumps are relative jumps.
JB bit, rel ; Jump to the relative address specified if direct bit in the bit address is set.
JNB bit, rel ; Jump to the relative address specified if direct bit in the bit address
is clear.
JBC bit, rel ;Jump to the relative address specified if direct bit in the bit address
is set
and then clear the bit
f. [PC]= address (16 bit); the new address of subroutine is loaded to PC.
No flags are affected.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
[PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of
PC.
[SP]=[SP]-1; (SP decrements)
[PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower
byte of PC.
[SP]=[SP]-1; (SP decrements again)
a. ANL C,BIT(BIT ADDRESS) ; „logically AND‟ carry flag and content of bit
address, store result in carry flag
b. ANL C, /BIT; „logically AND‟ carry flag and complement of content of
bit address, store result in carry flag
2. LOGICAL OR
a. ORL C,BIT(BIT ADDRESS) ; „logically OR‟ carry flag and content of bit
address, store result in carry flag
4. CPL bit
Example programs
Write an ALP to copy the data 66h to the memory addresses starting
from 30h to 34h
(i) Using direct addressing without using loop
(ii) Using indirect addressing without using Loop.
Answer
(i) Using direct addressing without using loop
ORG 00h
MOV 30h, #66h
MOV 31h, 30h
MOV 32h, 30h
MOV 33h, 30h
MOV 34h, 30h
L1: SJMP L1
END
Answer
ORG 00h
MOV DPTR, #1000h
MOVX A, @DPTR
RLC A
JC L1
CPL A
ADD A, #01h
L2: SJMP L2
END
Write an ALP that checks the content of RAM location 50h is positive or
negative. If positive store 00h else store FFh in RAM Location 80h.
ORG 00h
MOV A, 50h
RLC A
SJMP L2
END
Write an ALP that checks the content of RAM location 50h is ODD number or
EVEN number. If EVEN store 00h else store FFh in RAM Location 80h.
ORG 00h
MOV A, 50h
RRC A
SJMP L2
END
Program:
ORG 0000H
SJMP 30h
ORG 30h
//2-digit decimal number to be converted is given in data
MOV R0,#40H memory 40h
MOV A, @R0
ANL A, #0F0H //obtain upper decimal digit
SWAP A //bring to the units place
MOV B,#0AH //MULTIPLY tens digit with #0A-toget tens in hex
MUL AB
MOV R1,A //temporarily store the converted tens value
MOV A,@R0 //get the decimal number again
ANL A,#0FH //obtain the units digit
ADD A,R1 //add to the converted tens value
INC R0 //increment data address to store the result
MOV @R0,A //converted hexadecimal number in next location
HERE:SJMP HERE
END
Program:
ORG 0000H
SJMP 30h
ORG 30h
MOV R0,#40H
MOV A,@R0 //Get hex number
l1:MOV B,#10
DIV AB //divide by 10 (0AH)
INC R0
MOV @R0,B //Store the remainder (in B) In units place
JNZ l1 //Divide the quotient in A by 10
MOV A,42H
SWAP A
ORL A,41H
MOV 51h,A
MOV 50H,43H
HERE:SJMP HERE
END
Page 18