3. 8051 INSTRUCTION SET AND PROGRAMS
The 8051 microcontroller has a well-defined instruction set that allows it to perform a variety of operations. The instruction set can be broadly categorized into different types based on the task they perform.
3.1. Overview of 8051 Instruction Set
The 8051 instruction set consists of a total of 111 instructions. These instructions are divided into the following categories:
- Data Transfer Instructions: Move data between registers, memory, and I/O.
- Arithmetic Instructions: Perform mathematical operations.
- Logical Instructions: Perform logical operations like AND, OR, XOR.
- Branching Instructions: Control the flow of execution (loops, jumps).
- Bit Manipulation Instructions: Operate on specific bits.
- Stack, Subroutine, and Interrupt Instructions: Handle procedure calls, interrupts, and stack operations.
The 8051 uses 8-bit instructions and typically operates on 8-bit data, but it also supports operations on 16-bit registers and data in some cases.
3.2. Various Addressing Modes
The 8051 supports several addressing modes, which determine how the operand is accessed. The main addressing modes are:
Immediate Addressing Mode:
- The operand is directly provided in the instruction.
- Example:
MOV A, #55h
(The value 55h is directly moved to the accumulator).
Register Addressing Mode:
- The operand is located in one of the CPU's registers.
- Example:
MOV A, R0
(The value in register R0 is moved to the accumulator).
Direct Addressing Mode:
- The operand is located at a specific memory address.
- Example:
MOV A, 30h
(The value at memory location 30h is moved to the accumulator).
Indirect Addressing Mode:
- The operand is pointed to by a register (usually R0 or R1 in the 8051).
- Example:
MOV A, @R0
(The value at the address stored in register R0 is moved to the accumulator).
Indexed Addressing Mode:
- A base address is added with an offset (used in accessing data in ROM or external memory).
- Example:
MOVC A, @A+DPTR
(The value at the address given by the sum of the accumulator and the Data Pointer (DPTR) is moved to the accumulator).
3.3. Classification of Instructions
The 8051 instruction set can be classified into various types based on the operations they perform. Below are the classifications:
3.3.1. Data Transfer Instructions
These instructions are used to move data between registers, memory, and ports.
MOV: Moves data from one location to another.
- Example:
MOV A, #30h
(Move the immediate data 30h into the accumulator).
- Example:
MOVX: Used for transferring data to/from external memory.
- Example:
MOVX A, @DPTR
(Move the data at the address pointed by DPTR into A).
- Example:
PUSH: Pushes a register or memory value onto the stack.
- Example:
PUSH ACC
(Push the accumulator onto the stack).
- Example:
POP: Pops data from the stack into a register or memory.
- Example:
POP ACC
(Pop the data into the accumulator).
- Example:
Diagram (MOV example):
3.3.2. Arithmetic Instructions
These instructions perform basic arithmetic operations.
ADD: Adds data to the accumulator.
- Example:
ADD A, R0
(Add the content of R0 to the accumulator).
- Example:
SUB: Subtracts data from the accumulator.
- Example:
SUB A, R0
(Subtract the content of R0 from the accumulator).
- Example:
INC: Increments the value of a register or memory.
- Example:
INC A
(Increment the accumulator by 1).
- Example:
DEC: Decrements the value of a register or memory.
- Example:
DEC A
(Decrement the accumulator by 1).
- Example:
MUL: Multiplies the accumulator by the contents of register B.
- Example:
MUL AB
(Multiply A by B and store the result in A and B).
- Example:
Diagram (ADD example):
3.3.3. Logical Instructions
These instructions perform logical operations on data.
ANL: Logical AND operation.
- Example:
ANL A, R0
(Perform AND between A and R0, result in A).
- Example:
ORL: Logical OR operation.
- Example:
ORL A, R0
(Perform OR between A and R0, result in A).
- Example:
XRL: Logical XOR operation.
- Example:
XRL A, R0
(Perform XOR between A and R0, result in A).
- Example:
CPL: Complement (flip bits) the value in the accumulator.
- Example:
CPL A
(Invert the bits of the accumulator).
- Example:
CLR: Clear (set to 0) a specific bit.
- Example:
CLR C
(Clear the carry flag).
- Example:
Diagram (ANL example):
3.3.4. Branching Instructions
Branching instructions control the flow of execution.
SJMP: Short jump (relative address).
- Example:
SJMP Label
(Jump to the address specified by Label).
- Example:
LJMP: Long jump (absolute address).
- Example:
LJMP 1000h
(Jump to address 1000h).
- Example:
JZ: Jump if zero flag is set.
- Example:
JZ Label
(Jump if the zero flag is set).
- Example:
JNZ: Jump if zero flag is not set.
- Example:
JNZ Label
(Jump if the zero flag is not set).
- Example:
CALL: Call a subroutine.
- Example:
CALL Subroutine
(Call a subroutine located at the label Subroutine).
- Example:
RET: Return from a subroutine.
- Example:
RET
(Return from the current subroutine).
- Example:
3.3.5. Bit Manipulation Instructions
These instructions perform bit-level operations.
SETB: Set a bit to 1.
- Example:
SETB P1.0
(Set bit 0 of Port 1).
- Example:
CLR: Clear a bit (set it to 0).
- Example:
CLR P1.0
(Clear bit 0 of Port 1).
- Example:
CPL: Complement a bit.
- Example:
CPL P1.0
(Invert bit 0 of Port 1).
- Example:
ANL: Logical AND on individual bits.
- Example:
ANL P1.0, 1
(Perform AND operation between bit 0 of Port 1 and 1).
- Example:
3.3.6. Stack, Subroutine, and Interrupt Related Instructions
These instructions manage the stack and handle interrupts and subroutine calls.
PUSH: Push a value onto the stack.
- Example:
PUSH ACC
(Push the accumulator onto the stack).
- Example:
POP: Pop a value from the stack.
- Example:
POP ACC
(Pop the top value from the stack into the accumulator).
- Example:
RET: Return from a subroutine.
- Example:
RET
(Return from a subroutine).
- Example:
RETI: Return from interrupt.
- Example:
RETI
(Return from interrupt handling routine).
- Example:
SIM: Set the interrupt control register.
- Example:
SIM
(Set interrupt priorities).
- Example:
0 Comments