Win32 compilation environment and foundation

zhaozj2021-02-08  252

(Luo Yunbin)

1.32 Environment Profile

In the DOS, we can manage all resources of the system, we can change all the memory in the system, such as the memory control block to allocate memory, modify the interrupt vector table to intercept the interrupt, etc., the other operations, If we directly operate the keyboard port directly, you can describe the DOS system: The system has only one privilege level. In programming, any program and operating system are in the same level, so under DOS, one editor The bad program will affect all other programs. If a program turns off the keyboard port, all programs cannot be typed from the keyboard until any program reopen the keyboard, one program falls into a dead cycle. No other programs can terminate it. The programming idea under DOS is "single task", as you think your program will follow your process step by step, you don't have to consider the problem (of course the program may be interrupted, but you can think they will put Environment recovery, if the interrupt program does not recover the environment, it is their fault).

In memory management, DOS assembly and Win32 assembly have many different: DOS work In real mode, we can address 1M memory, addressing the initial address of the segment when the segment register is addressed, each segment size is 64K, more than 1M part, only use him as XMS, that is, it can only be used as data storage and cannot execute it.

And Windows executes in protection mode, all resources are "protected" for applications: Programs have levels in execution, only operating system works in the highest level --0, all applications All working in Level 3 (Ring3), in Ring3, you can't access the IO port directly, you can't access the memory running in other programs, and even write data to the program own code segment is illegal, will be on the Windows screen Extended a familiar blue screen. Only a program for Ring0 is fully open.

In terms of memory, Windows uses the processor's paging mechanism so that all memory is "flat", you don't have to use a segment register to specify the address of the segment, because in the protection mode, segment register The meaning is different (see books in 80386 manual), you can specify a 32-bit address to address 4GB of memory.

In terms of program structure, the Windows program is also very different. It is "message", you can imagine such a common windows window, there are several buttons above, if you use DOS programming ideas, you will find It is difficult to implement it: When the mouse moves to the edge of the window, dragging the window size. When you click the button, you will do something to do, you will find that your program is waiting since the start of execution, you don't know the mouse first What is the place, in fact, you are waiting for all possible things. And under DOS, you can just implement it first, when you need to enter, stop, you don't enter, don't execute, and I let you enter data A You can't enter data B.

Ok, the words come true, because the above is the foundation of Win32 programming, whether it is also the same for Win32 compilation or VC , they are all the same, let's take a look at the content about Win32 compilation.

2.Win32ASM compiler

There are two most commonly used by Win32ASM: Borland's Tasm5.0 and Microsoft's Masm6.11 or higher, both compilers have their own advantages and disadvantages, TASM has a unclear IMPORT library, and Masm has no belt, but MASM is optimized above the code, but it is better than TASM, but it does not take the Import library. It seems that it is more difficult to choose, but Steve Hutchesson gave us an answer. He established a full Import library for MASM, basically involving most of the API functions of Windows, these libraries, The include files and other tools have Masm6.14 versions together into a Masm32 compiler - Masm32v5. In this way, we use the assembly programming as easy as C. Because of Masm32V5, I personally, I recommend using MASM as a compilation tool for Win32ASM, but MASM and TASM's macro syntax have many different, my tutorial is written in MASM format.

3. Masm32 environment setting

In Win32 programming, because Windows has a lot of data structures and definitions, these are placed in the include file, and it is also used to use the import library (the popular talk is the list of functions in the DLL file provided by Windows, that is, tell Where is the program to call an API function), these are placed in the include and lib directory. We should specify the following system environment when compiling:

Set include = / masm32v5 / incrude

Set lib = / masmv5 / lib

SET PATH = / MASMV5 / BIN

This way the compiler will go to the correct path to find the include file and lib file. You can add the above statement in the autoexec.bat file, in order to generate the execution file in the Windows PE format, you want to specify the corresponding parameters in compilation and connections:

Compilation: ml / c / coff file name .asm

Connection: LINK / SUBSYSTEM: Windows Obj file name .Obj resource file name .res

In order not to hit so many parameters every time you compile, we can use the nmake file to be executed for execution. NMAKE is the code maintenance program, he will check. Sasm .obj .exe .res and other files, if you update the source Procedure, he will automatically perform a compiler or a connection program to generate a corresponding file. You can specify the compiler and connector and the corresponding parameters in the file name Makefile, and the following is an example of a Makefile file:

Name = clock

Objs = $ (name) .Obj

RES = $ (name) .res

$ (Name) .EXE: $ (OBJS) $ (RES)

LINK / Debug / Subsystem: Windows $ (OBJS) $ (RES)

$ (RES): $ (Name) .rc

RC $ (Name) .rc

.asm.obj:

ML / C / COFF $ (Name) .asm

The file tells the NMAKE program, the program is named clock, generating the clock.exe file requires a clock.obj and clock.res file, and the clock.rs file is required to use the clock.obj file to use the clock.asm file, As for whether ML, LINK, and RCs need to be executed, the program automatically determines according to the time of the file.

转载请注明原文地址:https://www.9cbs.com/read-1426.html

New Post(0)