Monthly Archives: May 2014


Write an algorithm and draw a corresponding flow chart to print the sum of the digits of a given number

Write an algorithm and draw a corresponding flow chart to print the sum of the digits of a given number 10m Dec2005 

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm is expressed in pseudo code – something resembling C language or Pascal, but with some statements in English rather than within the programming language

  1. A sequential solution of any program that written in human language, called algorithm.
  2. Algorithm is first step of the solution process, after the analysis of problem, programmers write the algorithm of that problem.

Pseudo code: 

  • Input a Number
  • Initialize Sum to zero
  • While Number is not zero
  •                Get Remainder by Number Mod 10
  •                Add Remainder to Sum
  •                Divide Number by 10
  • Print sum 

Detailed Algorithm:

Step 1:  Input N

Step 2:  Sum = 0

Step 3:  While (N != 0)

                        Rem = N % 10;

                        Sum = Sum + Rem;

                        N = N / 10;

Step 4:  Print Sum

 Flowchart:-

FCSumOfDigits 

Q Write a recursive program to find the G.C.D. of the two given numbers. 10m Dec2005 

Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-the-gcd-of-given-numbers-using-recursion

An Assembly Lanuage Program to search for a character in a given string and calculate the number of occurrences of the character in the given string

Now we will write another Assembly Lanuage Program to search for a character in a given string and calculate the number of occurrences of the character in the given string

Let’s identify variables needed for this program.
First variable will be the one which will hold the Strings entered by user in the variables P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) to save string given by user, Other variables will be holding character entered by the user, it will be CHAR, next  will be used for calculating number of occurences, it will be COUNT and Other variables will be holding the Messages ’ENTER ANY STRING :- $’, ’ENTER ANY CHARACTER :- $’, ‘NO, CHARACTER FOUND IN THE GIVEN STRING $’  and ’ CHARACTER(S) FOUND IN THE GIVEN STRING $’STRING IS : $’ to be printed for the User, So in all Eight variables.The identified variables are P11, CHAR, COUNT, MSG1, MSG2, MSG3, MSG4 and MSG5.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

Next Line – MSG1 DB 10,13,’ENTER ANY STRING :- $’
    MSG2 DB 10,13,’ENTER ANY CHARACTER :- $’
    MSG3 DB 10,13,’ $’
    MSG4 DB 10,13,’NO, CHARACTER FOUND IN THE GIVEN STRING $’ 
    MSG5 DB ‘ CHARACTER(S) FOUND IN THE GIVEN STRING $’
    CHAR DB ?
    COUNT DB 0
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)

P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) P22 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. Initialize CHAR to ? (? stands for blank value). COUNT to 0 (Zero). P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 and  P22 are the names refered for the Arrays in the program. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $).  MSG1 DB 10,13,’ENTER ANY STRING :- $’:- $’this line is a declaration of Charater Array initialized with ’ENTER ANY STRING :- $’  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 ). Similarly MSG2 DB 10,13,’ENTERED STRING IS :- $, ‘MSG3 DB 10,13,’LENGTH OF STRING IS :- $’, MSG4 DB 10,13,’NO, GIVEN STRING IS NOT A PALINDROME $’, MSG5 DB 10,13,’THE GIVEN STRING IS A PALINDROME $’ and MSG6 DB 10,13,’REVERSE OF ENTERED STRING IS :- $’.

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
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'ENTER ANY CHARACTER :- $'
    MSG3 DB 10,13,' $'
    MSG4 DB 10,13,'NO, CHARACTER FOUND IN THE GIVEN STRING $' 
    MSG5 DB ' CHARACTER(S) FOUND IN THE GIVEN STRING $'
    CHAR DB ?
    COUNT DB 0
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'ENTER ANY CHARACTER :- $'
    MSG3 DB 10,13,' $'
    MSG4 DB 10,13,'NO, CHARACTER FOUND IN THE GIVEN STRING $' 
    MSG5 DB ' CHARACTER(S) FOUND IN THE GIVEN STRING $'
    CHAR DB ?
    COUNT DB 0
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')   
DATA ENDS 
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H
       
        DISPLAY MSG2
       
        MOV AH,1
        INT 21H
        MOV CHAR,AL                           
       
        DISPLAY MSG3
       
        LEA SI,P11
                      
        MOV CL,L1
        MOV CH,0
       
CHECK:
        MOV AL,[SI]
        CMP CHAR,AL
        JNE SKIP
        INC COUNT       
SKIP:       
        INC SI
        LOOP CHECK
             
        CMP COUNT,0
        JE NOTFOUND
             
        DISPLAY MSG3
       
        MOV DL,COUNT
        ADD DL,30H
        MOV AH,2
        INT 21H 
               
        DISPLAY MSG5
        JMP EXIT 
NOTFOUND:      
        DISPLAY MSG4
               
EXIT:   MOV AH,4CH
        INT 21H
CODE ENDS
END START          

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line –  DISPLAY MSG2

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG2. This will display String Msg2 on Screen. 

Next Line – MOV AH,1
      INT 21H
      MOV CHAR,AL

The above three line code is used to Read a Character from Console and save the value entered in variable CHAR 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 CHAR,AL  means move value in AL register into variable CHAR.

Next Line –  DISPLAY MSG3

 DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen. 

Next Line –  LEA SI,P11

The above line code is used to initialize P11 to SI register. 

Next Line –   MOV CL,L1
        MOV CH,0

The above Two line code is used to Move L1 (i.e. Actual Length of String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – CHECK:

CHECK: is a LABEL and all the words ending in colon (:) are Labels.

Next Line –  MOV AL,[SI]
        CMP CHAR,AL
        JNE SKIP

MOV AL,[SI] Move value at Address of SI Register to AL register CMP CHAR,AL is used to compare AL (value of [SI]) with character present in CHAR  variable and JNE SKIP Short Jump if Not Equal i.e. [SI] is NotEqual to CHAR Than the Control jumps to the respective LABEL SKIP. The result of Comparision is not stored anywhere, but flags are set according to result.

Next Line –  INC COUNT

 INC COUNT will increment the value present in COUNT Variable by One.

Next Line – SKIP:

SKIP: is a LABEL and all the words ending in colon (:) are Labels.

Next Line –  INC SI

 INC SI will increment the value present in SI register by One.

Next Line – LOOP CHECK

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line –  CMP COUNT,0
        JE NOTFOUND

CMP COUNT,0 is used to compare 0 (i.e. ZERO) with value present in COUNT variable and JE NOTFOUND Short Jump if Equal i.e. 0 (i.e. ZERO) is Equal to COUNT Than the Control jumps to the respective LABEL NOTFOUND. The result of Comparision is not stored anywhere, but flags are set according to result.

Next Line –  DISPLAY MSG3

 DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen. 

  Next Line – MOV DL,COUNT
        ADD DL,30H
        MOV AH,2
        INT 21H

The above Four line code is used to Write a Character on Console present in COUNT variable (i.e. Number of occurences).

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

Next Line –  DISPLAY MSG5

 DISPLAY MSG5 is Calling of a Macro DISPLAY with Argument MSG5. This will display String Msg5 on Screen. 

JMP EXIT JMP is Unconditional Jump. This will Jump to Label EXIT.

Next Line –  NOTFOUND:
        DISPLAY MSG4

 NOTFOUND: is a LABEL and all the words ending in colon (:) are Labels. DISPLAY MSG4 is Calling of a Macro DISPLAY with Argument MSG4. This will display String Msg4 on Screen. 

Next Line –  EXIT:   MOV AH,4CH
        INT 21H

EXIT: is a LABEL and all the words ending in colon (:) are Labels. 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_Search_Char_in_Str

 Output After Execution :-

Asm_program_Search_Char_in_Str_Output

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

An Assembly Lanuage Program to determine a given string is a palindrome. If ‘Yes’ output the message “The given string is a palindrome”. If ‘No’ output the message “No, it is not a palindrome”.

Now we will write another Assembly Lanuage Program to determine a given string is a palindrome. If ‘Yes’ output the message “The given string is a palindrome”. If ‘No’ output the message “No, it is not a palindrome”.

Let’s identify variables needed for this program.
First variables will be the one which will hold the Strings entered by user in the variables P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) to concate characters of two strings given by user and Other variables will be holding the Messages ’ENTER ANY STRING :- $’, ’ENTERED STRING IS :- $’ and ’CONVERTED STRING IS : $’ to be printed for the User, So in all Eight variables.The identified variables are P11,P22, MSG1, MSG2, MSG3, MSG4, MSG5 and MSG6.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

Next Line – MSG1 DB 10,13,’ENTER ANY STRING :- $’
    MSG2 DB 10,13,’ENTERED STRING IS :- $’
    MSG3 DB 10,13,’LENGTH OF STRING IS :- $’
    MSG4 DB 10,13,’NO, GIVEN STRING IS NOT A PALINDROME $’ 
    MSG5 DB 10,13,’THE GIVEN STRING IS A PALINDROME $’
    MSG6 DB 10,13,’REVERSE OF ENTERED STRING IS :- $’ 
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)
    P22 DB 0FFH DUP (‘$’)

P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) P22 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 and  P22 are the names refered for the Arrays in the program. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $).  MSG1 DB 10,13,’ENTER ANY STRING :- $’:- $’this line is a declaration of Charater Array initialized with ’ENTER ANY STRING :- $’  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 ). Similarly MSG2 DB 10,13,’ENTERED STRING IS :- $, ‘MSG3 DB 10,13,’LENGTH OF STRING IS :- $’, MSG4 DB 10,13,’NO, GIVEN STRING IS NOT A PALINDROME $’, MSG5 DB 10,13,’THE GIVEN STRING IS A PALINDROME $’ and MSG6 DB 10,13,’REVERSE OF ENTERED STRING IS :- $’.

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
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'ENTERED STRING IS :- $'
    MSG3 DB 10,13,'LENGTH OF STRING IS :- $'
    MSG4 DB 10,13,'NO, GIVEN STRING IS NOT A PALINDROME $' 
    MSG5 DB 10,13,'THE GIVEN STRING IS A PALINDROME $'
    MSG6 DB 10,13,'REVERSE OF ENTERED STRING IS :- $' 
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
    P22 DB 0FFH DUP ('$')
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'ENTERED STRING IS :- $'
    MSG3 DB 10,13,'LENGTH OF STRING IS :- $'
    MSG4 DB 10,13,'NO, GIVEN STRING IS NOT A PALINDROME $' 
    MSG5 DB 10,13,'THE GIVEN STRING IS A PALINDROME $'
    MSG6 DB 10,13,'REVERSE OF ENTERED STRING IS :- $' 
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
    P22 DB 0FFH DUP ('$')
DATA ENDS 
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H
       
        DISPLAY MSG2
       
        DISPLAY P11
       
        DISPLAY MSG3
       
        MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H
       
        DISPLAY MSG6
               
        LEA SI,P11
        LEA DI,P22
       
        MOV DL,L1
        DEC DL
        MOV DH,0
        ADD SI,DX
        MOV CL,L1
        MOV CH,0
       
REVERSE:
        MOV AL,[SI]
        MOV [DI],AL
        INC DI
        DEC SI
        LOOP REVERSE
       
        DISPLAY P22
                      
        LEA SI,P11
        LEA DI,P22   
       
        MOV CL,L1
        MOV CH,0
       
CHECK:
        MOV AL,[SI]
        CMP [DI],AL
        JNE NOTPALIN
        INC DI
        INC SI
        LOOP CHECK
     
       
        DISPLAY MSG5
        JMP EXIT
NOTPALIN:
        DISPLAY MSG4
               
EXIT:   MOV AH,4CH
        INT 21H
CODE ENDS
END START

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line –  DISPLAY MSG2

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG2. This will display String Msg2 on Screen. 

Next Line –  DISPLAY P11

 DISPLAY P11 is Calling of a Macro DISPLAY with Argument P11. This will display String P11 on Screen. 

Next Line –  DISPLAY MSG3

 DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen. 

Next Line – MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H

The above Four line code is used to Write a Character on Console present in L1 Variable (i.e.Length of first String).

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

Next Line –  DISPLAY MSG6

 DISPLAY MSG6 is Calling of a Macro DISPLAY with Argument MSG6. This will display String Msg6 on Screen. 

Next Line – LEA SI,P11
        LEA DI,P22

The above Two line code is used to initialize P11 to SI registe rand P22 to DI register. 

Next Line – MOV DL,L1
        DEC DL
        MOV DH,0
        ADD SI,DX

The above Four line code is used to Jump SI to the Last Character of the String to be Reversed.

MOV DL,L1 is used to move L1 (i.e. Actual Length of first String Entered) to DL register DEC DL will decrement the value present in DL register by One and MOV DH,0 is used to move or assign value Zero (decimal value) to  DH Register(We have  to do all this because we can’t add DL with SI as DL is 8 bit and SI is 16 bit). After all this we can now add DX to SI by ADD SI,DX.

Next Line –   MOV CL,L1
        MOV CH,0

The above Two line code is used to Move L1 (i.e. Actual Length of String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – REVERSE:

REVERSE: is a LABEL and all the words ending in colon (:) are Labels.

Next Line –  MOV AL,[SI]
        MOV [DI],AL

Move value at Address of SI Register to AL register Move value of AL register to Address of DI Register as we want to copy Character from [SI] to [DI].

Next Line –  INC DI
        DEC SI

 INC DI will increment the value present in DI register by One. DEC SI will decrement the value present in SI register by One.

Next Line – LOOP REVERSE

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line –  DISPLAY P22

DISPLAY P22 is Calling of a Macro DISPLAY with Argument P22, get the Converted String in p22 Variable and  Display the Converted String Onscreen.

Next Line – LEA SI,P11
        LEA DI,P22

The above Two line code is used to initialize P11 to SI registe rand P22 to DI register. 

Next Line –   MOV CL,L1
        MOV CH,0

The above Two line code is used to Move L1 (i.e. Actual Length of String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – CHECK:

CHECK: is a LABEL and all the words ending in colon (:) are Labels.

Next Line –  MOV AL,[SI]
        CMP [DI],AL
        JNE NOTPALIN

MOV AL,[SI] Move value at Address of SI Register to AL register CMP [DI],AL is used to compare AL (value of [SI]) with Element of Array present in [DI] and JNE NOTPALIN Short Jump if Not Equal i.e. [SI] is Not Equal to [DI] Than the Control jumps to the respective LABEL NOTPALIN. The result of Comparision is not stored anywhere, but flags are set according to result.

 Next Line –  INC DI
        INC SI

 INC DI will increment the value present in DI register by One. Similarly INC SI will increment SI register.

Next Line – LOOP CHECK

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

 Next Line –  DISPLAY MSG5

 DISPLAY MSG5 is Calling of a Macro DISPLAY with Argument MSG5. This will display String Msg5 on Screen. 

JMP EXIT JMP is Unconditional Jump. This will Jump to Label EXIT.

Next Line –  NOTPALIN:
        DISPLAY MSG4

 NOTPALIN: is a LABEL and all the words ending in colon (:) are Labels. DISPLAY MSG4 is Calling of a Macro DISPLAY with Argument MSG4. This will display String Msg4 on Screen. 

Next Line –  EXIT:   MOV AH,4CH
        INT 21H

EXIT: is a LABEL and all the words ending in colon (:) are Labels. 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_Rev_Str_&Palin

 Output After Execution :-

Asm_program_Rev_Str_&Palin_Output

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

An Assembly Lanuage Program, which converts string to its ASCII value and store in array

Now we will write another Assembly Lanuage Program, which converts string to its ASCII value and store in array

Let’s identify variables needed for this program.
Let’s identify variables needed for this program.
First variables will be the one which will hold the Strings entered by user in the variables P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) to copy ascii value of string given by user to an Array and Other variables will be holding the Messages ’ENTER ANY STRING :- $’ to be printed for the User, So in all Three variables.The identified variables are P11, ARRAY and MSG1.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

Next Line – MSG1 DB 10,13,’ENTER FIRST STRING :- $’
   
    ARRAY DB 50 DUP (‘$’)
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)

ARRAY  DB 50 DUP (‘$’) this line is a declaration of Array initialized with ’$’ which works as New Line Character. $ is used as (\n) NULL character in C program. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the digits in the Resultant Decimal equivalent printable form, Therefore we take it approx size ten. Here 50 DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $). P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 is the name refered for the Array in the proram. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $). Similarly  P2 LABEL BYTE    M2 DB 0FFH    L2 DB ?    P22 DB 0FFH DUP (‘$’) and P3 LABEL BYTE    M3 DB 0FFH    L3 DB ?    P33 DB 0FFH DUP (‘$’) same as above. MSG1 DB ‘ENTER STRING HERE :- $’ this line is a declaration of Charater Array initialized with ’ENTER FIRST STRING :- $’  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 ). Similarly MSG2 DB 10,13,’ENTER SECOND STRING :- $’, MSG3 DB 10,13,’LENGTH OF FIRST STRING IS :- $’,MSG4 DB 10,13,’LENGTH OF SECOND STRING IS :- $’ and MSG5 DB 10,13, ’CONCATENATED STRING IS :- $’

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
    MSG1 DB 10,13,'ENTER FIRST STRING :- $'
   
    ARRAY DB 50 DUP ('$')
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
  
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER FIRST STRING :- $'
   
    ARRAY DB 50 DUP ('$')
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
  
DATA ENDS
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H           
                            
        LEA SI,P11
        LEA DI,ARRAY
               
        MOV CL,L1
        MOV CH,0
       
COPY1:  MOV AL,[SI]
        MOV [DI],AL
  
        INC DI
        INC SI
        LOOP COPY1                    
      
        MOV AH,4CH
        INT 21H
CODE ENDS
END START          

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line – LEA SI,P11
        LEA DI,ARRAY             
        MOV CL,L1
        MOV CH,0

The above Four line code is used to initialize P11 to SI register and to initialize ARRAY to DI register. Move L1 (i.e. Actual Length of first String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – COPY1:  MOV AL,[SI]
        MOV [DI],AL

COPY1: is a LABEL and all the words ending in colon (:) are Labels. Move value at Address of SI Register to AL register Move value of AL register to Address of DI Register as we want to copy Character from [SI] to [DI].

Next Line –  INC DI
        INC SI

 INC DI will increment the value present in DI register by One. Similarly INC SI will increment SI register.

Next Line – LOOP COPY1

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line –  MOV AH,4CH
      INT 21H

The above two line code is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 4ch, That means Return to Operating System or DOS which is the End of the program.

Next Line – CODE ENDS

CODE ENDS is the End point of the Code Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Code Segment.

Last Line – END START

END START is the end of the label used to show the ending point of the code which is written in the Code Segment.

Note :- In this Assembly Language Programming, We have Com format and EXE format. We are Learning in EXE format only which simple then COM format to understand and Write. We can write the program in lower or upper case, But i prepare Upper Case.

Screen Shots :-

 Asm_program_String_Ascii_to_Array

Output After Execution :-

Asm_program_String_Ascii_to_Array_Out

Output Variable Values After Execution :-

Asm_program_String_Ascii_to_Array_Vble

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

An Assembly Lanuage Program for reversing a given string

Now we will write another Assembly Lanuage Program for reversing a given string.

Let’s identify variables needed for this program.
First variables will be the one which will hold the Strings entered by user in the variables P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) to concate characters of two strings given by user and Other variables will be holding the Messages ’ENTER ANY STRING :- $’, ’ENTERED STRING IS :- $’ and ’CONVERTED STRING IS : $’ to be printed for the User, So in all Four variables.The identified variables are P11,P22, 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 – MSG1 DB 10,13,’ENTER ANY STRING :- $’
    MSG2 DB 10,13,’LENGTH OF STRING IS :- $’
    MSG3 DB 10,13,’REVERSE OF ENTERED STRING IS :- $’ 
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)
    P22 DB 0FFH DUP (‘$’)

P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) P22 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 and  P22 are the names refered for the Arrays in the program. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $).  MSG1 DB 10,13,’ENTER ANY STRING :- $’:- $’this line is a declaration of Charater Array initialized with ’ENTER ANY STRING :- $’  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 ). Similarly MSG2 DB 10,13,’LENGTH OF STRING IS :- $’ and MSG3 DB 10,13,’REVERSE OF ENTERED STRING IS :- $’.

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
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'LENGTH OF STRING IS :- $'
    MSG3 DB 10,13,'REVERSE OF ENTERED STRING IS :- $' 
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
    P22 DB 0FFH DUP ('$')
   
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'LENGTH OF STRING IS :- $'
    MSG3 DB 10,13,'REVERSE OF ENTERED STRING IS :- $' 
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
    P22 DB 0FFH DUP ('$')
   
DATA ENDS
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H
               
        DISPLAY MSG2
       
        MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H
       
        DISPLAY MSG3
               
        LEA SI,P11
        LEA DI,P22
       
        MOV DL,L1
        DEC DL
        MOV DH,0
        ADD SI,DX
        MOV CL,L1
        MOV CH,0
       
REVERSE:
        MOV AL,[SI]
        MOV [DI],AL
        INC DI
        DEC SI
        LOOP REVERSE
       
        DISPLAY P22
                      
        LEA SI,P11
        LEA DI,P22   
       
        MOV CL,L1
        MOV CH,0
                       
        MOV AH,4CH
        INT 21H
CODE ENDS
END START          

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line –  DISPLAY MSG2

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG2. This will display String Msg2 on Screen. 

Next Line – MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H

The above Four line code is used to Write a Character on Console present in L1 Variable (i.e.Length of first String).

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

Next Line –  DISPLAY MSG3

 DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen. 

Next Line – LEA SI,P11
        LEA DI,P22

The above Two line code is used to initialize P11 to SI registe rand P22 to DI register. 

Next Line – MOV DL,L1
        DEC DL
        MOV DH,0
        ADD SI,DX

The above Four line code is used to Jump SI to the Last Character of the String to be Reversed.

MOV DL,L1 is used to move L1 (i.e. Actual Length of first String Entered) to DL register DEC DL will decrement the value present in DL register by One and MOV DH,0 is used to move or assign value Zero (decimal value) to  DH Register(We have  to do all this because we can’t add DL with SI as DL is 8 bit and SI is 16 bit). After all this we can now add DX to SI by ADD SI,DX.

Next Line –   MOV CL,L1
        MOV CH,0

The above Two line code is used to Move L1 (i.e. Actual Length of String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – REVERSE:

REVERSE: is a LABEL and all the words ending in colon (:) are Labels.

Next Line –  MOV AL,[SI]
        MOV [DI],AL

Move value at Address of SI Register to AL register Move value of AL register to Address of DI Register as we want to copy Character from [SI] to [DI].

Next Line –  INC DI
        DEC SI

 INC DI will increment the value present in DI register by One. DEC SI will decrement the value present in SI register by One.

Next Line – LOOP REVERSE

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line –  DISPLAY P22

DISPLAY P22 is Calling of a Macro DISPLAY with Argument P22, get the Converted String in p22 Variable and  Display the Converted String Onscreen.

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_Rev_Str

Output After Execution :-

Asm_program_Rev_Str_Output

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

An Assembly Lanuage Program, which converts string lower case characters to upper case characters and upper case characters to lower case characters

Now we will write another Assembly Lanuage Program, which converts string lower case characters to upper case characters and upper case characters to lower case characters.

Let’s identify variables needed for this program.
First variables will be the one which will hold the Strings entered by user in the variables P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) to concate characters of two strings given by user and Other variables will be holding the Messages ’ENTER ANY STRING :- $’, ’ENTERED STRING IS :- $’ and ’CONVERTED STRING IS : $’ to be printed for the User, So in all Four variables.The identified variables are P11, 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 – MSG1 DB 10,13,’ENTER ANY STRING :- $’
    MSG2 DB 10,13,’ENTERED STRING IS :- $’
    MSG3 DB 10,13,’CONVERTED STRING IS : $’
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)

P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 is the name refered for the Array in the proram. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $).  MSG1 DB 10,13,’ENTER ANY STRING :- $’this line is a declaration of Charater Array initialized with ’ENTER ANY STRING :- $’  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 ). Similarly MSG2 DB 10,13,’ENTERED STRING IS :- $’ and MSG3 DB 10,13, ’CONCATENATED STRING IS :- $’.

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
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'ENTERED STRING IS :- $'
    MSG3 DB 10,13,'CONVERTED STRING IS : $'
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER ANY STRING :- $'
    MSG2 DB 10,13,'ENTERED STRING IS :- $'
    MSG3 DB 10,13,'CONVERTED STRING IS : $'
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
DATA ENDS
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM  
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H
       
        DISPLAY MSG2
       
        DISPLAY P11
                   
        DISPLAY MSG3
               
        LEA SI,P11
              
        MOV CL,L1
        MOV CH,0       
CHECK:
        CMP [SI],41H
        JB DONE
       
        CMP [SI],5BH
        JB LWR
       
        CMP [SI],61H
        JB DONE
       
        CMP [SI],7BH
        JG DONE
       
UPR:    SUB [SI],20H
        JMP DONE
LWR:    ADD [SI],20H
            
DONE:   INC SI
        LOOP CHECK
               
        DISPLAY P11
                               
        MOV AH,4CH
        INT 21H
CODE ENDS
END START

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line –  DISPLAY MSG2

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG2. This will display String Msg2 on Screen. 

Next Line –  DISPLAY P11

 DISPLAY P11 is Calling of a Macro DISPLAY with Argument P11. This will display String P11 on Screen. 

Next Line –  DISPLAY MSG3

 DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen. 

Next Line – LEA SI,P11
              
        MOV CL,L1
        MOV CH,0 

The above Three line code is used to initialize P11 to SI register, Move L1 (i.e. Actual Length of String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – CHECK: 

CHECK: is a LABEL and all the words ending in colon (:) are Labels.

Next Line –  CMP [SI],41H
        JB DONE
       
        CMP [SI],5BH
        JB LWR
       
        CMP [SI],61H
        JB DONE
       
        CMP [SI],7BH
        JG DONE

CMP [SI],41H is used to compare 41H (ASCII value of ‘A’) with Element of Array present in [SI] and JB DONE Short Jump if first operand i.e. ARR[SI] is Below second operand i.e. Control jumps to the respective LABEL DONE. The result of Comparision is not stored anywhere, but flags are set according to result. Similarly CMP [SI],5BH   JB LWR  CMP [SI],61H  JB DONE  CMP [SI],7BH JG DONEComparision to find Characters are Alphabets then convert in LWR and UPR labels else skip to DONE label.

Next Line – UPR:    SUB [SI],20H
        JMP DONE

       LWR:    ADD [SI],20H

UPR: is a LABEL and all the words ending in colon (:) are Labels. SUB [SI],20H means subtracting 20H from Address of SI Register, here we are reducing the ASCII value by 20H to convert Upper case to Lower case. JMP DONE JMP is Unconditional Jump. This will Jump to Label DONE.

LWR: is a LABEL and all the words ending in colon (:) are Labels. ADD [SI],20H means adding 20H to Address of SI Register, here we are increasing the ASCII value by 20H to convert Lower case to Upper case.

Next Line –  DONE:   INC SI

 DONE: is a LABEL and all the words ending in colon (:) are Labels.INC SI will increment the value present in SI register by One.

Next Line – LOOP CHECK

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line –  DISPLAY P11

DISPLAY P11 is Calling of a Macro DISPLAY with Argument P11, get the Converted String in p11 Variable and  Display the Converted String Onscreen.

This Code is Same as above to Copy the second string to Third String and get the Concatinated String in p33 Variable. Display the Concatinated String Onscreen.

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_Upr2Lwr_Lwr2Upr

Output After Execution :-

Asm_program_Upr2Lwr_Lwr2Upr_Output

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

An Assembly Lanuage Program, which takes two inputs as strings and display the Concatenated string.

Now we will write another Assembly Lanuage Program, which takes two inputs as strings and display the Concatenated string.

Let’s identify variables needed for this program.
First variables will be the one which will hold the Strings entered by user in the variables P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’), P2 LABEL BYTE    M2 DB 0FFH    L2 DB ?    P22 DB 0FFH DUP (‘$’) and P3 LABEL BYTE    M3 DB 0FFH    L3 DB ?    P33 DB 0FFH DUP (‘$’) to concate characters of two strings given by user and Other variables will be holding the Messages ENTER FIRST STRING :- $’ , ’ENTER SECOND STRING :- $’, ‘LENGTH OF FIRST STRING IS :- $’, ‘LENGTH OF SECOND STRING IS :- $’ and ’CONCATENATED STRING IS :- $’ to be printed for the User, So in all Eight variables.The identified variables are P11, P22, P33, MSG1, MSG2, MSG3, MSG4 and MSG5.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

Next Line – MSG1 DB 10,13,’ENTER FIRST STRING :- $’
    MSG2 DB 10,13,’ENTER SECOND STRING :- $’
    MSG3 DB 10,13,’LENGTH OF FIRST STRING IS :- $’  
    MSG4 DB 10,13,’LENGTH OF SECOND STRING IS :- $’
    MSG5 DB 10,13,’CONCATENATED STRING IS :- $’ 
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)
   
    P2 LABEL BYTE
    M2 DB 0FFH
    L2 DB ?
    P22 DB 0FFH DUP (‘$’)
   
    P3 LABEL BYTE
    M3 DB 0FFH
    L3 DB ?
    P33 DB 0FFH DUP (‘$’)

P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 is the name refered for the Array in the proram. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $). Similarly  P2 LABEL BYTE    M2 DB 0FFH    L2 DB ?    P22 DB 0FFH DUP (‘$’) and P3 LABEL BYTE    M3 DB 0FFH    L3 DB ?    P33 DB 0FFH DUP (‘$’) same as above. MSG1 DB ‘ENTER STRING HERE :- $’ this line is a declaration of Charater Array initialized with ’ENTER FIRST STRING :- $’  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 ). Similarly MSG2 DB 10,13,’ENTER SECOND STRING :- $’, MSG3 DB 10,13,’LENGTH OF FIRST STRING IS :- $’,MSG4 DB 10,13,’LENGTH OF SECOND STRING IS :- $’ and MSG5 DB 10,13, ’CONCATENATED STRING IS :- $’

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
    MSG1 DB 10,13,'ENTER FIRST STRING :- $'
    MSG2 DB 10,13,'ENTER SECOND STRING :- $'
    MSG3 DB 10,13,'LENGTH OF FIRST STRING IS :- $'  
    MSG4 DB 10,13,'LENGTH OF SECOND STRING IS :- $'
    MSG5 DB 10,13,'CONCATENATED STRING IS :- $' 
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
   
    P2 LABEL BYTE
    M2 DB 0FFH
    L2 DB ?
    P22 DB 0FFH DUP ('$')
   
    P3 LABEL BYTE
    M3 DB 0FFH
    L3 DB ?
    P33 DB 0FFH DUP ('$')
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER FIRST STRING :- $'
    MSG2 DB 10,13,'ENTER SECOND STRING :- $'
    MSG3 DB 10,13,'LENGTH OF FIRST STRING IS :- $'  
    MSG4 DB 10,13,'LENGTH OF SECOND STRING IS :- $'
    MSG5 DB 10,13,'CONCATENATED STRING IS :- $' 
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
   
    P2 LABEL BYTE
    M2 DB 0FFH
    L2 DB ?
    P22 DB 0FFH DUP ('$')
   
    P3 LABEL BYTE
    M3 DB 0FFH
    L3 DB ?
    P33 DB 0FFH DUP ('$')
DATA ENDS
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM  
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H
       
        DISPLAY MSG2
       
        LEA DX,P2
        MOV AH,0AH    
        INT 21H
       
        DISPLAY MSG3
       
        MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H
       
        DISPLAY MSG4
       
        MOV DL,L2
        ADD DL,30H
        MOV AH,2
        INT 21H
       
        DISPLAY MSG5
               
        LEA SI,P11
        LEA DI,P33
               
        MOV CL,L1
        MOV CH,0
       
COPY1:  MOV AL,[SI]
        MOV [DI],AL
  
        INC DI
        INC SI
        LOOP COPY1
       
        LEA SI,P22
                       
        MOV CL,L2
        MOV CH,0
       
COPY2:  MOV AL,[SI]
        MOV [DI],AL
  
        INC DI
        INC SI
        LOOP COPY2           
                  
        DISPLAY P33
       
        MOV AH,4CH
        INT 21H
CODE ENDS
END START

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line –  DISPLAY MSG2
       
        LEA DX,P2
        MOV AH,0AH    
        INT 21H

This Code is Same as above to Display the Message string and Scan the Variable Length String from User.

Next Line –  DISPLAY MSG3

 DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen. 

Next Line – MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H

The above Four line code is used to Write a Character on Console present in L1 Variable (i.e.Length of first String).

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

Next Line –  DISPLAY MSG4
       
        MOV DL,L2
        ADD DL,30H
        MOV AH,2
        INT 21H

This Code is Same as above to Display the Message string and Print the Length of the second String in L2 Variable.

Next Line –  DISPLAY MSG5

 DISPLAY MSG5 is Calling of a Macro DISPLAY with Argument MSG5. This will display String Msg5 on Screen.

Next Line – LEA SI,P11
        LEA DI,P33
             
        MOV CL,L1
        MOV CH,0

The above Four line code is used to initialize P11 to SI register and to initialize P33 to DI register. Move L1 (i.e. Actual Length of first String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to  CH Register. 

Next Line – COPY1:  MOV AL,[SI]
        MOV [DI],AL

COPY1: is a LABEL and all the words ending in colon (:) are Labels. Move value at Address of SI Register to AL register Move value of AL register to Address of DI Register as we want to copy Character from [SI] to [DI].

Next Line –  INC DI
        INC SI

 INC DI will increment the value present in DI register by One. Similarly INC SI will increment SI register.

Next Line – LOOP COPY1

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line –   LEA SI,P22
                       
        MOV CL,L2
        MOV CH,0
       
COPY2:  MOV AL,[SI]
        MOV [DI],AL
  
        INC DI
        INC SI
        LOOP COPY2           
                  
        DISPLAY P33

This Code is Same as above to Copy the second string to Third String and get the Concatinated String in p33 Variable. Display the Concatinated String Onscreen.

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_Concate_Strings

Output After Execution :-

Asm_program_Concate_Strings_Output

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

An Assembly Lanuage Program, which encodes the string into the ASCII value but not corresponding ASCII value; shift 5 place left in ASCII and write the encoding string

Now we will write another Assembly Lanuage Program, which encodes the string into the ASCII value but not corresponding ASCII value; shift 5 place left in ASCII and write the encoding string. Develop cryptographic algorithm where each letter is replaced by a different letter. Given the mapping of characters to encoded characters, it is simple to translate from encoded to decoded data. 

Let’s identify variables needed for this program.
First variables will be the one which will hold the String entered by user in the variable P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) to be encoded characters and Other variables will be holding the Messages ’ENTER STRING HERE :- $’ , ’ENCRYPTED STRING IS :- $’ and ‘DECRYPTED STRING IS : $’ to be printed for the User, So in all Four variables.The identified variables are P11, 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 – MSG1 DB 10,13,’ENTER STRING HERE :- $’   
    MSG2 DB 10,13,’ENCRYPTED STRING IS :- $’
    MSG3 DB 10,13,’DECRYPTED STRING IS : $’
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP (‘$’)

P1 LABEL BYTE    M1 DB 0FFH    L1 DB ?    P11 DB 0FFH DUP (‘$’) this line is a declaration of Array with Variable Length of User’s Choice (i.e. User can enter String of Variable Length) initialized with ’$’ which works as New Line Character $ is used as (\n) NULL character in C program. P1 is the Start of the Label Byte Data Type. M1 is used for assigning Maximum Length of the Array. L1 is used to Get the LENGTH of the entered String by User. P11 is the name refered for the Array in the proram. (A Number Character is of a BYTE size Hence we have to used only DB Define Byte ) as we don’t know the lenght of the Characters in the String. Therefore we take it approx size 256. Here 0FFH DUP (‘$’) stands for N i.e. Size of Array or Array Size. DUP stands for Duplicate i.e. it will duplicate the value in All the Array with the value present in Bracket (i.e. $). MSG1 DB ‘ENTER STRING HERE :- $’ this line is a declaration of Charater Array initialized with “’ENTER STRING HERE :- $” 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 ). Similarly MSG2 DB 10,13,’ENCRYPTED STRING IS :- $’ and MSG3 DB 10,13,’DECRYPTED STRING IS : $’

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
    MSG1 DB 10,13,'ENTER STRING HERE :- $'   
    MSG2 DB 10,13,'ENCRYPTED STRING IS :- $'
    MSG3 DB 10,13,'DECRYPTED STRING IS : $'
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
DATA ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

MACROS

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   

DISPLAY :- is the Name (Identifier) of the Macro. MACRO is the Keyword Used. MSG is the Argument Passed.
    MOV AH,9             }
    LEA DX,MSG        }      :- /* code inside macro */
    INT 21H                  }
ENDM   :- is the end of Macro.

The code which is used most of the time is written in between the macro for reducing the length of Code.

DATA SEGMENT
    MSG1 DB 10,13,'ENTER STRING HERE :- $'   
    MSG2 DB 10,13,'ENCRYPTED STRING IS :- $'
    MSG3 DB 10,13,'DECRYPTED STRING IS : $'
   
    P1 LABEL BYTE
    M1 DB 0FFH
    L1 DB ?
    P11 DB 0FFH DUP ('$')
DATA ENDS
DISPLAY MACRO MSG
    MOV AH,9
    LEA DX,MSG
    INT 21H
ENDM   
CODE SEGMENT
    ASSUME CS:CODE,DS:DATA
START:
        MOV AX,DATA
        MOV DS,AX                
               
        DISPLAY MSG1
       
        LEA DX,P1
        MOV AH,0AH    
        INT 21H
                      
        LEA SI,P11
        MOV CL,L1
        CALL ENCRYPT
       
        DISPLAY MSG2
        DISPLAY P11
                               
        LEA SI,P11
        MOV CL,L1
        CALL DECRYPT
       
        DISPLAY MSG2
        DISPLAY P11
       
        MOV AH,4CH
        INT 21H
CODE ENDS
ENCRYPT PROC NEAR
        MOV CH,0       
NEXT1:    
        SUB [SI],05
               
        INC SI
        LOOP NEXT1
       RET
ENCRYPT ENDP
DECRYPT PROC NEAR
       MOV CH,0       
NEXT2:    
        ADD [SI],05
               
        INC SI
        LOOP NEXT2      
       RET
DECRYPT ENDP
   
END START

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line –CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line –  DISPLAY MSG1

 DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen. 

Next Line – LEA DX,P1
        MOV AH,0AH    
        INT 21H

The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.

Now, lets understand line by line

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

MOV AH,0AH    
INT 21H

The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

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 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.

Next Line – LEA SI,P11
        MOV CL,L1
        CALL ENCRYPT

The above Three line code is used to initialize P11 to SI register, Move L1 (i.e. Actual Length of String Entered) to CL register and Call Procedure ENCRYPT

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

CALL ENCRYPT is used to Call a Procedure named ENCRYPT.

Next Line –  DISPLAY MSG2
        DISPLAY P11

 DISPLAY MSG2 is Calling of a Macro DISPLAY with Argument MSG2. This will display String Msg2 on Screen. Similarly to Print P11.

Next Line –  LEA SI,P11
        MOV CL,L1
        CALL DECRYPT
       
        DISPLAY MSG2
        DISPLAY P11

This Code is Same as above to call Procedure DECRYPT. and Display the resultant string.

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.

PROCEDURE Code starts here: 

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

Next Line – ENCRYPT 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. ENCRYPT is only the Name given to the Procedure Code. 

Next Line –  MOV CH,0 

MOV CH,0 is used to move or assign value 0 (decimal value) to  CH Register. The value is already passed to CL Register which is the Length of the String to be Encrypted. Loop starts here.

Next Line – NEXT1:    
        SUB [SI],05

NEXT1: is a LABEL and all the words ending in colon (:) are Labels. SUB [SI],05 means subtracting 05 from Address of SI Register, here we are reducing the ASCII value by Five. 

Next Line –  INC SI

 INC SI will increment the value present in SI register by One.

Next Line – LOOP NEXT1

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – RET

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

Next Line – ENCRYPT ENDP 

ENCRYPT 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. ENCRYPT is only the Name given to the Procedure Code. 

ANOTHER PROCEDURE Code starts here: 

Next Line – DECRYPT PROC NEAR
       MOV CH,0       
NEXT2:    
        ADD [SI],05
               
        INC SI
        LOOP NEXT2      
       RET
DECRYPT ENDP

Similarly the Code for DECRYPT the only Difference is ADD [SI],05 means Adding 05 to the Address of SI Register, here we are Increased the ASCII value by Five. 

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_Encrpt_Decrypt_5Left

Output After Execution :-

Asm_program_Encrpt_Decrypt_5Left_Output

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