Monthly Archives: March 2014


An Assembly program to find the LCM Least Common Multiplier of two 16-bit unsigned integers

Now we will write another Assembly program to find the LCM Least Common Multiplier of two 16-bit unsigned integers.

logic_HCF_or_GCD

The above Logic is a C like Program to Find LCM we need its GCD or HCF first beacuse there is small Formula Shown above in a very simple way, So Just we will covert the logic into Assembly There are many things uncommon in the programing Language. There are No While Loops or Modules but this things are to be implemented in different ways.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the variables to be Added and it will be NUM1 and NUM2. Other variables will be holding the Output or Result of the Highest Common Factor or Greatest Common Divisor and Least Common Multiplier and it will be HCF and LCM So in all Four variables.
The identified variables are NUM1, NUM2 HCF and LCM.

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.

Next Line – NUM1 DW 1500
     NUM2 DW 2500
     HCF DW ?
     LCM DW ?

We are initializing NUM1 to 1500 (Blank or Nothing after number) stands for Decimal ( By Default) ),  NUM2 to 2500 ((Blank or Nothing after number) stands for Decimal ( By Default) ), HCF to ? (? stands for blank value). 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.

DATA SEGMENT
     NUM1 DW 1500
     NUM2 DW 2500
     HCF DW ?
     LCM DW ?
ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     NUM1 DW 1500
     NUM2 DW 2500
     HCF DW ?
     LCM DW ?
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      MOV AX,NUM1
      MOV BX,NUM2
    
WHILE:MOV DX,0
      MOV CX,BX
     
      DIV BX
      MOV BX,DX
     
      MOV AX,CX
     
      CMP BX,0
      JNE WHILE
     
      MOV HCF,AX
      MOV CX,AX
     
      MOV AX,NUM1
      MOV BX,NUM2
     
      MUL BX
      DIV CX
      MOV LCM,AX     
           
      MOV AH,4CH
      INT 21H     
ENDS
END START

Explanation : 

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 AX,NUM1
      MOV BX,NUM2

The above two line of code is same as a=X and b=Y the Difference is that we are using Registers to Store Numbers, So we have t0 instruction MOV AX,NUM1 move NUM1 variable value to AX Register.  MOV BX,NUM1 move NUM1 variable value to BX Register.

Next Line – WHILE:  MOV DX,0
      MOV CX,BX

WHILE: this will be starting point of while loop the condition cannot be written here So we write it Down and this is a LABEL and all the words ending in colon (:).  i.e. MOV DX,0 here, DIV instruction is used to divide AX:DX for 16-bit and Not only AX register. Hence the unwanted value (garbage value) in DX register is removed by assigning ZERO to it. MOV CX,BX is to move BX register to CX register, Here We need a Temporary Storage register AX, BX and DX are used So we have only Option CX register.

Next Line –   DIV BX
      MOV BX,DX

In Assembly we can use Remainder as the Modules Output, So We have to Divide first to get Remainder. DIV BX  Now DIV BX will Divide AX:DX register with NUM2 which is passed to BX register and Remainder of division is present in DX register so move the value to BX register. i.e. MOV BX,DX

Next Line –  MOV AX,CX

MOV BX,CX is to move CX register to BX register, Here We are transfering a Temporary Storage in CX register to BX register.

Next Line –CMP BX,0
      JNE WHILE

CMP BX,0 is used to Compare BX register to ZERO. JNE WHILE is used to Jump to Label WHILE if Not Equal when compared with CMP instruction prior to this one.

Next Line – MOV HCF,AX
      MOV CX,AX

MOV HCF,AX is to move AX register (HCF of Two 16-bit Nos.) to HCF variable. MOV CX,AX is to move AX register to  CX register as to preserve the HCF value to be used later in Equation to get LCM.

Next Line – MOV AX,NUM1
      MOV BX,NUM2

MOV AX,NUM1 move NUM1 variable value to AX Register.  MOV BX,NUM1 move NUM1 variable value to BX Register.

Next Line – MUL BX
      DIV CX
      MOV LCM,AX

MUL BX  in this line BX register will be Multiplied with AX:DX register (BY DEFAULT) in which we have value of NUM1 is present. Now, This will give (X*Y). DIV CX in this line value in CX register will Divide AX:DX register (BY DEFAULT) in which the resultant value of (X*Y) is present. Now, This will give (X*Y)/HCF which is Result of LCM. MOV LCM,AX is to move AX register (LCM of Two 16-bit Nos.) to LCM variable.

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 :-

Asm_program_LCM_16-bit

Before Execution :-

Asm_program_LCM_16-bit_Output1

After Execution :-

Asm_program_LCM_16-bit_Output2

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

An Assembly program to find the HCF Highest Common Factor (GCD Greatest Common Divisor) of two 16-bit unsigned integers

Now we will write another Assembly program to find the HCF Highest Common Factor (GCD Greatest Common Divisor) of two 16-bit unsigned integers.

logic_HCF_or_GCD

The above Logic is a C like Program to Find GCD or HCF in a very simple way, So Just we will covert the logic into Assembly There are many things uncommon in the programing Language. There are No While Loops or Modules but this things are to be implemented in different ways.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the variables to be Added and it will be NUM1 and NUM2. Other variables will be holding the Output or Result of the Highest Common Factor or Greatest Common Divisor and it will be HCF So in all Three variables.
The identified variables are NUM1, NUM2 and HCF.

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.

Next Line – NUM1 DW 1500
     NUM2 DW 2500
     HCF DW ?

We are initializing NUM1 to 1500 (Blank or Nothing after number) stands for Decimal ( By Default) ),  NUM2 to 2500 ((Blank or Nothing after number) stands for Decimal ( By Default) ), HCF to ? (? stands for blank value). 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.

DATA SEGMENT
     NUM1 DW 1500
     NUM2 DW 2500
     HCF DW ?
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     NUM1 DW 1500
     NUM2 DW 2500
     HCF DW ?
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      MOV AX,NUM1
      MOV BX,NUM2
    
WHILE:MOV DX,0
      MOV CX,BX
     
      DIV BX
      MOV BX,DX
     
      MOV AX,CX
     
      CMP BX,0
      JNE WHILE
     
      MOV HCF,AX
     
      MOV AH,4CH
      INT 21H     
CODE ENDS
END START

Explanation : 

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 AX,NUM1
      MOV BX,NUM2

The above two line of code is same as a=X and b=Y the Difference is that we are using Registers to Store Numbers, So we have t0 instruction MOV AX,NUM1 move NUM1 variable value to AX Register.  MOV BX,NUM1 move NUM1 variable value to BX Register.

Next Line – WHILE:  MOV DX,0
      MOV CX,BX

WHILE: this will be starting point of while loop the condition cannot be written here So we write it Down and this is a LABEL and all the words ending in colon (:).  i.e. MOV DX,0 here, DIV instruction is used to divide AX:DX for 16-bit and Not only AX register. Hence the unwanted value (garbage value) in DX register is removed by assigning ZERO to it. MOV CX,BX is to move BX register to CX register, Here We need a Temporary Storage register AX, BX and DX are used So we have only Option CX register.

Next Line –   DIV BX
      MOV BX,DX

In Assembly we can use Remainder as the Modules Output, So We have to Divide first to get Remainder. DIV BX  Now DIV BX will Divide AX:DX register with NUM2 which is passed to BX register and Remainder of division is present in DX register so move the value to BX register. i.e. MOV BX,DX

Next Line –  MOV AX,CX

MOV BX,CX is to move CX register to BX register, Here We are transfering a Temporary Storage in CX register to BX register.

Next Line –CMP BX,0
      JNE WHILE

CMP BX,0 is used to Compare BX register to ZERO. JNE WHILE is used to Jump to Label WHILE if Not Equal when compared with CMP instruction prior to this one.

Next Line – MOV HCF,AX

MOV HCF,AX is to move AX register (HCF of Two 16-bit Nos.) to HCF variable.

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 :-

Asm_program_HCF_or_GCD_16-bit

Before Execution :-

Asm_program_HCF_or_GCD_16-bit_Output1

After Execution :-

Asm_program_HCF_or_GCD_16-bit_Output2

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

An Assembly program for finding the largest number in array of 10 elements

Now we will write another Assembly program for finding the largest number in array of 10 elements. 

First variables will be the one which will hold the value discovered as the Largest of All the Numbers in Array list and it will be LARGE and Second 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 ARR, LEN and LARGE.

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.

Next Line – ARR DB 1,4,2,3,9,8,6,7,5,10
     LEN DW $-ARR
     LARGE DB ? 

ARR DB 1,4,2,3,9,8,6,7,5,10 this line is a declaration of 8-bit Numbers Array initialized with 1,4,2,3,9,8,6,7,5,10 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. We are initializing LARGE DB ? to ? (? stands for blank value). 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 DB data type the numbers which we are adding will be integers so DB is sufficient.

 DATA SEGMENT
     ARR DB 1,4,2,3,9,8,6,7,5,10
     LEN DW $-ARR
     LARGE DBDATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     ARR DB 1,4,2,3,9,8,6,7,5,10
     LEN DW $-ARR
     LARGE DBDATA ENDS
CODE SEGMENT 
        ASSUME DS:DATA CS:CODE
START:
        MOV AX,DATA
        MOV DS,AX
           
        LEA SI,ARR
       
        MOV AL,ARR[SI]
        MOV LARGE,AL
               
        MOV CX,LEN
REPEAT:
        MOV AL,ARR[SI]
        CMP LARGE,AL
        JG NOCHANGE
       
        MOV LARGE,AL
NOCHANGE:
        INC SI
        LOOP REPEAT
     
        MOV AH,4CH
        INT 21H     
CODE ENDS
END START

Explanation : 

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 – LEA SI,ARR

LEA SI,ARR in 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 PRICE is loaded in DX register.

Next Line – MOV AL,ARR[SI]
        MOV LARGE,AL

MOV AL,ARR[SI]  means move value of Array ARR in index of SI register to AL register. MOV LARGE,AL is to move AL register (First value in Array) to LARGE variable as we want to compare it with All the Array elements.

Next Line – MOV CX,LEN

 MOV CX,LEN is used to move or assign value 8 (Length of Array) to  CX. 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 – REPEAT:

REPEAT: is a LABEL and all the words ending in colon (:).

Next Line – MOV AL,ARR[SI]

MOV AL,ARR[SI] is to move value of Array ARR in index of SI register to AL register (Different values in Array). As we want to compare All elements with variable LARGE.

Next Line – CMP LARGE,AL
        JG NOCHANGE

CMP LARGE,AL  is used to compare AX register with 9 and JG NOCHANGE jump if AL is greater to the respective LABEL NOCHANGE. The result of Comparision is not stored anywhere, but flags are set according to result.  is Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. SECOND is the label where the compiler will JUMP.

Next Line – MOV LARGE,AL 

MOV LARGE,AL is to move AL register (larger value in Array) to LARGE variable.

Next Line – NOCHANGE:

NOCHANGE: is a LABEL and all the words ending in colon (:).

Next Line – INC SI

INC SI will increment the Address value present in SI register. 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 – LOOP REPEAT

LOOP REPEAT  This 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 – EXIT: 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 :-

 Asm_program_Largest_in_Array

Output Before Execution :-

Asm_program_Largest_in_Array_Output1

Output After Execution :-

 Asm_program_Largest_in_Array_Output2

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

An Assembly program to Convert ASCII number into Decimal digit

Now we will write another Assembly program to Convert ASCII number into Decimal digit on Screen

Let’s identify variables needed for this program.
First variables will be the one which will hold the value present in the variable NUM converts Hexadecimal value to print its Decimal form on Console (Screen) and Other variable RES will be holding the Resultant Decimal equivalent printable form to be printed for the User on Screen, So in all  Two variables.
The identified variables are NUM and RES.

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.

Next Line – NUM DB 0FFH
     RES  DB 10 DUP (‘$’)

NUM DB 0FFH We are initializing NUM to 0FFH  (H stands for Hexadecimal value). The ASCII number which we are using is of 2 digits and DB is capable of holding one byte. RES  DB 10 DUP (‘$’) this line is a declaration of Array initialized with ’$’ which works as New Line Character. $ is used as (\n) NULL character in C program. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the digits in the Resultant Decimal equivalent printable form, Therefore we take it approx size ten. Here 10 DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $).

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 DB data type the numbers which we are adding will be integers so DB is sufficient.

 DATA SEGMENT
     NUM DB 0FFH
     RES  DB 10 DUP ('$')
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     NUM DB 0FFH
     RES  DB 10 DUP ('$')
DATA ENDS
CODE SEGMENT
        ASSUME DS:DATA,CS:CODE
START:       
    MOV AX,DATA
    MOV DS,AX
   
    MOV AH,0
    MOV AL,NUM
      
    LEA SI,RES
    CALL HEX2DEC
   
    LEA DX,RES
    MOV AH,9
    INT 21H 
            
    MOV AH,4CH
    INT 21H        
CODE ENDS
HEX2DEC PROC NEAR
    MOV CX,0
    MOV BX,10
   
LOOP1: MOV DX,0
       DIV BX
       ADD DL,30H
       PUSH DX
       INC CX
       CMP AX,9
       JG LOOP1
     
       ADD AL,30H
       MOV [SI],AL
     
LOOP2: POP AX
       INC SI
       MOV [SI],AL
       LOOP LOOP2
       RET
HEX2DEC ENDP           
   
END START

Explanation : 

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 AH,0

MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it.

Next Line – MOV AL,NUM

The above line code is used to Move  NUM variable value to AL Register.

Next Line – LEA SI,RES
    CALL HEX2DEC

The above Two line code is used to initialize RES to SI register and Call Procedure HEX2DEC

LEA SI,RES is used to Load Effective Address of RES variable to SI Register. 

CALL HEX2DEC is used to Call a Procedure named HEX2DEC

Next Line – LEA DX,RES
    MOV AH,9
    INT 21H

The above three line code is used to print String or Message present in the character Array till $  symbol which tells the compiler to stop. As we have initialized all the values in an Array to $ you will think what will be printed. The procedure is going to change the Array to its Resultant Decimal equivalent printable form i.e. ASCII form of a digit number.

Now, lets understand line by line

LEA DX,RES in 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 RES where OFFSET  means effective address and MOV means move  second element into the first element.

MOV AH,9
INT 21H

The above two line code is used to PRINT the String or Message of the address present in DX register.

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 9 or 9h, That means PRINT the String or Message of the address present in DX register.

Next Line – EXIT: 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.

PROCEDURE Code starts here: 

Procedure is a part of code that can be called from a program in order to perform specific task.

Next Line – HEX2DEC PROC NEAR

This line of code is used to start a procedure code and we can make out the procedure by the keyword PROC which tells us the procedure is started. In assembly language we have two types of Procedures one is NEAR and other is FAR. NEAR is used to call the Procedure within the program whereas FAR is used to call the procedure outside the program. HEX2DEC is only the Name given to the Procedure Code. 

Next Line –  MOV CX,0
    MOV BX,10

MOV CX,0 is used to move or assign value 0 (decimal value) to  CX. The program which we are wishing to write is to covert HexaDecimal value to Decimal value, In which we will divide the number till the Quotient is going to be Zero. CX register ( CX is also Called COUNTER). CX register will count the number digit generated by dividing the Hexadecimal number by Base value of Decimal i.e.Ten. MOV BX,10 in this Base value 10 is moved to BX register, So that it is used to divide hexa number by 10.

Next Line – LOOP1: MOV DX,0

LOOP1: is a LABEL and all the words ending in colon (:) are Labels. MOV DX,0 is used to clear the unwanted value (garbage value) in DX register is removed by assigning ZERO to it. First Loop starts here.

Next Line – DIV BX
       ADD DL,30H

DIV instruction only works with REG or MEMORY hence we cannot use DIV 10 where 10 is immediate, So we have to move 10 to BX register (we can take any register) this we have already done above and Then DIV BX  Now DIV BX will Divide AX register with 10 which is passed to BX register and Result of division is present in AX register contains Quotientand DX register contains Remainder. Here we will not touch Quotient AX as it will be used for furture Division, But DX Remainder will be Decimal Digit and will always be less than Ten so the value will be in DL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later So ADD DL,30H.

Next Line – PUSH DX
       INC CX

PUSH is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. PUSH DX stores 16 bit data inside DX register into Stack Area. INC is a instruction for Increment the present in Register or Memory. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack.

Next Line – MOV CX,10

MOV CX,10 is used to move or assign value 10 (decimal value) to  CX. The program which we are wishing to write is to input ten characters from console which will be entered by the user, Hence to do so we need a loop construct. 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 – CMP AX,9
       JG LOOP1

CMP AX,9  is used to compare AX register with 9 and jump if AX is greater to the respective LABEL LOOP1. The result of Comparision is not stored anywhere, but flags are set according to result.  is Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. SECOND is the label where the compiler will JUMP. First Loop ends here. Note :- this loop is without LOOP keyword and depends upon the number to be converted.

Next Line – ADD AL,30H
       MOV [SI],AL

ADD AL,30H The Last Remainder will be Decimal Digit in AX register only as the number cannot be divided future and will always be less than Ten so the value will be in AL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

 Next Line – LOOP2: POP AX
       INC SI

LOOP2: is a LABEL and all the words ending in colon (:) are Labels. POP is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. POP AX gets 16 bit data to AX register from Top of Stack. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack and save it in AX register. Second Loop starts here.

Next Line – MOV [SI],AL

The values out of Stack saved in AX register saved in string in this Loop. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

Next Line – LOOP LOOP2

This 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 – RET

RET is a return instruction. This instruction is used only if  the control is been passed to the code outside Main like to Procedure. this return the control to the place where the Procudure was called.

Next Line – HEX2DEC ENDP 

HEX2DEC ENDP is the End point of the Procedure in a Program.

This line of code is used to end the procedure code and we can make out the procedure by the keyword ENDP which tells us the procedure is ended. In assembly language we have two types of Procedures one is NEAR and other is FAR. NEAR is used to call the Procedure within the program whereas FAR is used to call the procedure outside the program. HEX2DEC is only the Name given to the Procedure Code. 

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 :-

Asm_program_Ascii_Num_To_Decimal_Num

Output After Execution :-

Asm_program_Ascii_Num_To_Decimal_Num_Output

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

An Assembly program to read in decimal inputs repeatedly untill a zero value is read, At this point it should print out the sum of the numbers read in so far

Now we will write another Assembly program to read  in decimal inputs repeatedly untill a zero value is read, At this point it should print out the sum of the numbers read in so far.

Let’s identify variables needed for this program.
First variables will be holding the Messages “ENTER NO.BETN (0-9) & (ENTER ZERO TO STOP) ” and “TOTAL SUM OF NUMBERS IS “ to be printed for the User, So in all  Two variables.
The identified variables are MSG1 and MSG2.

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.

Next Line – MSG1 DB 10,13,”ENTER NO.BETN (0-9) & (ENTER ZERO TO STOP) : $”
     MSG2 DB 10,13,”TOTAL SUM OF NUMBERS IS : $”

MSG1 DB 10,13,”ENTER NO.BETN (0-9) & (ENTER ZERO TO STOP) : $” this line is a declaration of Charater Array initialized with “ENTER NO.BETN (0-9) & (ENTER ZERO TO STOP) : $”. 10,13, works as New Line Character if this is not present All the Messages will be printed on the same line and $ is used as (\n) NULL character in C program. (A Character is of a BYTE Hence we have to use only DB Define Byte ) and Similarly to MSG1 and MSG2. 

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 DB data type the character array so DB is sufficient.

 DATA SEGMENT
     NUM1 DB ?
     NUM2 DB ?
     MSG1 DB 10,13,"ENTER FIRST NUMBER TO COMPARE : $"
     MSG2 DB 10,13,"ENTER SECOND NUMBER TO COMPARE : $"  
     MSG3 DB 10,13,"SMALLER NUMBER IS : $"
ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     MSG1 DB 10,13,"ENTER NO.BETN (0-9) & (ENTER ZERO TO STOP) : $"
     MSG2 DB 10,13,"TOTAL SUM OF NUMBERS IS : $"
DATA ENDS
CODE SEGMENT
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      MOV BL,0
     
INPUT:LEA DX,MSG1     
      MOV AH,9
      INT 21H
    
      MOV AH,1
      INT 21H
     
      CMP AL,30H
      JE EXIT
     
      SUB AL,30H
      ADD AL,BL
      MOV BL,AL
          
      JMP INPUT    
    
EXIT: LEA DX,MSG2     
      MOV AH,9
      INT 21H
     
      MOV AL,BL
      AAM
     
      ADD AX,3030H
      MOV BX,AX
     
      MOV DL,BH
      MOV AH,2
      INT 21H
     
      MOV DL,BL
      MOV AH,2
      INT 21H
     
      MOV AH,4CH
      INT 21H    
CODE ENDS
END START

Explanation : 

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 – INPUT: LEA DX,MSG1      
      MOV AH,9
      INT 21H

The above three line code is used to print String or Message present in the character Array till $  symbol which tells the compiler to stop.

Now, lets understand line by line

INPUT: is a LABEL and all the words ending in colon (:). LEA DX,MSG1  in 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 MSG1 where OFFSET  means effective address and MOV means move  second element into the first element.

MOV AH,9
INT 21H

The above two line code is used to PRINT the String or Message of the address present in DX register.

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 9 or 9h, That means PRINT the String or Message of the address present in DX register.

Next Line – MOV AH,1
      INT 21H

The above Four line code is used to Read a Character from Console and save the value entered in variable NUM1 in its ASCII form. When you enter 5 we see 35H,So by comparing 30H or 35H we get ASCII value unchanged as we have to print the smaller back to console.

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 1 or 1h, That means READ a Character from Console, Echo it on screen and save the value entered in AL register.

Next Line – CMP AL,30H
      JE EXIT

The above two line code is used to Compare and Jump if AL is Zero.

CMP AL,30h is used to Compare entered value i.e. AL register with Zero. JE EXIT is used to Jump to Label EXIT if if AL is Zero when compared with CMP instruction prior to this one.

 Next Line – SUB AL,30H
      ADD AL,BL
      MOV BL,AL

SUB AL,30H is used to convert ASCII value to DECIMAL value. ADD AL,BL is used to SUM UP the values in the LOOP. MOV BL,AL is used to preserve the total in BL register.

Next Line – JMP INPUT

The above line code is used to Jump to Label INPUT without any condition check. This jump will basically Continue the Loop execution.

Next Line – EXIT: LEA DX,MSG2
      MOV AH,9
      INT 21H 

EXIT: is a LABEL and all the words ending in colon (:).

The above two line code is used to PRINT the String or Message of the address present in DX register i.e. for MSG2.

 Next Line – MOV AL,BL
      AAM

 The first line is just to transfer BL register to AL register which is the Total Sum to printed later. The above two line code is used to split result in AH and AL after AAM in BCD form. AAM means ASCII Adjust after Multiplication. AAM Instruction has NO Operands or values attached to it. If we are adding two BCD numbers the Result is saved in AL register in HEXadecimal form. What AAA exactly does is it converts the result into BCD form and first digit is saved in AH register and second digit is saved in AL register.

Next Line – ADD AX,3030H
      MOV BX,AX

ADD AX,3030H is to move 3030H to AX register as we want to convert BCD form value to its ASCII equvalent by adding 30H to AH and AL registers.MOV BX,AX is used to preserve the ASCII equvalent in BX register which will be printed later.

Next Line – MOV DL,BH
      MOV AH,2
      INT 21H

The above line code is used to Write a Character on Console present in DL register i.e. BH register value.

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 2 or 2h, That means WRITE a Character on Console present in DL register hence the value to be printed is moved to DL register. Here we are printing BH register value. Which is the First digit of Total Sum printed. 

Next Line – MOV DL,BL
      MOV AH,2
      INT 21H

The above Three line code is used to Write a Character on Console present in DL register i.e. BL register value. Which is the Second digit of Total Sum printed. 

 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 :-

 Asm_program_Add_Nos_Untill_Zero

Output After Execution :-

 Asm_program_Add_Nos_Untill_Zero_Output

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

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 ARR and 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.

Next Line – ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666
     LEN DW $-ARR

 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.

DATA SEGMENT
     ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666
     LEN DW $-ARR    
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666
     LEN DW $-ARR
    
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
             
      MOV CX,(LEN/2)-1   
OUTER:
      LEA SI,ARR
      MOV BX,0       
INNER:
      INC BX
      MOV AX,ARR[SI]
      INC SI
      INC SI
      CMP AX,ARR[SI]
      JB SKIP
     
      XCHG AX,ARR[SI]
      MOV ARR[SI-2],AX
     
SKIP:
      CMP BX,CX
      JL  INNER
                
      LOOP OUTER
            
      MOV AH,4CH
      INT 21H     
CODE ENDS
END START

Explanation : 

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,ARR in 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 and INC 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,CX is 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 OUTER This 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 :-

Asm_program_Ascend_Order_16-bit

Before Execution :-

 Asm_program_Ascend_Order_16-bit_Output1

After Execution :-

Asm_program_Ascend_Order_16-bit_Output2

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

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

Now we will write another Assembly program to sort a given set of 16-bit unsigned intergers into Descending 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 ARR and 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.

Next Line – ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666
     LEN DW $-ARR

 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.

DATA SEGMENT
     ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666
     LEN DW $-ARR    
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     ARR DW 3333,4444,1111,9999,5555,2222,7777,8888,6666
     LEN DW $-ARR    
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
             
      MOV CX,(LEN/2)-1   
OUTER:
      LEA SI,ARR
      MOV BX,0       
INNER:
      INC BX
      MOV AX,ARR[SI]
      INC SI
      INC SI
      CMP ARR[SI],AX
      JB SKIP
     
      XCHG AX,ARR[SI]
      MOV ARR[SI-2],AX
     
SKIP:
      CMP BX,CX
      JL  INNER
                
      LOOP OUTER
            
      MOV AH,4CH
      INT 21H     
CODE ENDS
END START

Explanation : 

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,ARR in 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 and INC 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 ARR[SI],AX
      JB SKIP

CMP ARR[SI],AX  is used to compare Element of Array present in ARR[SI] with AX register 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,CX is 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 OUTER This 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 :-

Asm_program_Desc_Order_16-bit

Before Execution :-

Asm_program_Desc_Order_16-bit_Output1

After Execution :-

Asm_program_Desc_Order_16-bit_Output2

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

An Assembly program which adds the sales tax in the Price list of items and replace the Price list with the Calculated value

Now we will write another Assembly program to calculate the average of three given numbers stored in memory.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the Price list and it will be array PRICE. Other variables will be holding the Sale’s Tax in Percentage form. Other variables will be holding Length of the Array and it will be LENand it will be TAX So in all Three variables.
The identified variables are PRICE, LEN and TAX.

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.

Next Line – PRICE DB 36,55,27,42,38,41,29,39
     LEN DW $-PRICE
     TAX DB 12

 PRICE DB 36,55,27,42,38,41,29,39 this line is a declaration of 8-bit Numbers Array initialized with 36H,55H,27H,42H,38H,41H,29H,39H the numbers are seperated by Comma (,). LEN DW $-PRICE is used to Save the Length of the Array which will be generated by $-Name of the array i.e. $-PRICETAX DB 12 is used to Save the Sales’s Tax Percentage which value 12 decimal value (12%) which will be Calculated to each element of a Array PRICE. 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 DB data type the numbers which we are adding will be integers so DB is sufficient.

DATA SEGMENT
     PRICE DB 36,55,27,42,38,41,29,39
     LEN DW $-PRICE
     TAX DB 12
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     PRICE DB 36,55,27,42,38,41,29,39
     LEN DW $-PRICE
     TAX DB 12
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA BX,PRICE
     
       MOV CX,LEN
LOOP1:      
       MOV AH,0
       MOV AL,[BX]
                           
       MOV DL,TAX
       MUL DL
      
       MOV DL,100
       DIV DL
                   
       MOV AH,[BX]
       ADD AH,AL
      
       MOV [BX],AH         
       INC BX
             
       LOOP LOOP1   
     
      MOV AH,4CH
      INT 21H     
ENDS
END START

Explanation : 

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 – LEA BX,PRICE

LEA BX,PRICE in 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 PRICE is loaded in DX register.

Next Line – MOV CX,LEN

 MOV CX,LEN is used to move or assign value 8 (Length of Array) to  CX. 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 – LOOP1:

LOOP1: is a LABEL and all the words ending in colon (:).

Next Line – MOV AH,0

MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it.

Next Line –  MOV AL,[BX]

MOV AL,[BX]  means move value in Address of BX register to AL register. [] is Refered as Value present in the Address of the Register in it.

Next Line – MOV DL,TAX
       MUL DL

The above line code is used to Multiply Element of Array by TAX  i.e.  value present in AX register

MUL TAX cannot be used hence We first move TAX to DL register i.e. MOV DL,TAX. After that MUL DL in this line DL register will be Multiplied with AX register (BY DEFAULT) Now, This will give (PRICE * 12).

Next Line – MOV DL,100
    DIV DL

The above line code is used to Divide AX register by 100 i.e. (PRICE * 12) value present in AX register

DIV 100 cannot be used hence We first move 100 to BX register i.e. MOV DL,100. After that DIV DL in this line value in BX register will Divide  AX:DX register (BY DEFAULT) in which the resultant value of (PRICE * 12) is present. Now, This will give (PRICE * 12)/100.

Next Line – MOV AH,[BX]
       ADD AH,AL

MOV AH,[BX]  means move value in Address of BX register to AL register. [] is Refered as Value present in the Address of the Register in it. So that 12% which is in AL register can be added with original Price moved in AH register. ADD AL,TAX means move value in AL register from variable TAX. DAA means Decimal Adjust after Addition. e.g. 36H is present in AL register, If added with 0AH will give 40H. After DAA it will Convert to Decimal equivalent to 46H.

Next Line –  MOV [BX],AL

MOV [BX],AL  means move value in AL register to Address of BX register. [] is Refered as Value present in the Address of the Register in it.

 Next Line –  INC BX

INC BX will increment the Address 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 Eight Prices of Items.

Next Line – LOOP LOOP1

This 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 :-

 Asm_program_Calc_SalesTaxToList

Before Execution :-

Asm_program_Calc_SalesTaxToList_Output1

After Execution :-

Asm_program_Calc_SalesTaxToList_Output2

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

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

Now we will write another Assembly program to sort a given set of 8-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 ARR and 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.

Next Line – ARR DB 15,12,18,13,19,16,14,20,11,17
     LEN DW $-ARR 

 ARR DB 15,12,18,13,19,16,14,20,11,17 this line is a declaration of 8-bit Numbers Array initialized with 15,12,18,13,19,16,14,20,11,17 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 DB data type the numbers which we are adding will be integers so DB is sufficient.

DATA SEGMENT
     ARR DB 15,12,18,13,19,16,14,20,11,17
     LEN DW $-ARR
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     ARR DB 15,12,18,13,19,16,14,20,11,17
     LEN DW $-ARR
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
             
      MOV CX,LEN-1   
OUTER:
      LEA SI,ARR
      MOV BX,0       
INNER:
      INC BX
      MOV AL,ARR[SI]
      INC SI
      CMP AL,ARR[SI]
      JB SKIP
     
      XCHG AL,ARR[SI]
      MOV ARR[SI-1],AL
     
SKIP:
      CMP BX,CX
      JL  INNER
                
      LOOP OUTER
            
      MOV AH,4CH
      INT 21H     
CODE ENDS
END START

Explanation : 

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-1

MOV CX,LEN-1 is used to move or assign value 10-1 i.e. 9 (Length of Array – 1) to  CX. 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,ARR in 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 AL,ARR[SI]
      INC SI

MOV AL,ARR[SI]  means move value in Address of SI to AL register. [] is Refered as Value present in the Address of the Register in it. INC SI will increment the Address value present in SI register. 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 AL,ARR[SI]
      JB SKIP

 CMP AL,ARR[SI] is used to compare AL 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 AL,ARR[SI]
      MOV ARR[SI-1],AL

XCHG AL,ARR[SI] LOOP1:is used to Exchange the values in Address or Register. MOV ARR[SI-1],AL means move value in AL register to Address of SI-1 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,CX is 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 OUTER This 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 :-

Asm_program_Ascend_Order_8-bit

Before Execution :-

Asm_program_Ascend_Order_8-bit_Output1

After Execution :-

Asm_program_Ascend_Order_8-bit_Output2

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

An Assembly program to arrange given N numbers in Descending order

Now we will write another Assembly program to to arrange given N numbers in Descending 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 ARR and 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.

Next Line – ARR DB 15,12,18,13,19,16,14,20,11,17
     LEN DW $-ARR 

 ARR DB 15,12,18,13,19,16,14,20,11,17 this line is a declaration of 8-bit Numbers Array initialized with 15,12,18,13,19,16,14,20,11,17 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 DB data type the numbers which we are adding will be integers so DB is sufficient.

DATA SEGMENT
     ARR DB 15,12,18,13,19,16,14,20,11,17
     LEN DW $-ARR
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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 d stands 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).

DATA SEGMENT
     ARR DB 15,12,18,13,19,16,14,20,11,17
     LEN DW $-ARR
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
             
      MOV CX,LEN-1   
OUTER:
      LEA SI,ARR
      MOV BX,0       
INNER:
      INC BX
      MOV AL,ARR[SI]
      INC SI
      CMP ARR[SI],AL
      JB SKIP
     
      XCHG AL,ARR[SI]
      MOV ARR[SI-1],AL
     
SKIP:
      CMP BX,CX
      JL  INNER
                
      LOOP OUTER
            
      MOV AH,4CH
      INT 21H     
CODE ENDS
END START

Explanation : 

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-1

MOV CX,LEN-1 is used to move or assign value 10-1 i.e. 9 (Length of Array – 1) to  CX. 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,ARR in 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 AL,ARR[SI]
      INC SI

MOV AL,ARR[SI]  means move value in Address of SI to AL register. [] is Refered as Value present in the Address of the Register in it. INC SI will increment the Address value present in SI register. 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 ARR[SI],AL
      JB SKIP

CMP ARR[SI],AL  is used to compare Element of Array present in ARR[SI] with AL register 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 AL,ARR[SI]
      MOV ARR[SI-1],AL

XCHG AL,ARR[SI] LOOP1:is used to Exchange the values in Address or Register. MOV ARR[SI-1],AL means move value in AL register to Address of SI-1 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,CX is 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 OUTER This 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 :-

Asm_program_Desc_Order_8-bit

Before Execution :-

Asm_program_Desc_Order_8-bit_Output1

After Execution :-

Asm_program_Desc_Order_8-bit_Output2

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