Saturday 8 November 2014

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.

No comments:

Post a Comment