Skip to content

Instantly share code, notes, and snippets.

@berk76
Last active January 8, 2018 14:37
Show Gist options
  • Save berk76/411c7ea62d890a5a6584a3ec9bbe5308 to your computer and use it in GitHub Desktop.
Save berk76/411c7ea62d890a5a6584a3ec9bbe5308 to your computer and use it in GitHub Desktop.
DOS: Snow flakes

Snow flakes in assembly

Complete example from article Snow flakes keep falling on my... screen?

Compilation:

tasm flake
tlink flake

flake.asm

; flake.asm
; http://tuttlem.github.io/2012/12/01/snow-flakes-keep-falling-on-my-screen.html
;
; how to compile: 
; tasm flake
; tlink flake

.model small
.stack 100h

.code
        
        mov     ax, 0013h       ; set 320x200x256 mode
        int     10h
        
        mov     ax, 0a000h      ; we can't directly address ES so 
        mov     es, ax          ; we do so through AX. ES = A000

no_kbhit:

        ; put a new snowflake at the top 
        ; of the screen
        call    get_random
        mov     di, ax
        mov     byte ptr es:[di], 15

decend:

        ; we can't move snowflakes any further
        ; than the bottom of the screen so we
        ; process all other lines
        mov     di, 63680
        mov     cx, 63680

next_pixel:

        ; test if there is a snowflake at the 
        ; current location
        mov     al, es:[di]
        cmp     al, 0
        je      no_flake

        ; test if there is a snowflake beneath
        ; us at the moment
        mov     al, es:[di+320]
        cmp     al, 0
        jne     no_flake

        ; move the snowflake from where we are 
        ; at the moment to one line below us
        xor     al, al
        mov     byte ptr es:[di], al
        mov     al, 15
        mov     byte ptr es:[di+320], al

no_flake:

        ; move our way through video memory
        dec     di
        dec     cx
        jnz     next_pixel

        ; check for a keypress
        mov     ah, 01h
        int     16h
        jz      no_kbhit

        mov     ax, 0003h       ; set text mode
        int     10h
        
        mov     ax, 4c00h       ; return control back to dos
        int     21h

get_random:
        ; start out with ax and bx = 0
        xor     ax, ax
        xor     bx, bx

        ; get the first random number and
        ; store it off in bl
        mov     dx, 40h
        in      al, dx
        mov     bl, al

        ; get the second random number and
        ; store it off in al, but we only 
        ; want 6 bits of this number
        mov     dx, 40h
        in      al, dx
        and     al, 63

        ; add the two numbers to produce a
        ; random digit in range
        add     ax, bx

        ret
        
        END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment