COUNTERS
HEXA DECIMAL UP COUNTER
Assembly Code Description / Comment
Set the origin (starting address) of the program at memory
ORG 0000H
location 0000H.
MOV A,#00H Load the accumulator with 00H (start counting from 0).
BACK: MOV P0,A Output the value of the accumulator to Port 0 (display count).
ACALL DELAY Call the delay subroutine to create a time gap between counts.
INC A Increment the accumulator by 1 (A = A + 1).
Jump to label BACK if A ≠ 0 (continue counting until A rolls over to
JNZ BACK
00H).
Infinite loop to stop the program when the counter completes (A
HERE: SJMP HERE
= 00H again).
DELAY: MOV
Load register R1 with FFH for outer delay loop.
R1,#0FFH
LOOP1: MOV
Load register R2 with FFH for inner delay loop.
R2,#0FFH
LOOP: DJNZ
Decrement R2 and jump to LOOP until R2 = 0 (inner delay loop).
R2,LOOP
Decrement R1 and repeat inner loop until R1 = 0 (outer delay
DJNZ R1,LOOP1
loop).
RET Return from the delay subroutine to main program.
END End of the program.
HEXA DECIMAL DOWN COUNTER
Assembly Code Description / Comment
Set the origin (starting address) of the program at memory
ORG 0000H
location 0000H.
Load the accumulator with FFH (255 in decimal). The counter
MOV A,#0FFH
starts from FFH.
Send the value of accumulator A to Port 0 — output the current
BACK: MOV P0,A
count.
DEC A Decrement the accumulator by 1 (A = A - 1).
Call the delay subroutine to create a visible time gap between
ACALL DELAY
counts.
JNZ BACK Jump to BACK if A ≠ 0. Continue counting down until A = 00H.
DELAY: MOV
Load register R1 with FFH for the outer delay loop.
R1,#0FFH
LOOP1: MOV
Load register R2 with FFH for the inner delay loop.
R2,#0FFH
LOOP: DJNZ
Decrement R2 and jump to LOOP until R2 = 0 (inner loop).
R2,LOOP
DJNZ R1,LOOP1 Decrement R1 and repeat the inner loop until R1 = 0 (outer loop).
RET Return from the delay subroutine to the main program.
END End of the program.
DECIMAL UP COUNTER
Assembly Code Description / Comment
ORG 0000H Set the program starting address at 0000H.
MOV A,#00H Load accumulator A with 00H — counter starts from 00H.
BACK: MOV P0,A Send the accumulator value to Port 0 — display the count.
ACALL DELAY Call the delay subroutine to create a visible delay between counts.
ADD A,#01H Add 1 to the accumulator (A = A + 1).
Decimal Adjust — converts the binary result in A to valid BCD (for
DA A
proper decimal display on 7-segment or display).
JNZ BACK Jump to BACK if A ≠ 0, continue counting until overflow (A = 00H).
DELAY: MOV
Load R1 with FFH — outer delay loop counter.
R1,#0FFH
LOOP1: MOV
Load R2 with FFH — inner delay loop counter.
R2,#0FFH
LOOP: DJNZ
Decrement R2 and loop until R2 = 0 (inner loop).
R2,LOOP
DJNZ R1,LOOP1 Decrement R1 and repeat the inner loop until R1 = 0 (outer loop).
RET Return from delay subroutine to main program.
END End of program.
DECIMAL DOWN COUNTER
Assembly Code Description / Comment
ORG 0000H Set program start address at 0000H.
MOV A,#99H Load accumulator with 99 (starting value).
BACK: MOV P0,A Send the current value of A to Port 0 — display count.
ACALL DELAY Call delay subroutine for visible counting.
SUBB A,#01H Subtract 1 from A (A = A - 1).
JNC BACK Jump back if no borrow (continue until A = 00H).
SJMP $ Stay here (stop counting when A underflows).
DELAY: MOV R1,#0FFH Load R1 with FFH (outer loop for delay).
LOOP1: MOV R2,#0FFH Load R2 with FFH (inner loop).
LOOP: DJNZ R2,LOOP Decrement R2 until 0.
DJNZ R1,LOOP1 Decrement R1 until 0.
RET Return from delay subroutine.
END End of program.
DATA TRANSFER
BLOCK DATA TRANSFER
Assembly Instruction Description
ORG 0000H Set program starting address to 0000H
MOV DPH,#25H Set high byte of DPTR to 25H for external memory access
MOV R0,#35H Load starting source address offset
MOV R1,#50H Load starting destination address offset
MOV R3,#0AH Set loop counter (number of bytes to transfer)
LOOP: MOV DPL,R0 Load low byte of DPTR with current source address
MOVX A,@DPTR Read a byte from external source memory into accumulator
MOV DPL,R1 Load low byte of DPTR with current destination address
MOVX @DPTR,A Write byte from accumulator to external destination memory
INC R0 Increment source address
INC R1 Increment destination address
DJNZ R3, LOOP Decrement loop counter and repeat if not zero
END End of program
BLOCK EXCHANGE
Assembly Instruction Description
ORG 0000H Set program starting address to 0000H
MOV R3,#05H Set loop counter (number of bytes to exchange)
MOV R0,#30H Load starting address of first memory block
MOV R1,#40H Load starting address of second memory block
REPEAT: MOV A,@R0 Load a byte from the first block into accumulator
XCH A,@R1 Exchange accumulator with byte from second block
MOV @R0,A Store original second block byte into first block
INC R0 Increment first block address
INC R1 Increment second block address
DJNZ R3,REPEAT Decrement loop counter and repeat until zero
END End of program
SORTING
Assembly Instruction Description
ORG 0000H Set program starting address to 0000H
MOV R0,#05 Set outer loop counter (number of passes)
L1: MOV DPTR,#9000H Initialize DPTR to starting address of data block
MOV A,R0 Load outer loop counter into accumulator
MOV R1,A Copy to R1 for inner loop counter
L2: MOVX A,@DPTR Read first byte of the pair from external memory
MOV B,A Store in B temporarily
INC DPTR Move to next byte
MOVX A,@DPTR Read second byte of the pair
CLR C Clear carry for subtraction
MOV R2,A Store second byte in R2 temporarily
SUBB A,B Compare second byte with first byte
JNC L3 If A ≥ B, no swap needed (ascending)
JC (for descending) If descending, swap if needed
MOV A,B Prepare first byte for writing
MOVX @DPTR,A Write first byte to second location
DEC DPL Adjust pointer back to first location
MOV A,R2 Load second byte to accumulator
MOVX @DPTR,A Write second byte to first location
INC DPTR Move DPTR to next pair
L3: DJNZ R1,L2 Decrement inner loop counter and repeat inner loop
DJNZ R0,L1 Decrement outer loop counter and repeat outer loop
END End of program
ARITHEMATICS
ARITHEMATIC OPERATIONS
Assembly Instruction Description
ORG 0000H Start of program
MOV R1,40H Load operation code from memory location 40H into R1
MOV A,41H Load first number (operand 1) into accumulator
MOV B,42H Load second number (operand 2) into register B
If R1 ≠ 00, jump to subtraction check. If R1 = 00 → do
CJNE R1,#00,CKSUB
addition
ADD A,B Perform addition: A = A + B
MOV B,#00 Clear B (default, no carry)
JNC SKIP If no carry, skip next step
MOV B,#01H If carry occurs, set B = 01H (to indicate overflow)
SKIP: SJMP LAST Jump to end after addition
CKSUB: CJNE
If R1 ≠ 01, go check for multiplication
R1,#01,CKMUL
CLR C Clear carry before subtraction
SUBB A,B Perform subtraction: A = A – B
MOV B,#00 Clear B (default)
JNC SKIP1 If no borrow, skip next step
MOV B,#0FFH If borrow occurred, set B = FFH (negative indicator)
SKIP1: SJMP LAST Jump to end after subtraction
CKMUL: CJNE
If R1 ≠ 02, check for division
R1,#02,CKDIV
MUL AB Perform multiplication: A × B → result in A (low), B (high)
SJMP LAST Jump to end after multiplication
CKDIV: CJNE R1,#03,OTHER If R1 ≠ 03, go to OTHER
Assembly Instruction Description
DIV AB Perform division: A ÷ B → quotient in A, remainder in B
SJMP LAST Jump to end after division
OTHER: MOV A,#00 If operation code is not 0–3, make result 0
MOV B,#00 Clear B as well
LAST: MOV 43H,A Store final result (A) in memory 43H
MOV 44H,B Store overflow/borrow/remainder (B) in memory 44H
END End of program
SQUARE ROOT
Instruction Description
ORG 0000H Set the program starting address at 0000H.
MOV
Load DPTR with external memory address 9000H.
DPTR,#9000H
Move the data from external memory location 9000H into the
MOVX A,@DPTR
accumulator A. (This value is the input index.)
MOV Load DPTR with the base address of the lookup table stored in
DPTR,#0050H program memory.
MOVC Use the value in A as an offset and fetch the corresponding data byte
A,@A+DPTR from program memory starting at address 8050H.
MOV Load DPTR with external memory address 9001H, where the result
DPTR,#9001H will be stored.
MOVX @DPTR,A Store the fetched data (result) into external memory location 9001H.
ORG 0050H Define the start of the lookup table at address 8050H.
DB 00H Store data byte 00H at 8050H.
DB 01H Store data byte 01H at 8051H.
DB 04H Store data byte 04H at 8052H.
DB 09H Store data byte 09H at 8053H.
DB 10H Store data byte 10H at 8054H.
DB 19H Store data byte 19H at 8055H.
DB 24H Store data byte 24H at 8056H.
DB 31H Store data byte 31H at 8057H.
DB 40H Store data byte 40H at 8058H.
DB 51H Store data byte 51H at 8059H.
DB 64H Store data byte 64H at 805AH.
DB 79H Store data byte 79H at 805BH.
DB 90H Store data byte 90H at 805CH.
DB 0A9H Store data byte 0A9H at 805DH.
Instruction Description
DB 0C4H Store data byte 0C4H at 805EH.
DB 0E1H Store data byte 0E1H at 805FH.
END End of the program.
LOGICAL OPERATIONS
BYTE LEVEL
Instruction Description
ORG 0000H Set the program starting address at 0000H.
MOV A,20H Move the data from memory location 20H into accumulator A.
Copy the value of A (from 20H) into register R1. (This value
MOV R1,A
decides which logical operation to perform.)
Load the accumulator A with the contents of memory location
MOV A,21H
21H (first operand).
Compare R1 with 00H. If not equal, jump to label CKOR; if
CJNE R1,#0,CKOR
equal, perform AND operation.
Perform bitwise AND between A (from 21H) and the contents of
ANL A,22H
22H. Result stored in A.
SJMP END1 Jump to END1 (skip remaining checks).
CKOR: CJNE Compare R1 with 01H. If not equal, jump to CKXOR; if equal,
R1,#01,CKXOR perform OR operation.
Perform bitwise OR between A (from 21H) and contents of 22H.
ORL A,22H
Result stored in A.
SJMP END1 Jump to END1.
CKXOR: CJNE Compare R1 with 02H. If not equal, jump to OTHER; if equal,
R1,#02,OTHER perform XOR operation.
Perform bitwise XOR between A (from 21H) and contents of
XRL A,22H
22H. Result stored in A.
SJMP END1 Jump to END1.
OTHER: CLR A If R1 is not 0, 1, or 2, clear accumulator A (make A = 00H).
Store the final result from accumulator A into memory location
END1: MOV 23H,A
23H.
END End of the program.
BIT LEVEL
Instruction Description
ORG 0000H Set the starting address of the program at 0000H.
Move the data from memory location 20H into register R0. (This value
MOV R0,20H
decides which bit operation to perform.)
Compare R0 with 00H. If not equal, jump to label CK1; if equal,
CJNE R0,#0,CK1
perform AND operation.
MOV C,08H Move the bit at address 08H into the carry flag.
Perform bitwise AND between carry flag (C) and bit 10H. Result
ANL C,10H
stored in C.
SJMP LAST Jump to LAST to skip remaining checks.
CK1: CJNE Compare R0 with 01H. If not equal, jump to CK2; if equal, perform
R0,#1,CK2 OR operation.
MOV C,0FH Move the bit at address 0FH into carry flag C.
ORL C,17H Perform bitwise OR between C and bit 17H. Result stored in C.
SJMP LAST Jump to LAST.
CK2: CJNE Compare R0 with 02H. If not equal, jump to CK3; if equal, perform
R0,#2,CK3 NOT operation.
CPL 0FH Complement (invert) the bit at address 0FH.
MOV C,0FH Move the new value of bit 0FH into carry flag C.
SJMP LAST Jump to LAST.
CK3: CLR C Clear the carry flag if R0 is not 0, 1, or 2.
LAST: MOV
Move the final result (carry flag C) into bit address 1FH.
1FH,C
END End of the program.