Module-2 Assembly Programming and Instruction of 8051: Course Coordinator: Prof. Mahesh P.Yanagimath
Module-2 Assembly Programming and Instruction of 8051: Course Coordinator: Prof. Mahesh P.Yanagimath
of E & E
Hirasugar Institute of Technology,Nidasoshi. Notes
Inculcating Values, Promoting Prosperity Microcontroller
Approved by AICTE and Affiliated to VTU Belagavi 2018-19
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
Assembler directives
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 followed by H, it is decimal and the
assembler will convert it to hex. Some assemblers use “. ORG” (notice the dot) instead of
“ORG” for the origin directive.
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”.
Instruction Syntax
8051 instruction set is coded set that have been defined by manufacturer of 8051.
Operand is data address it specifies the destination for the data i.e being copied form the source
and also it gives us source address. Destination and source addresses are separated by ( ,)
comma. Assembler will come to know when one operand name ends and other begins.
MOV <dest-byte>,<src-byte>-
Description: The byte variable indicated by the second operand is copied into the location
specified by the first operand. The source byte is not affected. No other register or flag is
affected.
1. mov direct , A
2. mov A, @Ri
3. mov A, Rn
5. mov A, #data
MOV <dest-bit>,<src-bit>
Description: MOV <dest-bit>,<src-bit> copies the Boolean variable indicated by the second
operand into the location specified by the first operand. One of the operands must be the carry
flag; the other may be any directly addressable bit. No other register or flag is affected.
Example: MOV P1.3,C; moves the carry bit to 3rd bit of port1
MOV DPTR,#data16
Description: MOV DPTR,#data16 loads the Data Pointer with the 16-bit constant indicated. The
16-bit constant is loaded into the second and third bytes of the instruction. The second byte
(DPH) is the high-order byte, while the third byte(DPL) holds the lower-order byte. No flags are
affected.
Example: The instruction,MOV DPTR, # 4567H loads the value 4567H into the Data Pointer.
DPH holds 45H, and DPL holds 67H.
Description: The MOVC instructions load the Accumulator with a code byte or constant from
program memory. The address of the byte fetched is the sum of the original unsigned 8-bit
Accumulator contents and the contents of a 16-bit base register, which may be either the Data
Pointer or the PC. In the latter case, the PC is incremented to the address of the following
instruction before being added with the Accumulator; otherwise the base register is not altered.
Sixteen-bit addition is performed so a carry-out from the low-order eight bits may propagate
through higher-order bits. No flags are affected.
MOVC A,@A+PC
(PC) (PC) + 1
MOVX <dest-byte>,<src-byte>
Description: The MOVX instructions transfer data between the Accumulator and a byte of
external data memory, which is why “X” is appended to MOV. There are two types of
instructions, differing in whether they provide an 8-bit or 16-bit indirect address to the external
data RAM.
This form of MOVX is faster and more efficient when accessing very large data arrays (up to
64K bytes), since no additional instructions are needed to set up the output ports.
Example:
MOVX A,@DPTR
MOVX @DPTR, A
(A) ((DPTR))
PUSH direct
Description: The Stack Pointer is incremented by one. The contents of the indicated variable is
then copied into the internal RAM location addressed by the Stack Pointer. No flags are affected.
Example: On entering an interrupt routine, the Stack Pointer contains 09H. The Data Pointer
holds the value 0123H. The following instruction sequence,
PUSH DPL
PUSH DPH
leaves the Stack Pointer set to 0BH and stores 23H and 01H in internal RAM locations 0AH and
0BH respectively.
POP direct
Description: The contents of the internal RAM location addressed by the Stack Pointer is read,
and the Stack Pointer is decremented by one. The value read is then transferred to the directly
addressed byte indicated. No flags are affected.
POP DPH
POP DPL leaves the Stack Pointer equal to the value 30H and sets the Data Pointer to 0123H.
ADD A,<src-byte>
Function: Add
Description: ADD adds the byte variable indicated to the Accumulator, leaving the result in the
Accumulator. The carry and auxiliary-carry flags are set, respectively, if there is a carry-out from
bit 7 or bit 3, and cleared otherwise. When adding unsigned integers, the carry flag indicates an
overflow occurred. OV is set if a carry-out of bit 7 but not bit 6; otherwise, OV is cleared. When
adding signed integers, OV indicates a negative number produced as the sum of two positive
operands, or a positive sum from two negative operands. Four source operand addressing modes
are allowed: register, direct, register-indirect, or immediate.
Example: The Accumulator holds 0C3H (1100001lB), and register 0 holds 0AAH (10101010B).
The following instruction,
ADD A,R0
leaves 6DH (01101101B) in the Accumulator with the AC flag cleared and both the carry flag
and OV set to 1.
ADD A, direct
ADD A, @Ri
ADDC A, <src-byte>
Description: ADDC simultaneously adds the byte variable indicated, the carry flag and the
Accumulator contents, leaving the result in the Accumulator. The carry and auxiliary-carry flags
Example: The Accumulator holds 0C3H (11000011B) and register 0 holds 0AAH (10101010B)
with the carry flag set. The following instruction,
ADDC A,R0
leaves 6EH (01101110B) in the Accumulator with AC cleared and both the Carry flag and OV
set to 1.
ADDC A,Rn
Operation: ADDC
ADDC A, direct
Operation: ADDC
ADDC A,@Ri
Operation: ADDC
ADDC A, #data
Operation: ADDC
SUBB A,<src-byte>
Description: SUBB subtracts the indicated variable and the carry flag together from the
Accumulator, leaving the result in the Accumulator. SUBB sets the carry (borrow) flag if a
borrow is needed for bit 7 and clears C otherwise. (If C was set before executing a SUBB
Example: The Accumulator holds 0C9H (11001001B), register 2 holds 54H (01010100B), and
the carry flag is set. The instruction, SUBB A,R2 will leave the value 74H (01110100B) in the
accumulator, with the carry flag and AC cleared but OV set.
Instructions OpCode Bytes Flags
SUBB A,#data 0x94 2 C, AC, OV
SUBB A,iram addr 0x95 2 C, AC, OV
SUBB A,@R0 0x96 1 C, AC, OV
SUBB A,@R1 0x97 1 C, AC, OV
SUBB A,R0 0x98 1 C, AC, OV
SUBB A,R1 0x99 1 C, AC, OV
SUBB A,R2 0x9A 1 C, AC, OV
SUBB A,R3 0x9B 1 C, AC, OV
SUBB A,R4 0x9C 1 C, AC, OV
SUBB A,R5 0x9D 1 C, AC, OV
SUBB A,R6 0x9E 1 C, AC, OV
SUBB A,R7 0x9F 1 C, AC, OV
SUBB A,Rn
Operation: SUBB
SUBB A, direct
Operation: SUBB
Operation: SUBB
MUL AB
Function: Multiply
Description: MUL AB multiplies the unsigned 8-bit integers in the Accumulator and register B.
The low-order byte of the 16-bit product is left in the Accumulator, and the high-order byte in B.
If the product is greater than 255 (0FFH), the overflow flag is set; otherwise it is cleared. The
carry flag is always cleared.
Example: Originally the Accumulator holds the value 80 (50H). Register B holds the value 160
(0A0H). The instruction,
MUL AB
will give the product 12,800 (3200H), so B is changed to 32H (00110010B) and the Accumulator
is cleared. The overflow flag is set, carry is cleared.
DIV AB
Function: Divide
Description: DIV AB divides the unsigned eight-bit integer in the Accumulator by the unsigned
eight-bit integer in register B. The Accumulator receives the integer part of the quotient; register
B receives the integer remainder. The carry and OV flags are cleared. Exception: if B had
originally contained 00H, the values returned in the Accumulator and B-register are undefined
and the overflow flag are set. The carry flag is cleared in any case.
Example: The Accumulator contains 251 (0FBH or 11111011B) and B contains 18 (12H or
00010010B). The following instruction, DIV AB leaves 13 in the Accumulator (0DH or
00001101B) and the value 17 (11H or 00010001B) in B, since 251 = (13 x 18) + 17. Carry and
OV are both cleared.
DA A
If the carry flag is now set, or if the four high-order bits now exceed nine, these high-order bits
are added with six, producing the proper BCD digit in the high-order nibble. Again, this sets the
carry flag if there is a carry-out of the high-order bits, but does not clear the carry.
SWAP A
Description: SWAP A interchanges the low- and high-order nibbles (four-bit fields) of the
Accumulator (bits 3 through 0 and bits 7 through 4). The operation can also be thought of as a 4-
bit rotate instruction. No flags are affected.
Example: The Accumulator holds the value 0C5H (11000101B). The instruction, SWAP A
leaves the Accumulator holding the value 5CH (01011100B
Operation: SWAP
(A3-0) D (A7-4)
XCH A,<byte>
Description: XCH loads the Accumulator with the contents of the indicated variable, at the same
time writing the original Accumulator contents to the indicated variable. The source/destination
operand can use register, direct, or register-indirect addressing.
Example: R0 contains the address 20H. The Accumulator holds the value 3FH (0011111lB).
Internal RAM location 20H holds the value 75H (01110101B). The following instruction, XCH
A,@R0 leaves RAM location 20H holding the values 3FH (00111111B) and 75H (01110101B)
in the accumulator.
XCHD A,@Ri
Example: R0 contains the address 20H. The Accumulator holds the value 36H (00110110B).
Internal RAM location 20H holds the value 75H (01110101B). The following instruction, XCHD
A,@R0 leaves RAM location 20H holding the value 76H (01110110B) and 35H (00110101B) in
the Accumulator.
CPL A
Description: CPLA logically complements each bit of the Accumulator (one‟s complement).
Bits which previously contained a 1 are changed to a 0 and vice-versa. No flags are affected.
The following instruction, CPL A leaves the Accumulator set to 0A3H (10100011B).
CPL bit
Description: CPL bit complements the bit variable specified. A bit that had been a 1 is changed
to 0 and vice-versa. No other flags are affected. CLR can operate on the carry or any directly
addressable bit.
Example: Port 1 has previously been written with 5BH (01011101B). The following instruction
sequence, CPL P1.1CPL
DEC byte
Function: Decrement
Description: DEC byte decrements the variable indicated by 1. An original value of 00H
underflows to 0FFH. No flags are affected.
Example: Register 0 contains 7FH (01111111B). Internal RAM locations 7EH and 7FH contain
00H and 40H, respectively. The following instruction sequence,
DEC @R0
leaves register 0 set to 7EH and internal RAM locations 7EH and 7FH set to 0FFH and 3FH.
DEC A
DEC Rn
DEC direct
DEC @Ri
INC <byte>
Function: Increment
Description: INC increments the indicated variable by 1. An original value of 0FFH overflows
to 00H. No flags are affected.
Example: Register 0 contains 7EH (011111110B). Internal RAM locations 7EH and 7FH
contain 0FFH and 40H, respectively. The following instruction sequence,
INC R0
INC @R0 leaves register 0 set to 7FH and internal RAM locations 7EH and 7FH holding 00H
and 41H, respectively.
INC DPTR
Description: INC DPTR increments the 16-bit data pointer by 1. A 16-bit increment (modulo
216) is performed, and an overflow of the low-order byte of the data pointer (DPL) from 0FFH
to 00H increments the high-order byte (DPH). No flags are affected. This is the only 16-bit
register which can be incremented.
Example: Registers DPH and DPL contain 12H and 0FEH, respectively. The following
instruction sequence,
INC DPTR
NOP
Function: No Operation
Description: Execution continues at the following instruction. Other than the PC, no registers or
flags are affected.
Logical instructions
ANL <dest-byte>,<src-byte>
Description: ANL performs the bitwise logical-AND operation between the variables indicated
and stores the results in the destination variable. No flags are affected. The two operands allow
six addressing mode combinations. When the destination is the Accumulator, the source can use
register, direct, register-indirect, or immediate addressing; when the destination is a direct
address, the source can be the Accumulator or immediate data.
Example: If the Accumulator holds 0C3H (1100001lB), and register 0 holds 55H (01010101B),
then the following instruction, ANL A,R0 leaves 41H (01000001B) in the Accumulator.
When the destination is a directly addressed byte, this instruction clears combinations of bits in
any RAM location or hardware register. The mask byte determining the pattern of bits to be
cleared would either be a constant contained in the instruction or a value computed in the
Accumulator at run-time. The following instruction, ANL P1,#01110011B clears bits 7, 3, and 2
of output port 1.
Instructions OpCode Bytes Flags
ANL iram addr,A 0x52 2 None
ANL iram addr,#data 0x53 3 None
ANL A,#data 0x54 2 None
ANL A,iram addr 0x55 2 None
ANL A,@R0 0x56 1 None
ANL A,@R1 0x57 1 None
ANL A,R0 0x58 1 None
ANL A,R1 0x59 1 None
ANL A,R2 0x5A 1 None
ANL A,R3 0x5B 1 None
ANL A,R4 0x5C 1 None
ANL A,R5 0x5D 1 None
ANL A,Rn
Operation: AND the content of accumulator with the content of Register specified.
ANL A,@Ri
Operation: ANL
ANL direct,#data
Operation: ANL
(direct) #data^(direct)
Description: ORL performs the bitwise logical-OR operation between the indicated variables,
storing the results in the destination byte. No flags are affected.
Example: If the Accumulator holds 0C3H (11000011B) and R0 holds 55H (01010101B) then
the following instruction, ORL A,R0 leaves the Accumulator holding the value 0D7H
(1101011lB).
ORL A, Rn ; or the content of Accumulator and Register Rn and store the result in Accumulator
ORL A, direct ; or the content of Accumulator and the memory and store the result in
Accumulator
ORL A, @Ri ; or the content of accumulator and the memory location whose address is
specified in Ri
Description: Set the carry flag if the Boolean value is a logical 1; leave the carry in its current
state otherwise. A slash ( / ) preceding the operand in the assembly language indicates that the
logical complement of the addressed bit is used as the source value, but the source bit itself is not
affected. No other flags are affected.
Example:
SETB
Operation: SETB
Function: Set Bit
Syntax: SETB bitaddr
XRL <dest-byte>,<src-byte>
Description: XRL performs the bitwise logical Exclusive-OR operation between the indicated
variables, storing the results in the destination. No flags are affected. The two operands allow six
addressing mode combinations. When the destination is the Accumulator, the source can use
register, direct, register-indirect, or immediate addressing; when the destination is a direct
address, the source can be the Accumulator or immediate data.
Example: If the Accumulator holds 0C3H (1100001lB) and register 0 holds 0AAH (10101010B)
then the instruction, XRL A,R0 leaves the Accumulator holding the value 69H (01101001B).
Instructions OpCode Bytes Flags
XRL iram addr,A 0x62 2 None
XRL iram addr,#data 0x63 3 None
XRL A,#data 0x64 2 None
XRL A,iram addr 0x65 2 None
Rotate Instructions
RL A
Description: The eight bits in the Accumulator are rotated one bit to the left. Bit 7 is rotated into
the bit 0 position. No flags are affected.
Example: The Accumulator holds the value 0C5H (11000101B). The following instruction,
RL A leaves the Accumulator holding the value 8BH (10001011B) with the carry unaffected.
RLC A
Description: The eight bits in the Accumulator and the carry flag are together rotated one bit to
the left. Bit 7 moves into the carry flag; the original state of the carry flag moves into the bit 0
position. No other flags are affected.
Example: The Accumulator holds the value 0C5H(11000101B), and the carry is zero. The
following instruction, RLC A leaves the Accumulator holding the value 8BH (10001010B) with
the carry set.
RRC A
Description: The eight bits in the Accumulator and the carry flag are together rotated one bit to
the right. Bit 0 moves into the carry flag; the original value of the carry flag moves into the bit 7
position. No other flags are affected.
Example: The Accumulator holds the value 0C5H (11000101B), the carry is zero. The
following instruction, RRC A leaves the Accumulator holding the value 62 (01100010B) with
the carry set.
Jump Instructions
Description: AJMP unconditionally jumps to the indicated code address. The new value for the
Program Counter is calculated by replacing the least-significant-byte of the Program Counter
with the second byte of the AJMP instruction, and replacing bits 0-2 of the most-significant-byte
of the Program Counter with 3 bits that indicate the page of the byte following the AJMP
instruction. Bits 3-7 of the most-significant-byte of the Program Counter remain unchanged.
Since only 11 bits of the Program Counter are affected by AJMP, jumps may only be made to
code located within the same 2k block as the first byte that follows AJMP.
LJMP(Long jump)
Description: LJMP jumps unconditionally to the specified code address. It can jump in any
memory from 0000H to FFFFH.
SJMP
SJMP
Operation:
Function: Short Jump
Syntax: SJMP reladdr
Description: SJMP jumps unconditionally to the address specified reladdr. Reladdr must be
within -128 or +127 bytes of the instruction that follows the SJMP instruction
Description: JNC branches to the address indicated by reladdr if the carry bit is not set. If the
carry bit is set program execution continues with the instruction following the JNB instruction.
Operation: JC
Function: Jump if Carry Set
Syntax: JC reladdr
Description: JC will branch to the address indicated by reladdr if the Carry Bit is set. If the
Carry Bit is not set program execution continues with the instruction following the JC
instruction.
Operation: JNB
Function: Jump if Bit Not Set
Syntax: JNB bit addr,reladdr
Description: JNB will branch to the address indicated by reladdress if the indicated bit is not
set. If the bit is set program execution continues with the instruction following the JNB
instruction.
Operation: JB
Function: Jump if Bit Set
Syntax: JB bit addr, reladdr
Description: JNZ will branch to the address indicated by reladdr if the Accumulator contains
any value except 0. If the value of the Accumulator is zero program execution continues with the
instruction following the JNZ instruction.
Operation: JZ
Function: Jump if Accumulator Zero
Syntax: JNZ reladdr
Description: JZ branches to the address indicated by reladdr if the Accumulator contains the
value 0. If the value of the Accumulator is non-zero program execution continues with the
instruction following the JNZ instruction.
Operation: DJNZ
Function: Decrement and Jump if Not Zero
DJNZ register, reladdr
Syntax:
Description: DJNZ decrements the value of register by 1. If the initial value of register is 0,
decrementing the value will cause it to reset to 255 (0xFF Hex). If the new value of register is
not 0 the program will branch to the address indicated by relative addr. If the new value of
register is 0 program flow continues with the instruction following the DJNZ instruction.
Operation: CJNE
Function: Compare and Jump If Not Equal
Syntax: CJNE operand1,operand2,reladdr
Description: CJNE compares the value of operand1 and operand2 and branches to the indicated
relative address if operand1 and operand2 are not equal. If the two operands are equal program
flow continues with the instruction following the CJNE instruction. The Carry bit (C) is set if
operand1 is less than operand2, otherwise it is cleared.
CALL INSTRUCTIONS
Another control transfer instruction is the CALL instruction, which is used to call a subroutine.
Subroutines are often used to perform tasks that need to be performed frequently. This makes a
program more structured in addition to saving memory space.
In the 8051 there are two instructions for call: LCALL (long call) and ACALL (absolute call).
Deciding which one to use depends on the target address. Each instruction is explained next.
In this 3-byte instruction, the first byte is the opcode and the second and third bytes are used for
the address of the target subroutine. Therefore, LCALL can be used to call subroutines located
anywhere within the 64K-byte address space of the 8051. To make sure that after execution of
the called subroutine the 8051 knows where to come back to, the processor automatically saves
ACALL is a 2-byte instruction in contrast to LCALL. Since ACALL is a 2-byte instruction, the
target address of the subroutine must be within 2K bytes because only 11 bits of the 2 bytes are
used for the address. There is no difference between ACALL and LCALL in terms of saving the
program counter on the stack or the function of the RET instruction. The only difference is that
the target address for LCALL can be anywhere within the 64K-byte address space of the 8051
while the target address of ACALL must be within a 2K-byte range.
RET
Return From
Function:
Subroutine
Syntax: RET
Description: RET is used to return from a subroutine previously called by LCALL or ACALL.
Program execution continues at the address that is calculated by popping the topmost 2 bytes off
the stack. The most-significant-byte is popped off the stack first, followed by the least-
significant-byte.
RETI
Operation: RETI
Return From
Function:
Interrupt
Syntax: RETI
Description: RETI is used to return from an interrupt service routine. RETI first enables
interrupts of equal and lower priorities to the interrupt that is terminating. Program execution
continues at the address that is calculated by popping the topmost 2 bytes off the stack. The
most-significant-byte is popped off the stack first, followed by the least-significant-byte.