sap4help@gmail.com +91 8956877466
This website is always under updation with new contents
UNIT-2 Programming in MASM
Unit I
Unit II
Unit III
Unit IV
Unit V
Assembler
Assembler is a program which converts assembly language program to machine level. it converts assembly language programming into machine level language by following 4 different steps.
1. It makes assembler file having extension ‘.ASM’.
2. Create objective file having extension ‘.OBJ’.
3. It makes cross file of program having extension ‘.CRS’.
4. It makes executable file of program having extension ‘.EXF’.
There are 4 types of well known assemblers.
1 MASAM,
2 TASM,
3 Emulator,
4 Simulator
1) TASM:-
It is a turbo assembler. This assembler created by Borland international.
2) MASM:-
It is a macroassembler. This assembler created by Microsoft corporation.The programming structure of MASM and TASM are nearly same.
Directives
The assembly language program contains instruction as well as directions to the assembler.these directions to the assembler are usually called as assembler directives or pseudo operations.
Following are the assemblers directives.
1. SEGMENTS AND ENDS:
• These directives are used to tell the identify a group of data items or group of instructions that you want to put together in a particular segment.
• These directives are used in the same way that paranthesis are used to group like terms in algebra.
• A group of data segment or instruction contained between segments and ends.directives called logical segments.
• When we set a logical segment,we need to give it a name like
Data SEGMENT 
         Statement 1
         Statement 2
         .
         .
         .
         .
Data ENDS 
Code SEGMENT
         Statement 1
         Statement 2
Code ENDS.
2. ASSUME:
• The ASSUME directive is used to tell the assembler the name of logical segment.it should be used for specific segment.
• For example:-
ASSUME cs :code_Here
This ststement tells the assembler that the instructions for the program are in a logical segment.named ‘code_here’.

• Ex:-
ASSUME DS:Data_Here.
This statement tells the assembler that the datas of program are in a logical segment named ‘Data_Here’.
3. DB (Define Bytes):
• The DB directives is used to declare a bytes type variable or to set aside one or more memory locations of byte type variable.
• It is used to assign names to variables in program.
• Ex:-
1)	Variable1  DB  25H.
2)	Temp        DB   5 Dup(?)
3)	Pressure    DB   10 Dup(0)
4)	Variable2  DB   36H,37H,57H,25H.
4. DD (Define Double word):
• The DD directive is used to declare a variable of types double word or to reserve memory location which can be accessed as types double word.
• Ex:-
1)	Variabla 1 DD 32671698 H
2)	Temp        DD  5 Dup(?)
3)	Pressure    DD  10Dup(0)
4)	Variable    DD   367261344 ,4216813H.
• The lower word 1698(ex:-1) will be put in memory at a lower address than the high word.
• A declaration of this type is often used with the LES and LDS instruction.
5. DW (Define Word):
• The DW directive is used to tell the assembler to define a variable of types word or to reserve storage locations of types word in the memory.
• Ex:-
1)	variable1    DW 3267 H
2)	Temp         DW 5 Dup (?)
3)	Pressure     DW 10 Dup(0)
4)	Variable2   DW 2367H, 1623H, 6813H.
6. DQ (Define Quad):
• This directive is used to tell the assembler to declare a variable of 4 words in the length or to reserve storage locations of 4 words in the memory.
• Ex:-
1)	Variable 1 DQ 3267  6231  3273 1234H.
2)	Temp        DQ  5 Dup(?)
3)	Pressure    DQ  10Dup(0)
4)	Variable    DQ   23676132131312BA H, 1234567890103123 H.
7. DT (Define Ten Bytes):
• The DT directives is used to tell the assembler to define a variable which is 10 bytes in length or to reserve 10 bytes of storage in the memory.
• Ex:-
1)	variable DT 3267623112342348H
2)	Temp    DT  5 Dup(?)
3)	Pressure DT 10Dup(0)
• This directive is often used when declaring data arrays for the 8087 math-coprocessor.
8. END:
• The end directive is put after the last statement of the program to tell the assembler that this is the end of program.
• The assembler will ignore all the statements after END directive.
9. EQU (Equate):
• EQU is used to give a name to a value or symbol.
• Each times the assembler findes the given name in the program,it will replace the name with the value or symbol you equated with that name.
• Suppose for example,we have write abc EQU 25H.
At the start our program and later in the program you write the instruction
ADD AL,abc.
• The advantage of that EQU directive is,if abc is used 20 times in a program and you want to change the value all you have to do is change the EQU ststement and reassemble the program.the assembler automatically put the new value at the place of previous name’abc’.
10. PROC (Procedure):
• The PROC directive is used to identify the start of the procedure.the PROC directive follows the name which you have given to the procedure.after the PROC directive the term NEAR or FAR is used to specify the types of procedure whether it is near called procedure or far called procedure.
• Ex:-
Factorial PROC NEAR.
It identifies the start of the procedure named and factorial tells the assembler that procedure is near.
11. ENDP (end procedure)
This directive is used along with the name of the procedure to indicate the end of the procedure.this directive together with the processor directive PROC is used to breaklet a procedure.
Ex:- square_root PROC NEAR<
                            Statement 1
                            Statement 2
                            .
                            .
                            .
                           Statement n.
      Square_root   END P.
	  
12. EVEN (align on even memory location):
• As the assembler assembles the section of data declaration or instruction ststement.it uses a location counter to keep track of how many bytes it is from the start of the segment at a particular time.
• The even directive teels the assembler to increment thelocation counter to the not even address if it is not already at an even.
• Ex:-
Variable DB Dup(?)
EVEN ; it aligns the counter on next even location.
13. EXTRN (external directive):
• The EXTERN directive is used to tell the assembles that the names or labels following the directives are in some other ALP module.
• Ex:-
EXTRN smart_addition FAR.
14. GLOBAL (declare the symbols globly or EXTRN):
• The GLOBAL directive can be used in place of EXTRN or in place of public directive.
• For a name or symbol define in the current assembly module,the global directive is use to make the symbol available to the modules.
• Ex:- GLOBAL Divisor.
• This statement makes the variable divisor public so that it can be accessed by other assembly modules.
• Ex:-GLOBAL data1:DW
• This statement tells the assembler that data1 is a variable of type word.
15. PUBLIC:
• The PUBLIC directive is used to tell the assembler that a specified name of lable will be accessed from other modules.
• Ex:-PUBLIC data1,data2.
• This statement is used to tell the assembler two variables are available for other assembly module.
16. GROUP:
• The GROUP directive is used to tell the assembler to group the logical segment.named after the directive into one logical group segment.
• This allows the contents of all the segment to be accessed from the same group segment base.
• Ex:-
GROUP Data,code,Extra,Stack.
• Ex:-
Group Data,code.
17. ORG (originate):
• As the assembler assembles the section of data declaration or instruction ststement,it used a location counter to keep track of how many bytes,it is from the start of the segment execute at the instant of time.
• The location counter is automatically set to 0000 when assembler starts the reading a segment.
• The ORG directive allows you to set the location counter to a desired value at any point in the program.
• Ex:-
ORG 0100
ORG 1000
Operators
The opration are those which helps the assembler to assign a particular constant with a lable or initialize a particular memory location or lable with constant.
In fact the operator perform the arithmetic and logical operations unlike directives that just directs assembler to correctly interpret to code it appropriately.
Following are the operators which are generally used.
1] (‘+’ and ‘-’):
• These operators are used to increment or decrement the displacement in the relative addressing mode.
• Ex:- MOV AX,[SI+2]
ADD BX,[BI-2]
2] PTR:
• The PTR operator is used to assign a specific types to variable or a lable .it is necessary to do this in any instruction where the types of oprand is not clear.
• Ex:-INS[Bx]
• In the above example it not clear to whether to increment byte pointed by Bx or to increment word pointed by Bx.
• We use the PTR operator to clarify how we wants the assembler to code the instruction.
• We should have to write the above example as:
INS Byte PTR [BX] or
INS word PTR[BX]
3] OFFSET:
• Offset operator assigns the offset or displacement of the name,data item(variable) or procedure from the start of the segment which contains it to the specified register.
• This operator is usually use to load the offset address of variable into the register.
• Ex:- MOV Bx, OFFSET variable.
It will determine theoffset of variable from the start of the segment in which variable is define.
• Ex:-
Price DB 25H
MOV BX, OFFSET price.
ADD L, [BX].
AL= AL + price
AL + 25H
4] LENGTH:
• LENGTH operator teels the assembler to determine the number number of elements in the particular data item such as string of an array.
• Ex:-
String DB ‘Electronics’
Price DB 25H, 26H, 32H, 35H.
MOV BL, LENGTH prices.
BL = 04H
MOV BL, LENGTH string.
BL = OBH
5] TYPE:
• TYPE operator tells the assembler to determine the type of a specified variable.the assembler actually determines the number of bytes in the type of variable.
• For byte type variable the assembles give value 1 and for double word it will give value1.
• Ex:- prices DW 1225H
Press DT……….H
MOV BL, TYPE prices = BL = 02H
MOV CL, TYPE press = CL = 10H.
6] SHORT:
• SHORT operator is used to tell the assembler that only one byte displacement is needed to code a JMP instruction. • If the JMP destination is after the JMP instruction in the program.the assembler will automatically reserved 2 bytes for the displacement.
• Using SHORT operator it saves 1 byte memory by telling the assembler that it needs for this particular JMP.
• Ex:-
JMP SHORT lable
Programming in assembly language:-
1. Write an ALP to add 2, 16-bit data stored in result in different register.
MOV AX, 5000H	; Initialization of data
 	MOV DS, AX        	 ; segment
 	MOV BX, 1000H
  	MOV CX, 3000H
  	ADD BX, CX
MOV DX, BX
MOV AH, 4CH      	 ; to terminate the
	INT 21H                 	 ; program
2. Write on ALP, to perform addition of 2, 16-bit number stored at memory location.
   DS:SI=5000:0500 and
   DS:SI=5000:0600 and stored the result into memory location
   DS:SI=5000:0700

  	MOV   AX, 5000H
  	MOV   DS, AX
  	MOV   SI, 0500H
 	MOV   BX, [SI]
  	MOV   SI, 0600H
 	MOV CX, [SI]
 	ADD   BX, CX
  	MOV DI, 0700H
  	MOV [DI], BX
  	MOV AH, 4CH
 	INT 21H. 
3. Write an ALP to perform addition of 2, 16-bit data stored at the memory location.
    DS:SI=4000:0720H
    DS:SI=5000:0800H
    And stored the result  into
    DS:DI=5000:0900H

 	MOV AX, 4000H
 	MOV DS, AX
 	MOV SI, 0720H
 	MOV BX, [SI]
 	 MOV AX, 5000H
 		 MOV DS, AX
 		 MOV SI, 0800H
 		 MOV CX, [SI]
 		 ADD BX, CX
 		 MOV [DI], BX
 		 MOV AH, 4CH
 		 INT  21H.
		 
4. Write an ALP to subtract 2, 16-bit numbers stored into the memory location.
DS:SI=85A0:072BH and stored the result into next memory location.
MOV AX, 85A0 H
MOV DS, AX
MOV SI, 072B H
MOV BX, [SI]
MOV CX, [SI+2]
SUB BX, CX
MOV [SI+4], BX
MOV AH, 4CH
INT 21H
5. Write an ALP to perform multiplication of 2, 8-bit data stored into the memory location
DS: SI=85A0:072BH and stored the result into memory location
MOV AX, 85A0H
MOV DS, AX
MOV SI, 072BH
MOV BX, [SI]
MOV AL, BL
MUL BH
MOV SI+2], AX
MOV AH, 4CH
INT 21H.

Result :-AX=0170 H
              85A0:072D=70
                        072E=01.
6. Write an ALP to perform division of 2, 16-bit number syored at the memory location.
   DS:SI=7000:09AB=2A2BH
   DS:SI=7000:02AB.12A2H
   And result stored into the memory location.
   DS:DI=6000:09AB

MOV AX, 7000H
MOV DS, AX
MOV DI, 09AB H
MOV AX, [SI]
MOV SI, 02AB H
MOV BX, [SI]
DIV BX
MOV CX, 6000H
MOV DS, CX
MOV [DI], AX
MOV AH, 4CH
INT 21H

Result: -  AH=04 E7H
               AL=0002H
			   
7. Write an ALP to perform multiplication of 2, 16-bit numbers stored in the memory location.
 DS:SI=8000:1260H
 DS:SI=8000:2016H
And  stored the result into the memory location.
 DS:DI =8000:0120 H

MOV AX, 8000H
MOV DS, AX
MOV SI, 1260H
MOV BX, [SI]
MOV SI, 2016H
MOV AL, [SI]
MUL   BX
MOV CX, 8000H
MOV DS, CX
MOV [DI], AX
MOV [DI+2], DX
MOV AH, 4CH
INT 21H
Programming in MASM
Assembly language program development tools
The following sections are giving the introduction of ALP development toolsd.
1. Editor:-
An editor is a program which allow you to create a file containing the assembly language statements (mnemonics).
For example,suitable editors are PC write,word star and the editor that comes with the assembler.
As we type in the program the editor stores the ASCII codes for letters and numbers in the RAM.the program typed in editor should save by following’.ASM’ extation.
Ex:- .ASM
2. Assembler:-
An assembler is a program which is used to translate the assembly language.mnemonics to the corresponding binary codes.
When we run the assembler,it reads the source file. On the first pass through the source file the assembler determines the displacement of named data item,the offset,labels,etc. and puts this information in a symbol table on the second pass through the source program.the assembler produces binary codes for each instruction and inserts the offsets, etc.
The assembler generates two files.
• The first file called the object file having extation ‘.OBJ’.the binary codes for the instruction and information about the addresses of the instruction.
• The second file called the assembler list file having extation ‘.LST’
This file contains assembly language statement .the binary codes for the each instruction and the offset address for each instruction.
3. Linker:-
A linker is a program used to join several object files into a large object file. These small object files are nothing but small modules.
The linker produces a link file which contains the binary codes for all the combine modules.the linker also produces a link map file which contains the address information about the linked file.the linkers which comes with TASM or MASM produce.assembler produce linked file with ‘.EXE’ extentation.
4. Debugger:-
A debugger is a program which allows you to load your objects code program into system memory,execute the program and trubleshoot or debug it.
The debugger allows you to look at the contents of the register and memory.
Location after each instruction so that you can check or alter memory or register.
General format for commands line to assemble a program.we write our program in editor which gives source file having ‘.ASM’ extention.
Assembling the program:- To assemble the source file in assembler. We use a command in DOS:
C:\>MASM\MASM enter
The assembler converts the symbolic source file into hexadecimal object file which has ‘.OBJ’ extation .the assembler gives two additional file list file (.LST) and cross-reference file (.CRF).
List file contains assembled version of the source file and its hexadecimal codes.
The cross-reference file contains the statical information like size of file in bytes ,number of labels ,list of labels,routine to be called ,etc about the source file.
After the cross- reference file name is entered the assembly process starts.if the program contains syntax error they are displayed using error code number and correspondind line number.
Once the syntax errors and warnings are taken care of by the programmer the assembly process is completed successfully.
Linking of object file: The linker linkes the different object modules of a source program and function routine to generate an itegrated executable code of the source program.the linker program is run on DOS like.
C:\MASM\LINK enter
After executing this command the linker makes two different files.
• Executable file(.EXE)
• Map file (.MAP)
The output of the link program is an executable file which can be further entered at the DOS prompt to execute the file.
C:\>MASM\ enter
Debugging the program: DEBUG.com is a DOS utility.that facilitates the debugging and troubleshooting of a assembly language program.
Following are the debug.
C:\>MASM\DEBUG enter
• -T
This command is used to track the program step-by-step.after executing this command the contains of all registers are displayed on the screen which is the effect of current instruction of cerrent pointer(IP).
• -R
This command is +used to display all register and flags.
Ex:- -R enter
Or
-R AX enter
AX=256A H
• -D
This command is used to display the contents of 128 memory location of RAM starting from the current display pointer.
Ex:- -D SEG:OFFSET enter
It will display the contents of memory location addressed by SEG:OFFSET.
• -E
This command is used to enter hexadecimal data at current display pointer.
Ex:--E SEG:OFFSET enter
Or
-E SEG:OFFSET enter
It will enter data at the memory locations addressed by SEG:OFFSET byte-by- Byte.
• -F
This command is used to fill the memory area starting from SEG:OFFSET1 to SEG:OFFSET2.
Ex:- -F SEG:OFFSET1 OFFSET2 byte1, byte2, byte3…………………
This command will fill the memory area from OFFSET1 to OFFSET2 with the sequence byte1, byte2, byte3,………….., byte N.
• -A
This command is used to assemble the instruction from the current CS:IP
Ex:- -A CS:IP enter
• -U
This command is used to unassembles the instruction from the current CS:IP Ex:- -U CS:IP enter.
1. Write an ALP to find out the overage value of given 10 8-bit number.
  ASSUME CS:CODE, DS:DATA
  DATA SEGMENT
  ORG 2000H
  DATA1 DB 15H,14H,07H,02H,36H,55H,76H,81H,25H.
  DATA ENDS.

CODE SEGMENT
                  START:
                               MOV AX,DATA
                               MOV DA,AX.
                               MOV CL,OA H.
                               MOV SI,OFFSET DATA1.
                               XOR AX,AX
                               XOR DX,DX .
              REPEAT:ADD AL,[SI]
                              JC NEXT
                              INC SI
                              JMP NEXT1.
                  NEXT: INC DL
                               INC SI
                 NEXT1:LOOP AGAIN
                              MOV BH,DL
                              MOV BL,AL
                              MOV AX,BX
                              DIV OAH.
                              MOV  AVG,AX
                              MOV AH,4CH
                              INC 21H
CODE ENDS.
          END START.

1. Write an ALP to perform following equation.
Z= LM + MN.
ASSUME CS:CODE, DS:DATA
DATA SEGMENTS
      V1 DB 10H 	; L = V1 
      V2 DB 20H 	; M =V2
      V3 DB 30H	; N =V3
RES DW 10 DUP(0) 	; Z = RES
DATA ENDS.
CODE SEGMENT
                  START:
                               MOV AX, DATA
                               MOV DS, AX
                               MOV AX, 0000H
                               MOV BX, 0000H
                               MOV CX, 0000H
                               MOV DX, 0000H
                               MOV AL, V1
                               MOV BL, V2
                               MUL BL
                               MOV CX, AX
                               MOV AL, V3
                               MUL BL
                               MOV DX, AL
                              ADD CX, DX
                              MOV RES, CX
                              MOV AH, 4CH
                              INT 21H
CODE ENDS
            END START.

Result :-
               AX=70H
			   
			   
Q. Difference between assembly language programming and machine language programming.
For accessing computer programs go to TECHNOLOGY