8051/8951 microcontroller Instruction Set

MOV dest-byte, src-byte

Function: Move byte variable

Flags: None

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.

There are fifteen possible combinations for this instruction. They are as follows:

(a) Register A as the destination. This can have the following formats.

1. MOV A, #data         Example:    MOV A,#10H
2. MOV A, Rn             Example:    MOV A,R5
3. MOV A, direct         Example:    MOV A,50H
4. MOV A, @Ri           Example:    MOV A,@R0

(b) Register A is the source. This can have the following formats.

1. MoV Rn, A
2. MOV direct, A
3. MOV @Ri, A

(c) Rn is the destination

1. MOV Rn, #immediate
2. MOV Rn, A
3. MOV Rn, direct

(d) The destination direct address

1. MOV direct, #data
2. MOV direct, @ri
3. MOV direct, A
4. MOV direct, Rn

(e) Destination is an indirect address held by R0 and R1.

1. MOV @Ri, #data
2. MOV @Ri, A
3. MOV @Ri, direct

MOV dest-bit, src-bit

Function: Move bit data

Flags: None

Description: The Boolean variable indicated by the second operand is copied 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.2,C            ;copy carry bit to port bit P1.2

MOV DPTR, #data16

Function: Load data pointer with a 16-bit constant

Flags: None

Description: The data pointer is loaded 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 low-order byte. This is the only instruction which moves 16 bits of data at once.

Example:           MOV DPTR, #434FH        ;DPTR=434FH

MOVC A, @A + base-reg

Function: Move code byte

Flags: None

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 eight-bit accumulator contents and the contents of a sixteen-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 to 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.

Example:         MOVC A, @A + DPTR          ;mov data at A+DPTR into A

MOVC A, @A + PC               ;mov data at A+PC into A

MOVX dest-byte, src-byte

Function: Move external

Flags: None

Description: The MOVX instructions transfer data between the accumulator and a byte of external data memory, hence the ”X” appended to MOV. This instruction allows us to access externally connected memory. There are two types of instructions, differing in whether they provide an eight bit or sixteen-bit indirect address to the external data RAM as explained bellow.

(a) The 16-bit external memory address is held by the DPTR register.

MOVX A,@DPTR

This moves into the accumulator a byte from external memory whose address is pointed by DPTR.

MOVX @DPTR, A

This moves the content of accumulator to the external memory location whose address is held  by DPTR.

(b) The 8-bit address of external memory is held by R0 or R1

MOVX A,@Ri

This moves to the accumulator a byte from external memory whose 8-bit address is pointed by R0 or R1.

MOVX @Ri,A

This moves a byte from register A to an external memory whose 8-bit address is held  by R0 or R1.

MUL AB

Function: Multiply

Flags: OV, CY

Description: MUL AB multiplies the unsigned eight-bit integers in the accumulator and register B. The low-order byte of the sixteen-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:      MOV A,#5
MOV B,#7
MUL AB

Result:          A=35=23H, B=0

NOP

Function: No operation

Flags: None

This performs no operation and execution continues with the next instruction. It is sometimes used for timing delays to waste clock cycles. This instruction only updates the PC to point to the next instruction following NOP.

ORL dest-byte, src-byte

Function: Logical OR for byte variables

Flags: None

Description: ORL performs the bitwise logical OR operation between the indicated variables, storing the results in the destination byte.

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.

Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins.

Example: If the accumulator holds 0C3H (11000011B) and R0 holds 55H (01010101B) then the instruction ORL A,R0 will leave the accumulator holding the value 0D7H (11010111B).

Different types of addressing modes are as follows.

1. Immediate:         ORL A, #data

Eample : ORL A,#30h

2. Register:             ORL A, Rn

Example : ORL A, R5

3. Direct:               ORL A,direct

Example : ORL A,20H     ; OR A with data in RAM location 20H

4. Register-indirect: ORL A, @Ri

Example : ORL A,@R1   ; OR A with data pointed by R0

ORL C, src-bit

Function: Logical OR for bit variables

Flags: CY

Description: In this instruction the carry flag bit is ANDed with a source bit and the result is placed in carry. If the Boolean value of the source bit is a logic 0 then clear the carry flag; otherwise leave the carry flag in its current state. Only direct bit addressing is allowed for the source operand. 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.

POP direct

Function: Pop from stack

Flags: None

Description: This copies the byte pointed by SP to the location whose direct address indicated, and decrements SP by 1. Notice that this instruction supports only direct addressing mode. Therefore, instruction such as POP A, POP R3 is illegal.

Example:   POP 0E0H            Where E0H is the RAM address belonging to Register A.

Operation:
(direct) ← ((SP))

(SP) ← (SP) – 1

PUSH direct

Function: Push onto stack

Flags: None

Description: This copies the indicated byte onto the stack increments SP by 1. Notice that this instruction supports only direct addressing mode. Therefore, instruction such as PUSH A, PUSH R3 is illegal.

Example:    PUSH 0E0H          Where E0H is the RAM address belonging to Register A.

Operation:

(SP) ← (SP) + 1

((SP)) ← (direct)

next page

RET

Function: Return from subroutine

Flags: None

Description: This instruction is used to return from a subroutine. RET pops the high and low-order bytes of the PC successively from the stack, decrementing the stack pointer by two. Program execution continues at the resulting address, generally the instruction immediately following an ACALL or LCALL.

RETI

Function: Return from interrupt

Flags: None

Description: This instruction is used to return from an interrupt service routine. RETI pops the high and low-order bytes of the PC successively from the stack, and restores the interrupt logic to accept additional interrupts at the same priority level as the one just processed. The stack pointer is left decremented by two. The PSW is not automatically restored to its pre-interrupt status. Program execution continues at the resulting address, which is generally the instruction immediately after the point at which the interrupt request was detected. If a lower or same-level interrupt is pending when the RETI instruction is executed, that one instruction will be executed before the pending interrupt is processed.

RL A

Function: Rotate accumulator left

Flags: None

Description: The eight bits in the accumulator are rotated one bit to the left. Bit 7 is rotated into the bit 0 position.

Example: The accumulator holds the value 0C5H (11000101B). The instruction RL A leaves the accumulator holding the value 8BH (10001011B) with the carry unaffected.

RLC A

Function: Rotate accumulator left through carry flag

Flags: CY

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.

Example: The accumulator holds the value 0C5H (11000101B), and the carry is zero. The instruction

RLC A leaves the accumulator holding the value 8AH (10001010B) with the carry set.

RR A

Function: Rotate accumulator right

Flags: None

Description: The eight bits in the accumulator are rotated one bit to the right. Bit 0 is rotated into the bit 7 position.

Example: The accumulator holds the value 0C5H (11000101B). The instruction RR A leaves the accumulator holding the value 0E2H (11100010B) with the carry unaffected.

RRC A

Function: Rotate accumulator right through carry flag

Flags: CY

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.

Example: The accumulator holds the value 0C5H (11000101B), the carry is zero. The instruction

RRC A leaves the accumulator holding the value 62H (01100010B) with the carry set.

SETB bit

Function: Set bit

Description: SETB sets the indicated bit to one. SETB can operate on the carry flag or any

directiy addressable bit of a port, register, or RAM location.

Example:         SETB P1.2
SETB ACC.2
SETB C                   ;carry  flag CY=1

SJMP target

Function: Short jump

Flags: None

Description: Program control branches unconditionally to the address indicated. The branch destination is computed by adding the signed displacement in the second instruction byte to the PC, after incrementing the PC twice. Therefore, the range of destinations allowed is from 128 bytes preceding this instruction to 127 bytes following it.

Example: The label ”RELADR” is assigned to an instruction at program memory location 0123H. The instruction SJMP RELADR will assemble into location 0100H. After the instruction is executed, the PC will contain the value 0123H.

SUBB A, src-byte

Function: Subtract with borrow

Flags: OV, AC, CY

Description: SUBB subtracts the src-byte and the carry flag together from the accumulator, leaving the result in the accumulator. The steps for subtraction perform by the internal hardware of the CPU are as follows:

1. Take 2’s complement of the src-byte.
2. Add this to register A.
3. Invert the carry.

This instruction sets the carry flag according to the following.

CY

If  dest>source          0             The result is positive
If  dest=source          0             The result is 0
If  dest<source          1             The result is negative in 2’s compliment

Different types of addressing modes are as follows.

1. Immediate:         SUBB A, #data

Eample : SUBB A,#30h    ; A = A – 30H – CY

2. Register:             SUBB A, Rn

Example : SUBB A, R5     ; A = A – R5 – CY

3. Direct:               SUBB A,direct

Example : SUBB A,20H    ; A – data at (20H) – CY

4. Register-indirect: SUBB A, @Ri

Example : SUBB A,@R1   ; A – data at (R1) – CY

SWAP A

Function: Swap nibbles within the accumulator

Flags: None

Description: SWAP A interchanges the low and high-order nibbles (four-bit fields) of the accumulator (bits 3-0 and bits 7-4). The operation can also be thought of as a four bit rotate instruction.

Example:               MOV A, #25H
SWAP A

Result:                  A = 52H

XCH A, byte

Function: Exchange accumulator with byte variable

Flags: None

Description: This instruction swaps the content of register A and the source byte. The source byte can be any register or RAM location.

Example:              MOV A, #49H           ; A = 49H
MOV R2, #30H          ; R2 = 30H
XCH A,R2

Result:                 A=30H, R2=49H

Different types of addressing modes are as follows.

1. Register:             XCH A, Rn

Example : XCH A, R5

2. Direct:               XCH A, direct

Example : XCH A, 20H

3. Register-indirect: XCH A, @Ri

Example : XCH A,@R1

XCHD A,@Ri

Function: Exchange digit

Flags: None

Description: XCHD exchanges the low-order nibble of the accumulator (bits 3-0), with that of the internal RAM location indirectly addressed by the specified register. The high-order nibbles (bits 7-4) of each register are not affected.

Example:  R0 contains the address 20H. The accumulator holds the value 36H. Internal RAM location 20H holds the value 75H. The instruction XCHD A, @ R0 will leave RAM location 20H holding the value 76H and 35H in the accumulator.

XRL dest-byte, src-byte

Function: Logical Exclusive OR for byte variables

Flags: None

Description: XRL performs the bitwise logical Exclusive OR operation between the indicated variables, storing the results in the destination.

Example:               MOV A, #28H
XRL A, #08H

Result:                  A = 20H

For XRL instruction there are total of 6 addressing modes. In four of them the accumulator must be the destination. They are as follows:

1. Immediate:         XRL A, #data

Eample : XRL A,#30h

2. Register:             XRL A, Rn

Example : XRL A, R5

3. Direct:               XRL A,direct

Example : XRL A,20H     ; XOR A with data in RAM location 20H

4. Register-indirect: XRL A, @Ri

Example : XRL A,@R1   ; XOR A with data pointed by R0

In the next two addressing modes the destination is direct address while the source is either A or immediate data.

5. XRL direct,A

Example : XRL 20H, A

6. XRL direct, #data

Example : XRL 40H, #30H

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

21 thoughts on “8051/8951 microcontroller Instruction Set

  1. Highly impressed, discovered your post on Ask.Happy I finally tried it out. Not sure if its my Chrome browser,but sometimes when I visit your site, the fonts are really small? Anyway, love your blog and will check back.Bye

  2. hi im trying to design a circuit in which stepper motor is used to
    control a five nozzle fountain(college project) so i know how to program
    the motors …but i don’t know how to use a microcontrolller to
    interpret various segment of music and then interface it with a
    stepper motor simultaneously to get a
    musical fountain of sorts….can you help me out?
    thanks.

    P.S: i personally prefer 8051 but they say its outdated …
    what are my best alternatives…i know assembly language quite well.

  3. hey Tim, my combo of 10.6.8 and chrome and flash plaeyr doesnt like your site. Every time ive come here in the last week the Flash plaeyr has crashed.. Its fine everywhere else , but within 40 sec of getting here its over

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in