Saturday 8 November 2014

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

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.

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
MOV RESULT,AL



The above three 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.
 

No comments:

Post a Comment