3

Original Age of Empires 2 dev talks about its usage of assembly code

 8 months ago
source link: https://old.reddit.com/r/aoe2/comments/18ysttu/aoe_is_written_in_assembly_is_this_actually_true_o/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client



[–]ES_MattPES/FE [score hidden] 8 hours ago* (5 children)

I guess I can clarify this, since I wrote all the assembly code used in AoE / Aok, along with many other parts of those games.

There were about ~13,000 lines of x86 32-bit assembly code written in total.

The vast majority, about ~11,500 lines worth, was in the 'drawing core', which drew SLP sprites in a variety of ways (mirrored, stippled, clipped). The code for this was in a separate .asm file, which was "compiled" by Microsoft Macro Assembler 6.1 into a .obj file.

There were maybe a dozen other uses of assembly in the C++ code, as the compilers at the time supported 'inline assembly' in the middle of functions. These blocks of asm code were added using the __asm { } keyword. Microsoft Visual C++ 4.2 to 6.0 compilers were the ones used to compile the games as originally shipped. The inline assembly in other functions was generally used to speed up operations like scanning areas of the map for targeting and accessing compressed look up tables, which had bit-level operations that could be reduced in assembly, but c++ couldn't express as well.

The use of assembly in the drawing core resulting in a ~10x sprite drawing speed improvement over the C++ reference implementations, and AoE's drawing core was notably faster than competitors like StarCraft, which is why the default resolution 'out of the box' for AoE was 800 by 600, when nearly all our competition was 640 x 480 resolution - we could scroll the screen and fill it with sprites as fast or faster even though we had twice as many pixels-ish in the game world area.

The design of the drawing core used a number of techniques that could not easily (or at all) be expressed in C++ and took into account things like the cache architecture and UV pipes of the first Pentium processors. A key speedup technique AoE used was realized during discussions I had with iD software programmer and optimization guru Michael Abrash over lunch at Tia's Mexican Restaurant in Mesquite, TX. Other speedups came from being able eliminating function call overhead and hand-managing register usage. "register starvation" was a real issue for CPUs in the pre-Pentium Pro era and languages like C++ were hurt by it.

Additional assembly code was added in Age of Kings to add the clipped outlines of obscured units.

The assembly code remained in use in AoK:HD edition (a 32-bit game)

I re-wrote the assembly functions into C++ for both Definitive Editions, as they are 64-bit programs, and inline assembly was never supported by the 64-bit C++ compiler, and the vastly improved register sets and compiler optimizations made it un-necessary. Additionally, sprite drawing in the definitive editions is multi-threaded, and will use up to 4 cores for that task alone.

I hope that clears things up.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK