Assembly Language Programming Windows

While programming languages are getting more and more high level, assembly language is still the base of each and every modern device, smart phone, tablet, desktop or server. If you understand assembly language, each other language will be just a new set of syntax elements and a couple of interesting new concepts. The assembly programming is performed using the yasm assembler automatically from the ebe IDE under the Linux operating system. The book primarily teaches how to write assembly code compatible with C programs. The reader will learn to call C functions from assembly language and to call assembly functions from C in addition to writing complete programs in assembly language. The gcc compiler is used internally to compile C programs.

7 Oct 2016CPOL
This article shows how to access Windows API with plain x64 assembly programming language (MASM style). It shall also give an overview, how to apply some programming techniques like OOP and multithreading on a low level.

Introduction

This tool is a simple 64 bit file manager for Windows featuring a 100% assembly language source code. The tool shall be a sample for using advanced techniques in assembly language environment, like native 64 bit, object orientation and unwind ability for stack frames.

Wait, Assembly Language? Isn't It Dead Already?

Assembly Language Programming Windows 86

No, it's not. While programming languages are getting more and more high level, assembly language is still the base of each and every modern device, smart phone, tablet, desktop or server. If you understand assembly language, each other language will be just a new set of syntax elements and a couple of interesting new concepts.

There is probably no useful reason to do a large project in assembly, but every programmer should at least have an idea of what is going on under the hood.

How Will Assembly Language Be Able to Use Modern Techniques as OOP?

Most of the common programming paradigms are not a property of the language, but a way how to write source code. The language can give you support to avoid mistakes and make writing easily, however you can still do it on your own.

This project features a fully object orientated style, multi threaded working and all the Windows features for x64 programs.

How to Work with Assembly Language?

Visual Studio 2013 contains the MASM (Microsoft Macro Assembler) which will be able to assemble the source files out of the box. If you encounter any problems with the project, please don't hesitate to contact.

For better layout of the ASM sources, we use a VStudio plug in for formatting the source code. This will become a sub project in near future (hopefully). Currently, the plug in is not worth a public release.

Using the Code

Some interesting code snippets:

Procedure starts with PROC FRAME, since MASM cannot declare parameters in x64 mode nothing follows.

Afterwards local variables are declared, with appropriate data types. Used registers should be pushed and popped manually and declared to be so with .pushreg keyword. Space for variables must be allocated manually too and also space for any call usage inside this procedure (sub rsp, 48 in this example). Stack usage is declared with .allocstack. Since all calls are register based, stack must be corrected on return and use ret 0. Finally, the header end with .endprolog.

You must care about alignment of stack to be 8 bytes. Code should be aligned on 4.

Calling a register based function:

Registers rcx, rdx, r8 and r9 hold first four parameters. If more are required, they are placed on the stack. Keep space for first 64 bit parameters, as the procedure can write parameters back on stack if required.

Doing Basic Object Orientation

Each object has to be a small allocated memory block. It contains a list of the methods it defines (vtable), which will be always the first member in block. If you share this table between objects, it represents the difference between class and object. If you change methods in list, it be some kind of overload or inheritance.

Generate a new object via handmade new operation:

Assembly

Assembly Language Programming Windows 9

Objects size is allocated at process heap. The table with method pointers resists inside object data. All pointers are written to the table and the table pointer is filled into first position.

Sample class definition:

If the class is defined this way and kept properly aligned to 8 bytes, it will be fully compatible to Visual Studio C++ classes, so you can even interact with a C++ object or call assembly methods from C++.

Calling an object method (MS style, aka STDMETHODCALL):

Object has to be in rcx register, rax holds the vtable pointer, and the call goes to wanted method inside vtable.

History

  • October 2016: First release

Assembly Language Programming Windows