Analysis model: gpt-5.5 xhigh
Lion by Epsilon - Technical Dissection
Release year: 1995
Lion is Elliot / Epsilon's 4038-byte MS-DOS intro from the Wired 1995
4 KB intro competition. The Wired result file lists it as first place in that
category:
1. Epsilon "Lion" 265 pts
2. Cathedral - Digital Labs "Silence of the labs" 157 pts
3. Realtime "FLI" 145 pts
The public DIZ calls it the official 4K-compo release and credits it as
THE LiON by Elliot / Epsilon. No period contact details were needed for this
pass.
Sources:
- Scene.org archive: https://archive.scene.org/pub/parties/1995/wired95/in4k/lion.zip
- Scene.org DIZ: https://archive.scene.org/pub/parties/1995/wired95/in4k/lion.diz
- Scene.org Wired 1995 results: https://archive.scene.org/pub/parties/1995/wired95/results.txt
Archive Contents
Unpacked archive:
LION.COM 4,038 bytes
FILE_ID.DIZ 495 bytes
Hashes used for this pass:
de4510dd9d64236d186b0179b88477568698e168c2feda374b30e12b0d4bd270 lion.zip
7ff1f1ca869642cbccbc9dafd9b4455280aa7df755a75c48c6209a87a7138a08 lion.diz
6ea4f7afb460b74e4797b62bd28ee96c604650ff867f631c37cfdd87ddaff7fa LION.COM
a0ef13c27f461faadab676978ee777cf89ca994cf41a751f8851b4f6ebc54541 wired95-results.txt
The release is a plain COM file, but only the loader is plain. The useful body
is compressed, copied upward, expanded back to COM offset 0100h, and then run
from the normal DOS load segment.
Loader And Relocated Depacker
The first 0x31 bytes are a relocation launcher. It checks available stack
space, chooses a paragraph-aligned high scratch offset, copies 0xA2 words of
depacker code from COM offset 0131h, computes the matching segment, and enters
the copy with a far return. The compressed body length is held in DX=0x0e52.
The relocated depacker begins with an important overlap-avoidance move:
0000 std
0001 mov di,ax
0003 dec di
0004 dec di
0005 mov si,0273h
0008 add si,dx
000a mov cx,dx
000c shr cx,1
000e rep movsw
0010 cld
0011 xchg si,di
0013 inc si
0014 inc si
0015 mov di,0100h
AX is the high destination chosen by the launcher. SI=0273h+DX points to
the end of the compressed tail, and rep movsw runs backward under STD. That
means the compressed bytes are first moved up out of the way. Only after that
does the depacker switch back to forward mode, set output to 0100h, and start
expanding into the original COM body.
The bitstream is held in BP; DX is the remaining bit count. Refill snippets
are duplicated several times to avoid a central subroutine:
ad lodsw
95 xchg ax,bp
b2 10 mov dl,10h
The literal/match decision is compact:
004e jb 0058 ; carry set: begin match
0050 movsb ; carry clear: literal byte
0051 shr bp,1
0053 dec dx
0054 je 004a ; refill, then resume literal path
0056 jae 0050 ; keep copying literals while carry is clear
For matches it builds two values: CX becomes the copy length, and BX becomes
the backward distance. The length table lives directly after the code:
0085 mov cl,cs:[bx+011ch]
008a cmp cl,0ah
008d je 00c9 ; extended length and end marker path
...
00bc lodsb
00bd mov bl,al
00bf push si
00c0 mov si,di
00c2 sub si,bx
00c4 rep movsb
00c6 pop si
00c7 jmp 0051
The high-byte table at 012ch provides common distance classes. Larger matches
read more bits, fold them with and bx,00dfh, swap BL/BH, and then read the
low byte from the compressed stream. Length value 0x0a is special:
00c9 lodsb
00ca add cl,al
00cc adc ch,0
00cf cmp al,0ffh
00d1 jne 008f
00d3 jmp 0108
So repeated 0xff bytes extend the length, while a terminating 0xff also
acts as the end marker. At finish the depacker clears registers and far-returns
to the freshly expanded COM at 0100h.
This is a size-oriented LZ family depacker. It saves bytes by:
- using carry from
SHR BP,1as the entropy decision, - inlining refill snippets instead of calling them,
- putting length and high-distance tables in executable space,
- copying matches with
rep movsbfrom already expanded output.
Runtime Capture Map
The intro was run under DOSBox-X with VGA enabled and an autosave after startup.
The save state captured the expanded body while the intro was already in mode
13h. The DOS memory-control block name LION appears before the PSP; the PSP
signature starts at physical 0x81c8, so COM offset 0100h maps to physical
0x82c8.
Useful post-depacker landmarks:
0100 / 0x82c8 main entry after depack
0120 / 0x82e8 first 64 KB DOS allocation
0161 / 0x8329 startup calls table, texture, object prep
0307 / 0x84cf DOS block cleanup and exit
0312 / 0x84da palette gradient helper
0367 / 0x852f 1024-entry wave table generator
03aa / 0x8572 full-palette flash/fade routine
03f8 / 0x85c0 blue/cyan palette preset
0449 / 0x8611 green/yellow palette preset
049d / 0x8665 recursive texture generator
05ce / 0x8796 first warped-background renderer
0c3d / 0x8e05 precomputed two-source sampler
0c8c / 0x8e54 plot-field/table builder
0dbe / 0x8f86 320x200-to-320x200 texture copy helper
0df2 / 0x8fba rotating object scene loop
0e95 / 0x905d 3D frame builder
101e / 0x91d6 matrix builder
116e / 0x9336 byte-vector transform
11b0 / 0x9378 word-vector transform
11f2 / 0x93ba face-key sorter
1249 / 0x9411 perspective projection
1287 / 0x944f triangle rasterizer
14ee / 0x96b6 line interpolator
1521 / 0x96e9 vertex normal generator
16dd / 0x98a5 integer square-root approximation
All offsets above are COM logical offsets with the captured physical offset
beside them. The saved code uses 386 real-mode opcodes and FS/GS, but no
protected-mode switch is needed.
Main Entry And Scene Chain
The expanded entry starts by shrinking its DOS block, printing two text lines, and allocating four extra 64 KB DOS blocks:
0100 cld
0101 mov ah,4ah
0103 mov bx,6aaah
0106 shr bx,4
0109 inc bx
010a int 21h
...
0120 mov ah,48h
0122 mov bx,1000h
0125 int 21h
012c mov [05c8],ax
012f mov ah,48h
0131 mov bx,1000h
0134 int 21h
013b mov [4b5c],ax
013e mov ah,48h
0140 mov bx,1000h
0143 int 21h
014a mov [0d38],ax
014d mov ah,48h
014f mov bx,1000h
0152 int 21h
0159 mov [0d3a],ax
The blocks are used as:
[05c8] generated 256x256 byte texture, later also brightened texture
[4b5c] final scene back buffer / texture copy target
[0d38] first 64 KB table or frame half, depending on scene
[0d3a] second 64 KB table half
[0d42] visible 320x200 frame segment, usually A000h
Then the intro builds its data and starts the visual chain:
0161 call 0367 ; wave table at [3b5a]
0164 call 049d ; 256x256 procedural texture
0167 call 0c8c ; precomputed plot/sample field
016a call 1521 ; object vertex normals
016d mov ax,0013h
0170 int 10h
0172 call 03aa ; full-screen flash/fade
0175 mov ax,[05c8]
0178 mov [0c3b],ax ; texture source for first warp
017b call 0449 ; palette preset
The runtime has three visible phases:
Phase 1:
Call 05ce once per retrace.
Shrink [0c35] from 0x0bb8 down to 0x0040.
Increase angle [0c39] by 0x0a.
Overlay the small title glyph at screen offset 0xe10a.
Phase 2:
Reset mode 13h and palette.
Call 0c3d once per retrace.
Add 0x0101 to [0d3c].
Stop at frame counter [4b5e] == 0x870 or Escape.
Phase 3:
Copy the texture, brighten it with OR 80808080h, seed a few bytes,
set both palette presets, then run the rotating object scene at 0df2.
The exit path restores text mode, prints the intro text again, frees all four
DOS blocks with int 21h, AH=49h, and exits through 4c00h.
Wave Table
The wave table routine at COM offset 0367 fills 0x3b5a with 1024 signed
words. It does not call the FPU or BIOS; it uses a second-order integer
recurrence:
0367 mov dword [3b5a],01000000h
0370 mov dword [3b5e],00fffec4h
0379 mov si,3b5ah
037c lea di,[si+8]
037f mov cx,03feh
0382 mov ebx,[3b5e]
0387 mov eax,ebx
038a imul ebx
038d shrd eax,edx,17h
0392 sub eax,[di-8]
0396 stosd
0398 loop 0382h
039a mov di,si
039c mov cx,0400h
039f lodsd
03a1 sar eax,0eh
03a5 stosw
03a6 dec cx
03a7 jne 039fh
The first pass keeps high precision dwords. Each new value is derived from the
previous value squared, shifted down, and the value two entries back. The second
pass converts the same storage in place to signed words. Later code indexes it
with BH & 7 or BX & 07ffh, so it functions as a 1024-entry sine/cosine
table.
Palette Routines
The flash/fade routine at 03aa writes all 256 DAC entries repeatedly:
03aa mov dx,03c8h
03ad xor ax,ax
03af out dx,al ; DAC index 0
03b0 xor cl,cl
03b2 movzx ax,cl
03b5 shl al,1
03b7 mov dx,03c9h
03ba out dx,al
03bb out dx,al
03bc out dx,al
03bd dec ah
03bf jne 03bah
The AH trick is deliberate. movzx ax,cl makes AH=0; dec ah wraps to
0xff, so the loop writes 256 RGB triples without loading CX=256. It waits
for vertical retrace after each band, counts CL upward to 0x20, then counts
back down.
The reusable gradient helper at 0312 uses a small parameter block:
[4b62] start palette index
[4b63] start R
[4b64] start G
[4b65] start B
[4b66] end palette index
[4b67] end R
[4b68] end G
[4b69] end B
For each channel it evaluates:
channel = start + (end - start) * step / (end_index - start_index)
Preset 03f8 builds a black-to-blue/cyan range and a cyan-to-white range.
Preset 0449 builds a black-to-green range and a green/yellow range. The same
texture bytes therefore read as different material in different scenes.
Procedural Texture
The texture builder at 049d fills the [05c8] block with a 256x256 byte
height/brightness map. First it initializes every dword to ffffffffh:
049d xor di,di
049f mov es,[05c8]
04a3 stc
04a4 sbb eax,eax
04a7 mov cx,4000h
04aa rep stosd
Then it seeds four points in the map:
04bd mov byte es:[0080h],0feh
04c3 mov byte es:[8000h],0feh
04c9 mov es:[0000h],cl
04ce mov es:[8080h],cl
04d3 call 04edh
The recursive subdivision routine halves CX on entry and returns when the
cell is smaller than one byte:
04ed shr cx,1
04ef je 0580h
Each of the four edge midpoint cases:
- reads an endpoint byte from
ES:[BX], - steps
BLorBHby the current half size inCL, - calls the guarded midpoint writer at
05a4, - only writes if the target still contains
ffh.
The guarded midpoint writer is tiny:
05a4 cmp byte es:[di],0ffh
05a8 jne 05b5h
05aa add al,es:[bx]
05ad adc ah,ch
05af shr ax,1
05b1 call 0581h
05b4 stosb
05b1 adds a random displacement scaled by CL, clamps positive overflow to
0xfe, and returns the final byte in AL:
0581 mov ch,al
0583 call 05b6h ; update pseudo-random seed
0586 sub dx,67e8h
058a xor ax,ax
058c mov al,cl
058e shl ax,3
0591 imul dx
0593 xor ax,ax
0595 add dl,ch
0597 mov ch,al
0599 adc dh,ch
059b js 05a3h
059d je 05a1h
059f mov dl,0feh
05a1 mov al,dl
05a3 ret
The pseudo-random generator is a small multiply/add/mod step:
05b6 mov ax,00afh
05b9 mul si
05bb add ax,2bc0h
05be adc dx,0
05c1 div word [05cc]
05c5 mov si,dx
05c7 ret
After subdivision, the builder divides both bytes of every word by four:
04d6 xor di,di
04d8 mov ax,es:[di]
04db shr ah,2
04de shr al,2
04e1 stosw
04e2 or di,di
04e4 jne 04d8h
The result is a 64K texture with coherent large-scale structure, small noise, and values already in the 0..63 DAC-friendly range.
First Warp Renderer
The first visual renderer starts at 05ce. Its input is:
[0c39] angle
[0c35] scale, initially 0x0bb8 and reduced by 8 per frame
[0c3b] source segment, first set to [05c8]
[0d42] destination segment, usually A000h
The setup reads two wave samples, multiplies them by the current scale, and stores two signed step values:
05ce mov bx,[0c39]
05d2 shl bx,1
05d4 and bh,07h
05d7 movsx eax,word [bx+3b5a]
05dd imul dword [0c35]
05e2 shrd eax,edx,08h
05e7 mov edi,eax
05ea sub bh,02h ; quarter-wave shift
05ed and bh,07h
05f0 movsx eax,word [bx+3b5a]
05f6 imul dword [0c35]
05fb shrd eax,edx,08h
0600 mov esi,eax
0603 mov [0c31],di
0607 mov [0c33],si
Then it derives two 16-bit accumulators. BX and DX are the texture-plane
starting point, while [0c31] and [0c33] are the row-to-row increments:
060b lea bx,[esi+esi*4]
060f shl bx,5 ; si * 160
0612 imul dx,di,64h ; di * 100
0615 add bx,dx
0617 neg bx
0619 movsx ecx,word [0c31]
061f lea cx,[ecx+ecx*4]
0623 shl cx,5 ; [0c31] * 160
0626 imul dx,si,64h ; [0c33] * 100
0629 sub dx,cx
062b add bh,80h
062e add dh,80h
A short preloop writes a small offset table at 067ch:
0631 mov di,067ch
0638 mov si,[0c33]
063c mov cx,[0c31]
0640 mov al,dh
0642 mov ah,bh
0644 mov bp,00a0h
0647 stosw
0648 add di,2
064b add dx,cx
064d add bx,si
064f mov al,dh
0651 mov ah,bh
0653 stosw
0654 add di,3
0657 add dx,cx
0659 add bx,si
065b mov al,dh
065d mov ah,bh
065f dec bp
0660 jne 0647h
That table is not a full texture map. It is a compact byte-pair seed stream for
the unrolled renderer. The hot frame writer begins at 0662:
0662 mov es,[0d42]
0666 mov ds,[0c3b]
066a xor di,di
066c mov bx,di
066e mov dx,di
0670 mov cx,di
0672 mov si,cs:[0c33]
0677 mov bp,00c8h
The row body is a long straight-line chain. The beginning looks like this:
067a mov al,[bx-3b6dh]
067e mov ah,[bx-1359h]
0682 stosw
0683 mov al,[bx+14bbh]
0687 mov ah,[bx+3ccfh]
068b stosw
068c mov al,[bx+64e3h]
0690 mov ah,[bx-7409h]
0694 stosw
...
0c15 add cx,cs:[0c31]
0c1a sub dx,si
0c1c mov bh,ch
0c1e mov bl,dh
0c20 dec bp
0c21 jne 067ah
Each mov al,[bx+constant] / mov ah,[bx+constant] / stosw writes two adjacent
pixels. There are 160 stosw operations per row, so the entire 320-pixel row is
emitted without an inner loop branch. BX is a 16-bit texture offset assembled
from high bytes of two accumulators. The constants are the per-column offsets;
the row-end update moves the two accumulators to the next scanline.
This is why the first phase is fast enough for a 4K intro even in real mode: the per-pixel work is two byte loads and a word store, with all column stepping encoded as instruction displacement bytes.
Precomputed Two-Source Sampler
The second phase uses a different renderer at 0c3d. The table builder at
0c8c fills two 64 KB blocks with 32-bit source-offset pairs. A single 320x200
word table would be 128 KB, so the routine splits it across [0d38] and
[0d3a].
The builder draws a polar field into those two blocks. For each radius step
[0d36] and angle BX, it computes:
0cae mov ax,[bx+3b5a]
0cb2 imul word [0d36]
0cb6 shrd ax,dx,0dh
0cba cwde
0cbc add eax,000000a0h
0cc2 mov edi,eax ; x = sin(angle) * r + 160
...
0cd2 mov ax,[si+3b5a]
0cd6 imul word [0d36]
0cda shrd ax,dx,0dh
0cde cwde
0ce0 add eax,64h ; y = cos(angle) * r + 100
If the point is visible, the screen offset is converted to a word-table offset:
0ce9 lea eax,[eax+eax*4]
0cee shl eax,6 ; y * 320
0cf2 add edi,eax ; y * 320 + x
0cf5 shl edi,1 ; word table
0cf8 mov ax,[0d40] ; packed source seed
0cfb cmp edi,0fa00h
0d02 jb 0d0dh
0d04 add di,0600h
0d08 mov gs:[di],ax
0d0d mov fs:[di],ax
The 0xfa00h split is the 64K boundary. A 320x200 word table is 0x1f400
bytes; the first 0xfa00 bytes go to [0d38], and the rest wrap into [0d3a]
with the small +0600h correction.
The sampler itself is very tight:
0c3d mov es,[0d42]
0c41 mov fs,[0d38]
0c45 mov dx,[0d3c]
0c49 push dx
0c4a shl edx,10h
0c4e pop dx
0c4f mov ds,[05c8]
0c53 mov cx,3e80h
0c56 xor di,di
0c58 mov si,di
0c5a call 0c71h
0c5d xor si,si
0c5f mov fs,cs:[0d3a]
0c64 mov cx,3e80h
0c67 call 0c71h
The shared inner routine:
0c71 mov ebx,fs:[si] ; two 16-bit source offsets
0c75 add ebx,edx ; animated phase offset
0c78 mov al,[bx] ; sample first offset
0c7a shr ebx,10h
0c7e mov ah,[bx] ; sample second offset
0c80 mov es:[di],ax ; write two pixels
0c83 inc di
0c84 inc di
0c85 add si,4
0c88 dec cx
0c89 jne 0c71h
[0d3c] is added to both halves of EBX by packing it into the low and high
words of EDX. Each table entry therefore animates by sliding both source
offsets through the same 64K texture. The loop writes 0x3e80 words from the
first table half and 0x3e80 words from the second, which totals 320x200 pixels.
Small Glyph Overlay
Both early phases overlay a 16-by-10 bitmap at destination offset 0xe10a. The
same code also appears inside the final object loop. The glyph data starts at
0d96h and is read as 32-bit chunks:
01af mov cx,0010h
01b2 mov si,0d96h
01b5 mov di,0e10ah
01b8 mov dl,0ah
01ba push cs
01bb pop ds
01bc lodsd
01be ror eax,cl
01c1 shl eax,1
01c4 setb bl
01c7 or bl,bl
01c9 je 01ceh
01cb mov es:[di],bl
01ce inc di
01cf add ch,08h
01d2 jne 01beh
01d4 add di,0120h
01d8 dec dl
01da jne 01bch
The clever part is CH. Since CL=0x10, the rotate count remains 16, but
add ch,8 is a one-byte loop counter over 32 pixels. The code writes only set
bits, so it overlays the glyph without clearing the warped background.
Rotating Object Setup
Before the final object scene, 0dbe copies the generated texture into the
final scene buffer with a row skew:
0dbe mov es,cs:[4b5c]
0dc3 mov ds,cs:[0d38]
0dc8 xor di,di
0dca mov si,di
0dcc mov dl,0c8h
0dce mov cx,0050h
0dd1 rep movsd ; 80 dwords = 320 bytes
0dd4 sub si,0040h
0dd7 dec dl
0dd9 jne 0dceh
Each row copies 320 bytes, then backs SI up 64 bytes, so the next screen row
comes from a shifted source row. That produces a slanted texture field. The
helper also clears the frame counter and the three angle words:
0de1 xor eax,eax
0de4 mov [4b5e],eax
0de8 mov [2eac],ax
0deb mov [2eae],ax
0dee mov [2eb0],ax
The final loop at 0df2 then runs until counter 0x708 or Escape:
0df2 call 0e95 ; build and draw object
0df5 wait for retrace
0e02 mov es,[0d42]
0e06 mov ds,[4b5c]
0e0a mov cx,3e80h
0e0d xor si,si
0e0f mov di,si
0e11 rep movsd ; copy back buffer to visible page
...
0e6b add word [2eac],0ch
0e70 add word [2eae],02h
0e75 add word [2eb0],02h
0e82 inc dword [4b5e]
0e87 cmp dword [4b5e],0708h
0e90 jne 0df2h
The object renderer redraws into [4b5c], copies that buffer to [0d42], then
rebuilds the skewed texture copy for the next frame. That gives the object a
fresh textured background without needing a full second scene engine.
Matrix And Projection
The frame builder at 0e95 starts with a rotation matrix:
0e95 call 101eh ; matrix from [2eac], [2eae], [2eb0]
0e98 mov si,173eh ; source vertices, signed bytes
0e9b mov di,1ba6h ; transformed vertices, signed words
0e9e mov bx,1702h ; matrix
0ea1 mov ch,[16fe] ; vertex count
0ea5 call 116eh
...
0eaf mov si,1dcah ; vertex normals, signed words
0eb2 mov di,1feeh ; transformed normals
0eb5 mov bx,1702h
0eb8 mov ch,[16fe]
0ebc call 11b0h
0ec6 call 1249h ; perspective projection
The matrix builder indexes the wave table three times. The quarter-wave
subtractions (sub bh,2) provide the cosine value for each sine value. It then
builds a 3x3 matrix at 1702h..172ah, shifting products by 10 bits:
1029 mov bx,[2eac]
102d shl bx,1
102f and bh,07h
1031 movsx ecx,word [bx+3b5a] ; sin angle A
1037 sub bh,02h
103a and bh,07h
103c movsx ebp,word [bx+3b5a] ; cos angle A
...
1072 mov eax,edi
1075 imul dword [2eb2]
107a shrd eax,edx,0ah
107f mov [1702],eax
Vertex transform 116e computes three matrix dot products from signed byte
source coordinates:
116e mov cl,03h
1170 movsx eax,byte [si+0]
1174 imul dword [bx+0]
1177 shrd eax,edx,0ah
117c mov ebp,eax
117f movsx eax,byte [si+1]
1184 imul dword [bx+4]
1188 shrd eax,edx,0ah
118d add ebp,eax
1190 movsx eax,byte [si+2]
1195 imul dword [bx+8]
1199 shrd eax,edx,0ah
119e add eax,ebp
11a1 mov [di],ax
11a3 add di,2
11a5 add bx,10h
11a8 dec cl
11aa jne 1170h
Normal transform 11b0 is the same loop shape, but it reads signed words
instead of signed bytes.
Projection at 1249 is the classic mode-13h perspective divide:
1257 mov bx,[si+4] ; z
125a add bx,bp ; camera distance [16fc]
125c jne 125fh
125e inc bx
125f mov ax,[si+0] ; x
1261 cwd
1262 shld dx,ax,0bh
1266 shl ax,0bh
1269 idiv bx
126b add ax,00a0h ; center x = 160
126e stosw
126f mov ax,[si+2] ; y
1272 cwd
1273 shld dx,ax,0bh
1277 shl ax,0bh
127a idiv bx
127c add ax,0064h ; center y = 100
127f stosw
Projected screen coordinates are stored at 1a32h as x/y word pairs.
Face Selection And Sorting
The face pass does a backface/sign test before sorting. For each face it computes a 2D cross product from projected points:
0f28 imul ax,di
0f2b imul bx,cx
0f2e cmp ax,bx
0f30 jl 0f4eh ; culled
Visible faces get a 32-bit sort key written to a self-patched destination. The low word is the face index; the high word is an approximate depth:
0f38 shl eax,10h
0f3c mov ax,dx
0f3e add ax,[16fc]
0f42 mov es:[0457h],eax ; destination offset patched at 0f55
0f47 add word cs:[0f55],4
0f4d add word [2e92],4
The sorter at 11f2 is a compact radix-like pass. DX starts at bit 0, and
each pass partitions the 32-bit records into two temporary arrays:
11f2 mov dx,0001h
11f5 mov si,2212h
11f8 mov di,2852h
11fb xor bx,bx
11fd mov eax,[bx+383a]
1202 test ax,dx
1204 je 120eh
1206 mov [di],eax
1209 add di,4
120c jmp 1214h
120e mov [si],eax
1211 add si,4
1214 add bx,4
1217 cmp bx,[2e92]
121b jne 11fdh
After each partition, it copies the set-bit bucket first and the clear-bit
bucket second back to 383ah, shifts DX left, and repeats until carry:
121d push si
121e mov si,2852h
1221 mov cx,di
1223 sub cx,si
1225 shr cx,2
1228 mov di,383ah
122b rep movsd
122e pop cx
122f mov si,2212h
1232 sub cx,si
1234 shr cx,2
1237 rep movsd
123a shl dx,1
123c jae 11f5h
123e xor eax,eax
1241 mov [di],eax
1244 mov [di+4],eax
This is not a general library sort; it is a byte-cheap bitwise sort for the small face list the intro carries.
Triangle Rasterizer
The rasterizer at 1287 is the final hot path. It receives three projected
vertices and three transformed normal/light pairs in scratch slots
2eba..2ece.
First it sorts vertices by Y using 32-bit swaps so x/y and the paired light values stay together:
128b mov ebx,[2ebc]
1290 mov eax,[2eba]
1294 cmp ax,[2ec2]
1298 jle 12b0h
129a xchg eax,[2ec2]
129f mov [2eba],eax
12a3 mov eax,[2ebe]
12a7 xchg eax,[2ec6]
12ac mov [2ebe],eax
Then it rejects fully offscreen triangles:
12f0 cmp word [2eba],00c7h
12f9 jg 14edh
12fd cmp word [2eca],0000h
1302 jl 14edh
1306 cmp [2ebc],013fh
130a jb 131bh
130c cmp [2ec4],013fh
1310 jb 131bh
1312 cmp [2ecc],013fh
1316 jb 131bh
1318 jmp 14edh
Edge tables are built by calling 14ee three times. The routine takes
AX=start_y, BP=end_y, BX=start_x, DX=end_x, and writes one x value every
four bytes:
14ee sub bp,ax
14f0 inc bp
14f1 jne 14f4h
14f3 inc bp
14f4 movsx ebp,bp
14f8 mov ax,dx
14fa sub ax,bx
14fc cwde
14fe cdq
1500 shl eax,10h
1504 idiv ebp
1507 shl ebx,10h
150b ror ebx,10h
150f ror eax,10h
1513 adc ebx,eax
1516 mov [di],bx
1518 add di,4
151d dec bp
151e jne 1513h
The ror pair turns integer/fraction words into the form wanted by the short
add/store loop. The same mechanism fills left and right x tables at 2eda and
2edc.
Next the rasterizer builds two per-scanline attribute tables at 31fa and
351a. It interpolates from top to bottom, then computes the horizontal
attribute steps for the middle split. The critical part is self-patching:
140d mov ax,[2ed4]
1410 mov cs:[147b],ax
1414 mov cs:[14d2],ax
1418 mov ax,[2ed2]
141b mov cs:[1477],ax
141f mov cs:[14ce],ax
1423 mov bp,[2eba]
1427 mov ax,0140h
142a mul bp
142c mov cs:[146a],ax
1430 mov cs:[14c1],ax
The patched locations are the add ax,imm16, add dx,imm16, and screen-row
base constants inside the span loops. That saves register setup inside every
pixel.
Left-to-right span:
144d mov di,[si+2eda] ; left x
1451 mov cx,[si+2edc] ; right x
1455 mov ah,[si+31fa] ; attribute A, high byte used
1459 mov dh,[si+351a] ; attribute B, high byte used
145d xor al,al
145f xor dl,dl
1461 sub cx,di
1467 inc cx
1468 add di,0457h ; patched to y*320
146c mov bl,ah
146e mov bh,dh
1470 mov bl,fs:[bx] ; texture lookup
1473 mov es:[di],bl
1476 add ax,0457h ; patched attribute A step
1479 add dx,0457h ; patched attribute B step
147d mov bh,dh
147f inc di
1480 mov bl,ah
1482 dec cx
1483 jne 1470h
Right-to-left span:
149e mov cx,[si+2eda]
14a2 mov di,[si+2edc]
14a6 mov ah,[si+31fa]
14aa mov dh,[si+351a]
14b2 mov bx,cx
14b4 sub cx,di
14ba inc cx
14bd mov di,bx
14bf add di,0457h ; patched to y*320
14c3 mov bl,ah
14c5 mov bh,dh
14c7 mov bl,fs:[bx]
14ca mov es:[di],bl
14cd sub ax,0457h ; patched attribute A step
14d0 sub dx,0457h ; patched attribute B step
14d4 mov bl,ah
14d6 dec di
14d7 mov bh,dh
14d9 dec cx
14da jne 14c7h
The source texture is segment [05c8] in FS. The two high bytes AH and
DH form the 16-bit texture offset in BX; FS:[BX] is the shaded texel; and
ES:[DI] is the target pixel in [4b5c]. This is a very compact textured span
loop: one texture byte read, one screen byte write, two patched fixed-point
adds, and a branch.
Normal Generation
The setup routine at 1521 computes vertex normals once before mode 13h. It
starts by clearing the accumulator array at 1dcah:
1521 movzx ecx,word [16fe]
1527 lea cx,[ecx+ecx*2]
152b mov di,1dcah
152e xor ax,ax
1530 rep stosw
For each face record at 1846h, it reads three vertex indices, builds two edge
vectors from signed-byte vertices at 173eh, and computes a cross product:
nx = dy1 * dz2 - dz1 * dy2
ny = dz1 * dx2 - dx1 * dz2
nz = dx1 * dy2 - dy1 * dx2
The literal code is three multiply/subtract pairs:
15cc mov eax,[2e98]
15d0 imul dword [2ea8]
15d6 mov ecx,[2e9c]
15db imul dword [2ea4]
15e1 sub eax,ecx ; nx
15e4 mov ecx,[2e9c]
15e9 imul dword [2ea0]
15ef mov edi,[2e94]
15f4 imul dword [2ea8]
15fa sub ecx,edi ; ny
15fd mov edx,[2e94]
1602 imul dword [2ea4]
1608 mov edi,[2e98]
160d imul dword [2ea0]
1613 sub edx,edi ; nz
It adds that face normal to all three vertices:
1619 mov bp,0003h
161c movzx si,byte [bx+1846]
1621 imul si,si,0006h
1624 add [si+1dca],ax
1628 add [si+1dcc],cx
162c add [si+1dce],dx
1630 inc bx
1631 dec bp
1632 jne 161ch
Finally, each accumulated normal is normalized. It computes squared length,
uses a seven-iteration integer square-root approximation at 16dd, and scales
each component by <<7 / length:
1641 movsx eax,word [si+1dca]
1647 movsx ebx,word [si+1dcc]
164d movsx ecx,word [si+1dce]
1653 imul eax,eax
1657 imul ebx,ebx
165b imul ecx,ecx
165f add eax,ebx
1662 add eax,ecx
1665 mov ebp,eax
1668 call 16ddh
...
1687 shl eax,7
168b cdq
168d idiv edi
1690 mov [2e94],eax
The square-root helper is Newton-style:
16dd mov cl,07h
16df mov ebx,000000ffh
16e5 mov eax,ebp
16e8 xor edx,edx
16eb div ebx
16ee add eax,ebx
16f1 shr eax,1
16f4 mov ebx,eax
16f7 dec cl
16f9 jne 16e5h
16fb ret
The final object loop can therefore transform normals every frame without rebuilding them from faces.
What The 4K Budget Buys
Lion is fancy because it does not spend bytes on stored imagery. The archive
is a 4038-byte COM file, yet the running intro owns:
- a custom LZ depacker,
- a 1024-entry wave table,
- a recursive 256x256 texture generator,
- two different full-screen texture samplers,
- a precomputed polar source-offset field,
- two palette presets plus flash/fade code,
- a mesh with face data and generated vertex normals,
- a textured triangle rasterizer with self-patched spans.
The important design pattern is reuse. The same generated texture feeds the warp, the two-source sampler, and the final object. The same wave table drives the background and the object matrix. The same small glyph overlay is used in each phase. The triangle rasterizer is small because it turns per-triangle values into patched immediates and lets the inner span use high bytes as a texture coordinate pair.
The result is not a single trick stretched to 4 KB. It is a compact sequence of procedural setup, unrolled sampling, table-driven sampling, and real 3D texturing, all under a COM-sized custom depacker.