Write a microprocessor 8085 program to arrange an array of data in ascending order. The length of the block is in memory location 4200H and the block itself starts from memory location 4201H.
Arrange the numbers in ascending order and store them at memory location from 4201H. Assume that the numbers in the block are all 8-bit unsigned binary numbers.
- Initialize HL pair as memory pointer.
- Get the count at 4200 into C – register.
- Copy it in D – register (for bubble sort (N-1) times required).
- Get the first value in Accumulator.
- Compare it with the value at next location.
- If they are out of order, exchange the contents of Accumulator and Memory.
- Decrement D –register content by 1.
- Repeat steps 5 and 7 till the value in D- register become zero.
- Decrement contents of C –register by 1.
- Repeat steps 3 to 9 till the value in C – register becomes zero.
LXI H, 4200H ; Set pointer for array.
MOV C, M ; Load the count value.
DCR C ; Decrement counter.
REPEAT: MOV D, C
LXI H, 4201H ; Set the memory pointer for data.
LOOP: MOV A, M ; Move the number into accumulator.
INX H ; Increment memory pointer.
CMP M ; Compare memory and accumulator.
JC SKIP ; jump to skip if carry generated.
MOV B, M ; copy content of memory location to B – Register.
MOV M, A ; copy content of Accumulator to memory location.
DCX H ; Decrement content of HL pair of registers.
MOV M, B ; copy content of B – Register to memory location.
INX H ; Increment content of HL pair of registers.
SKIP: DCR D ; Decrement content of Register – D.
JNZ LOOP ; jump to loop if not equal to zero.
DCR C ; decrement counter.
JNZ REPEAT ; jump to repeat if not equal to zero.
HLT ; Terminate Program.
4200 05 (Array Size)
4200 05(Array Size)