Advertisement
palmerstone

emu

Nov 28th, 2012
1,148
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; this is the screen eating snake game...
  2. ;
  3. ; this game pushes the emulator to its limits,
  4. ; and even with maximum speed it still runs slowly.
  5. ; to enjoy this game it's recommended to run it on real
  6. ; computer, however the emulator can be useful to debug
  7. ; tiny games and other similar programs such as this before
  8. ; they become bug-free and workable.
  9. ;
  10. ; you can control the snake using arrow keys on your keyboard.
  11. ;
  12. ; all other keys will stop the snake.
  13. ;
  14. ; press esc to exit.
  15.  
  16.  
  17. name "snake"
  18.  
  19. org     100h
  20. .data
  21. newline db 10,13,0
  22. ; jump over data section:
  23. ;jmp     start
  24.  
  25. ; ------ data section ------
  26.  
  27. s_size  equ     20
  28. arr_size equ    35
  29. arr2_size equ 200
  30. flag_size equ 35
  31.  
  32. ; the snake coordinates
  33. ; (from head to tail)
  34. ; low byte is left, high byte
  35. ; is top - [top, left]
  36. snake dw s_size dup(0)
  37. x_array db arr_size dup(?)
  38. y_array db arr_size dup(?)
  39. X db arr2_size dup(?)
  40. Y db arr2_size dup(?)
  41. flag db flag_size dup(?)
  42.  
  43. tail    dw      ?
  44. hour db ?
  45. min db ?
  46. sec db ?
  47. mili_sec db ?
  48.  
  49. ; direction constants
  50. ;          (bios key codes):
  51. left    equ     4bh
  52. right   equ     4dh
  53. up      equ     48h
  54. down    equ     50h
  55.  
  56. ; current snake direction:
  57. cur_dir db      right
  58.  
  59. wait_time dw    0
  60. just_stop db 0
  61.  
  62. ; welcome message
  63. msg     db "==== how to play ====", 0dh,0ah
  64.     db "this game was debugged on emu8086", 0dh,0ah
  65.     db "but it is not designed to run on the emulator", 0dh,0ah
  66.     db "because it requires relatively fast video card and cpu.", 0dh,0ah, 0ah
  67.    
  68.     db "if you want to see how this game really works,", 0dh,0ah
  69.     db "run it on a real computer (click external->run from the menu).", 0dh,0ah, 0ah
  70.    
  71.     db "you can control the snake using arrow keys", 0dh,0ah   
  72.     db "all other keys will stop the snake.", 0dh,0ah, 0ah
  73.    
  74.     db "press esc to exit.", 0dh,0ah
  75.     db "====================", 0dh,0ah, 0ah
  76.     db "press any key to start...$"
  77.  
  78. ; ------ code section ------
  79. .code
  80. include 'emu8086.inc'
  81. main proc
  82. start:
  83.  
  84. ; print welcome message:
  85. ;mov dx, offset msg
  86. ;mov ah, 9
  87. ;int 21h
  88.  
  89.  
  90. ; wait for any key:
  91. mov ah, 00h
  92. int 16h
  93.  
  94.  
  95. ; hide text cursor:
  96. mov     ah, 1
  97. mov     ch, 2bh
  98. mov     cl, 0bh
  99. int     10h
  100.  
  101. ;1. print '#' at the location:
  102. mov dh,5
  103. mov y_array[7],dh
  104. mov dl,10
  105. mov x_array[7],dl
  106. mov flag[7],1
  107. mov cx,10
  108. mov ah,2
  109. int 10h
  110. loop1:
  111. push dx
  112. mov dl,'#'
  113. int 21h
  114. pop dx
  115. inc dh
  116. int 10h
  117. loop loop1
  118.  
  119. ;1. print '#' at the location:
  120. mov dh,5
  121. mov y_array[8],dh
  122. mov dl,45
  123. mov x_array[8],dl
  124. mov flag[8],1
  125. mov cx,10
  126. mov ah,2
  127. int 10h
  128. loop2:
  129. push dx
  130. mov dl,'#'
  131. int 21h
  132. pop dx
  133. inc dh
  134. int 10h
  135. loop loop2
  136.  
  137. ;1. print '#' at the location:
  138. mov dh,5
  139. mov y_array[9],dh
  140. mov dl,65
  141. mov x_array[9],dl
  142. mov flag[9],1
  143. mov cx,10
  144. mov ah,2
  145. int 10h
  146. loop3:
  147. push dx
  148. mov dl,'#'
  149. int 21h
  150. pop dx
  151. inc dh
  152. int 10h
  153. loop loop3
  154.  
  155. ;1. print '#' at the location:
  156. mov dh,0
  157. mov y_array[10],dh
  158. mov dl,55
  159. mov x_array[10],dl
  160. mov flag[10],1
  161. mov cx,10
  162. mov ah,2
  163. int 10h
  164. loop4:
  165. push dx
  166. mov dl,'#'
  167. int 21h
  168. pop dx
  169. inc dh
  170. int 10h
  171. loop loop4
  172.  
  173. ;1. print '#' at the location:
  174. mov dh,15    
  175. mov y_array[11],dh
  176. mov dl,55        
  177. mov x_array[11],dl
  178. mov flag[11],1
  179. mov cx,10
  180. mov ah,2
  181. int 10h
  182. loop5:
  183. push dx
  184. mov dl,'#'
  185. int 21h
  186. pop dx
  187. inc dh
  188. int 10h
  189. loop loop5
  190.  
  191. ;1. print '#' at the location:
  192. mov dh,5
  193. mov y_array[12],dh
  194. mov dl,80    
  195. mov x_array[12],dl
  196. mov flag[12],1
  197. mov cx,10
  198. mov ah,2
  199. int 10h
  200. loop6:
  201. push dx
  202. mov dl,'#'
  203. int 21h
  204. pop dx
  205. inc dh
  206. int 10h
  207. loop loop6
  208.  
  209. ;1. print '#' at the location:
  210. mov dh,15
  211. mov y_array[13],dh
  212. mov dl,75    
  213. mov x_array[13],dl
  214. mov flag[13],1
  215. mov cx,10
  216. mov ah,2
  217. int 10h
  218. loop7:
  219. push dx
  220. mov dl,'#'
  221. int 21h
  222. pop dx
  223. inc dh
  224. int 10h
  225. loop loop7
  226.  
  227. ;1. print '#' at the location:
  228. mov dh,0
  229. mov y_array[14],dh
  230. mov dl,75    
  231. mov x_array[14],dl
  232. mov flag[14],1
  233. mov cx,10
  234. mov ah,2
  235. int 10h
  236. loop8:
  237. push dx
  238. mov dl,'#'
  239. int 21h
  240. pop dx
  241. inc dh
  242. int 10h
  243. loop loop8
  244.  
  245. ;1. print '#' at the location:
  246. mov dh,5
  247. mov y_array[15],dh
  248. mov dl,5    
  249. mov x_array[15],dl
  250. mov flag[15],1
  251. mov cx,10
  252. mov ah,2
  253. int 10h
  254. loop9:
  255. push dx
  256. mov dl,'#'
  257. int 21h
  258. pop dx
  259. inc dh
  260. int 10h
  261. loop loop9
  262.  
  263. ;1. print '#' at the location:
  264. mov dh,5
  265. mov y_array[16],dh
  266. mov dl,30    
  267. mov x_array[16],dl
  268. mov flag[16],1
  269. mov cx,10
  270. mov ah,2
  271. int 10h
  272. loop10:
  273. push dx
  274. mov dl,'#'
  275. int 21h
  276. pop dx
  277. inc dh
  278. int 10h
  279. loop loop10
  280.  
  281. ;1. print '#' at the location:
  282. mov dh,15
  283. mov y_array[17],dh
  284. mov dl,25    
  285. mov x_array[17],dl
  286. mov flag[17],1
  287. mov cx,10
  288. mov ah,2
  289. int 10h
  290. loop11:
  291. push dx
  292. mov dl,'#'
  293. int 21h
  294. pop dx
  295. inc dh
  296. int 10h
  297. loop loop11
  298.  
  299. ;1. print '#' at the location:
  300. mov dh,0
  301. mov y_array[18],dh
  302. mov dl,15    
  303. mov x_array[18],dl
  304. mov flag[18],1
  305. mov cx,10
  306. mov ah,2
  307. int 10h
  308. loop12:
  309. push dx
  310. mov dl,'#'
  311. int 21h
  312. pop dx
  313. inc dh
  314. int 10h
  315. loop loop12
  316.  
  317. ;1.set cursor pos:
  318. mov dh,20
  319. mov y_array[21],dh
  320. mov dl,70
  321. mov x_array[21],dl
  322. mov flag[21],0
  323. mov ah,2
  324. int 10h
  325.          
  326. ; print '#' at the location:
  327. mov     al, '#'
  328. mov     ah, 09h
  329. mov     bl, 0ah ; attribute.
  330. mov     cx, 10   ; single char.
  331. int     10h
  332.  
  333. ;1.set cursor pos:
  334. mov dh,10
  335. mov y_array[22],dh
  336. mov dl,70
  337. mov x_array[22],dl
  338. mov flag[22],0
  339. mov ah,2
  340. int 10h
  341.          
  342. ; print '#' at the location:
  343. mov     al, '#'
  344. mov     ah, 09h
  345. mov     bl, 0ah ; attribute.
  346. mov     cx, 10   ; single char.
  347. int     10h
  348.  
  349. ;1.set cursor pos:
  350. mov dh,20
  351. mov y_array[23],dh
  352. mov dl,55
  353. mov x_array[23],dl
  354. mov flag[23],0
  355. mov ah,2
  356. int 10h
  357.          
  358. ; print '#' at the location:
  359. mov     al, '#'
  360. mov     ah, 09h
  361. mov     bl, 0ah ; attribute.
  362. mov     cx, 10   ; single char.
  363. int     10h
  364.  
  365. ;1.set cursor pos:
  366. mov dh,20
  367. mov y_array[24],dh
  368. mov dl,40
  369. mov x_array[24],dl
  370. mov flag[24],0
  371. mov ah,2
  372. int 10h
  373.          
  374. ; print '#' at the location:
  375. mov     al, '#'
  376. mov     ah, 09h
  377. mov     bl, 0ah ; attribute.
  378. mov     cx, 10   ; single char.
  379. int     10h
  380.  
  381. ;1.set cursor pos:
  382. mov dh,25
  383. mov y_array[25],dh
  384. mov dl,15
  385. mov x_array[25],dl
  386. mov flag[25],0
  387. mov ah,2
  388. int 10h
  389.          
  390. ; print '#' at the location:
  391. mov     al, '#'
  392. mov     ah, 09h
  393. mov     bl, 0ah ; attribute.
  394. mov     cx, 10   ; single char.
  395. int     10h
  396.  
  397. ;1.set cursor pos:
  398. mov dh,25
  399. mov y_array[26],dh
  400. mov dl,0
  401. mov x_array[26],dl
  402. mov flag[26],0
  403. mov ah,2
  404. int 10h
  405.          
  406. ; print '#' at the location:
  407. mov     al, '#'
  408. mov     ah, 09h
  409. mov     bl, 0ah ; attribute.
  410. mov     cx, 10   ; single char.
  411. int     10h
  412.  
  413. ;1.set cursor pos:
  414. mov dh,25
  415. mov y_array[27],dh
  416. mov dl,30
  417. mov x_array[27],dl
  418. mov flag[27],0
  419. mov ah,2
  420. int 10h
  421.          
  422. ; print '#' at the location:
  423. mov     al, '#'
  424. mov     ah, 09h
  425. mov     bl, 0ah ; attribute.
  426. mov     cx, 10   ; single char.
  427. int     10h
  428.  
  429. ;1.set cursor pos:
  430. mov dh,10
  431. mov y_array[0],dh
  432. mov dl,20
  433. mov x_array[0],dl
  434. mov flag[0],0
  435. mov ah,2
  436. int 10h
  437.          
  438. ; print '#' at the location:
  439. mov     al, '#'
  440. mov     ah, 09h
  441. mov     bl, 0ah ; attribute.
  442. mov     cx, 10   ; single char.
  443. int     10h
  444.  
  445. ;1.set cursor pos:
  446. mov dh,15
  447. mov y_array[19],dh
  448. mov dl,0
  449. mov x_array[19],dl
  450. mov flag[19],0
  451. mov ah,2
  452. int 10h
  453.          
  454. ; print '#' at the location:
  455. mov     al, '#'
  456. mov     ah, 09h
  457. mov     bl, 0ah ; attribute.
  458. mov     cx, 10   ; single char.
  459. int     10h
  460.  
  461. ;1.set cursor pos:
  462. mov dh,13
  463. mov y_array[20],dh
  464. mov dl,10
  465. mov x_array[20],dl
  466. mov flag[20],0
  467. mov ah,2
  468. int 10h
  469.          
  470. ; print '#' at the location:
  471. mov     al, '#'
  472. mov     ah, 09h
  473. mov     bl, 0ah ; attribute.
  474. mov     cx, 10   ; single char.
  475. int     10h
  476.  
  477. ;3.set cursor pos:
  478. mov dh,15
  479. mov y_array[1],dh
  480. mov dl,30
  481. mov x_array[1],dl
  482. mov flag[1],0
  483. mov ah,2
  484. int 10h
  485.          
  486. ; print '#' at the location:
  487. mov     al, '#'
  488. mov     ah, 09h
  489. mov     bl, 0ah ; attribute.
  490. mov     cx, 10   ; single char.
  491. int     10h
  492.  
  493. ;5.set cursor pos:
  494. mov dh,5        
  495. mov y_array[2],dh
  496. mov dl,30
  497. mov x_array[2],dl
  498. mov flag[2],0
  499. mov ah,2
  500. int 10h
  501.          
  502. ; print '#' at the location:
  503. mov     al, '#'
  504. mov     ah, 09h
  505. mov     bl, 0ah ; attribute.
  506. mov     cx, 10   ; single char.
  507. int     10h
  508.  
  509. ;7.set cursor pos:
  510. mov dh,20  
  511. mov y_array[3],dh
  512. mov dl,10
  513. mov x_array[3],dl
  514. mov flag[3],0
  515. mov ah,2
  516. int 10h
  517.          
  518. ; print '#' at the location:
  519. mov     al, '#'
  520. mov     ah, 09h
  521. mov     bl, 0ah ; attribute.
  522. mov     cx, 10   ; single char.
  523. int     10h
  524.  
  525. ;11.set cursor pos:
  526. mov dh,15        
  527. mov y_array[4],dh
  528. mov dl,15
  529. mov x_array[4],dl
  530. mov flag[4],0
  531. mov ah,2
  532. int 10h
  533.          
  534. ; print '#' at the location:
  535. mov     al, '#'
  536. mov     ah, 09h
  537. mov     bl, 0ah ; attribute.
  538. mov     cx, 10   ; single char.
  539. int     10h
  540.  
  541. ;11.set cursor pos:
  542. mov dh,15    
  543. mov y_array[5],dh
  544. mov dl,60      
  545. mov x_array[5],dl
  546. mov flag[5],0
  547. mov ah,2
  548. int 10h
  549.          
  550. ; print '#' at the location:
  551. mov     al, '#'
  552. mov     ah, 09h
  553. mov     bl, 0ah ; attribute.
  554. mov     cx, 10   ; single char.
  555. int     10h
  556.  
  557. ;11.set cursor pos:
  558. mov dh,25
  559. mov y_array[6],dh
  560. mov dl,65
  561. mov x_array[6],dl
  562. mov flag[6],0
  563. mov ah,2
  564. int 10h
  565.          
  566. ; print '#' at the location:
  567. mov     al, '#'
  568. mov     ah, 09h
  569. mov     bl, 0ah ; attribute.
  570. mov     cx, 10   ; single char.
  571. int     10h
  572.  
  573. ;set cursor pos:
  574. ;mov dh,15
  575. ;mov dl,0
  576. ;mov ah,2
  577. ;int 10h
  578.  
  579. game_loop:
  580.  
  581. push ax
  582. mov ah, 0
  583. mov al, just_stop
  584. cmp al, 1
  585. je gtfo
  586. pop ax
  587.  
  588. mov ah,2ch
  589. int 21h
  590. mov hour,ch
  591. mov min,cl
  592. mov sec,dh
  593. mov mili_sec,dl
  594. ; === select first video page
  595. mov     al, 0  ; page number.
  596. mov     ah, 05h
  597. int     10h
  598.  
  599. ; === show new head:
  600. mov     dx, snake[0]
  601.  
  602. ; set cursor at dl,dh
  603. mov     ah, 02h
  604. int     10h
  605.  
  606. ; print '*' at the location:
  607. mov     al, '*'
  608. mov     ah, 09h
  609. mov     bl, 0eh ; attribute.
  610. mov     cx, 1   ; single char.
  611. int     10h
  612.  
  613. ; === keep the tail:
  614. mov     ax, snake[s_size * 2 - 2]
  615. mov     tail, ax
  616.  
  617. call    move_snake
  618.  
  619.  
  620. ; === hide old tail:
  621. mov     dx, tail
  622.  
  623. ; set cursor at dl,dh
  624. mov     ah, 02h
  625. int     10h
  626.  
  627. ; print ' ' at the location:
  628. mov     al, ' '
  629. mov     ah, 09h
  630. mov     bl, 0eh ; attribute.
  631. mov     cx, 1   ; single char.
  632. int     10h
  633.  
  634.  
  635.  
  636. check_for_key:
  637.  
  638. ; === check for player commands:
  639. mov     ah, 01h
  640. int     16h
  641. jz      no_key
  642.  
  643. mov     ah, 00h
  644. int     16h
  645.  
  646. cmp     al, 1bh    ; esc - key?
  647. je      stop_game  ;
  648.  
  649. mov     cur_dir, ah
  650.  
  651. no_key:
  652.  
  653.  
  654.  
  655. ; === wait a few moments here:
  656. ; get number of clock ticks
  657. ; (about 18 per second)
  658. ; since midnight into cx:dx
  659. mov     ah, 00h
  660. int     1ah
  661. add dx,200
  662. cmp     dx, wait_time
  663. jb      check_for_key
  664. add     dx, 4
  665. mov     wait_time, dx
  666.  
  667.  
  668.  
  669. ; === eternal game loop:
  670. jmp     game_loop
  671.  
  672.  
  673. stop_game:
  674. ; show cursor back:
  675. mov     ah, 1
  676. mov     ch, 0bh
  677. mov     cl, 0bh
  678. int     10h
  679.  
  680. main endp
  681. ret
  682.  
  683. DEFINE_PRINT_STRING
  684. DEFINE_SCAN_NUM
  685. DEFINE_PRINT_NUM
  686. DEFINE_PRINT_NUM_UNS
  687.  
  688. ; ------ functions section ------
  689.  
  690. ; this procedure creates the
  691. ; animation by moving all snake
  692. ; body parts one step to tail,
  693. ; the old tail goes away:
  694. ; [last part (tail)]-> goes away
  695. ; [part i] -> [part i+1]
  696. ; ....
  697.  
  698. move_snake proc near
  699.  
  700. ; set es to bios info segment:  
  701. mov     ax, 40h
  702. mov     es, ax
  703.  
  704.  ; point di to tail
  705.  mov   di, s_size * 2 - 2
  706.  ; move all body parts
  707.  ; (last one simply goes away)
  708.  mov   cx, s_size-1
  709. move_array:
  710.  mov   ax, snake[di-2]
  711.  mov   snake[di], ax
  712.  sub   di, 2
  713.  loop  move_array
  714.  
  715.  
  716. cmp     cur_dir, left
  717.  je    move_left
  718. cmp     cur_dir, right
  719.  je    move_right
  720. cmp     cur_dir, up
  721.  je    move_up
  722. cmp     cur_dir, down
  723.  je    move_down
  724.  
  725. jmp     stop_move       ; no direction.
  726.  
  727.  
  728. move_left:
  729.  mov   al, b.snake[0]
  730.  dec   al
  731.  mov   b.snake[0], al
  732.  cmp   al, -1
  733.  jne   lool      
  734.  mov   al, es:[4ah]    ; col number.
  735.  dec   al
  736.  mov   b.snake[0], al  ; return to right.
  737.  lool:
  738.  
  739.  push cx
  740.  mov cx,28
  741.  push si
  742.  push di
  743.  push bp
  744.  mov si, offset x_array
  745.  mov di, offset y_array
  746.  
  747.  pool1:
  748.  push cx
  749.  mov cx, 10
  750.  
  751.  current_x db 0
  752.  push ax
  753.  mov ah, 0
  754.  mov al, [si]
  755.  mov current_x, al
  756.  
  757.  
  758.  current_y db 0
  759.  mov ah, 0
  760.  mov al, [di]
  761.  mov current_y, al
  762.  pop ax
  763.  
  764.  push bp
  765.  mov bp, offset flag
  766.  
  767.  nested_loop:
  768.  
  769.  push ax
  770.  mov ah, current_x
  771.  mov al, b.snake[0]
  772.  cmp ah, al
  773.  
  774.  jne skip14
  775.  push bx
  776.  mov bh, current_y
  777.  mov bl, b.snake[1]
  778.  cmp bh, bl
  779.  
  780.  jne skip16
  781.  pop bx
  782.  mov just_stop, 1
  783.  jmp skip17
  784.  
  785.  skip16:
  786.  pop bx
  787.  skip17:
  788.  
  789.  
  790.  skip14:
  791.  pop ax
  792.  
  793.  push ax
  794.  mov ah, 0
  795.  mov al, [bp]
  796.  cmp al, 1
  797.  jne skip156
  798.  dec current_y
  799.  jmp skip666  
  800.  skip156:
  801.  inc current_x
  802.  skip666:
  803.  
  804.  inc bp
  805.  pop ax
  806.  
  807.    
  808.  loop nested_loop
  809.  
  810.  pop bp
  811.  pop cx
  812.  inc si
  813.  inc di
  814.  
  815.  
  816.  loop pool1
  817.  pop di
  818.  pop si
  819.  pop cx
  820.  
  821.  jmp   stop_move
  822.  
  823. move_right:
  824.  mov   al, b.snake[0]
  825.  inc   al
  826.  mov   b.snake[0], al
  827.  cmp   al, es:[4ah]    ; col number.  
  828.  jb    stop_move
  829.  mov   b.snake[0], 0   ; return to left.
  830.  jmp   stop_move
  831.  
  832. move_up:
  833.  mov   al, b.snake[1]
  834.  dec   al
  835.  mov   b.snake[1], al
  836.  cmp   al, -1
  837.  jne   stop_move
  838.  mov   al, es:[84h]    ; row number -1.
  839.  mov   b.snake[1], al  ; return to bottom.
  840.  jmp   stop_move
  841.  
  842. move_down:
  843.  mov   al, b.snake[1]
  844.  inc   al
  845.  mov   b.snake[1], al
  846.  cmp   al, es:[84h]    ; row number -1.
  847.  jbe   stop_move
  848.  mov   b.snake[1], 0   ; return to top.
  849.  jmp   stop_move
  850.  
  851. stop_move:
  852.  ret
  853. move_snake endp
  854.  
  855.  
  856. gtfo:
Advertisement
RAW Paste Data Copied
Advertisement