In this blog post, we will explore an 8086 assembly language program designed to sort a list of numbers in descending order. Sorting is a fundamental operation in computer science, and understanding how to implement it at the assembly level provides valuable insights into low-level programming and processor operations.
The following assembly program sorts an array of numbers in descending order using the bubble sort algorithm:
DATA SEGMENT STRING1 DB 99H,12H,56H,45H,36H DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX MOV CH,04H UP2: MOV CL,04H LEA SI,STRING1 UP1:MOV AL,[SI] MOV BL,[SI+1] CMP AL,BL JNC DOWN MOV DL,[SI+1] XCHG [SI],DL MOV [SI+1],DL DOWN: INC SI DEC CL JNZ UP1 DEC CH JNZ UP2 INT 3 CODE ENDS END START
Step-by-Step Explanation:
1. Data Segment:
STRING1 DB 99H,12H,56H,45H,36H: Defines an array of bytes containing the numbers to be sorted: 99H, 12H, 56H, 45H, and 36H.
2. Code Segment:
MOV AX,DATAandMOV DS,AX: Initialize the data segment by loading the address of the data segment into the AX register and then moving it to the DS register.โMOV CH,04H: Sets the outer loop counter (CH) to 4, representing the number of passes needed for the bubble sort algorithm.
3. Outer Loop (UP2):
MOV CL,04H: Initializes the inner loop counter (CL) to 4 for each pass.โLEA SI,STRING1: Loads the effective address of the first element of the array into the SI register.
4. Inner Loop (UP1):
MOV AL,[SI]andMOV BL,[SI+1]: Load the current element and the next element of the array into the AL and BL registers, respectively.โCMP AL,BL: Compares the current element (AL) with the next element (BL).โJNC DOWN: If the current element is greater than or equal to the next element, no swap is needed, and the program jumps to theDOWNlabel.โMOV DL,[SI+1],XCHG [SI],DL, andMOV [SI+1],DL: If the current element is less than the next element, these instructions swap the two elements to arrange them in descending order.
5. Loop Control:
INC SI: Increments the SI register to point to the next pair of elements in the array.โDEC CLandJNZ UP1: Decrements the inner loop counter and jumps back to theUP1label if CL is not zero, continuing the inner loop.โ@ankurmDEC CHandJNZ UP2: Decrements the outer loop counter and jumps back to theUP2label if CH is not zero, initiating another pass through the array.
6. Program Termination:
INT 3: Generates a breakpoint interrupt to terminate the program execution.
Flowchart:

Overall Process:
- The program initializes loop counters and sets up registers for sorting.โ
- It iterates through the array multiple times to ensure all numbers are sorted in descending order.โ
- For each pair of adjacent numbers, it compares them to check if they are in the correct order.โ
- If the current number is smaller than the next number, it swaps them to move the larger number forward.โ
- After completing all passes, the numbers are fully sorted in descending order.โ
- The sorted numbers remain stored in memory, and the program terminates.
Output
C:\TASM>masm str_desc.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.
Object filename [str_desc.OBJ]:
Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:
str_desc.asm(1): warning A4001: Extra characters on line
50760 + 450936 Bytes symbol space free
1 Warning Errors
0 Severe Errors
C:\TASM>link str_desc.obj
Microsoft (R) Overlay Linker Version 3.60
Copyright (C) Microsoft Corp 1983-1987. All rights reserved.
Run File [STR_DESC.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment
C:\TASM>debug str_desc.exe
-g
AX=0B36 BX=0012 CX=0000 DX=0012 SP=0000 BP=0000 SI=0004 DI=0000
DS=0B37 ES=0B27 SS=0B37 CS=0B38 IP=0027 NV UP EI PL ZR NA PE NC
0B38:0027 CC INT 3
-d 0B37:0000
0B37:0000 99 56 45 36 12 00 00 00-00 00 00 00 00 00 00 00 .VE6............
0B37:0010 B8 37 0B 8E D8 B5 04 B1-04 8D 36 00 00 8A 04 8A .7........6.....
0B37:0020 5C 01 3A C3 73 08 8A 54-01 86 14 88 54 01 46 FE \.:.s..T....T.F.
0B37:0030 C9 75 EA FE CD 75 E0 CC-0C 2A E4 40 50 8B C3 05 .u...u...*.@P...
0B37:0040 0C 00 52 50 E8 19 46 83-C4 04 50 8D 86 00 FF 50 ..RP..F...P....P
0B37:0050 E8 6F 70 83 C4 06 B8 CD-05 50 8D 86 00 FF 50 E8 .op......P....P.
0B37:0060 CA 0C 83 C4 04 B8 FF FF-50 8D 86 00 FF 50 8D 46 ........P....P.F
0B37:0070 80 50 E8 4D FA 83 C4 06-0A C0 75 03 E9 7B FF 5E .P.M......u..{.^
-q
Understanding the Memory Dump
The memory dump after program execution shows the sorted array:โ
<code>0B37:0000 99 56 45 36 12 00 00 00-00 00 00 00 00 00 00 00 .VE6............
The output 99, 56, 45, 36, 12 confirms that the numbers have been successfully sorted in descending order.โ
By understanding and implementing this assembly program, we gain deeper insights into low-level data manipulation and the mechanics of sorting algorithms at the hardware interaction level.
Nice information… Thank you….
Welcome