In the last program, we learned how we can arrange an array of 8-bit hexadecimal numbers in ascending, now in this program, we are going to arrange the same 8-bit hexadecimal numbers in descending order.

Input:

For this program also we are going to use five 8-bit hexadecimal numbers, hence, we will be requiring five memory locations; 2000H – 2004H.

The 8-bit hexadecimal numbers which we are going to use are C2H, ABH, 1DH, F9H, and 9BH.

Instructions:

MVI C, 05H // C register will be used as the main counter which we will keep decrementing as we compare one 8-bit number after another

Label3 MVI B, 04H // B register will also be used as another counter which we will need for comparison

LXI H, 2000H // HL register pair will be loaded and will point to the content of 2000H memory location

Label2 MOV A, M // the content of 2000H ML pointed by HL register will be copied to the accumulator

INX H// HL register pair will get incremented by one

CMP M// the content of accumulator will be compared with the content of memory location pointed by HL register pair

JNC Label1 // JNC will jump if the content of ML pointed by HL register pair is smaller than the content of accumulator

MOV D, M // the content which is pointed by HL register will be moved to D register

MOV M, A // the content of accumulator will be moved to the memory location pointed by HL register

DCX H // using DCX we decremented the HL register pair by 1

MOV M, D // now the 8-bit number stored in D register will be moved to the memory location pointed by HL register

INX H // HL register pair will be incremented by 1

Label1 DCR B // the content of B register pair will be decremented by 1

JNZ Label2 // JNZ will keep on working unless the content of ML doesn’t become null

DCR C // the content of C register pair will be decremented by 1

JNZ Label3 // JNZ will keep on jumping until the content of B register becomes null

HLT

Programming Details
Memory Location OPCODE Operand Label Hex Code
2005 MVI C, 05 OEH
2006 05H
2007 MVI B, 04 Label3 06H
2008 04H
2009 LXI H, 2000H 21H
200A 00H
200B 20H
200C MOV A,M Label2 7EH
200D INX H 23H
200E CMP M BEH
200F JNC Label1 D2H
2010 17H
2011 20H
2012 MOV D,M 56H
2013 MOV M,A 77H
2014 DCX H 2BH
2015 MOV M, D 72H
2016 INX H 23H
2017 DCR B Label1 05H
2018 JNZ Label2 C2H
2019 0CH
201A 20H
201B DCR C 0DH
201C JNZ Label3 C2
201B 07H
201D 20H
201E 0CH
201F HLT 76H