# Instruction-Set

Updated on 2017/08/19 12:35

Instruction set of 8051

## Types of Instruction

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);
• 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;
• 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.
ARITHMETIC INSTRUCTIONS
MnemonicDescriptionByte Machine Cycle
ADDC A,directAdds the direct byte to the accumulator with a carry flag22
ADDC A,@RiAdds the indirect RAM to the accumulator with a carry flag12
ADDC A,#dataAdds the immediate data to the accumulator with a carry flag22
SUBB A,RnSubtracts the register from the accumulator with a borrow11
SUBB A,directSubtracts the direct byte from the accumulator with a borrow22
SUBB A,@RiSubtracts the indirect RAM from the accumulator with a borrow12
SUBB A,#dataSubtracts the immediate data from the accumulator with a borrow22
INC AIncrements the accumulator by 111
INC RnIncrements the register by 112
INC RxIncrements the direct byte by 123
INC @RiIncrements the indirect RAM by 113
DEC ADecrements the accumulator by 111
DEC RnDecrements the register by 111
DEC RxDecrements the direct byte by 112
DEC @RiDecrements the indirect RAM by 123
INC DPTRIncrements the Data Pointer by 113
MUL AB

Multiplies A and B and Stores Higher 8-bits of result in B and Lower 8-bits of result in A

Example:-
ORG 00H
MOV A,#02H;   A=02H
MOV B,#03H;   B=03H
MUL AB ;  B=00H (Higher 8-bits of result),  A=06H (Lower 8-bits of result)
end

5
DIV AB

Divides A by B Stores Quotient in A and Remainder in B

Example:-
ORG 00H
MOV A,#03H; A=03H (Divident)
MOV B,#02H; B=02H (Divisor)
DIV AB          ;B=01H (Remainder), A=01H (Quotient)
end

15
DA A

Decimal adjustment of the accumulator according to BCD code

Example:-
ORG 00H
MOV A,#03H; A=25H
MOV R0,#02H; R0=25H
DA A                    ;A=4F+06=50
end

11

### 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.

BRANCH INSTRUCTIONS
MnemonicDescriptionByteMachine Cycle
RETReturns from subroutine14
RETIReturns from interrupt subroutine14
SJMP relShort jump (from –128 to +127 locations relative to the following instruction)23
JC relJump if carry flag is set. Short jump.23
JNC relJump if carry flag is not set. Short jump.23
JB bit,relJump if direct bit is set. Short jump.34
JBC bit,relJump if direct bit is set and clears bit. Short jump.34
JMP @A+DPTRJump indirect relative to the DPTR12
JZ relJump if the accumulator is zero. Short jump.23
JNZ relJump if the accumulator is not zero. Short jump.23
CJNE A,direct,relCompares direct byte to the accumulator and jumps if not equal. Short jump.34
CJNE A,#data,relCompares immediate data to the accumulator and jumps if not equal. Short jump.34
CJNE Rn,#data,relCompares immediate data to the register and jumps if not equal. Short jump.34
CJNE @Ri,#data,relCompares immediate data to indirect register and jumps if not equal. Short jump.34
DJNZ Rn,relDecrements register and jumps if not equal to Zero. Short jump.23
DJNZ Rx,relDecrements direct byte and jump if not equal to Zero. Short jump.34
NOPNo operation11

### 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.

DATA TRANSFER INSTRUCTIONS
MnemonicDescriptionByteCycle
MOV A,RnMoves the register to the accumulator11
MOV A,directMoves the direct byte to the accumulator22
MOV A,@RiMoves the indirect RAM to the accumulator12
MOV A,#dataMoves the immediate data to the accumulator22
MOV Rn,AMoves the accumulator to the register12
MOV Rn,directMoves the direct byte to the register24
MOV Rn,#dataMoves the immediate data to the register22
MOV direct,AMoves the accumulator to the direct byte23
MOV direct,RnMoves the register to the direct byte23
MOV direct,directMoves the direct byte to the direct byte34
MOV direct,@RiMoves the indirect RAM to the direct byte24
MOV direct,#dataMoves the immediate data to the direct byte33
MOV @Ri,AMoves the accumulator to the indirect RAM13
MOV @Ri,directMoves the direct byte to the indirect RAM25
MOV @Ri,#dataMoves the immediate data to the indirect RAM23
MOV DPTR,#dataMoves a 16-bit data to the data pointer33
MOVC A,@A+DPTRMoves the code byte relative to the DPTR to the accumulator (address=A+DPTR)13
MOVC A,@A+PCMoves the code byte relative to the PC to the accumulator (address=A+PC)13
MOVX A,@RiMoves the external RAM (8-bit address) to the accumulator13-10
MOVX A,@DPTRMoves the external RAM (16-bit address) to the accumulator13-10
MOVX @Ri,AMoves the accumulator to the external RAM (8-bit address)14-11
MOVX @DPTR,AMoves the accumulator to the external RAM (16-bit address)14-11
PUSH directPushes the direct byte onto the stack24
POP directPops the direct byte from the stack/td>23
XCH A,RnExchanges the register with the accumulator12
XCH A,directExchanges the direct byte with the accumulator23
XCH A,@RiExchanges the indirect RAM with the accumulator13
XCHD A,@Ri

Exchanges the low-order nibble indirect RAM with the accumulator

Example:-
ORG 00H
MOV A,#23H; A=23H
MOV 50H,#87H; 50H=87H  as data at 50H Memory location
MOV R0,#50H  ;ADD A,R0          ; A=A+R0=4FH
XCHD A,@R0                  ;A=27H   AND  @R0=83H
end

### Logic Instructions

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

LOGIC INSTRUCTIONS
MnemonicDescriptionByteCycle
ANL A,RnAND register to accumulator11
ANL A,directAND direct byte to accumulator22
ANL A,@RiAND indirect RAM to accumulator12
ANL A,#dataAND immediate data to accumulator22
ANL direct,AAND accumulator to direct byte23
ANL direct,#dataAND immediae data to direct register34
ORL A,RnOR register to accumulator11
ORL A,directOR direct byte to accumulator22
ORL A,@RiOR indirect RAM to accumulator12
ORL direct,AOR accumulator to direct byte23
ORL direct,#dataOR immediate data to direct byte34
XRL A,RnExclusive OR register to accumulator11
XRL A,directExclusive OR direct byte to accumulator22
XRL A,@RiExclusive OR indirect RAM to accumulator12
XRL A,#dataExclusive OR immediate data to accumulator22
XRL direct,AExclusive OR accumulator to direct byte23
XORL direct,#dataExclusive OR immediate data to direct byte34
CLR AClears the accumulator11
CPL AComplements the accumulator (1=0, 0=1)11
SWAP ASwaps nibbles within the accumulator11
RL ARotates bits in the accumulator left11
RLC ARotates bits in the accumulator left through carry11
RR ARotates bits in the accumulator right11
RRC ARotates bits in the accumulator right through carry11

### Bit-oriented Instructions

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

BIT-ORIENTED INSTRUCTIONS
MnemonicDescriptionByteCycle
CLR CClears the carry flag11
CLR bitClears the direct bit23
SETB CSets the carry flag11
SETB bitSets the direct bit23
CPL CComplements the carry flag11
CPL bitComplements the direct bit23
ANL C,bitAND direct bit to the carry flag22
ANL C,/bitAND complements of direct bit to the carry flag22
ORL C,bitOR direct bit to the carry flag22
ORL C,/bitOR complements of direct bit to the carry flag22
MOV C,bitMoves the direct bit to the carry flag22
MOV bit,CMoves the carry flag to the direct bit23

## References

Tags:
Created by Sujit Wagh on 2017/08/19 12:35

Happy WikiNoting!