Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
104 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Why Assembly
 Language?

 Win32 ASM Basics
 The Design
 Document

 Code Framework
 Until Next Time

 Printable version

 


  The Series

 Part 1
 Part 2
 Part 3
 Part 4
 Part 5
 Part 6

 

Win32 ASM Basics

If you are already familiar with assembly language in the windows platform, you may want to skip this section. For those of you who aren't, this may be a bit boring, but hang with it ... this is very important stuff. For this discussion I will presume that you are at least familiar with the x86 architecture.

The first thing you need to understand are the instructions. There aren't very many that you will be using often so I will simply cover the ones that we care about.

MOV

This instruction moves a value from one location to another. You can only move from a register to register, memory to register, or register to memory. You can not move from a memory location to another memory location.

Example:

MOV EAX, 30 MOV EBX, EAX MOV my_var1, EAX MOV DWORD PTR my_var, EAX

The first example moves the value 30 into the EAX register. The second example moves the value in EAX into the EBX register. The third example moves the value of EAX into the variable my_var1. The fourth example moves the value of EAX into the ADDRESS pointed to by my_var, we need to use the DWORD specifier so that the assembler knows how much memory to move -- 1 byte ( BYTE ), 2 bytes ( WORD ), or 4 bytes ( DWORD ).

ADD & SUB

These two instructions perform addition and subtraction.

Example:

ADD EAX, 30 SUB EBX, EAX

The examples simply add 30 to the EAX register and then subtract that value from the EBX register.

MUL & DIV

These two instructions perform multiplication and division.

Example:

MOV EAX, 10 MOV ECX, 30 MUL ECX XOR EDX, EDX MOV ECX, 10 DIV ECX

The examples above first load EAX with 10 and ECX with 30. EAX is always the default multiplicand, and you get to select the other multiplier. When performing a multiplication the answer is in EAX:EDX. It only goes into EDX if the value is larger than the EAX register. When performing a divide you must first clear the EDX register that is what the XOR instruction does by performing an Exclusive OR on itself. After the divide, the answer is in EAX, with the remainder in EDX, if any exists.

Of course, there are many more instructions, but those should be enough to get you started. We will probably only be using a few others, but they fairly easy to figure out once you have seen the main ones. Now we need to deal with the calling convention. We will be using the Standard Call calling convention since that is what the Win32 API uses. What this means is that we push parameters onto the stack in right to left order, but we aren't responsible for the clearing the stack afterwards. Everything will be completely transparent to you however as we will be using the pseudo-op INVOKE to make our calls.

Next, there is the issue of calling Windows functions. In order to use invoke, you must have a function prototype. There is a program that comes with MASM32 which builds include files ( equivalent to header files in C ) out of the VC++ libraries. Then, you include the needed libraries in your code and you are free to make calls as you wish. You do have to build a special include file by hand for access to Win32 structures and constants. However, this too is included in the MASM32 package, and I have even put together a special one for game programmers which will be included in the source code and built upon as needed.

The final thing that I need to inform you about is the high level syntax that MASM provides. These are constructs that allow you to create If-Then-Else and For loops in assembly with C-like expressions. They are easiest to show once we have some code to put in, therefore you won't see them until next time. But, they are there ... and they make life 100000 times easier than without them.

That is really about all you need to know. The rest will come together as we take a look at the source code and such. So, now that we have that out of the way, we can work on designing the game and creating a code framework for it.





Next : The Design Document