; From Jerry Kassebaum
;
;Here's some code for newbies to work with.  Make sure numlock is on.
;Use the keys 1-9 to change direction.  The "c" key changes color.
;Alt-Q quits.
;
;Assemble, link and make into a com file.  For me that's:
;        Tasm newbie
;        Tlink newbie
;        Exe2bin newbie newbie.com
;
;Have fun.


screen segment at 0B800h 'screen'
screen ends

stak segment para stack 'stack'
dw 128 dup (?)
stak ends

data  segment byte 'data'
        col dw 55
        row dw 55
        dir db ?
        theimer dw 0
        color dw 1
data ends

code segment word 'code'
assume es:screen, ss:stak, cs:code, ds:data

start:
        push cs
        pop ds
        mov ax,screen
        mov es,ax
        mov ah,0                ;
        mov al,6                ; Sets the screen mode.
        int 10h                 ;

keyWait:

        mov dx,col
        mov cx,row

call putPixel

noKey:
    mov ah,01h                  ; Check for key waiting
    int 16h                     ;
    jnz aKey
    mov al,dir                  ; If no key pressed, go same direction
    mov theimer,9999            ; Change this value for speed of drawing
    stillGo:                    ;
    dec theimer                 ; Stall loop
    jnz stillGo                 ;
    jmp up
    aKey:
    mov ah,0                    ; Get key waiting
    int 16h
    cmp ax,1000h                ; Compare for ALT-Q
    jnz noQuit
    jmp quit
    noQuit:
    mov dir,al                   ; Save key pressed for future ref

up:
    cmp al,'8'
    jnz down
    mov dx,col
    dec dx                       ; Will stop if top of screen
    jnz upOK                     ;
    inc dx                       ;
    upOK:                        ;
    mov col,dx
    jmp keywait
down:
    cmp al,'2'
    jnz left
    mov dx,col
    inc dx
    cmp dx,200                   ;
    jnz downOK                   ; Will stop if right side of screen
    dec dx                       ;
    downOK:                      ;
    mov col,dx
    jmp keywait
left:
    cmp al,'4'
    jnz right
    mov cx,row
    dec cx
    jnz leftOK
    inc cx
    leftOK:
    mov row,cx
    jmp keywait
right:
    cmp al,'6'
    jnz downRight
    mov cx,row
    inc cx
    cmp cx,640
    jnz rightOK
    dec cx
    rightOK:
    mov row,cx
    jmp keywait
downRight:
    cmp al,'3'
    jnz upRight
    mov cx,row
    inc cx
    cmp cx,640
    jnz downRightOK
    dec cx
    downRightOK:
    mov row,cx
    mov dx,col
    inc dx
    cmp dx,200
    jnz downRightOK2
    dec dx
    downRightOK2:
    mov col,dx
    jmp keywait
upRight:
    cmp al,'9'
    jnz upLeft
    mov cx,row
    inc cx
    cmp cx,640
    jnz upRightOK
    dec cx
    upRightOK:
    mov row,cx
    mov dx,col
    dec dx
    jnz upRightOK2
    inc dx
    upRightOK2:
    mov col,dx
    jmp keywait
upLeft:
    cmp al,'7'
    jnz downLeft
    mov cx,row
    dec cx
    jnz upLeftOK
    inc cx
    upLeftOK:
    mov row,cx
    mov dx,col
    dec dx
    jnz upLeftOK2
    inc dx
    upLeftOK2:
    mov col,dx
    jmp keywait
downLeft:
    cmp al,'1'
    jnz colorUp
    mov cx,row
    dec cx
    jnz downLeftOK
    inc cx
    downLeftOK:
    mov row,cx
    mov dx,col
    inc dx
    cmp dx,200
    jnz downLeftOK2
    dec dx
    downLeftOK2:
    mov col,dx
    jmp keywait

colorUp:
    cmp al,'c'
    jz keyWaitHop
    jmp keyWait
    keyWaitHop:
    mov ah,11                 ;
    inc color
    mov bx,color
    int 10h                   ; Changes colors
    mov dir,'5'               ; Leave out this line for special effect!
    jmp keyWait

quit:
    mov ax,4c00h
    int 21h

putPixel proc near             ; Haven't figured out what this all means
                               ; <G>
xor bx,bx
shr dx,1
jnc calc
add bx,8*1024

calc:
mov ax,dx
shl dx,1
shl dx,1
add dx,ax
mov ax,cx
mov cl,4
shl dx,cl
add bx,dx
mov dx,ax
and dx,7
mov cl,3
shr ax,cl
add bx,ax
neg dl
add dl,7
mov cl,dl
mov al,1
shl al,cl
or  es:[bx],al
ret

putPixel endp

code ends
END start
