Demoscene Code Gems - Assembly x86
code32a PARA PUBLIC SEGMENT USE32
1. Implicit multiplication by 65536…
We need to compute the delta X of a triangle — the delta that helps us interpolate the edge to avoid jittering. To increase precision we multiply the numerator by 65536 (shl 16) before dividing by the denominator. You can do it like this:
mov eax,x2
sub eax,x1
shl eax,16
mov ecx,y2
sub ecx,y1
cdq
idiv ecx
assuming, of course, that x2, x1, y2, y1 are stored as dwords. Now a little trick to tighten up our code :) we lay out our coordinates like this:
first_vertex label dword
x1_e dw 0 ;always zero
x1 dw 0 ;coordinate x1
y1 dw 0 ;coordinate y1
y1_e dw 0
second_vertex label dword
x2_e dw 0
x2 dw 0
y2 dw 0
y2_e dw 0
The previous procedure shortens for us to:
mov eax,[second_vertex]
sub eax,[first_vertex]
mov ebx,[second_vertex+4]
sub ebx,[first_vertex+4]
cdq
idiv ebx
2. Direct addressing…
We’ve got the inner loop of our texturing procedure…
adc ebx,DELTAT1
adc edx,DELTAT2
mov bh,dl
mov al,[texture+bx]
stosb
Many of you probably think this can’t be made any faster… actually, it can…
-
ADC reg,mem— on a 386 takes 6 cycles -
ADC reg,immediate— only 2 cycles
So how do we apply this to our example? Nothing simpler:
adc ebx,10001h
DELTAT1=$-4
adc edx,10001h
DELTAT2=$-4
mov bh,dl
mov al,[texture+bx]
stosb
And what then? The delta we computed earlier gets written in:
...
idiv ebx
mov [DELTAT1],eax
3. Design…
How to make design in an intro/demo — many of you write to me asking this question… I’ll try to answer very briefly here, hoping you’ll take something away from it. I use Tran’s PMODE 2.51 and to this day I haven’t found a good mod-player for that version of pmode. In revolt, for example, I used Frontman’s SB-MOD (hi dude), a version that runs under RMODE… The first thing I do is fire up the mod, and only then the .exe running in protected mode (it’s a shame I don’t have as much time as Technomancer, otherwise I’d play around with my own sound system :) and kernel :))! The player hooks itself to the timer interrupt, where a certain routine responsible for our playback is called continuously. And that’s exactly where the first step to design begins… I add part of my own code in there, which during playback writes into interrupt vector 5 (printer) the number of the currently played song and pattern. And that’s pretty much it, now just a small check from the pmode side, and it works…
code32a ENDS
Enjoy Reading This Article?
Here are some more articles you might like to read next: