8051 MICROCONTROLLER

Instruction Set of 8051

The process of writing program for the microcontroller mainly consists of giving instructions (commands) in the specific order in which they should be executed in order to carry out a specific task. As electronics cannot “understand” what for example an instruction “if the push button is pressed- turn the light on” means, then a certain number of simpler and precisely defined orders that decoder can recognise must be used. All commands are known as INSTRUCTION SET. All microcontrollers compatibile with the 8051 have in total of 255 instructions, i.e. 255 different words available for program writing.

At first sight, it is imposing number of odd signs that must be known by heart. However, It is not so complicated as it looks like. Many instructions are considered to be “different”, even though they perform the same operation, so there are only 111 truly different commands. For example: ADD A,R0, ADD A,R1, ... ADD A,R7 are instructions that perform the same operation (additon of the accumulator and register). Since there are 8 such registers, each instruction is counted separately. Taking into account that all instructions perform only 53 operations (addition, subtraction, copy etc.) and most of them are rarely used in practice, there are actually 20-30 abbreviations to be learned, which is acceptable.

Types of instructions

Depending on operation they perform, all instructions are divided in several groups:

Arithmetic Instructions

Branch Instructions

Data Transfer Instructions

Logic Instructions

Bit-oriented Instructions


The first part of each instruction, called MNEMONIC refers to the operation an instruction performs (copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed. 

For example:

INC R1 - Means: Increment register R1 (increment register R1);

LJMP LAB5 - Means: Long Jump LAB5 (long jump to the address marked as LAB5);

JNZ LOOP - Means: Jump if Not Zero LOOP (if the number in the accumulator is not 0, jump to the address marked as LOOP);

The other part of instruction, called OPERAND is separated from mnemonic by at least one whitespace and defines data being processed by instructions. Some of the instructions have no operand, while some of them have one, two or three. If there is more than one operand in an instruction, they are separated by a comma. 

For example:

RET - return from a subroutine;

JZ TEMP - if the number in the accumulator is not 0, jump to the address marked as TEMP;

ADD A,R3 - add R3 and accumulator;

CJNE A,#20,LOOP - compare accumulator with 20. If they are not equal, jump to the address marked as LOOP;


Arithmetic instructions

Arithmetic instructions perform several basic operations such as addition, subtraction, division, multiplication etc. After execution, the result is stored in the first operand. For example:

ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.

 Branch Instructions

There are two kinds of branch instructions:

Unconditional jump instructions: upon their execution a jump to a new location from where the program continues execution is executed.

Conditional jump instructions: a jump to a new program location is executed only if a specified condition is met. Otherwise, the program normally proceeds with the next instruction

Data Transfer Instructions

Data transfer instructions move the content of one register to another. The register the content of which is moved remains unchanged. If they have the suffix “X” (MOVX), the data is exchanged with external memory.

Logic Instructions

Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the result is stored in the first operand.

Bit-oriented Instructions

Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that these are performed upon single bits.

 

ARITHMETIC INSTRUCTIONS

ADD A,Rn   //Adds the register to the accumulator

ADD A,direct  //Adds the direct byte to the accumulator

ADD A,@Ri  //Adds the indirect RAM to the accumulator

ADD A,#data  //Adds the immediate data to the accumulator

ADDC A,Rn  //Adds the register to the accumulator with a carry flag

ADDC A,direct   //Adds the direct byte to the accumulator with a carry flag

ADDC A,@Ri    //Adds the indirect RAM to the accumulator with a carry flag

ADDC A,#data   //Adds the immediate data to the accumulator with a carry flag

SUBB A,Rn  //Subtracts the register from the accumulator with a borrow

SUBB A,direct  //Subtracts the direct byte from the accumulator with a borrow

SUBB A,@Ri  //Subtracts the indirect RAM from the accumulator with a borrow

SUBB A,#data  //Subtracts the immediate data from the accumulator with a borrow

INC A  //Increments the accumulator by 1

INC Rn   //Increments the register by 1

INC Rx   //Increments the direct byte by 1

INC @Ri   //Increments the indirect RAM by 1

DEC A   //Decrements the accumulator by 1

DEC Rn    //Decrements the register by 1

DEC Rx    //Decrements the direct byte by 1

DEC @Ri    //Decrements the indirect RAM by 1

INC DPTR    //Increments the Data Pointer by 1

MUL AB    //Multiplies A and B

DIV AB     //Divides A by B

DA A    //Decimal adjustment of the accumulator according to BCD code

 

BRANCH INSTRUCTIONS


ACALL addr11

Absolute subroutine call

LCALL addr16

Long subroutine call

RET

Returns from subroutine

RETI

Returns from interrupt subroutine

AJMP addr11

Absolute jump

LJMP addr16

Long jump

SJMP rel

Short jump (from –128 to +127 locations relative to the following instruction)

JC rel

Jump if carry flag is set. Short jump.

JNC rel

Jump if carry flag is not set. Short jump.

JB bit,rel

Jump if direct bit is set. Short jump.

JBC bit,rel

Jump if direct bit is set and clears bit. Short jump.

JMP @A+DPTR

Jump indirect relative to the DPTR

JZ rel

Jump if the accumulator is zero. Short jump.

JNZ rel

Jump if the accumulator is not zero. Short jump.

CJNE A,direct,rel

Compares direct byte to the accumulator and jumps if not equal. Short jump.

CJNE A,#data,rel

Compares immediate data to the accumulator and jumps if not equal. Short jump.

CJNE Rn,#data,rel

Compares immediate data to the register and jumps if not equal. Short jump.

CJNE @Ri,#data,rel

Compares immediate data to indirect register and jumps if not equal. Short jump.

DJNZ Rn,rel

Decrements register and jumps if not 0. Short jump.

DJNZ Rx,rel

Decrements direct byte and jump if not 0. Short jump.

NOP

No operation


 

DATA TRANSFER INSTRUCTIONS


MOV A,Rn

Moves the register to the accumulator

MOV A,direct

Moves the direct byte to the accumulator

MOV A,@Ri

Moves the indirect RAM to the accumulator

MOV A,#data

Moves the immediate data to the accumulator

MOV Rn,A

Moves the accumulator to the register

MOV Rn,direct

Moves the direct byte to the register

MOV Rn,#data

Moves the immediate data to the register

MOV direct,A

Moves the accumulator to the direct byte

MOV direct,Rn

Moves the register to the direct byte

MOV direct,direct

Moves the direct byte to the direct byte

MOV direct,@Ri

Moves the indirect RAM to the direct byte

MOV direct,#data

Moves the immediate data to the direct byte

MOV @Ri,A

Moves the accumulator to the indirect RAM

MOV @Ri,direct

Moves the direct byte to the indirect RAM

MOV @Ri,#data

Moves the immediate data to the indirect RAM

MOV DPTR,#data

Moves a 16-bit data to the data pointer

MOVC A,@A+DPTR

Moves the code byte relative to the DPTR to the accumulator (address=A+DPTR)

MOVC A,@A+PC

Moves the code byte relative to the PC to the accumulator (address=A+PC)

MOVX A,@Ri

Moves the external RAM (8-bit address) to the accumulator

MOVX A,@DPTR

Moves the external RAM (16-bit address) to the accumulator

MOVX @Ri,A

Moves the accumulator to the external RAM (8-bit address)

MOVX @DPTR,A

Moves the accumulator to the external RAM (16-bit address)

PUSH direct

Pushes the direct byte onto the stack

POP direct

Pops the direct byte from the stack

XCH A,Rn

Exchanges the register with the accumulator

XCH A,direct

Exchanges the direct byte with the accumulator

XCH A,@Ri

Exchanges the indirect RAM with the accumulator

XCHD A,@Ri

Exchanges the low-order nibble indirect RAM with the accumulator

RETI

Returns from interrupt subroutine



LOGIC INSTRUCTIONS


ANL A,Rn

AND register to accumulator

ANL A,direct

AND direct byte to accumulator

ANL A,@Ri

AND indirect RAM to accumulator

ANL A,#data

AND immediate data to accumulator

ANL direct,A

AND accumulator to direct byte

ANL direct,#data

AND immediate data to direct register

ORL A,Rn

OR register to accumulator

ORL A,direct

OR direct byte to accumulator

ORL A,@Ri

OR indirect RAM to accumulator

ORL direct,A

OR accumulator to direct byte

ORL direct,#data

OR immediate data to direct byte

XRL A,Rn

Exclusive OR register to accumulator

XRL A,direct

Exclusive OR direct byte to accumulator

XRL A,@Ri

Exclusive OR indirect RAM to accumulator

XRL A,#data

Exclusive OR immediate data to accumulator

XRL direct,A

Exclusive OR accumulator to direct byte

XORL direct,#data

Exclusive OR immediate data to direct byte

CLR A

Clears the accumulator

CPL A

Complements the accumulator (1=0, 0=1)

SWAP A

Swaps nibbles within the accumulator

RL A

Rotates bits in the accumulator left

RLC A

Rotates bits in the accumulator left through carry

RR A

Rotates bits in the accumulator right

RRC A

Rotates bits in the accumulator right through carry

CJNE @Ri,#data,rel

Compares immediate data to indirect register and jumps if not equal. Short jump.


 

BIT-ORIENTED INSTRUCTIONS


CLR C

Clears the carry flag

CLR bit

Clears the direct bit

SETB C

Sets the carry flag

SETB bit

Sets the direct bit

CPL C

Complements the carry flag

CPL bit

Complements the direct bit

ANL C,bit

AND direct bit to the carry flag

ANL C,/bit

AND complements of direct bit to the carry flag

ORL C,bit

OR direct bit to the carry flag

ORL C,/bit

OR complements of direct bit to the carry flag

MOV C,bit

Moves the direct bit to the carry flag

MOV bit,C

Moves the carry flag to the direct bit



Description of all 8051 instructions

Here is a list of the operands and their meanings: