Monthly Archives: February 2014


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

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 variables to be Added and it will be NUM1, NUM2 and NUM3. Other variables will be holding the Output or Result of the Average and it will be AVG So in all Four variables.
The identified variables are NUM1, NUM2, NUM3 and AVG.

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 DB 5
     NUM2 DB 9
     NUM3 DB 7
     AVG  DB ?

We are initializing NUM1 to 5 (Blank (or Nothing after number) stands for Decimal ( By Default) ),  NUM2 to 9 ((Blank (or Nothing after number) stands for Decimal ( By Default) ), NUM3 to 7 (Blank (or Nothing after number) stands for Decimal ( By Default) ), AVG 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
     NUM1 DB 5
     NUM2 DB 9
     NUM3 DB 7
     AVG  DB ?
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 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.

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

Next Line – 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

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_Avg_3_Nums

Before Execution :-

Asm_program_Avg_3_Nums_V1

After Execution :-

Asm_program_Avg_3_Nums_V2

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

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

Now we will write another Assembly program to read  two decimal inputs and print out the smaller of the two, in decimal

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 and Other variables will be holding the Messages “ENTER FIRST NUMBER TO COMPARE”, “ENTER SECOND NUMBER TO COMPARE” and “SMALLER NUMBER IS”  to be printed for the User, So in all  Five variables.
The identified variables are NUM1, NUM2, MSG1, MSG2 and MSG3.

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

We are initializing NUM1 and NUM2 to ? (? stands for blank value), As we are accepting values from User from Console. Detailed explanation is given below.

 MSG1 DB 10,13,”ENTER FIRST NUMBER TO COMPARE : $” this line is a declaration of Charater Array initialized with “ENTER FIRST NUMBER TO COMPARE : $”. 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 MSG2 and MSG3. 

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

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

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

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

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

Next Line – MOV AL,NUM1

The above line code is to move NUM1 to AL register as we want to compare NUM1 with NUM2.

Next Line – CMP AL,NUM2
      JG SECOND

The above two line code is used to compare two variables and jump to the respective LABEL.

 cmpjg

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.

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

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

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_Smaller_of_Two_Numbers

Output After Execution :-

 Asm_program_Smaller_of_Two_Nums_Output

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

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

Now we will write another Assembly program to read  two decimal inputs and print out their sum, in decimal

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 variable will be holding the Output or Result of the Addition and it will be RESULT and Other variables will be holding the Messages “ENTER FIRST NUMBER TO ADD “, “ENTER SECOND NUMBER TO ADD” and “RESULT OF ADDITION IS”  to be printed for the User, So in all  Six variables.
The identified variables are NUM1, NUM2, RESULT, MSG1, MSG2 and MSG3.

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

We are initializing NUM1, NUM2 and RESULT to ? (? stands for blank value), As we are accepting values from User from Console. Detailed explanation is given below.

 MSG1 DB 10,13,”ENTER FIRST NUMBER TO MULTIPLY : $” this line is a declaration of Charater Array initialized with “ENTER FIRST NUMBER TO MULTIPLY : $”. 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 MSG2 and MSG3. 

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

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

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

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

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

add 

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

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

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

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

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

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

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

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_2_Numbers_Print

Output After Execution :-

 Asm_program_Add_2_Numbers_Print_Output

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

An Assembly program to convert ASCII code to its BCD equivalent

Now we will write another Assembly program to convert ASCII code to its BCD equivalent.

Let’s identify variables needed for this program.
First variable will be the one which will hold the values entered at Console in its ASCII code and it will be NUM. Other variable will be holding BCD equivalent of the ASCII code and it will be BCD. Other variable will be holding the Message “ENTER CHARACTER” to be printed for the User, So in all Three variables.
The identified variables are NUM, BCD and MESSAGE.

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 – MESSAGE DB “ENTER CHARACTER :$”
X DB ?

MESSAGE DB “ENTER CHARACTER :$” this line is a declaration of Charater Array initialized with “ENTER CHARACTER :$” 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 ) NUM, BCD 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 because Character needs BYTE which means DB is sufficient.

DATA SEGMENT
     MESSAGE DB "ENTER CHARACTER :$" 
     NUM DB ?
     BCD DB ?
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
     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.

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

Next Line – 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

Next 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.

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.

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.

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_Ascii_To_BCD_Number

Output:-

Asm_program_Ascii_To_BCD_Number_Output

Variables (Before Execution):-

Asm_program_Ascii_To_BCD_Number_V1

Variables (After Execution):-

Asm_program_Ascii_To_BCD_Number_V2

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

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

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

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 Multiplied and it will be NUM1 and NUM2. Other variable will be holding the Output or Result of the Multiplication and it will be RESULT and Other variables will be holding the Messages “ENTER FIRST NUMBER TO MULTIPLY “, “ENTER SECOND NUMBER TO MULTIPLY” and “RESULT OF MULTIPLICATION IS”  to be printed for the User, So in all  Six variables.
The identified variables are NUM1, NUM2, RESULT, MSG1, MSG2 and MSG3.

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

We are initializing NUM1, NUM2 and RESULT to ? (? stands for blank value), As we are accepting values from User from Console. Detailed explanation is given below.

 MSG1 DB 10,13,”ENTER FIRST NUMBER TO MULTIPLY : $” this line is a declaration of Charater Array initialized with “ENTER FIRST NUMBER TO MULTIPLY : $”. 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 MSG2 and MSG3. 

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

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

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

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

 Next Line – 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.
     
Next Line – 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 – MUL NUM1

The above line code is used to multiply the two variables and save the result in another variable.

 mult

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

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

ascii 

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

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

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

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

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

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_Mult_Two_Numbers

Output After Execution :-

 Asm_program_Mult_Two_Numbers_Output

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

An Assembly program to find the sum of two BCD numbers stored in memory

Now we will write another Assembly program to find the sum of two BCD 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 variables to be Added and it will be NUM1 and NUM2. Other variables will be holding the Output or Result of the Addition and it will be RESULT So in all Three variables.
The identified variables are NUM1, NUM2 and RESULT.

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 DB 9H
NUM2 DB 7H
RESULT DB ?

We are initializing NUM1 to 9H (H or h stands for Hexadecimal),  NUM2 to 7H (H or h stands for Hexadecimal), RESULT 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
     NUM1 DB 9H
     NUM2 DB 7H
     RESULT DB ?
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 DB 05
     NUM2 DB 06
     RESULT DB ?
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      MOV AL,NUM1
      ADD AL,NUM2
        
      MOV RESULT,AL
     
      MOV AH,0
      AAA 
     
      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 AL,NUM1
ADD AL,NUM2
MOV RESULT,AL

The above three line code is used to add the two variables and save the result in another variable.

add

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

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

Lets understand with results found in variables and registers.

Asm_program_Add_Two_BCD_Numbers_v1Asm_program_Add_Two_BCD_Numbers_v2

here we can see two BCD numbers having 05 and 06 respectively. Before execution RESULT  is 0 (zero)  and After execution RESULT  is B (B means Eleven in HEXadecimal).

Asm_program_Add_Two_BCD_Nums_R1Asm_program_Add_Two_BCD_Nums_R2

The Result of Addition is only in AL register. AH contains 07 which is garbage value, Hence we clear the value by assigning zero to it, Before using AAA which coverts B in AL to 01 in AH and 01 in AL MAKING it one one (11) eleven in BCD form.

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_Two_BCD_Numbers

Before Execution :-

Asm_program_Add_Two_BCD_Numbers_v1

After Execution :-

Asm_program_Add_Two_BCD_Numbers_v2

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

An Assembly program to exchange two memory variables using XCHG instruction

Now we will write another Assembly program which does some Exchange Operation.

 XCHG

Before actually starting to write the program, Let’s understand XCHG instruction.

As we know the programs work only with the instructions in the instruction set. Instruction XCHG is used to EXCHANGE memory variables 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 value can be Exchanged with memory value.

Second permutation :- memory , REG means memory value can be Exchanged with Register value.

Third permutation :- REG, REG means Register value can be Exchanged with Register value.

Here, we don’t have memory , memory permutation i.e. it means we can not Exchange any memory variable with another memory variable. Hence to do so we have to use combination of MOV and XCHG instructions. How we will see it below.

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.

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.So in all Two variables.
The identified variables are NUM1, and NUM2.

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 DB 9H
NUM2 DB 7H

We are initializing NUM1 to 9H (H or h stands for Hexadecimal),  NUM2 to 7H (H or h stands for Hexadecimal). 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
     NUM1 DB 9H
     NUM2 DB 7H
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 DB 9H
     NUM2 DB 7H
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      MOV AL,NUM1
      MOV BL,NUM2
        
      XCHG AL,NUM2
      XCHG BL,NUM1 
     
      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 AL,NUM1
      MOV BL,NUM2

The above two line code is used to move the two variables i.e. NUM1 and NUM2 to AL and BL registers respectively.

Next Line – XCHG AL,NUM2
      XCHG BL,NUM1

The above two line code is used to Exchange NUM2 variable to AL registers and to Exchange NUM1 variable to BL registers respectively.

Now, we have understood part of it to Exchange to number we can write XCHG NUM1, NUM2, But there is no permutation for XCHG memory, memory, Hence we have to send one memory variable to AL or AX depending on DB or DW and send another one memory variable to BL or BX depending on DB or DW. Now we are taking DB, So we have t0 instruction XCHG AL,NUM2 Exchange NUM2 variable value to AL Register in which value of NUM1 variable is already present XCHG BL,NUM1 Exchange NUM1 variable value to BL Register in which value of NUM2 variable is already present. Finally we have Exchanged NUM1 with NUM2 memory variables.

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_XCHG_Two_Numbers

Before Execution :-

Asm_program_XCHG_Two_Numbers_Variables

After Execution :-

Asm_program_XCHG_Two_Numbers_Variables2

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