Monthly Archives: April 2014


An Assembly program to conduct a binary search on a given sorted array of 16-bit, unsigned integers, and a given 16-bit unsigned key

Now we will write another Assembly program to conduct a binary search on a given sorted array of 16-bit, unsigned integers, and a given 16-bit unsigned key.

 BINARY_SEARCH

The above Logic is a C like Program to conduct a binary search we need small Algorithm Shown above in a very simple way, So Just we will covert the logic into Assembly There are many things uncommon in the programing Language. There are No While Loops or Modules but this things are to be implemented in different ways.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the Given Numbers in Array list and key of 16-bit and it will be array ARR and KEY. variables will be holding the Messages MSG1 “KEY IS FOUND AT “, RES “  POSITION”, 13, 10,” $” and MSG2 ‘KEY NOT FOUND!!!.$’ to be printed for the User. Other variables will be holding Length of the Array and it will be LEN, So in all Six variables.
The identified variables are ARR, KEY, LEN, RES, MSG1 and MSG2.

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 – ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H
     LEN DW ($-ARR)/2
     KEY EQU 7777H
     MSG1 DB “KEY IS FOUND AT ”
     RES DB “  POSITION”,13,10,” $”
     MSG2 DB ‘KEY NOT FOUND!!!.$’

ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H this line is a declaration of 16-bit Numbers Array initialized with 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H the numbers are seperated by Comma (,). LEN DW $-ARR is used to Save the Length of the Array which will be generated by $-Name of the array i.e. $-ARR. KEY EQU 7777H is used to save given KEY to be Searched in the Array and is equal to (EQU) 7777H. MSG1 DB “KEY IS FOUND AT ” this line is a declaration of Charater Array initialized with “KEY IS FOUND AT”. (A Character is of a BYTE Hence we have to use only DB Define Byte ) and Similarly to RES and MSG2.  Detailed explanation is given below.

Next Line – DATA ENDS

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

Now, Selection of data type is DW data type the numbers which we are adding is 16-bit  integers so DW is sufficient.

DATA SEGMENT
     ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H
     LEN DW ($-ARR)/2
     KEY EQU 7777H
     MSG1 DB "KEY IS FOUND AT "
     RES DB "  POSITION",13,10," $"
     MSG2 DB 'KEY NOT FOUND!!!.$'
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).

DATA SEGMENT
     ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H
     LEN DW ($-ARR)/2
     KEY EQU 7777H
     MSG1 DB "KEY IS FOUND AT "
     RES DB "  POSITION",13,10," $"
     MSG2 DB 'KEY NOT FOUND!!!.$'
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
    
      MOV BX,00
      MOV DX,LEN
      MOV CX,KEY
AGAIN: CMP BX,DX
       JA FAIL
       MOV AX,BX
       ADD AX,DX
       SHR AX,1
       MOV SI,AX
       ADD SI,SI
       CMP CX,ARR[SI]
       JAE BIG
       DEC AX
       MOV DX,AX
       JMP AGAIN
BIG:   JE SUCCESS
       INC AX
       MOV BX,AX
       JMP AGAIN
SUCCESS: ADD AL,01
         ADD AL,'0'
         MOV RES,AL
         LEA DX,MSG1
         JMP DISP
FAIL: LEA DX,MSG2
DISP: MOV AH,09H
      INT 21H
     
      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 – MOV BX,00
      MOV DX,LEN
      MOV CX,KEY

The above two line of code is same as FIRST=0 and LAST=LEN the Difference is that we are using Registers to Store Numbers, So we have t0 instruction MOV BX,00 move ZERO value to BX Register.  MOV DX,LEN move LEN variable value to DX Register. MOV CX,KEY move KEY variable value to CX Register.

Next Line – AGAIN: CMP BX,DX
JA FAIL

AGAIN: this will be starting point of while loop the condition cannot be written here So we write it Down and this is a LABEL and all the words ending in colon (:). CMP BX,DX is used to compare Element of BX register with DX register and JA FAIL Short Jump if first operand (i.e. BX) is Above second operand (i.e. DX) to the respective LABEL FAIL. The result of Comparision is not stored anywhere, but flags are set according to result.

Next Line – MOV AX,BX
ADD AX,DX
SHR AX,1
MOV SI,AX
ADD SI,SI

MOV BX,CX is to move CX register to BX register. ADD AX,DX means Adding value of DX register with AX register. SHR means Shift operand1 Right. The number of shifts is set by operand2. SHR AX,1 is used to Shift with 1. MOV SI,AX is to move AX register to SI register. ADD SI,SI means Adding value of SI register with SI register.

Next Line – CMP CX,ARR[SI]
JAE BIG
DEC AX
MOV DX,AX
JMP AGAIN

CMP CX,ARR[SI] is used to compare Element of CX register with Element of Array present in ARR[SI] and JAE BIG Short Jump if first operand (i.e. BX) is Above or Equal to second operand (i.e. DX) to the respective LABEL BIG. The result of Comparision is not stored anywhere, but flags are set according to result. DEC AX will decrement the Address value present in AX register. MOV DX,AX is to move AX register to DX register. JMP AGAIN is used to Jump to Label AGAIN without any condition check. This jump will basically Continue the Loop execution.

Next Line –BIG:   JE SUCCESS
       INC AX
       MOV BX,AX
       JMP AGAIN

BIG: is a LABEL and all the words ending in colon (:). JE SUCCESS Short Jump if first operand (i.e. BX) is Equal to second operand (i.e. DX) to the respective LABEL SUCCESS. The result of Comparision is not stored anywhere, but flags are set according to result. INC AX will increment the Address value present in AX register. MOV BX,AX is to move AX register to BX register. JMP AGAIN is used to Jump to Label AGAIN without any condition check. This jump will basically Continue the Loop execution.

Next Line – SUCCESS: ADD AL,01
         ADD AL,’0′
         MOV RES,AL
         LEA DX,MSG1
         JMP DISP

SUCCESS: is a LABEL and all the words ending in colon (:). ADD AL,01 means Adding value 01 to AL register. ADD AL,’0′ means Adding value of ’0′ (i.e. 30H) with AL register. MOV RES,AL is to move AL register to RES variable. LEA DX,MSG1  in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element. JMP DISP is used to Jump to Label DISP without any condition check. This jump will basically Continue the Loop execution.

Next Line – FAIL: LEA DX,MSG2

FAIL: is a LABEL and all the words ending in colon (:). LEA DX,MSG2  in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element.

Next Line – DISP: MOV AH,09H
      INT 21H

DISP: is a LABEL and all the words ending in colon (:). MOV AH,09H  INT 21H is used to PRINT the String or Message of the address present in DX register.

Next Line – MOV AH,4CH
INT 21H

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

Next Line – CODE ENDS

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

Last Line – END START

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

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

Screen Shots :-

 Asm_program_Binary_Search_16-bit

Output After Execution :-

 Asm_program_Binary_Search_16-bit_Output

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

An Assembly program which should convert 4 digits BCD number into its binary equivalent

Now we will write another Assembly which should convert 4 digits BCD number into its binary equivalent.

Let’s identify variables needed for this program.
First variables will be the one which will hold the value present in the variable BCD to be converted and TEMP will hold the converted Hexadecimal equivalent and then to print its Binary form on Console (Screen) and Other variable RES will be holding the Resultant Binary equivalent printable form to be printed for the User on Screen, So in all  Three variables.
The identified variables are BCD, TEMP and RES.

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 – BCD DW 1234H
     TEMP DB ?
     RES  DB 17 DUP (‘$’)

BCD DW 1234H We are initializing BCD to 1234H  (H stands for Hexadecimal value). TEMP DB ?  We are initializing to ? (? stands for blank value), As we will save converted BCD form number to Hexadecimal equivalent in it. RES  DB 10 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 10 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. $).

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
     BCD DW 1234H
     TEMP DB ?
     RES  DB 17 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).

DATA SEGMENT
     BCD DW 1234H
     TEMP DB ?
     RES  DB 17 DUP ('$')
DATA ENDS
CODE SEGMENT
        ASSUME DS:DATA,CS:CODE
START:       
    MOV AX,DATA
    MOV DS,AX
   
    MOV AX,BCD
    MOV BL,AL
     
    AND AL,0F0H
    AND BL,0FH
     
    MOV CL,04H
    ROL AL,CL
     
    MOV CL,10
    MUL CL
     
    ADD AL,BL
    MOV TEMP,AL
   
    MOV AX,BCD
    MOV AL,AH
    MOV AH,0
    MOV BL,AL
     
    AND AL,0F0H
    AND BL,0FH
     
    MOV CL,04H
    ROL AL,CL
     
    MOV CL,10
    MUL CL
     
    ADD AL,BL
   
    MOV CL,100
    MUL CL
   
    MOV BL,TEMP
    MOV BH,0
    ADD AX,BX   
      
    LEA SI,RES
    CALL HEX2BIN
   
    LEA DX,RES
    MOV AH,9
    INT 21H 
            
    MOV AH,4CH
    INT 21H        
CODE ENDS
HEX2BIN PROC NEAR
    MOV CX,0
    MOV BX,2
   
LOOP1: MOV DX,0
       DIV BX
       ADD DL,30H
       PUSH DX
       INC CX
       CMP AX,1
       JG LOOP1
     
       ADD AL,30H
       MOV [SI],AL
     
LOOP2: POP AX
       INC SI
       MOV [SI],AL
       LOOP LOOP2
       RET
HEX2BIN 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 – MOV AX,BCD
    MOV BL,AL

 MOV AX,BCD is used to Move  BCD variable value to AX Register.  MOV BL,AL to Move  BL Register to AL Register.

Next Line – AND AL,0F0H
    AND BL,0FH

The above line code is used to Mask Left and Right Digits in AL Register and BL Register.

Next Line –     MOV CL,04H
    ROL AL,CL

ROL means Rotate operand1 left. The number of rotates is set by operand2. We need 4 digits to be Rotated Hence MOV CL,04H is used to Move  04H to CL Register. ROL AL,CL is used to Rotate AL Register to 4 digits Left.

Next Line – MOV CL,10
    MUL CL

MUL 10 is not possible where 10 is immediate, So we have to move 10 to CL register (we can take any register) i.e. MOV CL,10. MUL CL in this line CL register will be Multiplied with AX register (BY DEFAULT).

Next Line – ADD AL,BL
    MOV TEMP,AL

ADD AL,BL means Adding value of AL register with BL register. MOV TEMP,AL is used to Move AL Register to TEMP variable.

Next Line – MOV AX,BCD
    MOV AL,AH
    MOV AH,0
    MOV BL,AL

 MOV AX,BCD is used to Move  BCD variable value to AX Register. MOV AL,AH to Move  AL Register to AH Register. MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it. MOV BL,AL to Move  BL Register to AL Register.

Next Line – AND AL,0F0H
    AND BL,0FH

The above line code is used to Mask Left and Right Digits in AL Register and BL Register.

Next Line –     MOV CL,04H
    ROL AL,CL

ROL means Rotate operand1 left. The number of rotates is set by operand2. We need 4 digits to be Rotated Hence MOV CL,04H is used to Move  04H to CL Register. ROL AL,CL is used to Rotate AL Register to 4 digits Left.

Next Line – MOV CL,10
    MUL CL

MUL 10 is not possible where 10 is immediate, So we have to move 10 to CL register (we can take any register) i.e. MOV CL,10. MUL CL in this line CL register will be Multiplied with AX register (BY DEFAULT).

Next Line – ADD AL,BL

ADD AL,BL means Adding value of AL register with BL register.

Next Line – MOV CL,100
    MUL CL

MUL 10 is not possible where 10 is immediate, So we have to move 10 to CL register (we can take any register) i.e. MOV CL,100. MUL CL in this line CL register will be Multiplied with AX register (BY DEFAULT).

Next Line – MOV BL,TEMP

MOV BL,TEMP is used to Move TEMP variable to BL Register.

Next Line – MOV BH,0

MOV BH,0 is used to clear the unwanted value (garbage value) in BH register is removed by assigning ZERO to it.

Next Line – ADD AX,BX

ADD AX,BX means Adding value of AX register with BX register.

Next Line – LEA SI,RES
    CALL HEX2BIN

The above Two line code is used to initialize RES to SI register and Call Procedure HEX2BIN

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

CALL HEX2BIN is used to Call a Procedure named HEX2BIN

Next Line – LEA DX,RES
    MOV AH,9
    INT 21H

The above three line code is used to print String or Message present in the character Array till $  symbol which tells the compiler to stop. As we have initialized all the values in an Array to $ you will think what will be printed. The procedure is going to change the Array to its Resultant Decimal equivalent printable form i.e. ASCII form of a digit number.

Now, lets understand line by line

LEA DX,RES in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element.  This same code can be interchangably written as MOV DX, OFFSET RES where OFFSET  means effective address and MOV means move  second element into the first element.

MOV AH,9
INT 21H

The above two line code is used to PRINT the String or Message of the address present in DX register.

Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 9 or 9h, That means PRINT the String or Message of the address present in DX register.

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

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

Next Line – CODE ENDS

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

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

Next Line –  MOV CX,0
    MOV BX,2

MOV CX,0 is used to move or assign value 0 (decimal value) to  CX. The program which we are wishing to write is to covert HexaDecimal value to Decimal value, In which we will divide the number till the Quotient is going to be Zero. CX register ( CX is also Called COUNTER). CX register will count the number digit generated by dividing the Hexadecimal number by Base value of Decimal i.e.Ten. MOV BX,2 in this Base value 10 is moved to BX register, So that it is used to divide hexa number by 10.

Next Line – LOOP1: MOV DX,0

LOOP1: is a LABEL and all the words ending in colon (:) are Labels. MOV DX,0 is used to clear the unwanted value (garbage value) in DX register is removed by assigning ZERO to it. First Loop starts here.

Next Line – DIV BX
       ADD DL,30H

DIV instruction only works with REG or MEMORY hence we cannot use DIV 10 where 10 is immediate, So we have to move 10 to BX register (we can take any register) this we have already done above and Then DIV BX  Now DIV BX will Divide AX register with 10 which is passed to BX register and Result of division is present in AX register contains Quotientand DX register contains Remainder. Here we will not touch Quotient AX as it will be used for furture Division, But DX Remainder will be Decimal Digit and will always be less than Ten so the value will be in DL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later So ADD DL,30H.

Next Line – PUSH DX
       INC CX

PUSH is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. PUSH DX stores 16 bit data inside DX register into Stack Area. INC is a instruction for Increment the present in Register or Memory. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack.

Next Line – CMP AX,1
       JG LOOP1

CMP AX,9  is used to compare AX register with 9 and jump if AX is greater to the respective LABEL LOOP1. The result of Comparision is not stored anywhere, but flags are set according to result.  is Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. SECOND is the label where the compiler will JUMP. First Loop ends here. Note :- this loop is without LOOP keyword and depends upon the number to be converted.

Next Line – ADD AL,30H
       MOV [SI],AL

ADD AL,30H The Last Remainder will be Decimal Digit in AX register only as the number cannot be divided future and will always be less than Ten so the value will be in AL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

 Next Line – LOOP2: POP AX
       INC SI

LOOP2: is a LABEL and all the words ending in colon (:) are Labels. POP is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. POP AX gets 16 bit data to AX register from Top of Stack. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack and save it in AX register. Second Loop starts here.

Next Line – MOV [SI],AL

The values out of Stack saved in AX register saved in string in this Loop. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

Next Line – LOOP LOOP2

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

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 – HEX2BIN ENDP 

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

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_4digitBCD_2_DBinary

Output After Execution :-

Asm_program_4digitBCD_2_DBinary_Output

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

An Assembly Program, which should add two 5-byte numbers (numbers are stored in array- NUM1 & NUM2), and stores the sum in another array named RESULT

Now we will write another Assembly Program, which should add two 5-byte numbers (numbers are stored in array- NUM1 & NUM2), and stores the sum in another array named RESULT.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the Given Arrays and it will be NUM1 and NUM2 . Other variable will be holding the Result of Array Addition and it will be RESULT, So in all Three variables.
The identified variables are NUM1, NUM2 and RESULT.

First Line – DATA SEGMENT

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

Next Line – NUM1 DB 36H,55H,27H,42H
     NUM2 DB 38H,41H,29H,39H
     RESULT DB 5 DUP (0)

 NUM1 DB 36H,55H,27H,42H and NUM2 DB 38H,41H,29H,39H this line is a declaration of 8-bit Numbers Array initialized with 36H,55H,27H,42H and 38H,41H,29H,39H the numbers are seperated by Comma (,). RESULT DB 5 DUP (0) is used to Save the Result of Array Addition which will save sum of each elements of a Array NUM1 and NUM2. DUP is used to Duplicate the value in the Bracket ahead.

Next Line – DATA ENDS

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

Now, Selection of data type is DB data type the numbers which we are adding will be integers so DB is sufficient.

DATA SEGMENT
     NUM1 DB 36H,55H,27H,42H
     NUM2 DB 38H,41H,29H,39H
     RESULT DB 5 DUP (0)                                
ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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

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

DATA SEGMENT
     NUM1 DB 36H,55H,27H,42H
     NUM2 DB 38H,41H,29H,39H
     RESULT DB 5 DUP (0)                                
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA SI,NUM1
      LEA DI,NUM2
      LEA BX,RESULT
     
     
       MOV CX,5
LOOP1:      
       MOV AL,[SI]
                           
       ADD AL,[DI]
       MOV [BX],AL
            
       INC BX
       INC SI
       INC DI
      
       LOOP LOOP1   
     
      MOV AH,4CH
      INT 21H     
ENDS
END START

Explanation : 

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

Next Line – CODE SEGMENT

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

Next Line –     ASSUME DS:DATA CS:CODE

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

Next Line – START:

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

Next Line – MOV AX,DATA
MOV DS,AX

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

Next Line – LEA SI,NUM1
      LEA DI,NUM2
      LEA BX,RESULT

LEA SI,NUM1 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 PRICE where OFFSET  means effective address and MOV means move  second element into the first element. Here Base Address of variable PRICE is loaded in DX register. Similarly For LEA DI,NUM2 and LEA BX,RESULT.

Next Line – MOV CX,5

MOV CX,5 is used to move or assign value 5 ( Since the array is of length 5 ) to  CX. The program which we are wishing to write is to input eight characters from console which will be entered by the user, Hence to do so we need a loop construct. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – LOOP1:

LOOP1: is a LABEL and all the words ending in colon (:).

Next Line –  MOV AL,[SI]

MOV AL,[SI]  means move value in Address of SI register to AL register. [] is Refered as Value present in the Address of the Register in it.

Next Line –       ADD AL,[DI]

ADD AL,[DI]  means Add value in AL register with value in Address of SI register. DAA means Decimal Adjust after Addition. e.g. 36H is present in AL register, If added with 0AH will give 40H. After DAA it will Convert to Decimal equivalent to 46H.

Next Line –  MOV [BX],AL

MOV [BX],AL  means move value in AL register to Address of BX register. [] is Refered as Value present in the Address of the Register in it.

 Next Line –  INC BX
       INC SI
       INC DI

INC BX will increment the Address value present in BX register. Here we are using BX register as a counter and counting the numbers of Array elements to Cover all the Five elements. Similarly For INC SI and INC DI.

Next Line – LOOP LOOP1

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_Sum_Two_Array

Before Execution :-

Asm_program_Sum_Two_Array_V1

After Execution :-

Asm_program_Sum_Two_Array_V2

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

An Assembly program using the method of “add-and-shift” loop, in which you use the binary digits of one number to control additions of a shifted version of the other number into a running total; this is essentially the same algorithm you use when multiplying numbers by hand in decimal

Now we will write another Assembly program, Using the method of “add-and-shift” loop, in which you use the binary digits of one number to control additions of a shifted version of the other number into a running total; this is essentially the same algorithm you use when multiplying numbers by hand in decimal

Let’s identify variables needed for this program.

First variables will be the one which will hold the value predefined in the variables FIRST, SECOND to save numbers whuich are multiplied later and variable RES1 will hold PRODUCT of multiplication and Other variable RES2 will be holding the Resultant Decimal equivalent printable form to be printed for the User on Screen and Other variable will be holding the Message  “RESULT : $”  to be printed for the User, So in all Five variables.
The identified variables are FIRST, SECOND, RES1, RES2, and MSG2.

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 – FIRST DB 37
     SECOND DB 25
     RES1 DW ?
     RES2  DB 10 DUP (‘$’)
     MSG2 DB 10,13,”RESULT : $”

FIRST DB 37 and SECOND DB 25. We are initializing FIRST to 37 (Blank (or Nothing after number) stands for Decimal ( By Default) ),  SECOND to 25 ((Blank (or Nothing after number) stands for Decimal ( By Default) ), RES1 DW ?  We are initializing RES1 to ? (? stands for blank value), As we are accepting value from User from Console as the 4-digit number to find Sum of square of digits of it. Detailed explanation is given below. RES  DB 10 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 10 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. $). MSG2 DB 10,13,”RESULT : $” this line is a declaration of Charater Array initialized with “RESULT : $” 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 ).  

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
     FIRST DB 37
     SECOND DB 25
     RES1 DW ?
     RES2  DB 10 DUP ('$')
     MSG2 DB 10,13,"RESULT : $" 
ENDS

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

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

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

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

DATA SEGMENT
     FIRST DB 37
     SECOND DB 25
     RES1 DW ?
     RES2  DB 10 DUP ('$')
     MSG2 DB 10,13,"RESULT : $" 
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
    
      MOV CX,8
      MOV AL,FIRST
      CBW
      MOV BL,SECOND   
     
NXT: 
      SHR BL,1
      JNC SKIP
      ADD RES1,AX
     
SKIP:
      SHL AX,1
      LOOP NXT
     
      MOV AX,RES1                 
     
      LEA SI,RES2
      CALL HEX2DEC
   
      LEA DX,MSG2
      MOV AH,9
      INT 21H
      
      LEA DX,RES2
      MOV AH,9
      INT 21H 
     
      MOV AH,4CH
      INT 21H     
CODE ENDS
HEX2DEC PROC NEAR
    MOV CX,0
    MOV BX,10
   
LOOP1: MOV DX,0
       DIV BX
       ADD DL,30H
       PUSH DX
       INC CX
       CMP AX,9
       JG LOOP1
     
       ADD AL,30H
       MOV [SI],AL
     
LOOP2: POP AX
       INC SI
       MOV [SI],AL
       LOOP LOOP2
       RET
HEX2DEC ENDP
END START

Explanation : 

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

Next Line –CODE SEGMENT

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

Next Line –     ASSUME DS:DATA CS:CODE

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

Next Line – START:

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

Next Line – MOV AX,DATA
MOV DS,AX

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

Next Line – MOV CX,8
      MOV AL,FIRST
      CBW
      MOV BL,SECOND

MOV CX,8 is used to move or assign value 8 (Length of byte as we are multipling two 8-bit numbers) to  CX. 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). MOV AL,FIRST means move value of Variable FIRST to AL register.  CBW means Convert byte into word, This will convert AL value to AX register. MOV BL,SECOND means move value of Variable SECONDto BL register.

Next Line – NXT:

NXT: is a LABEL and all the words ending in colon (:).

Next Line – SHR BL,1
      JNC SKIP
      ADD RES1,AX

SHR means Shift operand1 Right. The number of shifts is set by operand2. SHR BL,1  is used to Shift with 1 and JNC SKIP jump if if Carry flag is set to Zero LABEL SKIP, SKIP is the label where the compiler will JUMP. ADD RES1,AX means Adding value of AX register with variable RES1 in which value of previous addintion ( if any) is present and Finally the Product is Saved in RES1 variable.

Next Line – SKIP:

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

Next Line – SHL AX,1

SHL means Shift operand1 Left. The number of shifts is set by operand2. SHL AX,1  is used to Shift with 1.

Next Line – LOOP NXT

LOOP NXT 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 AX,RES1

MOV AX,RES1 is to move RES1 variable (Product of Multipication) to AX register. This is done due to change of AX register.

Next Line – LEA SI,RES2
    CALL HEX2DEC

The above Two line code is used to initialize RES2 to SI register and Call Procedure HEX2DEC which will covert AX register value as result and Print it on user screen.

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

CALL HEX2DEC is used to Call a Procedure named HEX2DEC

Next Line – LEA DX,MSG2
      MOV AH,9
      INT 21H 

The above two line code is used to PRINT the String or Message of the address present in DX register i.e. for MSG2.

Next Line – LEA DX,RES2
    MOV AH,9
    INT 21H

The above three line code is used to print String or Message present in the character Array till $  symbol which tells the compiler to stop. As we have initialized all the values in an Array to $ you will think what will be printed. The procedure is going to change the Array to its Resultant Decimal equivalent printable form i.e. ASCII form of a digit number.

Now, lets understand line by line

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

MOV AH,9
INT 21H

The above two line code is used to PRINT the String or Message of the address present in DX register.

Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 9 or 9h, That means PRINT the String or Message of the address present in DX register.

Next Line – MOV AH,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 – HEX2DEC PROC NEAR

This line of code is used to start a procedure code and we can make out the procedure by the keyword PROC which tells us the procedure is started. In assembly language we have two types of Procedures one is NEAR and other is FAR. NEAR is used to call the Procedure within the program whereas FAR is used to call the procedure outside the program. HEX2DEC is only the Name given to the Procedure Code. 

Next Line –  MOV CX,0
    MOV BX,10

MOV CX,0 is used to move or assign value 0 (decimal value) to  CX. The program which we are wishing to write is to covert HexaDecimal value to Decimal value, In which we will divide the number till the Quotient is going to be Zero. CX register ( CX is also Called COUNTER). CX register will count the number digit generated by dividing the Hexadecimal number by Base value of Decimal i.e.Ten. MOV BX,10 in this Base value 10 is moved to BX register, So that it is used to divide hexa number by 10.

Next Line – LOOP1: MOV DX,0

LOOP1: is a LABEL and all the words ending in colon (:) are Labels. MOV DX,0 is used to clear the unwanted value (garbage value) in DX register is removed by assigning ZERO to it. First Loop starts here.

Next Line – DIV BX
       ADD DL,30H

DIV instruction only works with REG or MEMORY hence we cannot use DIV 10 where 10 is immediate, So we have to move 10 to BX register (we can take any register) this we have already done above and Then DIV BX  Now DIV BX will Divide AX register with 10 which is passed to BX register and Result of division is present in AX register contains Quotientand DX register contains Remainder. Here we will not touch Quotient AX as it will be used for furture Division, But DX Remainder will be Decimal Digit and will always be less than Ten so the value will be in DL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later So ADD DL,30H.

Next Line – PUSH DX
       INC CX

PUSH is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. PUSH DX stores 16 bit data inside DX register into Stack Area. INC is a instruction for Increment the present in Register or Memory. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack.

Next Line – MOV CX,10

MOV CX,10 is used to move or assign value 10 (decimal value) to  CX. The program which we are wishing to write is to input ten characters from console which will be entered by the user, Hence to do so we need a loop construct. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – CMP AX,9
       JG LOOP1

CMP AX,9  is used to compare AX register with 9 and jump if AX is greater to the respective LABEL LOOP1. The result of Comparision is not stored anywhere, but flags are set according to result.  is Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. SECOND is the label where the compiler will JUMP. First Loop ends here. Note :- this loop is without LOOP keyword and depends upon the number to be converted.

Next Line – ADD AL,30H
       MOV [SI],AL

ADD AL,30H The Last Remainder will be Decimal Digit in AX register only as the number cannot be divided future and will always be less than Ten so the value will be in AL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

 Next Line – LOOP2: POP AX
       INC SI

LOOP2: is a LABEL and all the words ending in colon (:) are Labels. POP is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. POP AX gets 16 bit data to AX register from Top of Stack. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack and save it in AX register. Second Loop starts here.

Next Line – MOV [SI],AL

The values out of Stack saved in AX register saved in string in this Loop. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

Next Line – LOOP LOOP2

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

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 – HEX2DEC ENDP 

HEX2DEC ENDP is the End point of the Procedure in a Program.

This line of code is used to end the procedure code and we can make out the procedure by the keyword ENDP which tells us the procedure is ended. In assembly language we have two types of Procedures one is NEAR and other is FAR. NEAR is used to call the Procedure within the program whereas FAR is used to call the procedure outside the program. HEX2DEC is only the Name given to the Procedure Code. 

Last Line – END START

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

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

Screen Shots :- 

Asm_program_Mult_Using_Add_&_Shift

Variables Before Execution :-

Asm_program_Mult_Using_Add_&_Shift_V1

Variables After Execution :-

Asm_program_Mult_Using_Add_&_Shift_V2

Output :-

Asm_program_Mult_Using_Add_&_Shift_Output

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

An Assembly program, which takes the input of 4-digit number, and display the sum of square of digits

Now we will write another Assembly program, which takes the input of  4-digit number, and display the sum of square of digits

Let’s identify variables needed for this program.

First variables will be the one which will hold the value entered by user in the variables D1, D2, D3, D4 to save 4-Digits separately in 4 variables  and variable SUM will hold sum of square of digits  and Other variable RES will be holding the Resultant Decimal equivalent printable form to be printed for the User on Screen and Other variables will be holding the Messages “ENTER NUMBER WITH FOUR DIGITS : $” and “RESULT : $”  to be printed for the User, So in all Eight variables.
The identified variables are D1, D2, D3, D4SUM, RES, MSG1, and MSG2.

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 “ENTER NUMBER WITH FOUR DIGITS : $”
     MSG2 DB 10,13,”RESULT : $”
     D1 DB ? 
     D2 DB ?
     D3 DB ?
     D4 DB ?
     SUM DB ?
     RES  DB 10 DUP (‘$’)

D1 DB ?, D2 DB ?, D3 DB ?, D4 DB ?, SUM DB ?  We are initializing ALL to ? (? stands for blank value), As we are accepting value from User from Console as the 4-digit number to find Sum of square of digits of it. Detailed explanation is given below. RES  DB 10 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 10 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 NUMBER WITH FOUR DIGITS : $” this line is a declaration of Charater Array initialized with “ENTER NUMBER WITH FOUR DIGITS : $” 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 ). MSG2 DB 10,13,”RESULT : $” 

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 "ENTER NUMBER WITH FOUR DIGITS : $"
     MSG2 DB 10,13,"RESULT : $"
     D1 DB ? 
     D2 DB ?
     D3 DB ?
     D4 DB ?
     SUM DB ?
     RES  DB 10 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).

DATA SEGMENT
     MSG1 DB "ENTER NUMBER WITH FOUR DIGITS : $"
     MSG2 DB 10,13,"RESULT : $"
     D1 DB ? 
     D2 DB ?
     D3 DB ?
     D4 DB ?
     SUM DB ?
     RES  DB 10 DUP ('$')
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA DX,MSG1         
      MOV AH,9
      INT 21H     
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D1,AL
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D2,AL
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D3,AL
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D4,AL
                
      ADD AL,D3
      ADD AL,D2
      ADD AL,D1      
      MOV SUM,AL 
     
      LEA SI,RES
      CALL HEX2DEC
   
      LEA DX,MSG2
      MOV AH,9
      INT 21H
      
      LEA DX,RES
      MOV AH,9
      INT 21H 
     
      MOV AH,4CH
      INT 21H     
CODE ENDS
HEX2DEC PROC NEAR
    MOV CX,0
    MOV BX,10
   
LOOP1: MOV DX,0
       DIV BX
       ADD DL,30H
       PUSH DX
       INC CX
       CMP AX,9
       JG LOOP1
     
       ADD AL,30H
       MOV [SI],AL
     
LOOP2: POP AX
       INC SI
       MOV [SI],AL
       LOOP LOOP2
       RET
HEX2DEC ENDP
END START

Explanation : 

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

Next Line –CODE SEGMENT

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

Next Line –     ASSUME DS:DATA CS:CODE

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

Next Line – START:

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

Next Line – MOV AX,DATA
MOV DS,AX

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

Next Line – LEA DX,MSG1    
      MOV AH,9
      INT 21H

The above three line code is used to print String or Message present in the character Array till $  symbol which tells the compiler to stop.

Now, lets understand line by line

LEA DX,MSG1 in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element.  This same code can be interchangably written as MOV DX, OFFSET MSG1 where OFFSET  means effective address and MOV means move  second element into the first element.

MOV AH,9
INT 21H

The above two line code is used to PRINT the String or Message of the address present in DX register.

Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 9 or 9h, That means PRINT the String or Message of the address present in DX register.

Next Line – MOV AH,1
      INT 21H

 The above three line code is used to Read a Character from Console and save the value entered in variable R in its ASCII form.

Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 1 or 1h, That means READ a Character from Console, Echo it on screen and save the value entered in AL register.

Next Line – SUB AL,30H
      MOV AH,0
      MUL AL
    MOV D1,AL

The above Two line code is used to convert the value entered in variable R from ASCII form to its BCD form. This can be done by subtracting 30H i.e. SUB AL,30H. The value coming from Console is Basically in ASCII form. eg. When you enter 5 we see 35H,So by subtracting 30H we get back to value as 5. SUB AL,30H means subtracting 30H from AL. MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it. MUL AL is used to Multiply AL with AX register  i.e.Value present in AX register. MOV D1,AL  means move value in AL register into variable D1.

Similarly

Next Line – MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D2,AL
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D3,AL
     
      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV AH,0
      MUL AL
      MOV D4,AL

Same as above to scan 4-digit number separately multiply it with itself to get Square and save it in D1,D2,D3,D4 separately and from the Console (Screen) entered by User.

Next Line – ADD AL,D3
      ADD AL,D2
      ADD AL,D1      
      MOV SUM,AL

 ADD AL,D3 means Adding value of variable D3 from AL register in which value of variable D4 is present. ADD AL,D2 means Adding value of variable D2 from AL register. ADD AL,D1 means Adding value of variable D2 from AL register. MOV SUM,AL  means move value in AL register into variable SUM.

Next Line – LEA SI,RES
    CALL HEX2DEC

The above Two line code is used to initialize RES to SI register and Call Procedure HEX2DEC which will covert AX register value as result and Print it on user screen.

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

CALL HEX2DEC is used to Call a Procedure named HEX2DEC

Next Line – LEA DX,MSG2
      MOV AH,9
      INT 21H 

The above two line code is used to PRINT the String or Message of the address present in DX register i.e. for MSG2.

Next Line – LEA DX,RES
    MOV AH,9
    INT 21H

The above three line code is used to print String or Message present in the character Array till $  symbol which tells the compiler to stop. As we have initialized all the values in an Array to $ you will think what will be printed. The procedure is going to change the Array to its Resultant Decimal equivalent printable form i.e. ASCII form of a digit number.

Now, lets understand line by line

LEA DX,RES in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element.  This same code can be interchangably written as MOV DX, OFFSET RES where OFFSET  means effective address and MOV means move  second element into the first element.

MOV AH,9
INT 21H

The above two line code is used to PRINT the String or Message of the address present in DX register.

Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 9 or 9h, That means PRINT the String or Message of the address present in DX register.

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 – HEX2DEC PROC NEAR

This line of code is used to start a procedure code and we can make out the procedure by the keyword PROC which tells us the procedure is started. In assembly language we have two types of Procedures one is NEAR and other is FAR. NEAR is used to call the Procedure within the program whereas FAR is used to call the procedure outside the program. HEX2DEC is only the Name given to the Procedure Code. 

Next Line –  MOV CX,0
    MOV BX,10

MOV CX,0 is used to move or assign value 0 (decimal value) to  CX. The program which we are wishing to write is to covert HexaDecimal value to Decimal value, In which we will divide the number till the Quotient is going to be Zero. CX register ( CX is also Called COUNTER). CX register will count the number digit generated by dividing the Hexadecimal number by Base value of Decimal i.e.Ten. MOV BX,10 in this Base value 10 is moved to BX register, So that it is used to divide hexa number by 10.

Next Line – LOOP1: MOV DX,0

LOOP1: is a LABEL and all the words ending in colon (:) are Labels. MOV DX,0 is used to clear the unwanted value (garbage value) in DX register is removed by assigning ZERO to it. First Loop starts here.

Next Line – DIV BX
       ADD DL,30H

DIV instruction only works with REG or MEMORY hence we cannot use DIV 10 where 10 is immediate, So we have to move 10 to BX register (we can take any register) this we have already done above and Then DIV BX  Now DIV BX will Divide AX register with 10 which is passed to BX register and Result of division is present in AX register contains Quotientand DX register contains Remainder. Here we will not touch Quotient AX as it will be used for furture Division, But DX Remainder will be Decimal Digit and will always be less than Ten so the value will be in DL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later So ADD DL,30H.

Next Line – PUSH DX
       INC CX

PUSH is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. PUSH DX stores 16 bit data inside DX register into Stack Area. INC is a instruction for Increment the present in Register or Memory. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack.

Next Line – MOV CX,10

MOV CX,10 is used to move or assign value 10 (decimal value) to  CX. The program which we are wishing to write is to input ten characters from console which will be entered by the user, Hence to do so we need a loop construct. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – CMP AX,9
       JG LOOP1

CMP AX,9  is used to compare AX register with 9 and jump if AX is greater to the respective LABEL LOOP1. The result of Comparision is not stored anywhere, but flags are set according to result.  is Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. SECOND is the label where the compiler will JUMP. First Loop ends here. Note :- this loop is without LOOP keyword and depends upon the number to be converted.

Next Line – ADD AL,30H
       MOV [SI],AL

ADD AL,30H The Last Remainder will be Decimal Digit in AX register only as the number cannot be divided future and will always be less than Ten so the value will be in AL register only and to make it printable on Console (Screen) we have to add  30H So that it will become a ASCII character and will be saved in Charater Array and will be printed as String later. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

 Next Line – LOOP2: POP AX
       INC SI

LOOP2: is a LABEL and all the words ending in colon (:) are Labels. POP is a stack function. Stack is an area of memory for keeping temporary data. PUSH and POP are two stack operations which stores or gets 16 bits of data. POP AX gets 16 bit data to AX register from Top of Stack. INC CX will increment the value present in CX register by One. Here we are using CX register as a counter and counting the numbers of digits in their ASCII form which are pushed into Stack. So that the same count will help to POP the values out of Stack and save it in AX register. Second Loop starts here.

Next Line – MOV [SI],AL

The values out of Stack saved in AX register saved in string in this Loop. MOV [SI],AL Saving the Characters in Character Array (i.e. String) is done by Moving AL register to Address of SI register which is represented in Square Brackets i.e. [SI]. SI is assigned with the Character Array i.e. RES.

Next Line – LOOP LOOP2

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

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 – HEX2DEC ENDP 

HEX2DEC ENDP is the End point of the Procedure in a Program.

This line of code is used to end the procedure code and we can make out the procedure by the keyword ENDP which tells us the procedure is ended. In assembly language we have two types of Procedures one is NEAR and other is FAR. NEAR is used to call the Procedure within the program whereas FAR is used to call the procedure outside the program. HEX2DEC is only the Name given to the Procedure Code. 

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_Sum_Square_Digits

Output :-

 Asm_program_Sum_Square_Digits_Output

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

An Assembly Language program two subroutines encrypting and decrypting – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCSL-017
Course Title : C and Assembly Language Programming
Assignment Number : MCA(1)/L017/Assign/2013 

 

Write a Program in assembly language that has two subroutines: One for encrypting alphabets of a string and second for decrypting the encoded string. In Encryption, simply convert a character /number into its predefined numerical/character value. Decryption is a reverse process of encryption. Write suitable Main program in C that calls these function. Test your program suitably

 

 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 ENCRYPT
       
        DISPLAY MSG3
        DISPLAY P11
       
        MOV AH,4CH
        INT 21H
CODE ENDS

ENCRYPT PROC NEAR
        MOV CH,0       
CHECK1:
        CMP [SI],2FH
        JB DONE1
        CMP [SI],3AH
        JB NUM1
       
        CMP [SI],41H
        JB DONE1
        CMP [SI],5BH
        JB UPR1
       
        CMP [SI],61H
        JB DONE1
        CMP [SI],7BH
        JB LWR1
       
NUM1:    CMP [SI],35H
        JB LNUM1
        SUB [SI],05H
        JMP DONE1
   LNUM1:    
        ADD [SI],05H
        JMP DONE1   
       
UPR1:    CMP [SI],4EH
        JB LUPR1
        SUB [SI],0DH
        JMP DONE1
   LUPR1:    
        ADD [SI],0DH
        JMP DONE1

LWR1:    CMP [SI],6EH
        JB LLWR1
        SUB [SI],0DH
        JMP DONE1
   LLWR1:    
        ADD [SI],0DH
             
DONE1:       
        INC SI
        LOOP CHECK1
       RET
ENCRYPT ENDP

DECRYPT PROC NEAR
        MOV CH,0       
CHECK2:
        CMP [SI],2FH
        JB DONE2
        CMP [SI],3AH
        JB NUM2
       
        CMP [SI],41H
        JB DONE2
        CMP [SI],5BH
        JB UPR2
       
        CMP [SI],61H
        JB DONE2
        CMP [SI],7BH
        JB LWR2
       
NUM2:   CMP [SI],35H
        JB LNUM2
        ADD [SI],05H
        JMP DONE2
   LNUM2:    
        SUB [SI],05H
        JMP DONE2   
       
UPR2:    CMP [SI],4EH
        JB LUPR2
        ADD [SI],0DH
        JMP DONE2
   LUPR2:    
        SUB [SI],0DH
        JMP DONE2

LWR2:    CMP [SI],6EH
        JB LLWR2
        ADD [SI],0DH
        JMP DONE2
   LLWR2:    
        SUB [SI],0DH
             
DONE2:       
        INC SI
        LOOP CHECK2
       RET
DECRYPT ENDP
   
END START

 

program code :

  1.  DATA SEGMENT
  2.     MSG1 DB 10,13,'ENTER STRING HERE :- $'   
  3.     MSG2 DB 10,13,'ENCRYPTED STRING IS :- $'
  4.     MSG3 DB 10,13,'DECRYPTED STRING IS : $'
  5.    
  6.     P1 LABEL BYTE
  7.     M1 DB 0FFH
  8.     L1 DB ?
  9.     P11 DB 0FFH DUP ('$')
  10.    
  11. DATA ENDS
  12. DISPLAY MACRO MSG
  13.     MOV AH,9
  14.     LEA DX,MSG
  15.     INT 21H
  16. ENDM   
  17. CODE SEGMENT
  18.     ASSUME CS:CODE,DS:DATA
  19. START:
  20.         MOV AX,DATA
  21.         MOV DS,AX                
  22.                
  23.         DISPLAY MSG1
  24.        
  25.         LEA DX,P1
  26.         MOV AH,0AH    
  27.         INT 21H
  28.                       
  29.         LEA SI,P11
  30.         MOV CL,L1
  31.         CALL ENCRYPT
  32.        
  33.         DISPLAY MSG2
  34.         DISPLAY P11
  35.                                
  36.         LEA SI,P11
  37.         MOV CL,L1
  38.         CALL ENCRYPT
  39.        
  40.         DISPLAY MSG3
  41.         DISPLAY P11
  42.        
  43.         MOV AH,4CH
  44.         INT 21H
  45. CODE ENDS
  46. ENCRYPT PROC NEAR
  47.         MOV CH,0       
  48. CHECK1:
  49.         CMP [SI],2FH
  50.         JB DONE1
  51.         CMP [SI],3AH
  52.         JB NUM1
  53.        
  54.         CMP [SI],41H
  55.         JB DONE1
  56.         CMP [SI],5BH
  57.         JB UPR1
  58.        
  59.         CMP [SI],61H
  60.         JB DONE1
  61.         CMP [SI],7BH
  62.         JB LWR1
  63.        
  64. NUM1:    CMP [SI],35H
  65.         JB LNUM1
  66.         SUB [SI],05H
  67.         JMP DONE1
  68.    LNUM1:    
  69.         ADD [SI],05H
  70.         JMP DONE1   
  71.        
  72. UPR1:    CMP [SI],4EH
  73.         JB LUPR1
  74.         SUB [SI],0DH
  75.         JMP DONE1
  76.    LUPR1:    
  77.         ADD [SI],0DH
  78.         JMP DONE1
  79. LWR1:    CMP [SI],6EH
  80.         JB LLWR1
  81.         SUB [SI],0DH
  82.         JMP DONE1
  83.    LLWR1:    
  84.         ADD [SI],0DH
  85.              
  86. DONE1:       
  87.         INC SI
  88.         LOOP CHECK1
  89.        RET
  90. ENCRYPT ENDP
  91. DECRYPT PROC NEAR
  92.         MOV CH,0       
  93. CHECK2:
  94.         CMP [SI],2FH
  95.         JB DONE2
  96.         CMP [SI],3AH
  97.         JB NUM2
  98.        
  99.         CMP [SI],41H
  100.         JB DONE2
  101.         CMP [SI],5BH
  102.         JB UPR2
  103.        
  104.         CMP [SI],61H
  105.         JB DONE2
  106.         CMP [SI],7BH
  107.         JB LWR2
  108.        
  109. NUM2:   CMP [SI],35H
  110.         JB LNUM2
  111.         ADD [SI],05H
  112.         JMP DONE2
  113.    LNUM2:    
  114.         SUB [SI],05H
  115.         JMP DONE2   
  116.        
  117. UPR2:    CMP [SI],4EH
  118.         JB LUPR2
  119.         ADD [SI],0DH
  120.         JMP DONE2
  121.    LUPR2:    
  122.         SUB [SI],0DH
  123.         JMP DONE2
  124. LWR2:    CMP [SI],6EH
  125.         JB LLWR2
  126.         ADD [SI],0DH
  127.         JMP DONE2
  128.    LLWR2:    
  129.         SUB [SI],0DH
  130.              
  131. DONE2:       
  132.         INC SI
  133.         LOOP CHECK2
  134.        RET
  135. DECRYPT ENDP
  136.    
  137. END START

Screen shots :-

Asm_program_Encrpt_Decrypt

Output :-

Asm_program_Encrpt_Decrypt_Output

An assembly program to accept a decimal number and print it’s 2′s complement binary and hexadecimal – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCSL-017
Course Title : C and Assembly Language Programming
Assignment Number : MCA(1)/L017/Assign/2013 

 

Write an assembly language program to accept a decimal number and display it’s two’s complement representation in binary and hexadecimal formats

 

DATA SEGMENT
    DIGIT1 DB ?
    DIGIT2 DB ?
    HEXNUM DB ?
    HEXADEC DB 3 DUP (‘$’)
    TWOSCOMP DB 10 DUP (‘$’)
    MSG1 DB “ENTER NUMBER : $”
    MSG2 DB 10,13,”HEXADECIMAL FORM IS : $”
    MSG3 DB 10,13,”TWO’S COMPLEMENT FORM IS : $”
DATA ENDS

CODE SEGMENT
        ASSUME DS:DATA,CS:CODE
START:       
    MOV AX,DATA
    MOV DS,AX
   
    LEA DX,MSG1
    MOV AH,9
    INT 21H
   
    MOV AH,1
    INT 21H
    SUB AL,30H
    MOV DIGIT1,AL
   
    MOV AH,1
    INT 21H
    SUB AL,30H
    MOV DIGIT2,AL
   
    MOV AH,0
    MOV AL,DIGIT1
   
    MOV CL,10
    MUL CL
   
    ADD AL,DIGIT2
    MOV HEXNUM,AL
   
    NOT AL
    ADD AL,1

    LEA SI,TWOSCOMP
    CALL HEX2BIN
   
    MOV AL,HEXNUM
    MOV BL,AL
    AND AL,0F0H
    AND BL,0FH
     
    MOV CL,04H
    ROL AL,CL
     
    LEA DI,HEXADEC
    MOV [DI],AL
    MOV [DI+1],BL
   
       LEA SI,HEXADEC        
       MOV CL,2
       MOV CH,0       
CHECK:
       CMP [SI],9
       JG ALPHA
   
NUM:   ADD [SI],30H
       JMP DONE
      
ALPHA: ADD [SI],37H 
            
DONE:       
       INC SI
       LOOP CHECK
   
      
    LEA DX,MSG2
    MOV AH,9
    INT 21H
      
    LEA DX,HEXADEC
    MOV AH,9
    INT 21H
   
    LEA DX,MSG3
    MOV AH,9
    INT 21H
      
    LEA DX,TWOSCOMP
    MOV AH,9
    INT 21H
            
    MOV AH,4CH
    INT 21H        
CODE ENDS  

HEX2BIN PROC NEAR
    MOV CX,0
    MOV BX,2
   
LOOP1: MOV DX,0
       DIV BX
       ADD DL,30H
       PUSH DX
       INC CX
       CMP AX,1
       JG LOOP1
     
       ADD AL,30H
       MOV [SI],AL
     
LOOP2: POP AX
       INC SI
       MOV [SI],AL
       LOOP LOOP2
       RET
HEX2BIN ENDP           
   
END START

 

program code :

  1. DATA SEGMENT
  2.     DIGIT1 DB ?
  3.     DIGIT2 DB ?
  4.     HEXNUM DB ?
  5.     HEXADEC DB 3 DUP ('$')
  6.     TWOSCOMP DB 10 DUP ('$')
  7.     MSG1 DB "ENTER NUMBER : $"
  8.     MSG2 DB 10,13,"HEXADECIMAL FORM IS : $"
  9.     MSG3 DB 10,13,"TWO'S COMPLEMENT FORM IS : $"
  10. DATA ENDS
  11. CODE SEGMENT
  12.         ASSUME DS:DATA,CS:CODE
  13. START:       
  14.     MOV AX,DATA
  15.     MOV DS,AX
  16.    
  17.     LEA DX,MSG1
  18.     MOV AH,9
  19.     INT 21H
  20.    
  21.     MOV AH,1
  22.     INT 21H
  23.     SUB AL,30H
  24.     MOV DIGIT1,AL
  25.    
  26.     MOV AH,1
  27.     INT 21H
  28.     SUB AL,30H
  29.     MOV DIGIT2,AL
  30.    
  31.     MOV AH,0
  32.     MOV AL,DIGIT1
  33.    
  34.     MOV CL,10
  35.     MUL CL
  36.    
  37.     ADD AL,DIGIT2
  38.     MOV HEXNUM,AL
  39.    
  40.     NOT AL
  41.     ADD AL,1
  42.     LEA SI,TWOSCOMP
  43.     CALL HEX2BIN
  44.    
  45.     MOV AL,HEXNUM
  46.     MOV BL,AL
  47.     AND AL,0F0H
  48.     AND BL,0FH
  49.      
  50.     MOV CL,04H
  51.     ROL AL,CL
  52.      
  53.     LEA DI,HEXADEC
  54.     MOV [DI],AL
  55.     MOV [DI+1],BL
  56.    
  57.        LEA SI,HEXADEC        
  58.        MOV CL,2
  59.        MOV CH,0       
  60. CHECK:
  61.        CMP [SI],9
  62.        JG ALPHA
  63.    
  64. NUM:   ADD [SI],30H
  65.        JMP DONE
  66.       
  67. ALPHA: ADD [SI],37H 
  68.             
  69. DONE:       
  70.        INC SI
  71.        LOOP CHECK
  72.    
  73.       
  74.     LEA DX,MSG2
  75.     MOV AH,9
  76.     INT 21H
  77.       
  78.     LEA DX,HEXADEC
  79.     MOV AH,9
  80.     INT 21H
  81.    
  82.     LEA DX,MSG3
  83.     MOV AH,9
  84.     INT 21H
  85.       
  86.     LEA DX,TWOSCOMP
  87.     MOV AH,9
  88.     INT 21H
  89.             
  90.     MOV AH,4CH
  91.     INT 21H        
  92. CODE ENDS  
  93. HEX2BIN PROC NEAR
  94.     MOV CX,0
  95.     MOV BX,2
  96.    
  97. LOOP1: MOV DX,0
  98.        DIV BX
  99.        ADD DL,30H
  100.        PUSH DX
  101.        INC CX
  102.        CMP AX,1
  103.        JG LOOP1
  104.      
  105.        ADD AL,30H
  106.        MOV [SI],AL
  107.      
  108. LOOP2: POP AX
  109.        INC SI
  110.        MOV [SI],AL
  111.        LOOP LOOP2
  112.        RET
  113. HEX2BIN ENDP           
  114.    
  115. END START

Screen shots :-

Asm_program_Dec_to_2sCompBin_Hexa

Outputs :-

Asm_program_Dec_to_2sCompBin_Hexa_Output1

Asm_program_Dec_to_2sCompBin_Hexa_Output2

An Assembly Language program to convert packed BCD to equivalent binary – IGNOU MCA Assignment 2013

BACHELOR  OF COMPUTER APPLICATIONS
Course Code : BCSL-022
Course Title : Assembly Language Programming Lab
Assignment Number : BCA(II)/BCSL022/Assign/13

 

Write and run an assembly language program that converts a packed two digit BCD number in AL register to equivalent binary number. The binary number is stored in the AH register

 

DATA SEGMENT
     PACKEDBCD DB 52H
     BINARY    DB ?
DATA ENDS
CODE SEGMENT
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
    
      MOV AH,0
      MOV AL,PACKEDBCD
      MOV BL,AL
     
      AND AL,0F0H
      AND BL,0FH
     
      MOV CL,04H
      ROL AL,CL
     
      MOV CL,10
      MUL CL
     
      ADD AL,BL
     
      MOV AH,AL
      MOV BINARY,AH    
          
      MOV AH,4CH
      INT 21H    
CODE ENDS
END START

 

program code :

  1. DATA SEGMENT
  2.      PACKEDBCD DB 52H
  3.      BINARY    DB ?
  4. DATA ENDS
  5. CODE SEGMENT
  6.     ASSUME DS:DATA CS:CODE
  7. START:
  8.       MOV AX,DATA
  9.       MOV DS,AX
  10.     
  11.       MOV AH,0
  12.       MOV AL,PACKEDBCD
  13.       MOV BL,AL
  14.      
  15.       AND AL,0F0H
  16.       AND BL,0FH
  17.      
  18.       MOV CL,04H
  19.       ROL AL,CL
  20.      
  21.       MOV CL,10
  22.       MUL CL
  23.      
  24.       ADD AL,BL
  25.      
  26.       MOV AH,AL
  27.       MOV BINARY,AH    
  28.           
  29.       MOV AH,4CH
  30.       INT 21H    
  31. CODE ENDS
  32. END START

Screen shots :-

Asm_program_Packed_BCD_to_Binary

Before Execution :-

Asm_program_Packed_BCD_to_Binary_V1

After Execution :-

 Asm_program_Packed_BCD_to_Binary_V2

A near procedure assembly Language program to convert ASCII to binary – IGNOU MCA Assignment 2013

BACHELOR  OF COMPUTER APPLICATIONS
Course Code : BCSL-022
Course Title : Assembly Language Programming Lab
Assignment Number : BCA(II)/BCSL022/Assign/13

 

 Write and run (using appropriate calling program) a near procedure in assembly language that converts an ASCII digit passed to it on AL register to equivalent binary value. The value is returned back on the AL register itself

 

DATA SEGMENT
    STR1 DB “MAHESH$”
    STR2 DB 7 DUP (‘$’)
    MSG1 DB 10,13,’STORED STRING IN MEMORY IS : $’
    MSG2 DB 10,13,’REVERSE STRING IS : $’
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
                    
        DISPLAY STR1
                       
        LEA SI,STR2
        LEA DI,STR1
        ADD DI,5
               
        MOV CX,6
REVERSE:
        MOV AL,[DI]
        MOV [SI],AL
        INC SI
        DEC DI
        LOOP REVERSE
       
        DISPLAY MSG2
                   
        DISPLAY STR2
                     
        MOV AH,4CH
        INT 21H
CODE ENDS
END START

 

program code :

  1. DATA SEGMENT
  2.     STR1 DB "MAHESH$"
  3.     STR2 DB 7 DUP ('$')
  4.     MSG1 DB 10,13,'STORED STRING IN MEMORY IS : $'
  5.     MSG2 DB 10,13,'REVERSE STRING IS : $'
  6. DATA ENDS
  7. DISPLAY MACRO MSG
  8.     MOV AH,9
  9.     LEA DX,MSG
  10.     INT 21H
  11. ENDM   
  12. CODE SEGMENT
  13.     ASSUME CS:CODE,DS:DATA
  14. START:
  15.         MOV AX,DATA
  16.         MOV DS,AX                
  17.                
  18.         DISPLAY MSG1
  19.                     
  20.         DISPLAY STR1
  21.                        
  22.         LEA SI,STR2
  23.         LEA DI,STR1
  24.         ADD DI,5
  25.                
  26.         MOV CX,6
  27. REVERSE:
  28.         MOV AL,[DI]
  29.         MOV [SI],AL
  30.         INC SI
  31.         DEC DI
  32.         LOOP REVERSE
  33.        
  34.         DISPLAY MSG2
  35.                    
  36.         DISPLAY STR2
  37.                      
  38.         MOV AH,4CH
  39.         INT 21H
  40. CODE ENDS
  41. END START

Screen shots :-

Asm_program_Rev_Str_Memory

Output :-

 Asm_program_Rev_Str_Memory_Output

An Assembly Language program to reverse a string in memory and displayed – IGNOU MCA Assignment 2013

BACHELOR  OF COMPUTER APPLICATIONS
Course Code : BCSL-022
Course Title : Assembly Language Programming Lab
Assignment Number : BCA(II)/BCSL022/Assign/13

 

Write and run an Assembly language program that reverses a string. You may assume that the string is available in the memory and is 6 byte long. The reversed string should be stored in separate memory locations and should be displayed

 

DATA SEGMENT
    STR1 DB “MAHESH$”
    STR2 DB 7 DUP (‘$’)
    MSG1 DB 10,13,’STORED STRING IN MEMORY IS : $’
    MSG2 DB 10,13,’REVERSE STRING IS : $’
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
                    
        DISPLAY STR1
                       
        LEA SI,STR2
        LEA DI,STR1
        ADD DI,5
               
        MOV CX,6
REVERSE:
        MOV AL,[DI]
        MOV [SI],AL
        INC SI
        DEC DI
        LOOP REVERSE
       
        DISPLAY MSG2
                   
        DISPLAY STR2
                     
        MOV AH,4CH
        INT 21H
CODE ENDS
END START

 

program code :

  1. DATA SEGMENT
  2.     STR1 DB "MAHESH$"
  3.     STR2 DB 7 DUP ('$')
  4.     MSG1 DB 10,13,'STORED STRING IN MEMORY IS : $'
  5.     MSG2 DB 10,13,'REVERSE STRING IS : $'
  6. DATA ENDS
  7. DISPLAY MACRO MSG
  8.     MOV AH,9
  9.     LEA DX,MSG
  10.     INT 21H
  11. ENDM   
  12. CODE SEGMENT
  13.     ASSUME CS:CODE,DS:DATA
  14. START:
  15.         MOV AX,DATA
  16.         MOV DS,AX                
  17.                
  18.         DISPLAY MSG1
  19.                     
  20.         DISPLAY STR1
  21.                        
  22.         LEA SI,STR2
  23.         LEA DI,STR1
  24.         ADD DI,5
  25.                
  26.         MOV CX,6
  27. REVERSE:
  28.         MOV AL,[DI]
  29.         MOV [SI],AL
  30.         INC SI
  31.         DEC DI
  32.         LOOP REVERSE
  33.        
  34.         DISPLAY MSG2
  35.                    
  36.         DISPLAY STR2
  37.                      
  38.         MOV AH,4CH
  39.         INT 21H
  40. CODE ENDS
  41. END START

Screen shots :-

Asm_program_Rev_Str_Memory

Output :-

 Asm_program_Rev_Str_Memory_Output