Emerging Era, Win32 Assembly, MASM, Hello World!


Today, there are many high-level programming languages (known as fourth generation, the Object Oriented Programming) available so what fuzz is about this Win32 Assembly programming? As we know, the obvious advantage of using assembly is its speed of execution, the full control of the system, and the small size (reduce overhead of loading) of the file produced. So Assembly is often used as part of a project (such as inline assembler) where speed is critically important or used to write drivers for hardware (e.g. VxD).  It is rare that any entire project is written purely based on assembly, especially for large projects.

Win32 Assembly allows you to write programs for Windows using assembly language. The famous assembler is MASM32 and it provides some high-level macros such as if, while so the use of various jump statements can be avoided, which speeds up the coding process.

The following will introduce the simplest example of Hello World in Win32 Assembly. It compiles on MASM32 and produces a Win32 EXE (PE) of just less than 3 KB!

.386
.model flat, stdcall
option casemap: none

include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib

.data
	szCaption	db	'Hello', 0
	szText		db	'Hello, World!', 0

.code
	start:
			invoke MessageBox, NULL, offset szText, offset szCaption, MB_OK
			invoke ExitProcess, NULL		
	end start

The first line specifies which instructions to use, .386 specifies it is x86, where there are some other options, such as .486, .586 for different processors and different instruction sets. If you want to use MMX instructions, you need to add .MMX after .586 for instances. Normally, all code runs at level 3 which is a normal mode, but if you want your program takes some system low level control, you will need a ‘p’ (for instance, .386p) to specify it runs at privileged level (which is level 0).

The second line .model flat specifies the type of program (for instance, tiny produces a .COM assembly and small produces a DOS EXE where data and program should be no more than 64KB).  stdcall specifies the function calling conventions, as we are using Win32 APIs, which are using stdcall conventions.

The third line option casemap: none tells the assembler that the program is case sensitive because Win32 APIs are case sensitive and will cause trouble if we don’t do so.

Then followed by some include that includes the source code with recommended file extensions .inc and includelib will help the linker to link to its corresponding lib file if there is any. windows.inc has some constants and macros defined. The user32.inc contains the header API interface and user32.lib is the implementation (binary execution) for inclusion in the final produced EXE file.

.data tells assembler to start declaration of some data, this is similar to what we did at DOS times. However, ending a message is no longer a dollar sign $ which you need to feed to DOS 9th function of interrupt 21h.

.code starts the actual code implementation but this does not mean that the code entry here, that is why the next label start: and its corresponding end start tells that the code should start at this entry label.

The next line invoke calls the Win32 MessageBox API and gives parameters. This is a recommended method for calling sub-procedures because you don’t need to manually push parameters one by one (the sequence matters because of the calling convention) The assembler will decide how to push these parameters depending on the method of calling conventions such as stdcall, pascal.

The next line will terminate the program by calling ExitProcess API
and give a return code, which is NULL here.

Last, here is the download for this tiny PE we produce using Win32 Assembly!.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
814 words
Last Post: Lost Era, Microsoft DOS, .COM Assembly, Print Letters using Loop
Next Post: Lost Era, Microsoft DOS, .COM Assembly, 8 byte program, GetKey

The Permanent URL is: Emerging Era, Win32 Assembly, MASM, Hello World!

Leave a Reply