5. To write a program to arrange an array of data in ascending and descending order.
ORG 0000h ; Starting address of the program
MVI B, 10h ; Set the length of the array to 16 (10h)
; Initialize the array with random data
LXI H, 3000h ; Load the starting address of the array in HL
MOV C, B ; Copy the length of the array to register C
INITLOOP:
MOV A, M ; Load the current array element in A
INR A ; Increment the value of the current array element by 1
MOV M, A ; Store the updated value of the current array element
INX H ; Move to the next array element
DCR C ; Decrement the counter
JNZ INITLOOP ; Jump back to INITLOOP if counter is not zero
; Arrange the array in ascending order
LXI H, 3000h ; Load the starting address of the array in HL
MOV B, C ; Copy the length of the array to B
DEC B ; Decrement B for use in the outer loop
OUTERLOOP:
MOV A, M ; Load the current element in A
MOV C, B ; Copy the remaining length of the array to C
INNERLOOP:
INX H ; Move to the next element
MOV E, M ; Load the next element in E
CMP E ; Compare the current element with the next element
JC NOTSWAP ; Jump to NOTSWAP if current element is not greater than next element
MOV D, A ; Copy the current element to D
MOV A, E ; Copy the next element to A
MOV M, D ; Store the current element in the next element's location
DCX H ; Move back to the current element's location
MOV M, A ; Store the next element in the current element's location
NOTSWAP:
DCR C ; Decrement the counter
JNZ INNERLOOP ; Jump back to INNERLOOP if counter is not zero
DCR B ; Decrement the outer loop counter
JNZ OUTERLOOP ; Jump back to OUTERLOOP if counter is not zero
; Arrange the array in descending order
LXI H, 3000h ; Load the starting address of the array in HL
MOV B, C ; Copy the length of the array to B
DEC B ; Decrement B for use in the outer loop
OUTERLOOP2:
MOV A, M ; Load the current element in A
MOV C, B ; Copy the remaining length of the array to C
INNERLOOP2:
INX H ; Move to the next element
MOV E, M ; Load the next element in E
CMP E ; Compare the current element with the next element
JNC NOTSWAP2 ; Jump to NOTSWAP2 if current element is not less than next element
MOV D, A ; Copy the current element to D
MOV A, E ; Copy the next element to A
MOV M, D ; Store the current element in the next element's location
DCX H ; Move back to the current element's location
MOV M, A ; Store the next element in the current element's location
NOTSWAP2:
DCR C ; Decrement the counter
JNZ INNERLOOP2 ; Jump back to INNERLOOP2 if counter is not zero
DCR B ; Decrement the outer loop counter
JNZ OUTERLOOP2 ; Jump back to OUTERLOOP2 if counter is