Write code with NASM
This article is mainly compiled, and it is used to using friends who write code with VC.
Why use NASM?
It is not conducive to the transformation of the code. Plus the VC's support speed for the new instruction set (3D now!, XMM, etc.) is not enough, it is very inconvenient, so we tend to use external compilation. If you Familiar with MASM or TASM, you don't have to replace it, otherwise the cloud is recommended NASM. NASM and its documents can be downloaded in http://www.web-sites.co.uk/nasm. Best to read the documentation before use. It will only emphasize some key points, but add some omissions, not the full repertoire. NASM supports a variety of latest instruction sets, with considerable macro language support. You will get a more flexible use space than the VC's Inline ASM.
Choose a good programming environment
The editing environment of the NASM is fully feasible, just add Nasm.exe to the Tools-> Customize-> Tools. This is not described in detail here. Yunfeng recommends the compact editor. You You can specify the color of various keywords. The grammar files you made by Yunfeng are here, and it is still not complete. Many NASM's macro statement has not been added, and some of them have been made. So readers will be modified as appropriate. The method of adding EditPlus is to add a syntax file in Tools-> Preferences-> Files-> Syntax. EditPlus supports Output window information capture, add Nasm.exe in Tools-> Preferences-> Tools-> User Tools, and select Capture Output is ok. Of course, if you want to make a separate Win32 / PE file, not compiling into OBJ for VC Link, then you need a link program. It is best to use the Makefile.
How to compile OBJ for VC connection
NASM supports a variety of output formats, although the VC is known as the COFF format used, but actually distins. In addition to the output Coff format, NASM also supports the target file format of the VC, called Win32. I am in the User Tools of Editplus Set up
-i i: / nasm / include / -f win32-$ (filenamenoext) .Obj $ (filename)
Note: The -i here is the path to the header file to add the compiled OBJ. In the project of the VC. In general, the target file has three segments, respectively, respectively, respectively.
The TEXT segment places the code, which is read-only and can run segment DATA segment to place static data. This data is readable. This segment is readable, but it cannot be run. BSS segment placed dynamic data, these data is not put Enter the EXE file, the space is assigned after the EXE file is loaded into the memory.
A simple program framework is like this:
[BITS 32]
[section .text]
Global _func
_func:
Write the code of the FUNC function here
[section .data]
[section.bss]
Here, using Global declares a function of the function that can be called by C (C function has a downline prefix) _func: is the entry of this function. I want to use the func in the corresponding C code, but also use the extern declaration of FUNC. External function. If you use it in C , you will need to explain the function as a C call. The method is to use extern "c" {}
Parameter incoming and processing
The parameter processing of the function is related to its call mode. The default call mode of C is _cdel call mode, and the C non-static member function uses _thiscall, the method of processing stack of various call mode, can check in MSDN The arrival. Write Inline ASM may rarely involve these, but it must be clear here.
WIN32 program and other Win32 / PE EXE, I recommend using Borland's OBJ format, because the API in the DLL is more convenient. But you need to pay attention to some points:
NASM default segment is use6, and USE32 must be used in Win32, and the type of segment must be specified. So you must write such a line on the source file header:
[section .text class = code use32]
[section .data class = data use32]
[section .BSS class = bss use32]
% Define __sect__
Borland's OBJ file can specify the starting tag (..Start), which can generally be written:
[section .text]
..Start
Winmain:
Your code will start running from Winmain. Unlike C, your primary code does not have any parameters, so you must get the necessary parameters yourself. For example::
Push dword 0
Call getModuleHandle; get hinstance
Get the Instance handle (to EAX). Exit programs cannot be used directly, because your program is a process, not a function, so you must call:
Push DWORD 0; exit code
Call EXITPROCESS
The above GetModuleHandle is the API: getModuleHandlea in kernel32.dll, so you need to declare the export in the assembly source code: Import getModuleHandle Kernel32.dll getModuleHandlea
Extern getModuleHandle
Also exitprocess is also the case:
Import exitprocess kernel32.dll
Extern EXITPROCESS
Please refer to NASM documentation for specific usage.
Good at using NASM powerful macro
This is a section that should be very rich, but the time is limited, only a simple example, defines the following macro:
% Imacro Callapi 1- *
% Define %% API% 1
% rotate -1
% REP% 0-1
% rotate -1
Push DWORD% 2
% endRep
Call %% API
% Endmacro
Then, if you need to call MessageBox, you can write Callapi MessageBox, [HWnd], LPText, LPCAP, 0
HWnd is a global variable saved window handle, of course, 0 lptext is the character string pointer LPCAP is the title string of MessageBox
In addition, I have made many macros such as handling function parameters, protect stacks, etc. This is not one. I hope that I will lead to NASM