MASM is maintained by Microsoft and is an x86 assembler that consumes Windows and Intel syntax to produce a COFF executable. It is appropriate for both 16 bit and 32 bit sources. Fortunately, Microsoft’s Visual Studio IDE endorses MASM programming tasks just by making a few project property changes. The prime goal behind this text is to introduce the ability of assembly code by way of speed and full control over programs that are typically not seen in other programming languages. Even although there are quite a few editors and software available to do such a process in a standalone method, the aspirant system or security programmers who’re only limited to .NET software IDE to date can enter into the actual system programming world by using none apart from visible studio IDE.
In this text, we’d get an understanding about creating both EXE and DLL using MASM with Visual Studio. So, the newbies ought to to have a short knowledge of those technologies:
- Visual Studio 2010 or Later Version
- MASM (Microsoft Macro Assembler) SDK Library
- Basic Assembly Coding Competency
We shall exhibit assembly programming by making a easy Windows executable which generally shows “Hello World!” in a message box the second it’s initiated. It may be very tough to do such an implementation because Visual Studio 2010 IDE doesn’t offer any explicit templates for writing assembly code like C#, VC++ and VB.NET programming languages. It in reality has an in-built option to compile or run assembly programs.
Opening New Project
We shall have to create a VC++ project solution which later is accompanied with an assembly code file. Hence, open Visual Studio and choose an Empty Project of VC++ template type. There is not any want to create a sub-directory for this empty solution, so uncheck the corresponding check box as follows:
Once the test_masm of VC++ type solution is created, go to the answer explorer and right click to choose Build Customization option as follows:
The Build Customization options open up the MASM compiler options which uncheck by default. This is the key option which have to be enabled so as to edit and compile the native assembly code file.
As we’ve stated earlier, VS 2o1o doesn’t provide assembly file templates, nevertheless choose a project from the answer explorer and right click to add a text file which can be provided a *.ASM extension as follows:
Now, a blank text.asm file is added to our test_masm solution. Open it and paste the next assembly code, which is liable for displaying a message box, as follows:
.386 ; Tells MASM to use Intel 80386 instruction set.
.model flat,stdcall ; Flat memory model
option casemap:none ; Treat labels as case-sensitive
.data ; Begin initialized data segment
MsgBoxCaption db “Win32 Assembly Programming”,0
MsgBoxText db “Hello World!!!Welcome to ASM Programming under CLR”,0
.code ; Beginning of code
start: ; Entry point of the code
invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK
invoke ExitProcess, NULL
The assembly code file is written, however keep patience, this isn’t ready to compile or execute because a few of important project settings are nonetheless remaining.
Mandatory Project Configurations
Successful execution of an assembly code file with Visual Studio IDE is determined by an external library file, which can be available from MASM SDK. Hence, choose project Properties by right clicking it from the answer explorer. Here, choose General by increasing Linker and within the Additional Library Directories, insert the trail of include, lib and macros directories as follows:
Next, come to the Input section within the Linker and mention the reference of masm32.lib file as additional dependencies:
It just isn’t required to generate a manifest file for such manipulation, therefore disable it as follows:
Now, come to System from the Linker and set Windows within the subsystem section as follows:
Finally configure the code entry point because the start from the Advanced option within the Linker, which determines the code execution flow. We can identify the entry point of the ASM file from the .code section.
Now come to the Microsoft Macro Assembly section from the answer properties which seems the second after we add an assembly file in solution directory, otherwise it shall be hidden. Here, set the directory name where the MASM SDK was installed earlier as follows:
Finally, the whole lot is prepared and the answer is compiled. If the entire configuration is appropriate, then a test_masm.exe file is created within the Debug folder of the answer.
Testing and Debugging
It is time to test the executable. When the exe is clicked, a “Hello World!” message box would seem as follows:
We may even debug the assembly code by inserting a breaking point as a selected location, and thru the Register window within the Debug menu, we will observe all of the CPU registers with corresponding flags as follows:
We shall cover the advanced debugging of an application in later articles. The following image shows the assembly code in debug mode which helps us to perceive what is going on behind the scenes.
Although this section just isn’t relevant to this text, however only for knowledge point view, we will disassemble any C++ file to its corresponding ASM code. The Visual Studio IDE is inbuilt with a Disassembly option, which may be very helpful to detect a run time bug such as buffer overflow within the code via converting the source code file to an assembly code file as follows:
Developing DLL using MASM
In the previous section, we’ve seen how to create an EXE file using MASM with VS 2o10. We can even develop a library (DLL) by using MASM programming very similar to other technologies such as C#, VB, and C++. Therefore, the tactic will be utilized within the other client application in that created DLL. The procedure of generating a DLL is sort of the identical as EXE however requires some refined configuration. First of we’ve to set Configuration type as DLL within the General section because now we’re coping with DLL. Such modification can occur from solution properties as:
And as all of us know, DLL files are libraries which contain method definitions. Entry point is usually absent within the DLL file. Hence we’ve to change this setting as follows:
Finally, add a text file as masmlib with ASM extension within the solution like earlier and place the next code, which generally contains a testing method which can show some alert throughout the load and unload of DLL within the client program as follows:
include masm32includemasm32rt.inc .data dLoading BYTE "HELLO ,DLL is Loading…..", 0 dUnloading BYTE "DLL is un-loading.???????", 0 dOrdinal BYTE "Good-Bye", 0 .data? hInst DWORD ? .code testingMethod proc hInstDLL:DWORD, fdwReason:DWORD, lpvReserved:DWORD .if fdwReason == DLL_PROCESS_ATTACH invoke MessageBox, HWND_DESKTOP, offset dLoading, NULL, MB_OK push hInstDLL pop hInst mov eax, TRUE ret .elseif fdwReason == DLL_PROCESS_DETACH invoke MessageBox, HWND_DESKTOP, offset dUnloading, NULL, MB_OK .elseif fdwReason == DLL_THREAD_ATTACH .elseif fdwReason == DLL_THREAD_DETACH .endif ret testingMethod endp ProcByOrdinal proc invoke MessageBox, NULL, offset dOrdinal, NULL, NULL ret ProcByOrdinal endp end testingMethod
Finally, compile this program and test_masm_dll. The DLL file could be created within the Debug folder which may referenced within the C++ program or within the MASM client program itself.
So, we’ve seen how to create both EXE and DLL files using MASM programming languages employed with visible studio IDE. In fact, such a process could possibly be achieved by hard-core MASM SDK however .NET programmers typically concern assembly programming due to unusual syntax and platforms. Assembly language programming opens a brand new horizon of advance coding by way of faster code executing, exploit writing and shell-coding. Programmers are sometimes comfortable with Visual Studio due to having quite a few in-built features and functionality. Hence, this text is devoted to these professionals who’re planning to shift in direction of system programming with out leaving the .NET framework.