Programming graphics in assembler?

I developed the launch of Super Mario Sprite using Visual C ++ 6.0 and DirectX. But this is not very pleasant for me (rape of a 3D multimedia frame to display only a 2D sprite), so I would like to be able to program an animated sprite using only C and assembler.

So, looking at old games (like Wolfenstein), it seems like most of the game is written in C, and every time it comes to graphical output, assembler is used.

Unfortunately, when trying to use this old assembler code, the error message "NTVDM.exe detected an invalid instruction" always appears, so now it does not work.

Is there any tutorial on graphical assembly language that is still useful?

(I don’t want to use any bloated frames or libraries, I just want to develop everything myself. WinAPI will be fine for creating a full-screen window and for user login, but not for graphics, because I read GDI too slow for fast graphics.)

I am using WindowsXP and MASM or A86.

+4
source share
6 answers

It looks like you are using Assembler because it seems like it's necessary. This is not true. Unless you have other reasons for this (i.e., you want to know it), do not use Assembler here unless you know exactly what you are doing.

For your average graphics engine, assembly programming is completely unnecessary. Especially when it comes to the Super Mario 2D scenario. Even “slow” scripting languages ​​such as Python are fast enough for such things nowadays.

Adding to this, if you don’t know exactly what you are doing, Assembler will not be faster than C (in fact, it is more likely that it will be slower because you implement existing C functions less efficiently).

+13
source

I totally agree with samcl

The main reason for not using assembler is that you can no longer access Videomemory. Back in the early days (you mentioned Castle Wolfenstein), a special video mode called 0x13h appeared , where your graphic was just a block of memory (each pixel was a palette color ranging from 0-255 to 1 byte). You were able to access this memory through this particular video mode, however today everything is much more complicated.

Today you have a very fast Videomemory, and using your processor to access it simply disrupts all the performance, since your processor is connected via PCI-Express / AGP / PCI / VESA-LOCALBUS / ISA (<- remember someone !?)

Graphical programming often provides many read and write capabilities (reading pixels, checking transparency, multiplying by alpha, pix, etc.). Modern memory interfaces are much slower than direct access internally to the graphics card. That's why you really should use shaders, as Robert Gould suggests. This way you can write faster and easier to understand code, and it will not stop your GFX memory.

IF you're more interested in GFX programming, you can water your appetite shadertoy , a community dedicated to shaderbased effects, complemented by Running Shadercode using WebGL.

Also your beginner assembler code will be pretty lame. in quality, as in performance. Trust me. Optimizing such primitive code takes a lot of time. Thus, your compiled C / C ++ code will significantly exceed your handwritten asm.

If you are initialized in Assembler, try doing something like diskaccess. Here you can get great performance.

+11
source

I assume that if you are already using C with DirectX, speed is not an issue, and that this is more of a training exercise. For 2D under Windows, C and DirectX will be very fast, and as Conrad Rudolph points out, manual assembler is unlikely to be faster than a highly optimized SDK.

From a purely educational point of view, this is an interesting activity, but rather complicated. In the early days of home computers and the first PC, the graphic screen appeared to a large extent as a memory block, where the bytes corresponded to one or more color pixels. By changing the value of the screen memory, you can create dots and, therefore, lines, as well as sprites, etc. On modern PCs, this is not an option, because you are programming a graphics card, usually through the SDK, to do the same job. The map then does the hard work, and you are given a much higher level of abstraction. If you really wanted to feel like it was that day, I would recommend an emulator. Stick to your SDK for a modern game.

+6
source

You can program your own 2D engine in the latest Directx if you want to explore this prospectus. You can create an alignable “screen space” polygon without correcting the perspective from which the texture is displayed. Then you can draw your sprites on a pixel basis on this texture map.

as for 13h mode (Peter Parker), he brings back some memories!

__asm { mov ax,0x13 int 10h } 

I would try to avoid assembler with a barge pole, it can be partially debugged and maintained; however, if you want to study this issue in more detail, I can recommend the Black Book of Graphics Programming Michal Abrash . It's a bit outdated, but well read and will give you some insight into graphical programming technologies in front of 3D equipment.

+4
source

The assembler for graphics was because at that time most people lacked a graphics card with support for 3d, so it no longer needed to be done on the processor. This currently concerns shader programming. Shader languages ​​allow you to cuddle bare metal. Therefore, if anything, you should try to encode your 2d graphics to be a shaded base, so experience will be of value as a career skill.

Try CUDA for starter.

+2
source

My recommendation is to experiment. Take your sprite code and write in several forms, starting with C / GDI and C ++ / DirectDraw. Do not worry about assembler.

DirectX is the best choice for fast action graphics. Examine it, then find out how to optimize the mini assembler. In general, assembler is not going to speed up your API calls. It will open the flexibility for faster calculation for things like 3D rotation, texture mapping, shading, etc.

Start with DirectDraw. Here is the FAQ . Technically, DirectDraw is deprecated after DirectX 7, but you can still use it and learn from it. This will allow you to directly modify the framebuffer that you are probably looking for.

There are several helpful guides and forums on TripleBuffer Software .

Also consider upgrading your compiler to Visual C ++ 2008 Express . VC ++ 6 has a compiler with an error that can be problematic when trying to compile some C ++ libraries.

+2
source

All Articles