Programming 2
Programming 2
Assume that the data to be arranged are available in an array which starts from 41h and the array length is
available in 40h.The result is to be stored in 60h.
MOV R0, #40H ; R0 initialized with 40h
MOV A, @R0 ; array length in 40h is moved to A
MOV R7, A ; value (array length) in A is copied into R7
DEC R7 ; Decrement R7
INC R0 ; Increment pointer
MOV B, @R0 ; Load the first data in B
AGAIN: INC R0 ; Increment pointer
MOV A, @R0 ; Next data from memory is moved to A
CJNE A, B, NEXT ; A and B are compared. Carry will be generated if B is bigger
NEXT: JC L1 ; if carry is generated, jump to label L1
MOV B, A ; if no carry, move the value in A to B
L1: DJNZ R7, AGAIN ; array length is decremented and jump to label AGAIN till array length is 0
MOV R0, #60H ; R0 is initialized with 60h to store the result as in B
MOV A, B ; value in B (bigger value) is moved to A
MOV @R0, A ; this value (bigger value) is moved to memory addressed by R0 (60h)
LBL: SJMP LBL
9. Finding the smallest number from an array-
Assume that the data to be arranged are available in array which starts from 41h and the array length is
available in 40h. The result is to be stored in 60h.
MOV R0, #40H ; R0 is initialized with 40h
MOV A, @ R0 ; array length in 40h in moved to A
MOV R7, A ; value (array length) in A is copied into R7
DEC R7 ; Decrement R7
INC R0 ; Increment pointer
MOV B, @ R0 ; data in memory is moved to A
AGAIN: INC R0 ; Increment pointer
MOV A, @R0 ; Next data from memory is moved to A
CJNE A, B, NEXT ; A and B are compared. Carry will be generated if B is bigger
NEXT: JNC L1 ; if carry is generated jump to label L1
MOV B, A ; if no carry, move the value in A to B
L1: DJNZ R7, AGAIN ; array length is decremented and jump to label AGAIN till array length is 0
MOV R0, 60H ; R0 is initialized with 60h to store bigger value in B
MOV A, B ; value in B (largest value) is moved to A
MOV @ R0, A ; This value (largest value) is moved to memory addressed by R0 (60h)
LBL: SJMP LBL
10. Arrange an array of numbers in ascending order-
Assume that the data to be arranged are available in array which starts from 41h and the array length is
stored at 40h.
MOV R1, #40H ; R1 initialized with 40h
MOV A, @R1 ; array length is moved to A
MOV R0, A ; array length is moved to R0
DEC R0 ; Decrement R0
AGAIN: MOV A, R0 ; array length is moved to A
MOV R6, A ; Accumulator data is moved to R6
MOV R1, #41H ; R1 is initialized with 41h
BACK: MOV A, @R1 ; first number is moved to A
MOV B, A ; this number is copied into B
INC R1 ; R1 is incremented
MOV A, @R1 ; second number is moved to A
CJNE A, B, LOOP ; first number in B and next number in A are compared. Carry will be
generated if B value is bigger
LOOP: JNC NEXT ; if no carry is generated, instruction at label NEXT will be executed
DEC R1 ; 01 from stack is moved to R1
MOV @R1, A ; second number is moved to first location
INC R1 ; R1 is incremented
MOV A, B ; first number in B is moved to A
MOV @R1, A ; this first number is moved to second location
NEXT: DJNZ R6, BACK ; jump for next number comparison in inner loop
DJNZ R0, AGAIN ; jump for next outer loop
LBL: SJMP LBL
11. Arrange an array of numbers in descending order-
Assume that the data to be arranged are available in array which starts from 8401h and the array length is
assumed as 09.
MOV R0, #08H ; array length 08h (09-01) is stored
AGAIN: MOV A, R0 ; 08h is moved to R1
MOV R1, A ; Data is moved to R1
MOV DPTR, #8401H ; DPTR is initialized with 8401h
BACK: PUSH DPH ; 84 is saved in stack
PUSH DPL ; 01 is saved in stack
MOVX A, @DPTR ; first number is moved to A
MOV B, A ; this number is copied into B
INC DPTR ; DPTR is incremented
MOVX A, @DPTR ; second number is moved to A
CJNE A, B, LOOP ; first number in B and next number in A are compared. Carry will be
generated if B value is bigger
LOOP: JC NEXT ; if carry is generated instruction at label NEXT will be executed
POP DPL ; Original DPL from stack is moved to DPL
POP DPH ; Original DPH from stack is moved to DPH
MOVX @DPTR, A ; second number is moved to first location
INC DPTR ; DPTR is incremented
MOV A, B ; first number in B is moved to A
MOVX @DPTR, A ; this number data is moved to second location
NEXT: DJNZ R1, BACK ; jump for comparison between the next two numbers
DJNZ R0, AGAIN ; jump for next scan
HLT: SJMP HLT ; last line of the code
12. BCD to ASCII Conversion of a number-
To covert BCD number, each digit is separately considered and equivalent ASCII value is generated. Ex-
To convert 65 into ASCII, 5 is converted into ASCII value 35 and 6 is converted into ASCII 36. Assume
that the BCD value 65 is stored in memory location 8400h and ASCII values are stored in 8401h, 8402h.
MOV DPTR, #8400H ; DPTR is initialized with 8400h
MOVX A, @DPTR ; BCD number is moved to A
MOV R1, A ; BCD number is copied to R1
ANL A, #0FH ; Get the Lower nibble of BCD number
ORL A, #30H ; OR logic with 30h
INC DPTR ; DPTR is incremented
MOVX @DPTR, A ; Result is stored in memory
INC DPTR ; DPTR is incremented
MOV A, R1 ; Data moved to R1
ANL A, #F0H ; Get the higher nibble of BCD number
SWAP A ; Move higher nibble into lower nibble position
ORL A, #30H ; OR logic with 30h
MOVX @DPTR, A ; Store the Result in memory
END
13. ASCII to Binary Conversion
Assume that ASCII value is stored in 8400h and answer is to be stored in 8401h.
MOV DPTR, #8400H ; DPTR is initialized with 8400h
MOV A, @DPTR ; First Data Is Moved To A
MOV R1, A ; Data moved to R1
CJNE A, #40H, NEXT ; data at A is compared with 40h and Carry is generated if A is lesser than 40h
NEXT: JC LOOP ; if carry is generated at label NEXT will be executed
CLR C ; Clear carry flag
SUBB A, #07H ; Subtract the 07 h from the value of Data: only for alphabets (A-F)
LOOP: CLR C ; Clear carry flag
SUBB A, #30H ; Subtract the 30 h from the value of Data: for both numbers and alphabets (A-F)
INC DPTR ; DPTR incremented
MOVX @DPTR, A ; Store the Result
END