Your SlideShare is downloading. ×
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
MASM -UNIT-III
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MASM -UNIT-III

5,531

Published on

This class notes is meant for M.Sc Physics of RU and other computer science students

This class notes is meant for M.Sc Physics of RU and other computer science students

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,531
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
266
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 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
  • 2. 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. 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]]
  • 4. 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.
  • 5. 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 .
  • 6. 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.
  • 7. 7SIMPLE EXAMPLES :Program: Multiplicationcode segment assume cs : codestart: mov ax,10h mov bx,2h mul bx mov cx,ax int 21h code ends end.Program : Display Datadata segment datas db I am an Indian $data endscode segment assume cs:code,ds:data start : mov ax,data mov ds,ax lea dx,datas mov ah,09h int 21hcode endsend startProgram : Additioncode segment assume cs: code start : mov ax,3h mov bx,3h add ax,bx mov ah,4ch code ends endProgram : Seven segmentdata endscode segment assume cs: code,ds:dataloop2: mov ax,data mov ds,ax mov dx,0d803h mov al,80h out dx,al lea bx,datas
  • 8. 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
  • 9. 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. 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,$
  • 11. 11MSG1 DB CR,LF,RESULT: ,$NUM1 DB ?NUM2 DB ?RES DB ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS,AXPRINTSTRING MSGREADNUM NUM1PRINTSTRING MSGREADNUM NUM2MOV AH,00HMOV AL,NUM1ADD AL,NUM2MOV RES,ALMOV SI,OFFSET RESCALL HEX2ASCPRINTSTRING MSG1PRINTSTRING RESMOV AH,4CHMOV AL,00HINT 21HHEX2ASC PROC NEARPUSH AXPUSH BXPUSH CXPUSH DXPUSH SIMOV CX,00HMOV BX,0AHRPT1:MOV DX,00DIV BXADD DL,0PUSH DXINC CXCMP AX,0AHJGE RPT1ADD AL,0MOV [SI],ALRPT2:POP AXINC SIMOV [SI],AL
  • 12. 12LOOP RPT2INC SIMOV AL,$MOV [SI],ALPOP SIPOP DXPOP CXPOP BXPOP AXRETHEX2ASC ENDPCODE ENDSEND STARTProgram: Multibyte additionDATA SEGMENTN1 DB 11H, 22H, 33HN2 DB 22 H, 33H, 44HRESULT DB 3H DUP (00)DATA ENDSCODE SEGMENTASSUME CS: CODE, DS: DATASTART: MOV AX, DATAMOV DS, AXMOV SI, OFFSET N1MOV DI, OFFSET N2MOV BX, OFFSET RESULTCLCMOV CX, 0003HMOV AX, 0000HBACK: MOV AL, [SI]MOV DL, [DI]ADC AL, DLMOV [BX], ALINC SIINC DIINC BXDEC CXJNZ BACKMOV AH, 4CHINT 21HINT 3HCODE ENDSEND STARTRESULT: 11H 22H 33H 22H 33H 44H 33H 55 H 77HProgram: Multi byte Multiplication
  • 13. 13DATA SEGMENTN1 DB 03H, 05 H, 07 HN2 DB 05 H, 03H, 02HRESULT DB 4H DUP (00)DATA ENDSCODE SEGMENTASSUME CS: CODE, DS: DATASTART: MOV AX, DATAMOV DS, AXMOV SI, OFFSET N1MOV DI, OFFSET N2MOV BX, OFFSET RESULTMOV CL, 03HMOV AX, 0000HMOV DX, 0000HBACK: MOV AL, [SI]MOV CH, [DI]MUL DHMOV [BX], ALINC SIINC DIINC BXLOOP BACKMOV AH, 4CHINT 21HINT 3HCODE ENDSEND STARTRESULT: 03H 05H 07H 05H 03H 02H 0FH 0FH 0EHReferences :1. Programmer’s Guide, Microsoft MASM, Micro soft Corporation.2.Microprocessor and Interfacing –D.V.Hall,TMH

×