Saturday 8 November 2014

Assembly program to convert Centigrade (Celsius) to Fahrenheit temperature measuring scales

DATA SEGMENT
     T    DB ?
     RES  DB 10 DUP ('$')
     MSG1 DB "ENTER TEMPERATURE IN CELSIUS (ONLY IN 2 DIGITS) : $"
     MSG2 DB 10,13,"CONVERTED IS FAHRENHEIT (TEMPERATURE) : $"
    
DATA ENDS
CODE SEGMENT
        ASSUME DS:DATA,CS:CODE
START:       
    MOV AX,DATA
    MOV DS,AX
   
    LEA DX,MSG1
    MOV AH,9
    INT 21H  
   
    MOV AH,1
    INT 21H
   
    SUB AL,30H
    MOV AH,0
   
    MOV BL,10
    MUL BL
    MOV BL,AL
       
    MOV AH,1
    INT 21H
   
    SUB AL,30H
    MOV AH,0
    ADD AL,BL
    MOV T,AL
         
    MOV DL,9
    MUL DL
   
    MOV BL,5
    DIV BL
    MOV AH,0
   
    ADD AL,32
   
    LEA SI,RES
    CALL HEX2DEC
   
    LEA DX,MSG2
    MOV AH,9
    INT 21H 
   
    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.
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.
    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 )
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.
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.
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
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.
Now, We have to Scan two values from the Screen BY reading two individual Character and convert it back to the original value entered. What we will do is except 25 in this 32H and 35H will be two ASCII values received. From 32H first digit substract 30H i.e. 32 -30 = 2, Muliply two with 10 to get 20 i.e. 2*10 = 20, 35h will From 35H Second digit substract 30H i.e. 35 -30 = 5, Add five with 20 to get i.e. 20 + 5 = 25.
MOV AH,1     INT 21H
The above two line code is used to Read a Character from Console (First Digit).
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.
SUB AL,30H     MOV AH,0
Convert AL to its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5. SUB AL,30H means subtracting 30H from AL. MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it.
Now, Let me tell you why we need to clear AH register. Because we will Multiply AL register by 10. and If we do not clear unwanted value we will get Wrong Product.
MOV BL,10     MUL BL     MOV BL,AL
The above three line code is used to Move 10 to BL register and Multiply 10 with AL register
MUL 10 cannot be used hence We first move 10 to BL register i.e. MOV BL,10. After that MUL BL in this line BX register will be Multiplied with AX register (BY DEFAULT) in which the resultant value of 10X is present. MOV BL,AL is used to move Result to BL.
MOV AH,1     INT 21H
The above two line code is used to Read a Character from Console(Second Digit).
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.
SUB AL,30H     MOV AH,0     ADD AL,BL     MOV T,AL
Convert AL to its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5. SUB AL,30H means subtracting 30H from AL. MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it. ADD AL,BL adding AL with BL register to get the Original Value entered by User. MOV T,AL means move value in AL register into variable T.
Now, Let me tell you why we need to clear AH register. Because we will Multiply AL register by 10. and If we do not clear unwanted value we will get Wrong Product.
MOV DL,9     MUL DL
The above line code is used to Multiply AX register by 9 i.e. Celsius value present in AX register
MUL 9 cannot be used hence We first move 9 to DL register i.e. MOV DL,9. After that MUL DL in this line DL register will be Multiplied with AX register (BY DEFAULT) in which the Temperature in Celsius value is present. Now, This will give (Celsius * 9).
MOV BL,5     DIV BL
The above line code is used to Divide AX register by 5 i.e. (Celsius * 9) value present in AX register
DIV 5 cannot be used hence We first move 5 to BL register i.e. MOV BL,5. After that DIV BL in this line value in BL register will Divide  AX register (BY DEFAULT) in which the resultant value of (Celsius * 9) is present. Now, This will give (Celsius * 9 / 5).
MOV AH,0        ADD AL,32
The above line code is used to ADD 32 to BX register and Store the resultant { (Celsius * 9 / 5) + 32 } in it. MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it. ADD AL,32  is used to ADD AL register to 32  and Store the resultant { (Celsius * 9 / 5) + 32 } in it.
  LEA SI,RES         CALL HEX2DEC
The above Two line code is used to initialize RES to SI register and Call Procedure HEX2DEC which will covert AX register value as result and Print it on user screen.
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
LEA DX,MSG2       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 i.e. for MSG2.
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.
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.
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.
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.
 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.
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.
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.
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.
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).
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.
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.
 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.
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.
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).
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.
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.
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.

Assembly program to Evaluate sphere volume if radius is given

DATA SEGMENT
     R    DB ?
     RES  DB 10 DUP ('$')
     MSG1 DB "ENTER RADIUS : $"
     MSG2 DB 10,13,"VOLUME : $"
DATA ENDS
CODE SEGMENT
        ASSUME DS:DATA,CS:CODE
START:       
    MOV AX,DATA
    MOV DS,AX
   
    LEA DX,MSG1
    MOV AH,9
    INT 21H  
   
    MOV AH,1
    INT 21H
   
    SUB AL,30H
    MOV R,AL
    MOV AH,0
   
    MUL R
    MUL R
   
    MOV DL,88
    MUL DL
   
    MOV DX,0
   
    MOV BX,21
    DIV BX
   
    LEA SI,RES
    CALL HEX2DEC
   
    LEA DX,MSG2
    MOV AH,9
    INT 21H 
   
    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.
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.
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 )
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.
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 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
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.
MOV AH,1       INT 21H
 The above three line code is used to Read a Character from Console and save the value entered in variable R in its ASCII form.
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.
SUB AL,30H     MOV R,AL     MOV AH,0
The above Two line code is used to convert the value entered in variable R from ASCII form to its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5. SUB AL,30H means subtracting 30H from AL. MOV R,AL  means move value in AL register into variable R. MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it.
MUL R     MUL R
The above line code is used to Multiply R i.e. Radius with the Radius present in AX register
MUL R in this line R variable will be Multiplied with AL register (BY DEFAULT). This will give R*R (R SQUARE). Now, again MUL R in this line R variable will be Multiplied with AL register (BY DEFAULT). This will give R*R*R (R CUDE).
Now, Let me tell you Formula of Volume of Sphere :- 4/3 pi r*r*r  ( pi= 22/7 )  This can be written (4*22*r*r*r)/(3/7) future simplified will give you (r*r*r*88)/21. <—-this is what we are IMPLIMENTING.
MOV DL,88     MUL DL
The above line code is used to Multiply AX register by 88 i.e. R*R*R (R CUDE) value present in AX register
MUL 88 cannot be used hence We first move 88 to DL register i.e. MOV DL,88. After that MUL DL in this line DL register will be Multiplied with AX register (BY DEFAULT) in which the resultant value of R*R*R (i.e. R CUDE) is present. Now, This will give (R*R*R*88).
MOV DX,0
MOV DX,0 is used to clear the unwanted value (garbage value) in DX register is removed by assigning ZERO to it.
This is done for Dividing the Bigger Value inside the AX register and the Divisor  will not fit inside 8 bit register AL register. Hence we have to use AX register to get the Divisor this can be  done by Dividing 16 bit register. When we Divide 16 bit register AX:DX both registers are considered for Division. Here AX register will hold Quoitent and DX register will hold Remainder. 
MOV BX,21     DIV BX
The above line code is used to Divide AX register by 21 i.e. (R*R*R*88) value present in AX register
DIV 21 cannot be used hence We first move 21 to BX register i.e. MOV BX,21. After that DIV BX in this line value in BX register will Divide  AX:DX register (BY DEFAULT) in which the resultant value of (R*R*R*88) is present. Now, This will give (R*R*R*88)/21.
LEA SI,RES     CALL HEX2DEC
The above Two line code is used to initialize RES to SI register and Call Procedure HEX2DEC which will covert AX register value as result and Print it on user screen.
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
LEA DX,MSG2       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 i.e. for MSG2.
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.
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.
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.
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.
 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.
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.
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.
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.
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).
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.
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.
 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.
 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.
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).
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.
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.
END START
END START is the end of the label used to show the ending point of the code whichis written in the Code Segment.

Assembly program to calculate the average of three given numbers stored in memory

DATA SEGMENT
     NUM1 DB 5
     NUM2 DB 9
     NUM3 DB 7
     AVG  DB ?
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      MOV AL,NUM1
      ADD AL,NUM2  
      ADD AL,NUM3
     
      MOV AH,0
     
      MOV DL,3
      DIV DL
     
      MOV AVG,AL
           
      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.
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.
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 )
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.
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.
MOV AL,NUM1       ADD AL,NUM2         ADD AL,NUM3
The above three line code is used to add the three variables and the result in saved by default in AL reister.
 Now we have to send one number to AL or AX depending on DB or DW. AX Register is called Accumalator. and is used for holding the result of Addition in it After Addition. Now we are taking DB, So we have t0 instruction MOV AL,NUM1 move NUM1 variable value to AL Register.  After moving NUM1 to AL, We can Add REG to memory, So we have ADD AL,NUM2 or (We can Add memory to REG , So we have ADD NUM2,AL) Both are allowed as per permutations so use one from two. Now, the Resultant Value is saved in Accumalator AL for DB and AX for DW.
MOV AH,0             MOV DL,3             DIV DL
So divide Resultant value by 3 so that Average is found in AL register, But the DIV instruction is used to divide AX and Not only AL register. Hence the unwanted value (garbage value) in AH register is removed by assigning ZERO to it. i.e. MOV AH,0
DIV instruction only works with REG or MEMORY hence we cannot use DIV 3 where 3 is immediate, So we have to move 3 to DL register (we can take any register) i.e. MOV DL,3 and Then DIV DL  Now DIV DL will Divide AX register with 3 which is passed to DL register and Result of division is present in AL register so move the resultant value i.e. Average to variable by instruction MOV AVG,AL
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.
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.
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.

Assembly program to read in two decimal inputs and print out the smaller of the two, in decimal

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
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA DX,MSG1
      MOV AH,9
      INT 21H
     
      MOV AH,1
      INT 21H
      MOV NUM1,AL
     
      LEA DX,MSG2
      MOV AH,9
      INT 21H
     
      MOV AH,1
      INT 21H
      MOV NUM2,AL
     
      LEA DX,MSG3
      MOV AH,9
      INT 21H
     
      MOV AL,NUM1
     
      CMP AL,NUM2
      JG SECOND
      
FIRST:  MOV AH,2
        MOV DL,NUM1
        INT 21H
        JMP EXIT
     
SECOND: MOV AH,2
        MOV DL,NUM2
        INT 21H
     
EXIT: 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.
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.
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 )
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.
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.
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
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.
MOV AH,1       INT 21H        MOV NUM1,AL
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.
MOV NUM1,AL  means move value in AL register into variable NUM1.
LEA DX,MSG2       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 i.e. for MSG2.       MOV AH,1       INT 21H       MOV NUM2,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM2 in its ASCII form.
LEA DX,MSG3       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 i.e. for MSG3.
 MOV AL,NUM1
The above line code is to move NUM1 to AL register as we want to compare NUM1 with NUM2.
CMP AL,NUM2       JG SECOND
The above two line code is used to compare two variables and jump to the respective LABEL.
As we know the programs work only with the instructions in the instruction set. Instruction ADD is used to add to numbers in the following permutations above. REG stands for Registers (Eg.  AX, BX, CX, DX ). memory stands for Variable or Address. immediate stands for Numbers or Values. Let us understand the meanings of the above permutations.
First permutation :- REG , memory means Register can be added with memory.
Second permutation :- memory , REG means memory can be added with Register.
Third permutation :- REG, REG means Register can be added with Register.
Fourth permutation :- memory , immediate means memory can be added with immediate.
Fifth permutation :- REG, immediate means Register can be added with immediate.
Note :- In the permutations above it will work only in the order mentioned above and not by interchanging the first to second and second to first.
Now, we have understood part of it to add to number we can write CMP NUM1, NUM2, But there is no permutation for CMP memory, memory, Hence we have to send one number to AL or AX depending on DB or DW. The result of Comparision is not stored anywhere, but flags are set according to result.
JG SECOND 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:  MOV AH,2         MOV DL,NUM1         INT 21H         JMP EXIT
The above Four line code is used to Write a Character on Console present in DL register i.e. NUM1.
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 NUM1 variable. JMP is Unconditional Jump. Label FIRST is to tell that first number is going to be printed and If there is no JUMP in between then Label SECOND will be printed showing both Variables on console. Hence   JMP EXIT so that Label SECOND is not executed.

SECOND: MOV AH,2         MOV DL,NUM2         INT 21H
The above Three line code is used to Write a Character on Console present in DL register i.e. NUM2.
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.
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.
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.

Assembly program to read in two decimal inputs and print out their sum, in decimal

DATA SEGMENT
     NUM1 DB ?
     NUM2 DB ?
     RESULT DB ?
     MSG1 DB 10,13,"ENTER FIRST NUMBER TO ADD : $"
     MSG2 DB 10,13,"ENTER SECOND NUMBER TO ADD : $"  
     MSG3 DB 10,13,"RESULT OF ADDITION IS : $"
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA DX,MSG1
      MOV AH,9
      INT 21H
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV NUM1,AL
     
      LEA DX,MSG2
      MOV AH,9
      INT 21H
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV NUM2,AL
     
      ADD AL,NUM1
              
      MOV RESULT,AL
     
      MOV AH,0 
      AAA
     
      ADD AH,30H
      ADD AL,30H 
     
      MOV BX,AX
     
      LEA DX,MSG3
      MOV AH,9
      INT 21H
     
      MOV AH,2
      MOV DL,BH
      INT 21H
     
      MOV AH,2
      MOV DL,BL
      INT 21H
     
      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.
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.
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 )
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.
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.
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
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.
MOV AH,1       INT 21H       SUB AL,30H       MOV NUM1,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM1 in its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5.
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.
SUB AL,30H means subtracting 30H from AL.
MOV NUM1,AL  means move value in AL register into variable NUM1.
LEA DX,MSG2       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 i.e. for MSG2.       MOV AH,1       INT 21H       SUB AL,30H       MOV NUM2,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM2 in its BCD form.
Next Line – ADD AL,NUM1
The above line code is used to add the two variables and save the result in another variable.
As we know the programs work only with the instructions in the instruction set. Instruction ADD is used to add to numbers in the following permutations above. REG stands for Registers (Eg.  AX, BX, CX, DX ). memory stands for Variable or Address. immediate stands for Numbers or Values. Let us understand the meanings of the above permutations.
First permutation :- REG , memory means Register can be added with memory.
Second permutation :- memory , REG means memory can be added with Register.
Third permutation :- REG, REG means Register can be added with Register.
Fourth permutation :- memory , immediate means memory can be added with immediate.
Fifth permutation :- REG, immediate means Register can be added with immediate.
Note :- In the permutations above it will work only in the order mentioned above and not by interchanging the first to second and second to first.
Now, we have understood part of it to add to number we can write ADD NUM1, NUM2, But there is no permutation for ADD m emory, memory, Hence we have to send one number to AL or AX depending on DB or DW. AX Register is called Accumalator. and is used for holding the result of Addition in it After Addition. Now we are taking DB, So we have t0 instruction MOV AL,NUM1 move NUM1 variable value to AL Register.  After moving NUM1 to AL, We can Add REG to memory, So we have ADD AL,NUM2 or (We can Add memory to REG , So we have ADD NUM2,AL) Both are allowed as per permutations so use one from two. Now, the Resultant Value is saved in Accumalator AL for DB and AX for DW, So move Resultant value to RESULT variable by instruction MOV RESULT,AL
  MOV AH,0      AAA
 The first line is just to clear the unwanted garbage value present in AH  register as the AH register is going to be used later. The above two line code is used to corrects result in AH and AL after addition when working with BCD values . AAA means ASCII Adjust after Addition. AAA 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.
ADD AH,30H       ADD AL,30H
Since the Result of Multiplication is in AH and AL register in BCD form After using AAM. Now we want to print the result on screen or console. the BCD form value will not show us the Result But will print the Coresponding Ascii Codes of the number, Hence By adding 30H to BCD will Convert it to ASCII code which will print the digit (number) on screen.
MOV BX,AX
Since the AH register is used again and again. We cannot lose the result in AH and AL register, So to save the to be printed value in BH and BL register. By moving AX to BX.
LEA DX,MSG3       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 i.e. for MSG3.
MOV AH,2       MOV DL,BH       INT 21H
The above Three line code is used to Write a Character on Console present in BH 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 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.
MOV AH,2       MOV DL,BL       INT 21H
The above Three line code is used to Write a Character on Console present in BL register.
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.
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.
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.

Assembly program to convert ASCII code to its BCD equivalent

DATA SEGMENT
     MESSAGE DB "ENTER CHARACTER :$"
     NUM DB ?
     BCD DB ?
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA DX,MESSAGE      
      MOV AH,9
      INT 21H     
     
      MOV AH,1
      INT 21H
      MOV NUM,AL
     
      SUB AL,30H
      MOV BCD,AL 
     
      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.
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.
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 )
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.
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.
LEA DX,MESSAGE             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
LEA DX,MESSAGE  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 MESSAGE 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.
MOV AH,1       INT 21H       MOV NUM,AL
The above three line code is used to Read a Character from Console and save the value entered in variable NUM in its ASCII form.
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.
MOV NUM,AL  means move value in AL register into variable NUM.
Now, lets understand line by line
SUB AL,30H       MOV BCD,AL
The above Two line code is used to convert the value entered in variable NUM from ASCII form to its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5.
SUB AL,30H means subtracting 30H from AL.
MOV BCD,AL  means move value in AL register into variable BCD.
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.
MOV AH,4CH This same code can be interchangably written as MOV AX,4C00H where AX register is initialized with 4C00H which means 4C gets saved in AH register and 00 gets saved in AL register. different books follow different forms.
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.
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.

Assembly program to read two decimal numbers, then multiply them together and finally print out the result (in decimal )

DATA SEGMENT
     NUM1 DB ?
     NUM2 DB ?
     RESULT DB ?
     MSG1 DB 10,13,"ENTER FIRST NUMBER TO MULTIPLY : $"
     MSG2 DB 10,13,"ENTER SECOND NUMBER TO MULTIPLY : $"  
     MSG3 DB 10,13,"RESULT OF MULTIPLICATION IS : $"
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA DX,MSG1
      MOV AH,9
      INT 21H
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV NUM1,AL
     
      LEA DX,MSG2
      MOV AH,9
      INT 21H
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV NUM2,AL
     
      MUL NUM1
              
      MOV RESULT,AL
      AAM
     
      ADD AH,30H
      ADD AL,30H 
     
      MOV BX,AX
     
      LEA DX,MSG3
      MOV AH,9
      INT 21H
     
      MOV AH,2
      MOV DL,BH
      INT 21H
     
      MOV AH,2
      MOV DL,BL
      INT 21H
     
      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.
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.
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 )
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.
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.
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
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.
MOV AH,1       INT 21H       SUB AL,30H       MOV NUM1,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM1 in its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5.
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.
SUB AL,30H means subtracting 30H from AL.
MOV NUM1,AL  means move value in AL register into variable NUM1.
 LEA DX,MSG2       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 i.e. for MSG2.       MOV AH,1       INT 21H       SUB AL,30H       MOV NUM2,AL
The above Four line code is used to Read a Character from Console and save the value entered in variable NUM2 in its BCD form.
MUL NUM1
The above line code is used to multiply the two variables and save the result in another variable.

As we know the programs work only with the instructions in the instruction set. Instruction MUL is used to multiply two numbers in the following permutations above. REG stands for Registers (Eg.  AX, BX, CX, DX ). memory stands for Variable or Address.  Let us understand the meanings of the above permutations.
First permutation :- REG means Register can be multiplied with AL register (BY DEFAULT).
Second permutation :- memory means memory can be multiplied with AL register (BY DEFAULT).
Now, we have understood part of it to multiply two number With  AL register (BY DEFAULT) we can write MUL NUM1 and MUL BL provided the value to be multiplied is passed to BL register in prior. Now, the Resultant Value is saved in Accumalator AX for DB and (DX AX) for DW, So move Resultant value to RESULT variable by instruction MOV RESULT,AL
 AAM
AAM means ASCII Adjust after Multiplication. AAM Instruction has NO Operands or values attached to it. If we are multipling two BCD numbers the Result is saved in AX register in HEXadecimal form. What AAM 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.
The Result of Multiplication is in AH and AL register in BCD form form After using AAM.
ADD AH,30H       ADD AL,30H
Since the Result of Multiplication is in AH and AL register in BCD form After using AAM. Now we want to print the result on screen or console. the BCD form value will not show us the Result But will print the Coresponding Ascii Codes of the number, Hence By adding 30H to BCD will Convert it to ASCII code which will print the digit (number) on screen.
MOV BX,AX
Since the AH register is used again and again. We cannot lose the result in AH and AL register, So to save the to be printed value in BH and BL register. By moving AX to BX.
LEA DX,MSG3       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 i.e. for MSG3.
MOV AH,2       MOV DL,BH       INT 21H
The above Three line code is used to Write a Character on Console present in BH 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 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.
MOV AH,2       MOV DL,BL       INT 21H
The above Three line code is used to Write a Character on Console present in BL register.
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.
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.
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.