Upload
narasimha-murthy-yayavaram
View
35
Download
3
Embed Size (px)
DESCRIPTION
Brief Introduction to Microprocessor development tools MASM. Intended for Physics and Electronics students of SVU and RU .
Citation preview
1
UNIT III – MASM86
The 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 .Turbo
Assembler (TASM) is also an assembler package developed by Borland which runs on and
produces code for 16- or 32-bit x86 MS-DOS or Microsoft Windows. These softwares provide
the assembly language tools to program the 8086 processor based systems.
To program the x86 processors using in MASM , certain concepts are very important. They are
reserved 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 be
used 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 assembler
generates 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
Predefined Symbols :Unlike most MASM reserved words, predefined symbols are case
sensitive.
@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 TR7
Identifiers : An identifier is a name that you invent and attach to a definition. Identifiers can be
symbols representing variables, constants, procedure names, code labels, segment names, and
user-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 : @ , _ , $ , ?
The other characters in the identifier can be any of the characters listed above or a decimal digit (0–9).
3
Predefined Symbols : The assembler includes a number of predefined symbols (also called
predefined equates). These symbols can be used at any point in the code to represent the equate
value. For example, the predefined equate @ FileName represents the base name of the current
file. 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 more
numerals followed by an optional radix specifier. For example, in the following statements
mov ax, 25 mov bx, 0B3h
the numbers 25 and 0B3h are integer constants.
Operators : Operators are used in expressions. The assembler evaluates expressions that contain
more 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, .TYPE
Data 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 pointer
registers like SP,BP and Index registers like DI and SI etc…
Statements : Statements are the line-by-line components of source files. Each MASM statement
specifies an instruction or directive for the assembler. Statements have up to four fields,
[[name:]] [[operation]] [[operands]] [[;comment]]
Assembly Language Development Tools :
4
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 assembly
language statements for the program. Examples of some editors are PC write, Word-star. As we
type the program the editor stores the ACSII codes for the letters and numbers in successive
RAM locations. If any typing mistake is done editor will alert us to correct it. If we leave out a
program statement an editor will let you move everything down and insert a line. After typing all
the program we have to save the program for a hard disk. This we call it as source file. The next
step is to process the source file with an assembler. While using TASM or MASM we should
give a file name and extension .ASM.
Ex: Sample. asm
2.Assembler : An Assembler is used to translate the assembly language mnemonics into
machine language( i.e binary codes). When you run the assembler it reads the source file of your
program from where you have saved it. The assembler generates two files . The first file is the
Object file with the extension .OBJ. The object file consists of the binary codes for the
instructions 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 list
file 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. Each
module can be individually written, tested and debugged. Then all the object modules are linked
together to form one, functioning program. These object modules can also be kept in library file
and linked into other programs as needed. A linker produces a link file which contains the binary
codes for all the combined modules. The linker also produces a link map file which contains the
address information about the linked files. The linkers which comes with TASM or MASM
assemblers produce link files with the .EXE extension.
4. Locator : A locator is a program used to assign the specific addresses of where the segments
of object code are to be loaded into memory. A locator program called EXE2BIN comes with the
5
IBM PC Disk Operating System (DOS). EXE2BIN converts a .EXE file to a .BIN file which has
physical addresses.
5. Debugger: A debugger is a program which allows to load your object code program into
system memory, execute the program, and troubleshoot or debug it. The debugger allows to
look into the contents of registers and memory locations after the program runs. We can also
change the contents of registers and memory locations and rerun the program. Some debuggers
allows to stop the program after each instruction so that you can check or alter memory and
register contents. This is called single step debug. A debugger also allows to set a breakpoint at
any point in the program. If we insert a break point , the debugger will run the program up to the
instruction 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 and debug the hardware and software of an external system such as the prototype of a microprocessor based instrument.
ASSEMBLER DIRECTIVES:
Assembler directives are the directions to the assembler which indicate how an operand
or section of the program is to be processed. These are also called pseudo operations which are
not 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 logical
segment it should use for a specified segment.
Ex: ASSUME DS : DATA tells the assembler that for any program instruction which refers to
the 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 locations
of type byte in memory.
For example, CURRENT_VALUE DB 36H tells the assembler to reserve 1 byte of memory for a variable named CURRENT_ VALUE and to put the value 36 H in that memory location when the program is loaded into RAM .
3. DW : Define word. It tells the assembler to define a variable of type word or to reserve storage locations of type word in memory.
6
4. DD(define double word) :This directive is used to declare a variable of type double word or restore 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 4 words 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 bytes in 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 assembler finds the given name in the program, it will replace the name with the value or symbol we have equated with that name
8.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 example the 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 program module. 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 end of that logical segment.
Ex: CODE SEGMENT : Start of logical segment containing code
CODE ENDS : End of the segment named CODE.
SIMPLE EXAMPLES :
7
Program: Multiplication
code segment assume cs : codestart: mov ax,10h mov bx,2h mul bx mov cx,ax int 21h code ends end.
Program : Display Data
data segment datas db 'I am an Indian $' data ends code segment assume cs:code,ds:data start : mov ax,data mov ds,ax lea dx,datas mov ah,09h int 21h code ends end start
Program : Addition
code segment assume cs: code start : mov ax,3h mov bx,3h add ax,bx mov ah,4ch code ends end
Program : Seven segment
data 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 mov cl,10h loop1: mov al, [bx]
8
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 back2 : mov bx,0ffffh back1: dec bx jnz back1 loop back2 pop bx pop cx retdelay endp
Program : 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 back2: mov bx,0ffffh back1: dec bx
9
jnz back1 loop back2 pop bx pop cx ret delay endp
program:Factorial
code 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 pop ax popf
10
ret facto endp code ends end Program :Message Display
assume cs:code,ds:data data segment message db 0dh,0ah,"mallika is the student of m.sc",0dh,0ah,"$" ;preparing string of the message data ends code segment start: 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 dos code ends end start
Program : Read Numbes MacroMOV AH,01HINT 21HSUB AL,'0'MOV BH,0AHMUL BHMOV NUM,ALMOV AH,01HINT 21HSUB AL,'0'ADD NUM,ALENDM
PRINTSTRING MACRO MSGMOV AH,09HMOV DX,OFFSET MSGINT 21HENDM
DATA SEGMENTCR EQU 0DHLF EQU 0AHMSG DB CR,LF,'ENTER NUMBER','$'MSG1 DB CR,LF,'RESULT: ','$'NUM1 DB ?
11
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 21H
HEX2ASC PROC NEARPUSH AXPUSH BXPUSH CXPUSH DXPUSH SIMOV CX,00HMOV BX,0AHRPT1:MOV DX,00DIV BXADD DL,'0'PUSH DXINC CXCMP AX,0AHJGE RPT1ADD AL,'0'MOV [SI],ALRPT2:POP AXINC SIMOV [SI],ALLOOP RPT2INC SI
12
MOV AL,'$'MOV [SI],ALPOP SIPOP DXPOP CXPOP BXPOP AXRETHEX2ASC ENDPCODE ENDSEND START
Program: 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 START
RESULT: 11H 22H 33H 22H 33H 44H 33H 55 H 77H
Program: Multi byte Multiplication
DATA SEGMENTN1 DB 03H, 05 H, 07 H
13
N2 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 START
RESULT: 03H 05H 07H 05H 03H 02H 0FH 0FH 0EH
References : 1. Programmer’s Guide, Microsoft MASM, Micro soft Corporation.2.Microprocessor and Interfacing –D.V.Hall,TMH