Text Mode Bouncing Ball

bad_dreams99 0 Tallied Votes 385 Views Share

This is a very simple program that uses text mode to display a boucing "ball." The ball is just a character that moves across the screen, erasing all in its path. It is coded to work like a screensaver, so the program ends when any key is hit.

NOTE: I am in no ways an assembly guru, if anything i'm a noob; however, if this code helps someone out, then thats great.

This could be good framework for coding in graphics mode. Just replace the ball character with a bit pattern. Anyways, enjoi.

page 60,132
;--------------------------------------------------------------------------
; Name: Nathan Gallagher
; Description:  Creates a character (ball) that moves accross the screen, acts as a screensaver
;		    will end when a key is pressed
;--------------------------------------------------------------------------



;-----------------------------------------------------------------------
;stack segment

sseg      segment para stack 'stack'
          db   120 dup(?)    ;reserve 120 bytes for the stack
sseg      ends

;-----------------------------------------------------------------------
;constants

             

;-----------------------------------------------------------------------
;data segment

dseg      segment para 'data'

          ball db 'O'		;create ball character
		  x db 01h			;create the x-coordinate variable
		  y db 01h			;create the y-coordinate variable
		  incX db 01h		;create the variable that moves x
		  incY db 01h		;create the cariable that moves y
		  eraser db ' '		;create an erasor to clear the ball character
		  

dseg      ends

;----------------------------------------------------------------------
;code segment
cseg      segment para 'code'

main      proc far               ;this is the program entry point
          assume cs:cseg, ds:dseg, ss:sseg 
          mov  ax,dseg           ;load the data segment value
          mov  ds,ax             ;assign value to ds

		call cursor		;Set up initial cursor
		;this runs until user hits the keyboard
start:	mov dl,incX
			add dl,x
			mov x,dl		;increment X
		mov dl,incY
			add dl,y
			mov y,dl		;increment Y
		call cursor			;set up our cursor
		;see if we need to swap anything			
		call swapX				;swap our X inc value if needed
		call swapY				;swap our Y inc value if needed

		;time to draw our ball!		
		mov dl,ball
		mov ah,2h		
		int 21h				;output the ball
		;delay
		call wait		
		;erase ball
		call cursor
		mov dl,eraser		;erase the ball
		mov ah,2h
		int 21h

keyboard:         mov ah,1		;loop at end to display results		 
		  int	16H
		  jnz	quit		;if a key is pressed, hit the bricks!
		  jmp start		 	;jump to start if key isnt hit

quit:     call cls				;clear screen before returning to DOS
		  mov  ah,4Ch            ;set up interupt
          int  21h               ;Interupt to return to DOS
main      endp

;----------------------------------------------
; Delay process
;runs through multiple loops to make the wait more potent
;----------------------------------------------
wait proc		
		mov cx,12000d
delay1:	in al,61h
		and al,10h
		cmp al,ah		
		je wait
		mov ah,al
		loop delay1
		
		mov cx,12000d
delay2:	in al,61h
		and al,10h
		cmp al,ah		
		je wait
		mov ah,al
		loop delay2
		
		mov cx,12000d
delay3:	in al,61h
		and al,10h
		cmp al,ah		
		je wait
		mov ah,al
		loop delay3
		
		mov cx,12000d
delay4:	in al,61h
		and al,10h
		cmp al,ah		
		je wait
		mov ah,al
		loop delay4
		
		mov cx,12000d
delay5:	in al,61h
		and al,10h
		cmp al,ah		
		je wait
		mov ah,al
		loop delay5
		ret
wait endp
;----------------------------------------------
;----------------------------------------------------------------------
;here's a procedure that sets the cursor
;----------------------------------------------------------------------
cursor  proc
		 mov ah,02		;load sub function for int
		 mov bh,00		;sets page 0
		 mov dl,x		;sets the column based on x
		 mov dh,y		;sets the row based on y
		 int 10h		;calls interupt
		 ret
cursor	endp
;----------------------------------------------------------------------


;Loops for changing incX and incY ------------------------------------------------------------------
swapX proc
		cmp x,79d
		jge goLeft			;if x is at the limit, jump
		cmp x,0d
		jle goRight			;if x is at the limit, jump
		jmp Xbottom
		
goLeft:	mov incX,-1			;make the ball go left
		jmp Xbottom
		
goRight: mov incX,1			;make the ball go right
		 jmp Xbottom
		 
Xbottom:	nop
		ret		 
swapX endp
;---------------------------------------------------------------------------
swapY proc
		cmp y,25d			;if y is at the limit, jump
		jge goUp
		cmp y,0d
		jle goDown			;if y is at the limit, jump
		jmp yBottom
		
goUp:	mov incY,-1			;make the ball go up
		jmp yBottom
		
goDown:  mov incY,1			;make the ballgo down
		 jmp yBottom
		 
yBottom: nop	
		ret	
		
swapY endp
;---------------------------------------------------------------------------
;----------------------------------------------------------------------
; Here is a procedure that clears the screen
;----------------------------------------------------------------------
cls		 proc
		 mov ax,0600h	;scroll screen
		 mov bh,07		;normal attribute
		 mov cx,0000	;start at 00,00
		 mov dx,184fh	;end at 24,79
		 int 10h		;call int 10
		 ret
cls		 endp
;---------------------------------------------------------------------		

cseg      ends
          end     main           ;Program exit point