12.0 first program
It is when you create your first program. The guidance in this chapter will organize this:
12.1
If you have everything, you should have a Win32 (or Win32ASM) directory on your MASM. For each project, you should create a subdirectory.
Create a subdirectory named "firstprogram" in the Win32 directory. Create a new text file and rename "first.asm".
12.2 Step 2
Enter code in first.asm:
.486.Model flat, stdcall
Option CaseMAP: NONE
IncludeLib /masm32/lib/kernel32.libincludelib /masm32/lib/User32.lib
Include /masm32/include/kernel32.incinclude /masm32/include/user32.incinclude /masm32/include/windows.inc
Because now, we only need Keernel32 and USER32 two DLLs.
12.3 third step
We will create a famous "Hello World" program. To display the "Hello World" string, we want to use the message dialog box. The message dialog is created by the MessageBox function. You can find this function in the Win32 Programmer Reference (see Chapter 2). This is the book:
The MessageBox function is created, displayed and operating the message dialog. The message dialog contains messages and titles defined by the application, coupled with any predefined icon combination of buttons.
INT MessageBox (HWND HWND, // Handle of Owner Windowlpctstr LPText, // Address of Text In Message Boxlpctstr LPCAPTION, / / Address of Title of Message Box Uint Utype // Style of Message Box);
Parameters
HWnd
Identifies The Owner Window of The Message Box To Be Created. IF this parameter is Null, The Message Box Has No Owner WINDOW.
LPText
Points to a null-terminated string containing the message to be displayed.
LPCAPTION
Points to a null-terminated string used for the dialog box title. If this parameter is Null, The Default Title Error IS USED.
UTYPE
Specifies a set of bit flags tria determine the contents and behavior of the dialog box. This parameter can be becombination of flags from the folload groups of flags.
[- SnIP -]
There is a list of all constants and logos after this text (they are defined in Windows.inc). Because it is too long, I have not listed here. By viewing a reference, you know the MessageBox function to 4 parameters: parent window (OWNER), pointing to the message string, pointing to the type of the header string and the message box.
HWnd can be NULL. Because our programs are not available.
LPText must be a pointer to our text. This is only intended to be the Office of the Office of the memory address where the text is located.
LPCAPTION is the OFFSET of the title string. UTYPE is a combination of the equivalence of MB_OK, MB_OKCANCANCEL, MB_ICONERROR, explained.
Let us first define two strings for MessageBox:
Add: in First.asm:
.DATA
Msgtext DB "Hello World!", 0msgtitle DB "this is a messagebox", 0
.DATA indicates the beginning of the DATA section. With DB, bytes, it is directly inserted, and the string is only a collection of bytes, and the DATA portion is at the end of the string containing the above. MsgText is equipped with the first string offset. Msgtitle has a second string of Offset. Now we can use functions:
Invoke Messagebox, Null, Offset Msgtext, Offset Msgtitle, NULL
But because INVOKE is used, you can use (safer) AddR instead of Offset:
Invoke Messagebox, Null, Addr Msgtext, Addr Msgtitle, NULL
We haven't seen the last parameters yet, but this will not have any problems. Because MB_OK (with a style of a message dialog) is equal to 0 (NULL). But you can also use anything else. The definition of UTYPE (4th parameters) is:
Specifies a series of decision dialog box content and behavior. This parameter can be a combination of markers in the following flag group.
Now uses the simple message dialog box with the OK button with the "Information" icon. MB_OK is the style of the OK button, MB_ICONInInformation is the style of the Information icon. The style is combined with the "OR" operator. This is not OR pseudo code. MASM will process OR operations before assembly. Without OR, you can replace it with number (plus sign), but sometimes there is a problem with the stacked pattern (a style contains some other styles). But in this case you can also use the number.
.code
Start:
Invoke Messagebox, Null, Addr Msgtext, Addr Msgtitle, MB_OK MB_ICONITION
End Start
Add the above code to your first.asm file.
We also added a Start tag. If you are now compiling your program and run it, it will display a message dialog but it is very likely that it will collapse after you click OK. This is because the program has not ended, and the processor starts to perform anything behind the MessageBox code. The program in Windows is ended with the EXITPROCESS function:
Void EXITPROCESS
UINT UEXITCODE / / Exit code for all threads);
We can use 0 as an exit code.
Change your code to this:
.code
Start:
Invoke Messagebox, Null, Addr Msgtext, Addr Msgtitle, MB_OK MB_ICONITIONINFORMATIONINVOKE EXITPROCESS, NULL
End Start
12.4 Step 4
So our final procedure is:
.486.Model flat, stdcall
Option CaseMAP: NONE
IncludeLib /masm32/lib/kernel32.libincludelib /masm32/lib/User32.lib
Include /masm32/include/kernel32.incinclude /masm32/include/user32.include /masm32/include/windows.inc.datamsgtext DB "Hello World!", 0MSGTITLE DB "this is a messagebox", 0
.code
Start: Invoke Messagebox, Null, Addr Msgtext, Addr Msgtitle, MB_OK OR MB_ICONITIONINFORMATIONINVOKE EXITPROCESS, NULL
End Start
12.5 Step 5
Now we will generate executables from the source code.
Use the content to create a text file and name it.bat:
@echo offml / c / coff first.asmlink / subsystem: windows first.objpause> NUL
Explanation:
ML / C / Coff first.asm
ML is Macro Sugger (MASM). MASM will create the original code from the program. The parameter means: / c = Compilation is not linked (because we use link.exe to do this) / COFF = generates the Object file in the COFF format, which is the standard format of Windows executable files. First.asm = a Compilation First.asm file LINK / SUBSYSTEM: Windows First.Obj
The linker links the Object file and all imported DLLs and library: / subsystem: windows = Create a Windows executable. First.Obj = link first.obj
If you complete all things correctly, run the batch file. First.exe will be generated. Run it and see what results.