Experiment-3
Examples
Example-1
; SUM OF TWO-DIGIT BCD NUMBERS IN XX40 TO XX49.
; STORE RESULT IN R0, R1 AND IN XX50H, XX51H.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV R1, #00H ; CARRY REGISTER
MOV R3, #10 ; LENGTH OF ARRAY WITH DECIMAL 10
MOV R0, #00H ; INITIALISE THE SUM
MOV DPTR, #1040H ; DATA LOCATION STARTING ADDRESS
UP:
MOVX A, @DPTR ; GET DATA
ADD A, R0 ; APPEND TO THE SUM
DA A ; DECIMALLY ADJUST ACCUMULATOR SINCE DEALING WITH BCD DATA
MOV R0, A ; UPDATE SUM REGISTER
JNC DOWN
; IF CARRY EXECUTE BELOW
MOV A, #00H
ADDC A, R1
DA A ; ACCUMULATE CARRY DECIMALLY
MOV R1, A ; UPDATE CARRY REGISTER
DOWN:
INC DPTR ; GET THE NEXT DATA ADDRESS
DJNZ R3, UP ; REPEAT (R3) TIMES
MOV A, R0 ; GET THE FINAL SUM
MOV DPTR, #1050H
MOVX @DPTR, A ; COPY INTO MEMORY
INC DPTR
MOV A, R1 ; GET THE FINAL CARRY
MOVX @DPTR, A ; APPEND THE CARRY
HERE:
SJMP HERE ; LOGICAL END
END
Example-2
; FIND LARGEST NUMBER IN A MEMORY ARRAY. ARRAY BEGNIS AT 40H, WITH LENGTH STORED AT 30H
; STORE THE RESULT IN XX60H
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV R0, #30H ; LENGTH OF THE ARRAY
MOV A, @R0
MOV R7, A ; STORE LENGTH IN (R7)
DEC R7 ; [N-1] COMPARISIONS REQUIRED
MOV R1, #40H ; STARTING LOCATION OF THE DATA
MOV A, @R1 ; GET DATA CONTENT
UP:
INC R1 ; MOVE TO NEXT POINTER
CLR C ; CLEAR CARRY
MOV B, A ; DUPLICATE IN B
SUBB A, @R1 ; SUBTRACT WITH THE NEXT POINTERS DATA
JNC DOWN1 ; NO CARRY => A IS GREATER THAN NEXT POINTERS DATA
MOV A, @R1 ; UPDATE THE CURRENT LARGEST IN (R0)
SJMP DOWN ; SKIP (DOWN1)
DOWN1:
MOV A, B ; CARRY => NEXT POINTERS DATA IS GREATER THAN A
DOWN:
DJNZ R7, UP ; REPEAT (R7) TIMES
MOV DPTR, #0060H
MOVX @DPTR, A ; STORE THE FINAL RESULT
HERE:
SJMP HERE ; LOGICAL END
END
Example-3
; TRANSFER MEMORY CONTENTS STARTING FROM 50H TO 59H IN THE SAME SEQUENCE, TO LOCATION STARTING FROM 70H
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV R2, #0AH ; LENGTH OF THE ARRAYS TO BE DUPLICATED. I.E. DECIMAL 10.
MOV R0, #50H ; ADDRESS OF SOURCE
MOV R1, #70H ; ADDRESS OF DESTINATION
UP:
MOV A, @R0 ; GET SOURCE CONTENT
MOV @R1, A ; MOVE TO DESTINATION
; INCREMENT BOTH SOURCE AND DESTINATION DATA POINTERS
INC R0
INC R1
DJNZ R2, UP ; REPEAT (R2) TIMES. I.E. TILL THE LENGTH OF THE ARRAY
HERE:
SJMP HERE ; LOGICAL END
END
Example-4
; ARRANGE ELEMENTS OF MEMORY ARRAY IN ASCENDING ORDER
; [BUBBLE SORT]
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV A, #12 ; LENGTH OF ARRAY. CAN ENTER IN DECIMAL AS WELL.
DEC A ; [N-1] PASSES IN BUBBLE SORT
MOV 30H, A ; COPY LENGTH AT ADDRESS (30H). TO BE USED AS A FIXED DATA.
NXT_PSS:
; RELOAD (R7) AND (R6) WITH THE LENGTH OF THE ARRAY
MOV R7, 30H ; NOT REQUIRED?
MOV R6, 30H
MOV R0, #50H ; STARTING LOCATION OF THE ARRAY IN (R0). BUBBLING FROM LOWEST [FIXED] TO HIGHEST INDEX [VARYING].
NXT_CMP:
MOV A, @R0 ; GET DATA FROM THE ARRAY
MOV R3, A ; DUPLICATE THE DATA TO (R3)
INC R0 ; INCREMENT TO THE NEXT LOCATION IN THE ARRAY
MOV 0F0H, @R0 ; DUPLICATE NEXT CONTENTS TO REG. B
CLR C ; CLEAR THE CARRY. SO THAT A ISN'T DECREMENTED WITH THE BORROW.
SUBB A, B ; COMPARE A AND B
; CARRY => B IS GREATER THAN A. NO SWAP REQUIRED.
JC DOWN
; NO CARRY => A IS GREATER THAN B. SWAP REQUIRED.
MOV A, R3 ; (R3) HAD THE COPY OF THE ORIGINAL DATA BACKED UP, AT (R0)
MOV @R0, A ; BUBBLE IT UP. (R0) POINTS TO THE NEXT INDEX.
DEC R0 ; DECREMENT POINTER TO TEMPORARILY USE FOR THE SWAP
MOV @R0, 0F0H ; THE ORIGINAL CONTENTS OF THE LOWER NUMBER WERE IN (B)
INC R0 ; INCREMENT FOR NEXT PASS
DOWN:
DJNZ R6, NXT_CMP ; DO (R6) COMPARISIONS IN EACH PASS
DJNZ 30H, NXT_PSS ; (@30H) CONTAINS THE PASS COUNTER
HERE:
SJMP HERE ; LOGICAL END
END
Example-5
; CONVERT 8-BIT HEXADECIMAL TO DECIMAL
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV A, #0ABH ; INPUT DATA
MOV 0F0H, #10 ; DECIMAL 10 STORED IN B
DIV AB ; DIV INPUT BY DECIMAL 10
MOV R7, 0F0H ; REMINDER STORED IN (R7). THIS IS THE LOWER DECIMAL DIGIT
MOV B, #10 ; DECIMAL 10 STORED IN B AGAIN
DIV AB ; DIV [QUOTIENT] BY DECIMAL 10. (A) CONTAINED THE QUOTIENT FROM PREVIOUS STEP
MOV R6, 0F0H ; REMINDER STORED IN (R6). THIS IS THE MIDDLE DIGIT
MOV R5, A ; QUOTIENT STORED IN (R5). THIS IS THE UPPER DIGIT
HERE:
SJMP HERE ; LOGICAL END
END
Example-6
; CONVERT 2-DIGIT BCD IN LOCATION XX50 INTO HEXADECIMAL AND STORE THE HEXADECIMAL EQUIVALENT IN NEXT MEMORY LOCATION.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV DPTR, #1050H ; LOAD DATA LOCATION
MOVX A, @DPTR ; GET INPUT DATA. 2-DIGIT BCD IS ONE BYTE LONG
ANL A, #0FH ; MASK THE UPPER NIBBLE. RETURNS LOWER NIBBLE
MOV R0, A ; SAVE THE LOWER NIBBLE IN (R0)
MOVX A, @DPTR ; RESTORE INPUT DATA
ANL A, #0F0H ; MASK THE LOWER NIBBLE. RETURNS UPPER NIBBLE
CLR C ; CLEAR CARRY
; ROTATE RIGHT WITH CARRY 4 TIMES. EFFECTIVELY SWAPS LOWER AND UPPER NIBBLES, BUT WITH AN ADDITIONAL CARRY [NOT REQUIRED?]
RRC A
RRC A
RRC A
RRC A
; LOWER NIBBLE OF CURRENT (A) IS THE PREVIOUS UPPER NIBBLE
RLC A ; ROTATE LEFT WITH CARRY, ONCE. MULTIPLIES BY 2
MOV 0F0H, A ; SAVE IN B
; ROTATE LEFT WITH CARRY TWO MORE TIMES. MULTIPLIES BY 8 EFFECTIVELY, IN THE END
RLC A
RLC A
ADD A, B ; [x2] + [x8] => [x10] DECIMAL
ADD A, R0 ; ADD THE LOWER NIBBLE, ~HEXADECIMALLY. [WHY?]
; STORE IN NEXT LOCATION
INC DPTR
MOVX @DPTR, A
HERE:
SJMP HERE ; LOGICAL END
END
Example-7
; CONVERT SINGLE DIGIT DATA AVAILABLE IN XX50H INTO ASCII AND STORE IN THE NEXT LOCATION.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
; LOAD FROM DATA LOCATION
MOV DPTR, #1050H
MOVX A, @DPTR
CLR C ; CLEAR CARRY
MOV B, A ; DUPLICATE DATA TO B
SUBB A, #0AH ; CHECK WHETHER INPUT DATA IS >9
MOV A, B ; RESTORE A WITH THE ORIGINAL DATA. NON-DESTRUCTIVE SUBTRACTION
; CARRY => [0-9]
JC DOWN
; NO CARRY => [A-F]
ADD A, #07H ; ADD ADDITIONAL 07H OFFSET
DOWN:
ADD A, #30H ; ADD 30H BY DEFAULT
; STORE IN NEXT LOCATION
INC DPTR
MOVX @DPTR, A
HERE:
SJMP HERE ; LOGICAL END
END
Exercises
Exercise-1
; Find the sum of ten 16 –bit hexadecimal numbers available in memory
; starting from location XX10h. store the result in XX50h onwards.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV DPTR, #1010H ; STARTING LOCATION OF THE MEMORY
MOV R0, #00H ; LOWER SUM REGISTER; CLR (R0) DOESN'T WORK;
MOV R1, #00H ; HIGHER SUM REGISTER; CLR (R1) DOESN'T WORK;
MOV R2, #00H ; CARRY REGISTER; CLR (R2) DOESN'T WORK;
MOV R3, #10 ; NUMBER OF ELEMENTS, DECIMAL 10
ITER:
MOVX A, @DPTR ; GET THE LOWER BYTE OF THE DATA
MOV 0F0H, A ; DUPLICATE IT TO REG (B)
MOV A, R0 ; GET THE CURRENT SUM, LOWER BYTE.
ADD A, B ; ADD THE LOWER BYTE TO THE SUM, WITHOUT CARRY.
MOV R0, A ; SAVE THE LOWER BYTE OF SUM IN (R0)
INC DPTR ; GO TO NEXT LOCATION
MOVX A, @DPTR ; GET THE HIGHER BYTE OF THE DATA
MOV 0F0H, A ; DUPLICATE IT TO REG (B)
MOV A, R1 ; GET THE CURRENT SUM, HIGHER BYTE.
ADDC A, B ; ADD THE HIGHER BYTE, ALONG WITH THE CARRY.
MOV R1, A ; SAVE THE HIGHER BYTE OF SUM IN (R1)
MOV A, R2 ; GET THE CURRENT SUM, CARRY BYTE.
ADDC A, #00H ; INCREMENT CARRY REGISTER WITH THE CARRY.
INC DPTR ; GO TO NEXT LOCATION
DJNZ R3, ITER ; REPEAT TILL END OF ARRAY
STORE:
; STORE THE FINAL SUM IN THE DESIRED LOCATION
MOV DPTR, #1050H ; LOWER BYTE OF SUM
MOV A, R0
MOVX @DPTR, A
INC DPTR ; HIGHER BYTE OF SUM
MOV A, R1
MOVX @DPTR, A
INC DPTR ; CARRY BYTE OF SUM
MOV A, R2
MOVX @DPTR, A
HERE:
SJMP HERE ; LOGICAL END
END
Exercise-2
; Find the number of occurrences of data ‘2Ah’ in a memory array.
; The last element of the array is ‘3Ah’. The array begins at XX30h.
; Store the result in memory location XX80h.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV DPTR, #1030H ; THE START LOCATION OF THE ARRAY
MOV R0, #2AH ; THE VALUE TO BE COMPARED FOR EQUALITY
CLR C ; CLEAR THE CARRY
MOV R1, #00H ; COUNT REGISTER; CLR (R1) DOESN'T WORK;
REPEAT:
MOVX A, @DPTR ; GET THE DATA
SUBB A, R0 ; PERFORM A SUBTRACTION TO CHECK FOR EQUALITY
INC DPTR ; INCREMENT TO THE NEXT ARRAY POSITION
; IF THE DIFFERENCE IS NOT ZERO
JNZ CHECK
; IF THE DIFFERENCE IS ZERO, UPDATE COUNT AND PROCEED TO CHECK
INC R1
CHECK:
; CHECK WHETHER TO STOP
SUBB A, #10H ; SUBTRACT 10H TO CHECK FOR STOP CONDITION
; REPEAT THE PROCESS IF NOT REACHED THE STOP CONDITION
JNZ REPEAT
STORE:
; STOP AND STORE THE FINAL COUNT IN THE DESIRED LOCATION
MOV A, R1
MOV DPTR, #1080H
MOVX @DPTR, A
HERE:
SJMP HERE ; LOGICAL END
END
Exercise-2_2
; Find the number of occurrences of data ‘2AH’ in a memory array.
; The last element of the array is ‘3AH’. The array begins at XX30H.
; Store the result in memory location XX80H.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV DPTR, #1030H ; THE START LOCATION OF THE ARRAY
CLR C ; CLEAR THE CARRY
; INITIALISE WITH #0FFH SINCE UPDATE WILL BE RUN ONCE BY DEFAULT, AFTER WHICH IT'S 00H;
MOV R1, #0FFH ; COUNT REGISTER
UPDATE:
; INCREMENT THE COUNTER
INC R1
REPEAT:
MOVX A, @DPTR ; GET THE DATA
SUBB A, #2AH ; PERFORM A SUBTRACTION TO CHECK FOR #2AH
INC DPTR ; INCREMENT TO THE NEXT ARRAY POSITION
; IF THE DIFFERENCE IS ZERO => DATA IS ACTUALLY #2AH
JZ UPDATE
; IF THE DIFFERENCE IS NOT ZERO => CHECK WHETHER TO STOP
; SUBTRACT AN ADDITIONAL #10H TO CHECK FOR STOP CONDITION
SUBB A, #10H ; => EFFECTIVELY SUBTRACTING #3AH FROM DATA
; REPEAT THE PROCESS IF NOT REACHED THE STOP CONDITION
JNZ REPEAT
; STOP CONDITION IS MET; STORE THE FINAL COUNT IN THE DESIRED LOCATION
MOV A, R1
MOV DPTR, #1080H
MOVX @DPTR, A
HERE:
SJMP HERE ; LOGICAL END
END
Exercise-3
; Transfer ten elements of an array starting at location XX40h
; in external data memory to a location XX45 in the same memory
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV DPTR, #1049H ; GO TO LAST ELEMENT OF THE ARRAY
MOV R0, #10 ; NUMBER OF ELEMENTS, DECIMAL 10
MOV R1, #05H ; NUMBER OF ELEMENTS, DECIMAL 5, I.E. (XX49 - XX45 + 1)
REPEAT:
MOV A, R1 ; GET DISPLACEMENT
MOV R2, A ; COPY TO (R2)
MOVX A, @DPTR ; GET THE DATA TO ACCUMULATOR
; INCREMENT DPTR BY (R1)
INCREMENT:
INC DPTR
DJNZ R2, INCREMENT
MOVX @DPTR, A ; COPY DATA TO DESTINATION
MOV A, R1 ; GET DISPLACEMENT AGAIN SINCE R2 IS DESTROYED
MOV R2, A ; COPY TO (R2)
INC R2 ; SO AS TO POINT TO PREVIOUS LOCATION IN ORIGINAL ARRAY
; DECREMENT DPTR BY (R1)+1; DEC DPTR IS NOT AVAILABLE
DECREMENT:
CLR C ; CLEAR CARRY
MOV A, DPL ; GET DPTR LOW
SUBB A, #01H ; DECREMENT DPL
MOV DPL, A ; RESTORE DPL
MOV A, DPH ; GET DPTR HIGH
SUBB A, #00H ; SUBTRACT CARRY [IF EXISTS] FROM (DPH)
MOV DPH, A ; RESTORE DPH
DJNZ R2, DECREMENT ; REPEAT UNTIL (R2) IS ZERO
DJNZ R0, REPEAT ; LOOP UNTIL ARRAY LENGTH IS COVERED
HERE:
SJMP HERE ; LOGICAL END
END
Exercise-3_2
; Transfer ten elements of an array starting at location XX40h
; in external data memory to a location XX45 in the same memory
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
; SHIFT DATA FROM #1049H TO #1040H, WHICH IS IN THE REVERSE
; ORDER TO AVOID OVERWRITING THE OVERLAPPING PARTS
; MOV DATA FROM #1049H TO #104EH
MOV DPTR, #1049H
MOVX A, @DPTR
MOV DPTR, #104EH
MOVX @DPTR, A
; MOV DATA FROM #1048H TO #104DH
MOV DPTR, #1048H
MOVX A, @DPTR
MOV DPTR, #104DH
MOVX @DPTR, A
; MOV DATA FROM #1047H TO #104CH
MOV DPTR, #1047H
MOVX A, @DPTR
MOV DPTR, #104CH
MOVX @DPTR, A
; MOV DATA FROM #1046H TO #104BH
MOV DPTR, #1046H
MOVX A, @DPTR
MOV DPTR, #104BH
MOVX @DPTR, A
; MOV DATA FROM #1045H TO #104AH
MOV DPTR, #1045H
MOVX A, @DPTR
MOV DPTR, #104AH
MOVX @DPTR, A
; MOV DATA FROM #1044H TO #1049H
MOV DPTR, #1044H
MOVX A, @DPTR
MOV DPTR, #1049H
MOVX @DPTR, A
; MOV DATA FROM #1043H TO #1048H
MOV DPTR, #1043H
MOVX A, @DPTR
MOV DPTR, #1048H
MOVX @DPTR, A
; MOV DATA FROM #1042H TO #1047H
MOV DPTR, #1042H
MOVX A, @DPTR
MOV DPTR, #1047H
MOVX @DPTR, A
; MOV DATA FROM #1041H TO #1046H
MOV DPTR, #1041H
MOVX A, @DPTR
MOV DPTR, #1046H
MOVX @DPTR, A
; MOV DATA FROM #1040H TO #1045H
MOV DPTR, #1040H
MOVX A, @DPTR
MOV DPTR, #1045H
MOVX @DPTR, A
HERE:
SJMP HERE
END
Exercise-4
; Convert a 2-digit hexadecimal number into BCD number.
; STORE RESULT IN R6, R7;
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
MOV R0, #0FAH ; INPUT 2-DIGIT HEXADECIMAL NUMBER
MOV 0F0H, #10 ; DIVISION BY DECIMAL 10
MOV A, R0 ; PERORM OPERATIONS ON ACCUMULATOR
DIV AB ; PERFORM DIVISION
MOV R1, A ; QUOTIENT IS IN A; SAVE IN (R1)
MOV A, B ; REMAINDER IS IN B
MOV R6, A ; LOWER BYTE; APPEND IN (R6)
MOV 0F0H, #10 ; DIVISION BY DECIMAL 10
MOV A, R1 ; DIVIDE QUOTIENT BY 10
DIV AB ; PERFORM DIVISION
MOV R7, A ; QUOTIENT IS IN A; SAVE DIRECTLY IN (R7)
MOV A, B ; REMAINDER IS IN B
SWAP A ; SWAP THE NIBBLES IN THE ACCUMULATOR
ORL A, R6 ; LOWER BYTE UPDATED
MOV R6, A ; STORE LOWER BYTE
HERE:
SJMP HERE ; LOGICAL END
END
Problems
Problem-1
; Find the sum of odd and even numbers available in a
; memory array and store the result in memory locations.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
HERE:
SJMP HERE ; LOGICAL END
END
Problem-2
; Convert a 2-digit BCD number into ASCII code.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
HERE:
SJMP HERE ; LOGICAL END
END
Problem-3
; Convert binary number into Gray code.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
HERE:
SJMP HERE ; LOGICAL END
END
Problem-4
; Convert Gray code into binary number.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
HERE:
SJMP HERE ; LOGICAL END
END
Problem-5
; Reverse the elements of an array.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
HERE:
SJMP HERE ; LOGICAL END
END
Problem-6
; Read a single digit hexadecimal number from the memory location
; and generate 7-segment code for the number read and send the
; code to port1. Assume common-anode seven-segment display.
ORG 0000H ; ORIGINATE
AJMP START ; JUMP TO THE LABEL START
START:
HERE:
SJMP HERE ; LOGICAL END
END
Last updated