joriszwart.nl

Tetris

TETRIS.COM

Introduction

TETRIS.COM1 is a game written using 80386 assembler in the so-calledVGA mode 13h2 for MS-DOS™.

Without much code golfing the result is a compressed3 executable of 1663 bytes. This is less than the size of the screenshot (68%, 2429 bytes optimized PNG)!

tetris.com single player screenshot

TETRIS.COM - single player

Features

Source code

Probably on a 1.44″ floppy somewhere, but here are some disassembled snippets. Back by popular demand.

The source code is 23 kB and approximately 1200 lines, including the graphics. It is written in Borland’s Turbo Assembler syntax.

The build script

The build script assembles the source using as many passes as needed (-m), then links using the TINY memory model. To pack the file from ~8 kB to the mentioned 1663 bytes a so-called executable packer3 is used.

tasm tetris.asm -m
tlink tetris -t
apack tetris.com tetris.com

Initialization

        .model tiny
        .code
        .386

        include blocks.inc
        include font.inc

;        jumps

        org 100h


start:  mov ax,1a00h
        int 10h
        cmp al,1ah
        jne novga
        cmp bl,7
        jb novga
        jmp ok
novga:  mov dx,offset errmsg
        mov ah,9
        int 21h
        ret

errmsg  db 'No VGA detected',13,10,'$'

ok:     xor bx,bx      ;set keyboard rate&delay fast!
        mov ax,305h
        int 16h
        call init
        call drawbucket
        ...

Drawing a tetromino

putblock proc        
        call calcpos
        mov dx,4
copybrick:
        mov bp,di
        add di,word ptr [offset data_147+bx]
        mov cx,8

copyrow:
        movsw          ; copy 8 bytes
        movsw
        movsw
        movsw
        add di,320-8
        loop copyrow

        mov di,bp
        add bx,2
        dec dx
        jnz copybrick
        ret
putblock endp

Calculate position of a tetromino

calcpos proc        
        mov ax,0a000h
        mov es,ax
        mov cl,5
        shl si,cl
        mov cl,3
        shl di,cl
        mov dx,si
        add dx,di
        mov cl,5
        shl si,cl
        add si,offset data_22
        mov cl,5
        shl di,cl
        add si,di
        shl bx,1
        shl bp,1
        mov di,[bx+data_17]
        add di,[bp+data_18]
        mov bx,dx
        ret
calcpos endp

Clear full lines

clrlines proc          ;ds=source, es=dest
        xor di,di
        xor si,si
        xor bp,bp      ;linect

loc_23: mov bx,72      ;9*8
loc_24: cmp byte ptr es:[bx+di],0  ; full?
        je loc_27
        sub bx,8
        jnc loc_24
        inc bp         ;linect++
        mov ax,di
        mov bx,si

loc_25: mov dx,8
loc_26: mov cx,40      ; 10 columns * 8 bytes (= 40 words)
        rep movsw

        add di,320-80
        add si,320-80
        dec dx
        jnz loc_26
        sub di,16*320
        sub si,16*320
        cmp si,0f600h
        jne loc_25
        mov si,bx
        mov di,ax

loc_27: add si,8*320
        add di,8*320
        cmp si,23*8*320
        jne loc_23
        ret
clrlines endp

Downloads

FilenameNoteTypeSize
tetris.comMissing IPXCOM1663 bytes
tetris.png-PNG2429 bytes

  1. the name is not about the TLD, but about the ancient executable format. ↩︎

  2. 320×200 pixels, 256 colors (palette). ↩︎

  3. aPACK compressor for 16-bit DOS executables ↩︎ ↩︎

Related