The Environment and foundation of the WIN32 Assembly

Source: Internet
Author: User
Tags data structures include win32

1.32-bit Environment profile

Assemble the program under DOS, we can manage all the resources of the system, we can change all the memory in the system, such as their own changes in the memory control block to allocate memory, modify the interrupt vector table to intercept the interrupt, and so on other operations, such as our keyboard port directly to the keyboard can be blocked off, This can be described as a DOS system: The system has only one privilege level, in programming, any program and operating system are the same level, so in DOS, a poorly programmed program will affect all other programs, such as a program to shut down the keyboard, all programs can not get the data typed from the keyboard, Until any program turns the keyboard back on, a program falls into a dead loop, and no other program can terminate it. The programming idea under DOS is "single task", you just think that your program will follow your process step-by-step, regardless of the sequence of issues (of course, the program may be interrupted interrupted, but you can think that they will restore the environment, if the interrupt program does not restore the environment, it is their fault).

In the memory management mode, DOS Assembly and WIN32 Assembly also have a lot of different: dos work in real mode, we can address the 1M of memory, addressing through the segment register to make the initial address of the paragraph, each segment of the size of 64K, more than 1M part, you can only use him as a xms, that is to say, Can only be used for data storage and cannot execute programs in it.

and Windows executes in protected mode, where all the resources are "protected" by the application: The program has a level of execution, only the operating system is at the highest level-0, all applications are working in Level 3 (RING3),
In Ring3, you can't access IO ports directly, you can't access the memory that other programs run, it's illegal to write to the program's own code snippets, and a familiar blue screen pops up on the Windows screen. Only for RING0 programs, the system is fully open.

In terms of memory, Windows uses the processor paging mechanism, so that all the memory is "flat" for the application, you do not have to use a segment register to specify the address of the segment, because in the protected mode, the meaning of the segment register is different (see 80386 manual books), You can directly specify a 32-bit address for addressing 4GB of memory.

In the program structure, the Windows program is also very different, it is "message based", you can imagine such a common Windows window, there are several buttons, if you use the idea of DOS programming to consider, You'll find it difficult to implement it: dragging changes the window's size when the mouse moves to the edge of the window. When the mouse clicks on the button to do, you will find that your program from the start after the wait, you do not know where the mouse will be the first point, in fact, you are waiting for all possible things happen. And in DOS, you can only do it yourself first, need user input, and then stop, you do not input I will no longer execute, and, I let you enter data a you can not enter data B.
Well, back to the bottom, because the above is the basis of WIN32 programming, whether for WIN32 assembly or VC + +, they are the same, let's look at the contents of the Win32 assembly.
2.win32asm compiler

Win32asm compilers are most commonly used in two ways: The Borland Company's Tasm5.0 and Microsoft's Masm6.11 version, the two compilers each have their own advantages and disadvantages, tasm with a modest import library, and MASM did not bring, but MASM in the code optimization on top of the image than Tasm did well, but it did not take the import library. It seems difficult to choose which compiler to use, but Steve
Hutchesson gave us an answer, and he built a very complete import library for MASM, which basically included most of the API functions of Windows, and these libraries, include files and other tools and Masm6.14 versions made a Masm32 Compiler--masm32v5. In this way, we use assembler programming to be as convenient as C.

Because of the masm32v5, so personally, I recommend using MASM as Win32asm Compilation tool, but MASM and tasm have many different macro syntax, this tutorial is written in MASM format.
Environment settings for 3.masm32

In Win32 programming, because Windows has a number of data structures and definitions, these are placed in include files, and the import library is used when connecting (popularly speaking is the list of functions in the DLL file provided by Windows that tells the program where to call API functions) , all of which are placed in the Include
and Lib directories. We will specify the following system environment at compile time:
Set Include=\masm32v5\include
Set Lib=\masmv5\lib
Set Path=\masmv5\bin
The compiler then goes to the correct path to find the include file and Lib file. You can do it yourself in Autoexec.bat.
File with the above statement, in order to produce the Windows PE format of the executable file, in the compilation and connection to specify the appropriate parameters:
Compiling: ml/c/coff filename. asm
Connection: link/subsystem:windows obj filename. obj resource file name. res
In order not to have to play so many parameters at each compile time, we can use the NMAKE file to execute, NMAKE is the Code maintenance program, and he will check. asm. obj. exe. res
such as the time of the file, if you update the source program, he will automatically execute the compiler or connect the program to produce the corresponding file. You can makefile in the file name
file that specifies the compiler and linker to use and the corresponding parameters, 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 that the program name is clock, producing clock.exe files that require clock.obj and clock.res files, and produces clock.res
Files need clock.rc files, resulting in clock.obj files to use clock.asm files, as to whether the need to perform ML, link and
RC, the program will automatically judge the time according to the file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.