DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   asm (http://www.daniweb.com/code/asm.html)
-   -   Text Mode Bouncing Ball (http://www.daniweb.com/code/snippet850.html)

bad_dreams99 asm syntax
Apr 19th, 2008
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.

  1. page 60,132
  2. ;--------------------------------------------------------------------------
  3. ; Name: Nathan Gallagher
  4. ; Description: Creates a character (ball) that moves accross the screen, acts as a screensaver
  5. ; will end when a key is pressed
  6. ;--------------------------------------------------------------------------
  7.  
  8.  
  9.  
  10. ;-----------------------------------------------------------------------
  11. ;stack segment
  12.  
  13. sseg segment para stack 'stack'
  14. db 120 dup(?) ;reserve 120 bytes for the stack
  15. sseg ends
  16.  
  17. ;-----------------------------------------------------------------------
  18. ;constants
  19.  
  20.  
  21.  
  22. ;-----------------------------------------------------------------------
  23. ;data segment
  24.  
  25. dseg segment para 'data'
  26.  
  27. ball db 'O' ;create ball character
  28. x db 01h ;create the x-coordinate variable
  29. y db 01h ;create the y-coordinate variable
  30. incX db 01h ;create the variable that moves x
  31. incY db 01h ;create the cariable that moves y
  32. eraser db ' ' ;create an erasor to clear the ball character
  33.  
  34.  
  35. dseg ends
  36.  
  37. ;----------------------------------------------------------------------
  38. ;code segment
  39. cseg segment para 'code'
  40.  
  41. main proc far ;this is the program entry point
  42. assume cs:cseg, ds:dseg, ss:sseg
  43. mov ax,dseg ;load the data segment value
  44. mov ds,ax ;assign value to ds
  45.  
  46. call cursor ;Set up initial cursor
  47. ;this runs until user hits the keyboard
  48. start: mov dl,incX
  49. add dl,x
  50. mov x,dl ;increment X
  51. mov dl,incY
  52. add dl,y
  53. mov y,dl ;increment Y
  54. call cursor ;set up our cursor
  55. ;see if we need to swap anything
  56. call swapX ;swap our X inc value if needed
  57. call swapY ;swap our Y inc value if needed
  58.  
  59. ;time to draw our ball!
  60. mov dl,ball
  61. mov ah,2h
  62. int 21h ;output the ball
  63. ;delay
  64. call wait
  65. ;erase ball
  66. call cursor
  67. mov dl,eraser ;erase the ball
  68. mov ah,2h
  69. int 21h
  70.  
  71. keyboard: mov ah,1 ;loop at end to display results
  72. int 16H
  73. jnz quit ;if a key is pressed, hit the bricks!
  74. jmp start ;jump to start if key isnt hit
  75.  
  76. quit: call cls ;clear screen before returning to DOS
  77. mov ah,4Ch ;set up interupt
  78. int 21h ;Interupt to return to DOS
  79. main endp
  80.  
  81. ;----------------------------------------------
  82. ; Delay process
  83. ;runs through multiple loops to make the wait more potent
  84. ;----------------------------------------------
  85. wait proc
  86. mov cx,12000d
  87. delay1: in al,61h
  88. and al,10h
  89. cmp al,ah
  90. je wait
  91. mov ah,al
  92. loop delay1
  93.  
  94. mov cx,12000d
  95. delay2: in al,61h
  96. and al,10h
  97. cmp al,ah
  98. je wait
  99. mov ah,al
  100. loop delay2
  101.  
  102. mov cx,12000d
  103. delay3: in al,61h
  104. and al,10h
  105. cmp al,ah
  106. je wait
  107. mov ah,al
  108. loop delay3
  109.  
  110. mov cx,12000d
  111. delay4: in al,61h
  112. and al,10h
  113. cmp al,ah
  114. je wait
  115. mov ah,al
  116. loop delay4
  117.  
  118. mov cx,12000d
  119. delay5: in al,61h
  120. and al,10h
  121. cmp al,ah
  122. je wait
  123. mov ah,al
  124. loop delay5
  125. ret
  126. wait endp
  127. ;----------------------------------------------
  128. ;----------------------------------------------------------------------
  129. ;here's a procedure that sets the cursor
  130. ;----------------------------------------------------------------------
  131. cursor proc
  132. mov ah,02 ;load sub function for int
  133. mov bh,00 ;sets page 0
  134. mov dl,x ;sets the column based on x
  135. mov dh,y ;sets the row based on y
  136. int 10h ;calls interupt
  137. ret
  138. cursor endp
  139. ;----------------------------------------------------------------------
  140.  
  141.  
  142. ;Loops for changing incX and incY ------------------------------------------------------------------
  143. swapX proc
  144. cmp x,79d
  145. jge goLeft ;if x is at the limit, jump
  146. cmp x,0d
  147. jle goRight ;if x is at the limit, jump
  148. jmp Xbottom
  149.  
  150. goLeft: mov incX,-1 ;make the ball go left
  151. jmp Xbottom
  152.  
  153. goRight: mov incX,1 ;make the ball go right
  154. jmp Xbottom
  155.  
  156. Xbottom: nop
  157. ret
  158. swapX endp
  159. ;---------------------------------------------------------------------------
  160. swapY proc
  161. cmp y,25d ;if y is at the limit, jump
  162. jge goUp
  163. cmp y,0d
  164. jle goDown ;if y is at the limit, jump
  165. jmp yBottom
  166.  
  167. goUp: mov incY,-1 ;make the ball go up
  168. jmp yBottom
  169.  
  170. goDown: mov incY,1 ;make the ballgo down
  171. jmp yBottom
  172.  
  173. yBottom: nop
  174. ret
  175.  
  176. swapY endp
  177. ;---------------------------------------------------------------------------
  178. ;----------------------------------------------------------------------
  179. ; Here is a procedure that clears the screen
  180. ;----------------------------------------------------------------------
  181. cls proc
  182. mov ax,0600h ;scroll screen
  183. mov bh,07 ;normal attribute
  184. mov cx,0000 ;start at 00,00
  185. mov dx,184fh ;end at 24,79
  186. int 10h ;call int 10
  187. ret
  188. cls endp
  189. ;---------------------------------------------------------------------
  190.  
  191. cseg ends
  192. end main ;Program exit point