An Assembly program to sort a given set of 16-bit unsigned intergers into Ascending order

Now we will write another Assembly program to sort a given set of 16-bit unsigned intergers into Ascending order.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the Given Numbers in Array list and it will be array ARR. Other variables will be holding Length of the Array and it will be LEN, So in all Two variables.
The identified variables are ARRand LEN.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666 this line is a declaration of 8-bit Numbers Array initialized with 3333,4444,1111,9999,5555,2222,7777,8888,6666 the numbers are seperated by Comma (,). LEN DW $-ARR is used to Save the Length of the Array which will be generated by $-Name of the array i.e. $-ARR. Detailed explanation is given below.

Next Line – DATA ENDS

DATA ENDS is the End point of the Data Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Data Segment.

Now, Selection of data type is DW data type the numbers which we are adding is 16-bit integers so DW is sufficient.

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or dstands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line – CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line – ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register. MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line – MOV CX,(LEN/2)-1

MOV CX,(LEN/2)-1 is used to move or assign value (18/2)-1 i.e. 8 ((Length of Array /2)- 1) to CX. LEN is assigned Double the size of Array as it counts BYTES in it, Hence we divide it with Two. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – OUTER:
LEA SI,ARR

OUTER: is a LABEL and all the words ending in colon (:). LEA SI,ARRin this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element. This same code can be interchangably written as MOV DX, OFFSET PRICE where OFFSET means effective address and MOV means move second element into the first element. Here Base Address of variable ARR is loaded in DX register.

Next Line – MOV BX,0

MOV BX,0 is used to move zero to BX register i.e. Initialize BX to ZERO. This will be used for constructing inner Loop. Since we cannot use Two LOOP instruction in One program.

Next Line – INNER:
INC BX

INNER: is a LABEL and all the words ending in colon (:). INC BX will increment the value present in BX register. Here we are using BX register as a counter and counting the numbers of Array elements to Cover all the elements in Array.

Next Line – MOV AX,ARR[SI]
INC SI
INC SI

MOV AX,ARR[SI] means move value in Address of SI to AX register. [] is Refered as Value present in the Address of the Register in it. INC SI andINC SI will increment the Address value present in SI register Twice, As we are using WORD (16-bits). Here we are using SI register as a SOURCE INDEX which holds the Address of Array elements to Cover all the elements in Array.

Next Line – CMP AX,ARR[SI]
JB SKIP

CMP AX,ARR[SI] is used to compare AX register with Element of Array present in ARR[SI] and JB SKIP Short Jump if first operand i.e. ARR[SI] is Below second operand i.e. AL to the respective LABEL SKIP. The result of Comparision is not stored anywhere, but flags are set according to result.

Next Line – XCHG AX,ARR[SI]
MOV ARR[SI-2],AX

XCHG AX,ARR[SI]LOOP1:is used to Exchange the values in Address or Register. MOV ARR[SI-2],AX means move value in AX register to Address of SI-2 i.e. previous value of SI address. [] is Refered as Value present in the Address of the Register in it.

Next Line – SKIP:
CMP BX,CX
JL INNER

SKIP: is a LABEL and all the words ending in colon (:). CMP BX,CXis used to compare Element of BX register with CX register and JL INNER Short Jump if first operand i.e. BX is Less then second operand i.e. CX to the respective LABEL INNER. The result of Comparision is not stored anywhere, but flags are set according to result.

Next Line – LOOP OUTER

LOOP OUTERThis end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – MOV AH,4CH
INT 21H

The above two line code is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 4ch, That means Return to Operating System or DOS which is the End of the program.

Next Line – CODE ENDS

CODE ENDS is the End point of the Code Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Code Segment.

Last Line – END START

END START is the end of the label used to show the ending point of the code which is written in the Code Segment.

Note :- In this Assembly Language Programming, We have Com format and EXE format. We are Learning in EXE format only which simple then COM format to understand and Write. We can write the program in lower or upper case, But i prepare Upper Case.

Screen Shots :-

Before Execution :-

After Execution :-

Note :- To see the variable and its value you have to click vars button in the emulator