comment +

For compilation  use:

Turbo  Assembler 2.02
tasm /m2 ball.asm
tlink /t ball.obj

code: bitl/7dump

comment end! +

.model tiny
.data


.code
.386
org 0100h

start:
     mov al, 13h              ; set VGA 320x200x8bpp
     int 10h

     add dh, al
     mov fs, dx

     add dh, al
     mov gs, dx

     add dh, al
     mov ds, dx

     push 0a000h
     pop es


     ;xor bx, bx                        ; set palette
     mov ch, 7
     mov cl, 10
     xor dx, dx
     mov dl, 2
@pal1:
     mov ax, 1010h           ; cl=blue, ch=green, dh=red
     int 10h
     inc bx

     mov al, bl
     cmp al, 64
     jna @pal2
     mov dl, 7
     sub al, 64
@pal2:
     shr al, 1
     mov cl, al
     mov ch, al
     mov dh, al

     add dh, dl
     add ch, dl

     cmp bx, 128
     jl @pal1


  xor di, di
  mov cx, 128*128
  @textloop:
     mov ax, cx
     mov bl, 128
     div bl
     xor al, ah
     mov dl, 127
     shr dl, 1
     and ax, dx
     mov ah, al
     shl ah, 1
     and ah, 64
     add al, 4
     mov gs:[di], ax
     inc di
     inc di
  loop @textloop




   mov bp, 64   ;R
   xor di, di
   mov si, 128

@l1:  ;{for y:=0 to 127 do begin }

   mov ax, bp  ;{dy:=R-y;}
   sub ax, si
   mul ax      ;{dy:=dy*dy;}
   xchg bx, ax

   mov cx, 128 ;mov X, ax ;{x:=127;}

   @l2: ;{for x:=0 to 127 do begin}

     mov ax, bp

     sub ax, cx ;word ptr [esp] ;X  ;{dx:=R-x;}
     mul ax     ;{M:=dx*dx;}
     add ax, bx ;{M:=M+dy;}

     mov ds:[di], word ptr 0

     cmp ax, 4096  ;{if M < R*R then begin}
     jae @skip

        ;{Z:=byte(255-M div (R div 16));}
        shr ax, 4
        mov dx, 255
        sub dx, ax

        mov ds:[di], dl ;{mem[map:n]:=Z;}

        ;{light-map}
        cmp dl, 54
        jna @skip3
        mov ds:[di+1+16-128*16], dl
        @skip3:

        shr dx, 2       ;{Z:=Z div 4;}
        add dx, bp      ;{Z:=Z+R;}

        mov ax, cx      ;{Xt:=(X-R)*R div Z;}
        sub ax, bp

        shl ax, 6  ;cl
        idiv dl
        mov dh, al      ;{=dh}

        mov ax, si      ;{Yt:=(Y-R)*R div Z;}
        sub ax, bp
        shl ax, 6
        idiv dl         ;{=ax}

        shr dx, 8       ;{C:= (Xt+r) + (Yt+r)*128;}

        add dl, 64
        add al, 64
        shl ax, 7
        add ax, dx

        mov ds:[di+128*128*2], ax

     @skip:
     inc di
     inc di
     loop @l2
dec si
jnz @l1



;**********************************************************************
; MAIN LOOP
;**********************************************************************
        push si  ; THETA

        push si
        push si
        inc si
        push si
        push si

        fninit

@mainloop:

        mov bp, sp

        add word ptr [bp+8], 5 ; rotate (THETA)

   ;----------------------------------------------
        pop ax ; XV
        pop bx ; YV
        pop cx ; XC
        pop di ; YC

        add cx, ax
        add di, bx

        cmp di, 199-128
        jna @test1
        neg bx
@test1:
        cmp cl, 319-128
        jna @test2
        neg ax
@test2:
        push di
        push cx
        push bx
        push ax
   ;----------------------------------------------

        mov ax, di
        shl ax, 7                      ; {XY:=xc+yc*R2;}
        add ax, cx
        push ax                        ; {XY} esp +10

        imul di, di, 320               ; {m:=word(320*yc+xc);}
        add di, cx


        ;{sscale := round(sin(rad(theta))*256);}
        ;{cscale := round(cos(rad(theta))*256);}
        push 256
        fild word ptr [bp-4] ; 256
        fild word ptr [bp+8] ; THETA
        fdiv st, st(1)
        fcos
        fldpi
        fmulp st(1),st

        push ax
        fsincos
        fmul st, st(2)  ; * 256
        fistp word ptr [bp-6]  ; CSCALE
        pop cx
        fmulp st(1), st ; * 256
        fistp word ptr [bp-4]  ; SSCALE
        pop bx

        push cx                ; esp + 8  (CSCALE)
        push bx                ; esp + 6  (SSCALE)

        ;{ xq:=R shl 8 - (R * (cscale+sscale));}
        mov ax, bx
        add ax, cx
        mov si, 64  ;R
        mul si
        shl si, 8
        mov dx, si
        sub dx, ax
        push dx               ; esp +4  (XQ)

        ;{ yq:=R shl 8 - (R * (cscale-sscale));}
        sub bx, cx
        imul bx, bx, 64
        sub si, bx
        push si               ; esp +2  (YQ)

        push 128              ; esp +0 (YY=128)

        xor si, si

        @draw:  ;{for y:=0 to YY do begin}


            mov ax, [bp+4]       ;XQ   ;{ xtemp:=xq;}
            mov cx, [bp+2]       ;YQ   ;{ ytemp:=yq;}
            mov bp, [bp+10]      ;BP=XY

            @drawline:  ;{for x:=0 to XX-1 do begin}

                mov dx, ds:[si]  ;{ C_L:=memw[map:n shl 1];}

                test dl, dl ;{if C>Z then}
                jz @drawskip

                ;mov dl, ah    ; { xtr:=byte(xtemp shr 8); }
                mov bl, ch    ; { ytr:=byte(ytemp shr 8); }

                ;{ UV:=memw[map:xtr+ytr*r2 *2   +128*128*2]; }
                shl bx, 7
                add bl, ah
                shl bx, 1
                mov bx, ds:[bx + 128*128*2] ;map {UV}

                sub bx, bp  ;{UV-XY}

                ;{ T:=memw[texture:word((UV+XY) shl 1) and (R2*R2*2-1)];}
                shl bx, 1
                and bx, 128*128*2-1
                mov bx, gs:[bx] ;{mov bx, gs:[bx] ; texture}

                add bl, dh  ;{ add light-map value }
                sbb dh, dh  ;{ clamping (if AL>255 then AL=255) }
                or bl, dh

                shr bl, 2
                add bl, bh  ;{add color plane}

                ;{mem[buf:m]:=byte(C);}
                mov fs:[di], bl

                @drawskip:
                add ax, [esp+6] ;CSCALE   ;{xtemp:=xtemp+cscale;}
                sub cx, [esp+8] ;SSCALE   ;{ytemp:=ytemp-sscale;}

                inc si  ; {inc(n);}
                inc si
                inc di  ; {inc(m);}

            test si, 255
            jnz @drawline

        mov bp, sp

        mov ax, [bp+8] ;SSCALE
        add [bp+4], ax ;{xq:=xq+sscale;}

        mov ax, [bp+6] ;CSCALE
        add [bp+2], ax ;{yq:=yq+cscale;}

        add di, 192

        dec word ptr [bp] ;YY
        jnz @draw


add sp, 6*2  ; reset stack pointer



; Wait for vertical retrace
     mov  dx, 3DAh
@w2:
     in   al, dx
     test al, 8
     jz   @w2


     mov ch, 125
     xor di, di
     xor si, si
     xor ax, ax
     @l:
        db 64h
        movsw
        mov fs:[si-2], ax
     loop @l


     in al, 60h               ; wait for ESC
     dec ax
jnz @mainloop                 ; none received, start over

;**********************************************************************
; EXIT
;**********************************************************************
     mov ax, 3                ; back to text
     int 10h                  ; mode
     int 20h                  ; exit

end start
