Explanation of The Following 8051 Instructions With Examples
Explanation of The Following 8051 Instructions With Examples
Result: A = 45H
MOV DPTR, #3000H ; Load external memory address 3000H into DPTR
MOV A, #55H ; Load 55H into accumulator
MOVX @DPTR, A ; Store 55H at external memory address 3000H
1
(iv) XCH A, R1
Instruction: XCH A, R1
Explanation: This instruction exchanges the contents of the accumulator (A) with register R1.
Example:
(v) RLC A
Instruction: RLC A
Explanation: This instruction rotates the accumulator (A) left through the carry flag (CY).
The MSB (bit 7) is moved into CY, and CY is moved into the LSB (bit 0).
Example:
(VI) XCH A, R2
Function: This instruction exchanges the contents of the accumulator (A) with register R2.
Example:
MOV A, #12H ; Load 12H into A
MOV R2, #34H ; Load 34H into R2
XCH A, R2 ; Swap values of A and R2
Function: Adds the immediate value 23H to the accumulator (A) and stores the result in A.
Example:
MOV A, #15H ; Load 15H into A
2
ADD A, #23H ; Add 23H to A
Function: Loads the DPTR (Data Pointer Register) with the immediate 16-bit address
(9000H). This instruction is useful when working with external memory.
Example:
MOV DPTR, #9000H ; Load DPTR with the address 9000H
(IX) SUBB A, R2
Function: Subtracts the value in register R2 from the accumulator (A), including the
borrow (CY flag).
Example:
MOV A, #40H ; Load 40H into accumulator
MOV R2, #10H ; Load 10H into R2
CLR C ; Clear carry to ensure no borrow
SUBB A, R2 ; A = A - R2 - CY
Function: Rotates the accumulator (A) right through the carry flag (CY). The LSB
(bit 0) moves into CY, and CY moves into MSB (bit 7).
Example:
MOV A, #8BH ; Load A with 8BH (10001011B)
CLR C ; Clear carry flag (CY = 0)
RRC A ; Rotate right through carry
3
Function: Adds the value stored at the internal RAM location pointed by register R0
to the accumulator (A).
Example:
MOV R0, #30H ; Load address 30H into R0
MOV A, #25H ; Load 25H into accumulator
MOV 30H, #15H ; Store 15H at RAM location 30H
ADD A, @R0 ; A = A + (value at address in R0)
Function: Adds the immediate value 23H to the accumulator (A) along with the carry
(CY) flag.
Example:
MOV A, #50H ; Load A with 50H
SETB C ; Set carry flag (CY = 1)
ADDC A, #23H ; A = A + 23H + CY
Function: Rotates the accumulator (A) left through the carry flag (CY). The MSB
(bit 7) moves into CY, and CY moves into LSB (bit 0).
Example:
MOV A, #85H ; Load A with 85H (10000101B)
CLR C ; Clear carry flag (CY = 0)
RLC A ; Rotate left through carry
(iii) XCH A, R1
Function: Exchanges the contents of the accumulator (A) with register R1.
Example:
MOV A, #12H ; Load A with 12H
MOV R1, #34H ; Load R1 with 34H
XCH A, R1 ; Swap values of A and R1
4
5
(b) Comparison of Instruction Pairs
Instruction Function
Copies the contents of memory location 20H into memory location 10H. (Indirect
MOV 10H, 20H
move)
MOV 20H, #20 Loads the immediate value 20H into memory location 20H.
Example:
Instruction Function
MOV R1, A Copies the value from accumulator (A) to register R1.
MOV @R1, A Stores the value of accumulator (A) at the internal RAM address pointed by R1.
Example:
6
(i) MOV 15H, 20H vs. MOV 15H, #20H
Instruction Function
Copies the contents of memory location 20H into memory location 15H.
MOV 15H, 20H
(Indirect move)
MOV 15H,
Loads the immediate value 20H into memory location 15H.
#20H
Example:
Instruction Function
MOV R1, A Copies the value from accumulator (A) to register R1.
Stores the value of accumulator (A) at the internal RAM address pointed by
MOV @R1, A
R1.
Example: