Instructions are all 1 to 3 bytes long, consisting of an initial opcode byte, followed by up to 2 bytes of operands. of the opcode bytes, '''
x0–
x3''', are used for irregular opcodes. of the opcode bytes, '''
x4–
xF''', are assigned to 16 basic ALU instructions with 12 possible operands. The least significant
nibble of the opcode selects the primary operand as follows: • '''
x8–
xF''': Register direct, R0–R7. • '''
x6–
x7''': Register indirect, or . • '''
x5''': Memory direct , a following byte specifies an IRAM or SFR location. • '''
x4'
: Immediate , a following byte specifies an 8-bit constant. When the operand is a destination (, ) or'' the operation already includes an immediate source (, ), this instead specifies that the accumulator is used. The three
bitwise operations ANL. ORL and XRL, which normally operate on the accumulator, also support two memory-destination modes: • '''
x3''': • '''
x2''': The most significant nibble specifies the operation as follows. Not all support all addressing modes; the immediate mode, in particular, is unavailable when the primary operand is written to, so is only applicable to six operations, and encodes an accumulator destination in four more. Instruction mnemonics use
destination,
source operand order. ; 0
y : Increment the specified operand. Immediate mode (opcode 0x04) specifies the accumulator, . ; 1
y : Decrement the specified operand. Immediate mode (opcode 0x14) specifies the accumulator, . ; 2
y : Add the operand to the accumulator, A. Opcode 0x23 (, "rotate left" but actually a
shift left) may be thought of as . ; 3
y : Add the operand, plus the C bit, to the accumulator. Opcode 0x33 (, rotate left through carry) may be thought of as . ; 4
y : Logical OR the operand into the accumulator. Two memory-destination forms of this operation, and , are specified by opcodes 0x43 and 0x42. ; 5
y : Logical AND the operand into the accumulator. Two memory-destination forms of this operation, and , are specified by opcodes 0x53 and 0x52. ; 6
y : Logical exclusive-OR the operand into the accumulator. Two memory-destination forms of this operation, and , are specified by opcodes 0x63 and 0x62. ; 7
y : Move immediate to the operand. Immediate mode (opcode 0x74) specifies the accumulator, . ; 8
y : Move value to an IRAM or SFR register. Memory direct mode (opcode 0x85) moves directly between two IRAM or SFR registers, . Immediate mode (opcode 0x84) is not used for this operation, as it duplicates opcode 0x75. ; 9
y : Subtract the operand and the carry flag from the accumulator. This operation borrows and there is no subtract
without borrow. ; A
y : Move value from an IRAM or SFR register. Memory direct mode (opcode 0xA5) is not used, as it duplicates 0x85. Immediate/accumulator mode (opcode 0xA4) is not used, as it duplicates 0xE5. ; B
y : Compare
operand to the immediate , and jump to if not equal. Because this instruction already has the maximum possible two bytes of operand, immediate and memory direct modes (opcodes 0xB4 and 0xB5) omit the immediate and instead compare the operand against the accumulator, amd . Note that there is no compare and jump if equal instruction, . ; C
y : Exchange the accumulator and the operand. Immediate/accumulator mode (opcode 0xC4) is not used for this operation, as would have no effect. ; D
y : Decrement the operand, and jump to if the result is non-zero. Immediate mode (opcode 0xD4), and register indirect mode (0xD6, 0xD7) are not used. ; E
y : Move operand to the accumulator. Immediate mode is not used for this operation (opcode 0xE4), as it duplicates opcode 0x74. ; F
y : Move accumulator to the operand. Immediate/accumulator mode (opcode 0xF4) is not used, as would have no effect. Only the , , and instructions set PSW flags. The , , and logical instructions do not. The instruction modifies the C bit only, to the borrow that results from . The irregular instructions have more limited addressing modes, and are encoded in the range '''
x0–
x3
, plus a few opcodes in the x4–
x7''' range not used by the regular instructions. Additional irregular instructions: ; A5:
Unused by the 8051, but used for extended opcodes by successors such as the . ; D6–7: exchange low-order nibble of operands. The (short jump) opcode takes a signed relative offset byte operand and transfers control there relative to the address of the following instruction. The / opcodes combine the three most significant bits of the opcode byte with the following byte to specify an 11-bit destination that is used to replace the low 11 bits of the PC register (the top 5 bits of the PC register remain intact, so this can only jump within a 2048-byte page). For larger distances, the and instructions allow a 16-bit destination. One of the reasons for the 8051's popularity is its range of operations on single bits. Bits are always specified by absolute addresses; there is no register-indirect or indexed addressing. Instructions that operate on single bits are: • , , : Set, clear, or complement the specified bit • : Jump (to PC+
offset) if bit set • : Jump if bit clear • : Jump if bit set, and clear bit • , : Move the specified bit to the carry bit, or vice versa • , : Or the bit (or its complement) into the carry bit • , : And the bit (or its complement) into the carry bit A bit operand is written in the form . Because the carry flag is bit 7 of the bit-addressable program status word, the , and instructions are shorter equivalents to , and . == Programming ==