Title: Win32 Programming with Masm Author: Evil Homer Dated: March 7, 2004 So, you want to write win32 programs in MASM and are totally clueless? What's win32? Whats MASM? Gee, you really ARE clueless hehehe. Oh, you DO know? Well, let's pretend you don't - this document caters to the average village idiot. MASM is the Macro Assembler package which was written by Microsoft. Win32 programming is Microsoft Windows 32-bit programming - we want to write Windows programs. Where do we begin? Let's begin by looking at a VERY simple sourcecode and describing it in detail. All your Win32 programs will start with three lines similar to this: .486 ;<- opcode set = lowest cpu the program will run on .model flat,stdcall ;<- flat memory model, with standard calling convention option casemap:none ;<- compiler is case-sensitive to sourcecode (capitals matter) This isn't really part of your program, it's just telling the compiler how it should behave. Now let's continue ... We use the keywords INCLUDE and INCLUDELIB to add any "api includes" we want. Win32 programming often uses the "Win32 API" quite heavily. The API is a bunch of functions that you can call, and is well-described in the "Win32 api helpfile". Seek this HelpFile, it's your friend, and contains DETAILED information on almost all api functions. include \masm32\include\windows.inc ;<- this contains a bunch of Windows equates and structs include \masm32\include\user32.inc ;<- now we add includes and libs which support functions we want to call include \masm32\include\kernel32.inc ;<- most of your Win32 apps will contain at least these two "api includes" includelib \masm32\lib\user32.lib ;<- for every "api include" there should be a matching "LIB" includelib \masm32\lib\kernel32.lib Now we have our includes, we can now define some FILE SEGMENTS. We will always have a CODE segment, and we may also have a DATA segment. Another type of data segment called DATA? is available, and we'll describe it later. Whats a file segment? Its just how a win32 program (should be) organized: all the code lives in the CODE segment, all the data in the DATA segment. We can switch from one segment to another any time in our source using .CODE and .DATA statements, but its good practise to keep your source neat and orderly by putting all the data in one place etc. Even if you don't, if you use switches everywhere, defining data all over the place, the compiler will collect them all up and put them together in the right segments in the executable file you are making. .DATA szMyFirstString db "Hello World!",0 szMyTitle db "Win32 Demo",0 Here we have defined a string. Note the 0 on the end, thats a "zero terminator", the Win32 api functions generally expect strings to be zero terminated. Note also the naming convention, szSomething means "stringzeroSomething" - totally up to you whether to adopt such naming conventions or not, but its a good idea to use sensible naming conventions. .CODE start: invoke MessageBox,0,addr szMyFirstString,addr szMyTitle,MB_OK invoke ExitProcess,0 end start That is a complete and working (but very simple) win32 program. If you collect all the program statements so far and save them to a file called "test.asm" or similar, you can BUILD this program source into an exe file right now. Note the very last statement, its important. It contains the name of a LABEL, and the compiler will set the "Program EntryPoint" at that label. That means when we run the program, execution begins at "start". Since a win32 program is a Process, we should always call ExitProcess as the last active line of code. Have we learned anything at all yet? Now I'm going to get a little funky and begin talking about a "real" win32 program. We have not really got a Windows program yet, because all we did is make a MessageBox. Our Win32 application will have a Main Window, often called the Application Window. It may or may not contain various CONTROLS inside it, like Buttons and ListBoxes etc. A control is in fact a subwindow, we'll get to that. If you are new to this, and my tutorial is making sense so far, then read on. If you are struggling already, please review what you have read so far. Read it again, and again, and again, until it makes sense. If you are TOTALLY in the dark, now might be a good time to ask specific questions. You can do so by visiting board.win32asmcommunity.net and joining our forums there. Please do read the Rules before posting any questions.