masm(microsoft macro assembler)

31
M & M

Upload: akash-shejule

Post on 22-Oct-2014

262 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: MASM(Microsoft Macro Assembler)

M & M

Page 2: MASM(Microsoft Macro Assembler)

MASM: The Microsoft Macro Assembler Without question, MASM is the world's most

popular assembly language translator. There are many reasons for its popularity, though the fact that Microsoft produced it has a lot to do with this. Of course, MASM has been around for over two decades and has had lots of time to develop a huge following. It doesn't hurt that MASM is one of the most powerful and stable assemblers around. It also doesn't hurt that there are lots of on-line and off-line resources available for MASM. The real question isn't "why is MASM so popular?" Rather, the question really is "why isn't everyone using MASM?" There are a couple of reasons:

Page 3: MASM(Microsoft Macro Assembler)

MASM(Microsoft Macro Assembler)

The Microsoft Macro Assembler (MASM) is an x86assembler for Microsoft Windows that uses the Intel syntax. The current versions of MASM exist in 2 flavors, the first one understands 16-bit and 32-bit assembly sources, the latter (ML64] is restricted to 64-bit sources.

Page 4: MASM(Microsoft Macro Assembler)

STEPS TO WORK ON MASM

C:masm.edit[filename.asm] Save file with filename asm C:\masm.masm filename.asm C:\masm.link filename+io C:\masm>filename

Page 5: MASM(Microsoft Macro Assembler)

Which Version of MASM are you Using?

Like most function files, the MASM.exe file is constantly updated. In production, typically some date is chosen as a production date, but changes continue.

COMMAND TO KNOW THE VERSION OF MASM-

C:> dir MASM*

Page 6: MASM(Microsoft Macro Assembler)

What to type on the Command Line

to assemble hello.asm

C:> Path = c:\masm613\binC:> Masm hello

This assumes that masm.exe is in the bin subdirectory.

Note that the extension of the file is not typed on the command line (it is assumed to be asm)

This function creates an object file (file containing machine language code – binary file) named hello.obj

Page 7: MASM(Microsoft Macro Assembler)

MASM.exe

In lab, you type:C:> masm hello,hello,helloThis is (source filename, object filename, listing

filename)

In order to run codeview, you type:C:> masm /zi hello, hello, hello

Page 8: MASM(Microsoft Macro Assembler)

What to type on the Command Line

to Link Hello.obj

Want to link Hello.obj with any external files used in the source code (none needed)

Want to create an executable fileC:> Path = c:\masm613\binrC:> Link hello

This creates an executable file. If you don’t put any of the extensions, it will ask you.

This assumes that Link.exe is located in the bin subdirectory.

Page 9: MASM(Microsoft Macro Assembler)

The OFFSET Operator If a data label is referenced in an expression,

MASM will generally assume that the content of the memory reference is to be targeted. To access the address and not the content of a data label, MASM supplies addditional operators - OFFSET and SEG - to tell the assembler the different intention:

mov eax, data_label ; load the content of data_label ( assumed DWORD here )

mov eax, OFFSET data_label; load the address ( the offset part of it )

Page 10: MASM(Microsoft Macro Assembler)

Square Bracket Operator [] The square bracket operator behaves

differently depending on the context. For direct addressing, the square brackets are similar to the + operator. As a result, square brackets around a data label have no effect:

mov eax, [eax] ; dereference the CONTENT of the variable and copy it into the EAX register

Page 11: MASM(Microsoft Macro Assembler)

HIGH LEVEL CONTROL LOOPS- MASM provides a notation to emulate a

variety of high level control and loop structures.It supports the .IF block structure,

.if – .elseif – .else – .endif

Page 12: MASM(Microsoft Macro Assembler)

ABOUT MASM EDITOR

There is a specific notation with the programmable menus so that programmers can run assemblers or compilers directly from Quick Editor.The notation works by getting the name of the file loaded in the editor and parsing the full path and file name in a range of formats that can be used with compilers, linkers, assembler and batch files.

Page 13: MASM(Microsoft Macro Assembler)

If you have the following file loaded in Quick Editor,

c:\masm\bin\myprog.asm

The following notation is expanded by Quick Editor into the various components of the files path and filename.

full path and name with extension{a} => c:\masm\bin\myprog.asm

full path and name with NO extension{b} => c:\masm\bin\myprog

Page 14: MASM(Microsoft Macro Assembler)

name with extension{f} => myprog.asm

name with NO extension{n} => myprog

full path with NO name{p} => c:\masm\bin\

Page 15: MASM(Microsoft Macro Assembler)

EXAMPLES-

to run a compiled EXE file, the menu entry would be like this,&Run EXE File,{b}.exeThis option is expanded by Quick Editor into,c:\masm\bin\myprog.exe

If you need to run a file in the same directory as the file loaded in the editor, the menu entry would look like this,

Run &Other File,{p}otherfile.exeThis is expanded by Quick Editor into,c:\masm\bin\otherfile.exe

Page 16: MASM(Microsoft Macro Assembler)

Some intructions in MASM

AAA ASCII Adjust After Addition AAD ASCII Adjust AX Before Division AAM ASCII Adjust AX After Multiply AAS ASCII Adjust AL After

Subtraction ADC Add With Carry ADD Arithmetic Addition AND Logical And

Page 19: MASM(Microsoft Macro Assembler)

SOME EXAMLES ON ARITHEMATIC OPERATIOPNS-

This notation applies to both named and numbered variables.

add = add to numeric variable     add #16 1

sub = sub from numeric variable   sub #24 12

Page 20: MASM(Microsoft Macro Assembler)

Example: Adding 3 Integers

TITLE Add and Subtract (AddSub.asm)

; This program adds and subtracts 32-bit integers.

INCLUDE Irvine32.inc .code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs ; display registers exit main ENDP END main

Page 21: MASM(Microsoft Macro Assembler)

DATA ASSIGNMENT

string = "literal"      $5 = "Quoted Text"string = string         $6 = $5number = immediate      #2 = 125number = number         #2 = #36

number1STRING Text$

number1 = 1Text$ = "Hi, I am text data !"

Page 22: MASM(Microsoft Macro Assembler)

BRANCHING

goto    branch to label

          lbl0:            ....            goto lbl1            ....            goto lbl0            ....          lbl1:

call    branch to label and store return IP

       

Page 23: MASM(Microsoft Macro Assembler)

EXAMPLE

        call initialise    ; call the label "initialise        ....               ; <---- return here        ....        ....        ....

      initialise:        ....        ret                ; return to the next instruction                           ; after the last "call"

Page 24: MASM(Microsoft Macro Assembler)

RUNTIME OPERATORS

run time operators -    equal               "==" or "="    not equal           "!=" or "<>"    greater than        ">"    less than           "<"    greater or equal    ">=" or "=>"    less or equal       "<=" or "=<“

equal               "==" or "=" not equal           "!=" or "<>"

Page 25: MASM(Microsoft Macro Assembler)

TERMINATION

Termination

 ’end’ terminate script (required to terminate script

Page 26: MASM(Microsoft Macro Assembler)

DIRECTORY COMMANDS

mkdir    create a directory    arg1 quoted text or a string variable

chdir    change directory    arg1 quoted text or a string variable

rmdir    delete an empty directory    arg1 quoted text or a string variable

Page 27: MASM(Microsoft Macro Assembler)

copyfile    copy a disk file    arg1 file to copy as quoted text or a string variable    arg2 new file name as quoted text or a string variable    existing file will be overwritten by new file name

movefile    move a disk file from one location to another    arg1 file to copy as quoted text or a string variable    arg2 new file name as quoted text or a string variable

deletefile    delete a disk file    arg1 quoted text or a string variable

Page 28: MASM(Microsoft Macro Assembler)

FILE I/O FUNCTIONS

fcreate    create a file and return its file handle    hFile = fcreate "myfile.ext"

fopen    open an existing file for read / write    and return its file handle    hFile = fopen "myfile.ext"

Page 29: MASM(Microsoft Macro Assembler)

fclose    close an open file handle.    #3 = fclose hFile fprint  fprintc    write string data to an open file. The fprintc variation    expands C style escapes into their match characters.

   

Page 30: MASM(Microsoft Macro Assembler)

fsize    return the size of an open file    flen = fsize hFile

fread    read a count of bytes from an open file and    return that data as a user defined string    $22  = fread #2 16    arg1 = open file handle    arg2 = byte count to read

Page 31: MASM(Microsoft Macro Assembler)

THANK YOU

SUBMITTED BY-