1 UNIT III – MASM86The Microsoft Macro Assembler (MASM) is an x86 assembler that uses the Intel syntax for MS-DOS and Microsoft Windows. The earlier versions were released in the year 1981 .TurboAssembler (TASM) is also an assembler package developed by Borland which runs on andproduces code for 16- or 32-bit x86 MS-DOS or Microsoft Windows. These softwares providethe assembly language tools to program the 8086 processor based systems.To program the x86 processors using in MASM , certain concepts are very important. They arereserved words, identifiers, predefined symbols, constants, expressions, operators, data types, registers,and statements.Reserved Words : A reserved word has a special meaning fixed by the language. This must beused under certain special conditions. These reserved words are Instructions, which correspond to operations the processor can execute. Directives, which give commands to the assembler. Attributes, which provide a value for a field, such as segment alignment. Operators, which are used in expressions.MASM reserved words are not case sensitive except for predefined symbols. The assemblergenerates an error if you use a reserved word as a variable.For Ex: The following operands are reserved words. Reserved words are not case sensitive. $ DWORD PASCAL SWORD ? FAR QWORD SYSCALL @B FAR16 REAL4 TBYTE @F FORTRAN REAL8 VARARG ADDR FWORD REAL10 WORD BASIC NEAR SBYTE ZERO? BYTE NEAR16 SDWORD C OVERFLOW? SIGN? CARRY? PARITY? STDCALL
2Predefined Symbols :Unlike most MASM reserved words, predefined symbols are casesensitive. @CatStr* @Environ* @Model* @code @fardata @SizeStr* @CodeSize @fardata? @stack* @Cpu @FileCur* @SubStr* @CurSeg @FileName @Time* @data @InStr* @Version @DataSize @Interface* @WordSize @Date* @Line* @Model*Registers : AH CS DX SI AL CX EAX SP AX DH EBP SS BH DI EBX ST BL DL ECX TR3* BP DR0 EDI TR4* BX DR1 EDX TR5* CH DR2 ES TR6 CL DR3 ESI TR7Identifiers : An identifier is a name that you invent and attach to a definition. Identifiers can besymbols representing variables, constants, procedure names, code labels, segment names, anduser-defined data types such as structures, unions, records, and types defined with TYPEDEF.Identifiers longer than 247 characters generate an error. The first character of the identifier can be an alphabetic character (A–Z) or any of these four characters : @ , _ , $ , ?
3 The other characters in the identifier can be any of the characters listed above or a decimal digit (0–9).Predefined Symbols : The assembler includes a number of predefined symbols (also calledpredefined equates). These symbols can be used at any point in the code to represent the equatevalue. For example, the predefined equate @ FileName represents the base name of the currentfile. If the current source file is TASK.ASM , the value of @File Name is TASK.Integer Constants and Constant Expressions : An integer constant is a series of one or morenumerals followed by an optional radix specifier. For example, in the following statements mov ax, 25 mov bx, 0B3hthe numbers 25 and 0B3h are integer constants.Operators : Operators are used in expressions. The assembler evaluates expressions that containmore than one operator according to the following rules. Operations in parentheses are performed before adjacent operations. Binary operations of highest precedence are performed first. Operations of equal precedence are performed from left to right. Unary operations of equal precedence are performed right to left.Examples of the operators are : + ,– (unary) , &, *, /, MOD, SHL, SHR , +, – (binary) , EQ,NE, LT, LE, GT, GE NOT , AND , OR, XOR , OPATTR, SHORT, .TYPEData Types : A “data type” describes a set of values.For example ,BYTE, SBYTE, WORD,SWORD, DWORD, SDWORD, FWORD, QWORD, and TBYTE.Registers : The 8086 family of processors have a set of 16-bit registers. They are AX,BX,CX,DX (General purpose registers) segment registers like ,SS.CS.DS.ES and pointerregisters like SP,BP and Index registers like DI and SI etc…Statements : Statements are the line-by-line components of source files. Each MASM statementspecifies an instruction or directive for the assembler. Statements have up to four fields,[[name:]] [[operation]] [[operands]] [[;comment]]
4Assembly Language Development Tools : To develop an assembly language program we need certain program development tools .The various development tools required for 8086 programming are explained below.1. Editor : An Editor is a program which allows us to create a file containing the assemblylanguage statements for the program. Examples of some editors are PC write, Word-star. As wetype the program the editor stores the ACSII codes for the letters and numbers in successiveRAM locations. If any typing mistake is done editor will alert us to correct it. If we leave out aprogram statement an editor will let you move everything down and insert a line. After typing allthe program we have to save the program for a hard disk. This we call it as source file. The nextstep is to process the source file with an assembler. While using TASM or MASM we shouldgive a file name and extension .ASM.Ex: Sample. asm2.Assembler : An Assembler is used to translate the assembly language mnemonics intomachine language( i.e binary codes). When you run the assembler it reads the source file of yourprogram from where you have saved it. The assembler generates two files . The first file is theObject file with the extension .OBJ. The object file consists of the binary codes for theinstructions and information about the addresses of the instructions. After further processing,the contents of the file will be loaded in to memory and run. The second file is the assembler listfile with the extension .LST.3. Linker : A linker is a program used to connect several object files into one large object file.While writing large programs it is better to divide the large program into smaller modules. Eachmodule can be individually written, tested and debugged. Then all the object modules are linkedtogether to form one, functioning program. These object modules can also be kept in library fileand linked into other programs as needed. A linker produces a link file which contains the binarycodes for all the combined modules. The linker also produces a link map file which contains theaddress information about the linked files. The linkers which comes with TASM or MASMassemblers produce link files with the .EXE extension.
54. Locator : A locator is a program used to assign the specific addresses of where the segmentsof object code are to be loaded into memory. A locator program called EXE2BIN comes with theIBM PC Disk Operating System (DOS). EXE2BIN converts a .EXE file to a .BIN file which hasphysical addresses.5. Debugger: A debugger is a program which allows to load your object code program intosystem memory, execute the program, and troubleshoot or debug it. The debugger allows tolook into the contents of registers and memory locations after the program runs. We can alsochange the contents of registers and memory locations and rerun the program. Some debuggersallows to stop the program after each instruction so that you can check or alter memory andregister contents. This is called single step debug. A debugger also allows to set a breakpoint atany point in the program. If we insert a break point , the debugger will run the program up to theinstruction where the breakpoint is put and then stop the execution.6. Emulator: An emulator is a mixture of hard ware and software. It is usually used to test anddebug the hardware and software of an external system such as the prototype of a microprocessorbased instrument.ASSEMBLER DIRECTIVES: Assembler directives are the directions to the assembler which indicate how an operandor section of the program is to be processed. These are also called pseudo operations which arenot executable by the microprocessor. The various directives are explained below.1. ASSUME : The ASSUME directive is used to inform the assembler the name of the logicalsegment it should use for a specified segment.Ex: ASSUME DS : DATA tells the assembler that for any program instruction which refers tothe data segment ,it should use the logical segment called DATA.2.DB : Define byte. It is used to declare a byte variable or set aside one or more storage locationsof type byte in memory.For example, CURRENT_VALUE DB 36H tells the assembler to reserve 1 byte of memory fora variable named CURRENT_ VALUE and to put the value 36 H in that memory location whenthe program is loaded into RAM .
63. DW : Define word. It tells the assembler to define a variable of type word or to reservestorage locations of type word in memory.4. DD(define double word) :This directive is used to declare a variable of type double word orrestore memory locations which can be accessed as type double word.5.DQ (define quadword) :This directive is used to tell the assembler to declare a variable 4words in length or to reserve 4 words of storage in memory .6.DT (define ten bytes):It is used to inform the assembler to define a variable which is 10 bytesin length or to reserve 10 bytes of storage in memory.7. EQU : Equate It is used to give a name to some value or symbol. Every time the assemblerfinds the given name in the program, it will replace the name with the value or symbol we haveequated with that name8.ORG : Originate : The ORG statement changes the starting offset address of the data.It allows to set the location counter to a desired value at any point in the program. For examplethe statement ORG 3000H tells the assembler to set the location counter to 3000H.9 .PROC- Procedures: It is used to identify the start of a procedure. Or subroutine.10. END- End program .This directive indicates the assembler that this is the end of the programmodule. The assembler ignores any statements after an END directive.11. ENDP- End procedure: It indicates the end of the procedure (subroutine) to the assembler.12.ENDS-End Segment: This directive is used with the name of the segment to indicate the endof that logical segment.Ex: CODE SEGMENT : Start of logical segment containing code CODE ENDS : End of the segment named CODE.
8 mov cl,10h loop1: mov al, [bx] mov dx,0d800h out dx,al call delay inc bx dec cl jnz loop1 jmp loop2delay proc near push cx push bx mov cx, 07fehback2 : mov bx,0ffffhback1: dec bx jnz back1 loop back2 pop bx pop cx retdelay endpProgram : Seven-segmentdata segment datas db 3fh,06h,5bh,4fh,66h,6dh,7dh,07h db 7fh,6fh,77h,7ch,39h,5eh,79h,71hdata endscode segment assume cs:code,ds:data loop2 : mov ax,data mov ds,ax mov dx,0d803h mov al,80h out dx,al lea bx,datas mov cl,10h loop1: mov al,[bx] mov dx,0d800h out dx,al call delay inc bx dec cl jnz loop1 jmp loop2 delay proc near push cx push bx mov cx,07feh
9back2: mov bx,0ffffh back1: dec bx jnz back1 loop back2 pop bx pop cx ret delay endpprogram:Factorialcode segment assume cs:code mov ax,200 mov ss,ax mov sp,2000 sub sp,0004h mov ax,6 push ax call facto pop ax nop nop nop facto proc near pushf push ax push dx push bp mov bp,sp mov ax,[bp+10] cmp ax,0001h jne go_on mov word ptr [bp+12],0001h mov word ptr [bp+14],0000h jmp exit go_on : sub sp,0004h dec ax push ax call facto mov bp,sp mov ax,[bp+2] mul word ptr [bp+16] mov [bp+18],ax mov [bp+20],dx add sp,0006h exit : pop bp pop dx
10 pop ax popf ret facto endp code ends endProgram :Message Displayassume cs:code,ds:datadata segmentmessage db 0dh,0ah,"mallika is the student of m.sc",0dh,0ah,"$" ;preparing string of the messagedata endscode segmentstart: mov ax,data ; initializes ds mov ds,ax mov ah,09 ; set function value for display mov dx, offset message int 21h ; point to message and run mov ah,4ch ; the interrupt int 21h ; return to doscode ends end startProgram : Read Numbes MacroMOV AH,01HINT 21HSUB AL,0MOV BH,0AHMUL BHMOV NUM,ALMOV AH,01HINT 21HSUB AL,0ADD NUM,ALENDMPRINTSTRING MACRO MSGMOV AH,09HMOV DX,OFFSET MSGINT 21HENDMDATA SEGMENTCR EQU 0DHLF EQU 0AHMSG DB CR,LF,ENTER NUMBER,$