Module - 2: 8051 Instruction Set
Module - 2: 8051 Instruction Set
Since R0 and R1 are 8 bits wide, their use is limited to access any
information in the internal RAM
Whether accessing externally connected RAM or on-chip ROM,
we need 16-bit pointer
• In such case, the DPTR register is used.
Indexed addressing mode is widely used in accessing data elements
of look-up table entries located in the program ROM.
The look-up table allows access to elements of a frequently used table
with minimum operations
The instruction used for this purpose is MOVC A,@A+DPTR
• Use instruction MOVC, “C” means code
• The contents of A are added to the 16-bit register DPTR to form
the 16-bit address of the needed data
Data Transfer instructions
The MOV opcodes involve data transfers within the following
four distinct physical parts of 8051 memory :
• Internal RAM
• Internal special. function registers
• External RAM
• Internal and external ROM
Finally, the following five types of opcodes are used to move
data:
• MOV
• MOVX
• MOVC
• PUSH and POP
• XCH
Data Transfer using immediate and register addressing modes
A data MOV does not alter the contents of the data source
address.
A copy of the data is made from the source and moved to the
destination address.
The contents of the destination address are replaced by the
source address contents.
It is impossible to have immediate data as a destination.
All numbers must start with a decimal number (0-9), or the
assembler assumes the number is a label.
Register-to-register moves using the register addressing
mode occur between registers A and RO to R7.
Data Transfer using direct, immediate and
register addressing modes
Data Transfer - External Data Moves
Data Transfer - Code Memory Read-Only
Data Moves
Data Transfer - External Data Moves
Data Transfer - Data Exchanges
Arithmetic instructions
• The 8051 can perform addition, subtraction. Multiplication and division
operations on 8 bit numbers. The 24 arithmetic opcodes are grouped into as
follows
Arithmetic instructions - Addition
All addition is done with the A register as the destination of
the result.
All addressing modes may be used for the source: an
immediate number, a register, a direct address, and an indirect
address.
Some instructions include the carry flag as an additional
source of a single bit that is included in the operation at the least
significant bit position
ADD A, source; A = A + source
These instructions affect 3 bits in PSW:
• C = 1 if result of add is greater than FF
• AC = 1 if there is a carry out of bit 3
• OV = 1 if there is a carry out of bit 7, but not from bit 6, or
vice versa.
Addition of Unsigned Numbers
ADD A, source; A = A + source
The instruction ADD is used to add two operands
Destination operand is always in register A
Source operand can be a register, immediate data, or in
memory
Memory-to-memory arithmetic operations are never allowed
in 8051 Assembly language
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
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
Adding two BCD numbers must give a BCD result
DA Instruction
DA A ;decimal adjust for addition
The DA instruction is provided to correct the aforementioned
problem associated with BCD addition
The DA instruction will add 6 to the lower nibble or higher
nibble if need
Subtraction of Unsigned Numbers
In many microprocessor there are two different instructions for
subtraction:
SUBB when CY = 0
Take the 2’s complement of the subtrahend (source operand)
Add it to the minuend (A)
Invert the carry
Multiplication of Unsigned Numbers
The 8051 supports byte by byte multiplication only
The byte are assumed to be unsigned data
MUL AB; A*B, 16-bit result in B, A
Division of Unsigned Numbers
The 8051 supports byte over byte division only
The byte are assumed to be unsigned data
DIV AB ; A/B, Result(R, Q) in B, A
Logical instructions
ANL destination, source ; dest = dest AND source
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
ORL destination, source; dest = dest OR source
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
XRL destination, source; dest = dest XOR source
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
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.