fork download
  1. ;Game of NIM -- Pooya
  2. ;******Some defs******
  3. prnt_msg equ 09h
  4. cr equ 0dh
  5. nl equ 0ah
  6. _in equ 01h
  7. _out equ 0eh
  8. spc equ 2 dup (' ')
  9. str equ 0b1h, 0b2h
  10.  
  11. ;*****Color Table*****
  12. black equ 00h
  13. blue equ 01h
  14. green equ 02h
  15. cyan equ 03h
  16. red equ 04h
  17. magenta equ 05h
  18. brown equ 06h
  19. light_gray equ 07h
  20. dark_gray equ 08h
  21. light_blue equ 09h
  22. light_green equ 0ah
  23. light_cyan equ 0bh
  24. light_red equ 0ch
  25. light_magenta equ 0dh
  26. yellow equ 0eh
  27. white equ 0fh
  28. ;*********************
  29.  
  30. .MODEL SMALL
  31.  
  32. ;******Stack Segment******
  33. .STACK
  34.  
  35. dw 128 dup (?)
  36. ;*************************
  37.  
  38. ;******Data Segement******
  39. .DATA
  40.  
  41. star db cr, nl, spc, str, '$'
  42. star2 db cr, nl, spc, str, 32 dup (' '), str, '$'
  43. space db spc, '$'
  44. nim_desc db spc, spc, spc, spc
  45. db 'THE GAME OF NIM'
  46. db cr, nl, nl, '$'
  47.  
  48. pilesAsk db spc
  49. db 'How many piles? (No more than 5)'
  50. db cr, nl, '$'
  51.  
  52. wrong db cr, nl, nl
  53. db spc
  54. db 'Your input is wrong'
  55. db cr, nl, '$'
  56.  
  57. sticks db cr, nl, nl
  58. db spc
  59. db , 'How many sticks in ', '$'
  60. sticks2 db ' pile? (no more than 9)'
  61. db cr, nl, '$'
  62.  
  63. pile_num1 db cr, spc, str, '$'
  64.  
  65. pile_num2 db nl, cr
  66. db spc, str, spc
  67. db 'Pile #', '$'
  68.  
  69. who_first db cr, nl, nl
  70. db spc
  71. db 'Who plays first?'
  72. db cr, nl
  73. db spc
  74. db '0 = Computer, 1 = Player'
  75. db cr, nl, nl, '$'
  76.  
  77. computer_think db cr, nl, nl
  78. db 'Hmm... What should I pick now...?'
  79. db cr, nl, '$'
  80.  
  81. computer_pick db cr, nl
  82. db 'I picked ', '$'
  83. computer_pick2 db ' sticks from pile #', '$'
  84.  
  85. first db -1
  86. piles dw 0
  87. pile1 db 5 dup (?)
  88.  
  89. shape db 0f4h, 0f5h, '$'
  90. new db cr, nl, '$'
  91.  
  92. pick_pile db cr, nl, nl
  93. db spc
  94. db 'Please pick one pile.'
  95. db cr, nl, nl, '$'
  96.  
  97. pick_stick_ db cr, nl, nl
  98. db spc
  99. db 'How many sticks to remove? '
  100. db '(Zero, or numbers greater than ', '$'
  101.  
  102. pick_stick__ db ' are illegal.)'
  103. db cr, nl, '$'
  104.  
  105. iwin db cr, nl
  106. db spc
  107. db 'I WON!!'
  108. db cr, nl, '$'
  109.  
  110. uwin db cr, nl
  111. db spc
  112. db 'YOU WON!!'
  113. db cr, nl, '$'
  114.  
  115.  
  116. top db cr, nl
  117. db spc
  118. db 17 dup (str), 0b1h, 0b2h, nl, '$'
  119.  
  120. again_ul db cr, nl, spc
  121. db 'Do you want to play again?'
  122. db cr, nl, spc
  123. db 'Maybe this time you could beat me? (Y/n)'
  124. db cr, nl, '$'
  125.  
  126. again_cl db cr, nl, spc
  127. db 'Do you want to play again?'
  128. db cr, nl, spc
  129. db 'This time I will win for sure! (Y/n)'
  130. db cr, nl, '$'
  131.  
  132. win_flag db -1
  133.  
  134. gState db 0
  135. computer_move db 0
  136. rand dw 0
  137. pile_choice dw 0
  138. sum db 1
  139. err db cr, spc
  140. db 'You need to have at least two piles'
  141. db cr, nl, '$'
  142.  
  143. temp dw 0
  144. temp2 db 24h
  145. ;*************************
  146.  
  147. ;******Code Segemnt*******
  148. .CODE
  149. .STARTUP
  150. ;**********Macro**********
  151. set_color macro par ;macro to set a color for printing
  152. pusha
  153. mov bl, par
  154. mov ah, 9
  155. mov al, 0
  156. int 10h
  157. popa
  158. endm
  159. ;************************
  160.  
  161. start:
  162. ;set video mode
  163. mov ah, 0h
  164. mov al, 03h
  165. int 10h
  166. ;======================
  167.  
  168. set_color light_gray ; change color for second run (if i don't add this line,
  169. ; in second run (after play_again proc) some of character
  170. ; are colored in a random manner
  171.  
  172. ;print description
  173. lea dx, nim_desc
  174. call print_msg
  175. ;======================
  176.  
  177. ;ask for the number of piles
  178. lea dx, pilesAsk
  179. call print_msg
  180. ;===========================
  181.  
  182.  
  183. ;get piles:
  184. jmp get_pile
  185. wrong_pile:
  186. lea dx, wrong
  187. call print_msg
  188.  
  189. jmp get_pile
  190. wrong_pile2:
  191. lea dx, wrong
  192. call print_msg
  193. lea dx, err
  194. call print_msg
  195.  
  196. get_pile:
  197. call get_ch
  198. sub al, 30h
  199. cmp al, 5
  200. jg wrong_pile
  201. cmp al, 0
  202. jz wrong_pile
  203. cmp al, 1
  204. jz wrong_pile2
  205. mov b. piles, al
  206. ;===========================
  207.  
  208. mov cl, '1'
  209. mov si, 0
  210.  
  211.  
  212. jmp input
  213. wrong_stick:
  214. lea dx, wrong
  215. call print_msg
  216. dec cl
  217.  
  218. input:
  219.  
  220. ;ask for number of sticks each pile contains:
  221. lea dx, sticks
  222. call print_msg
  223.  
  224. mov al, cl
  225. call print_ch
  226.  
  227. mov dx, offset sticks2
  228. call print_msg
  229. inc cl
  230. ;==========================
  231.  
  232. ;get sticks:
  233. xor al, al
  234. call get_ch
  235. sub al, 30h
  236. cmp al, 0
  237. jz wrong_stick
  238. mov pile1[si], al
  239. inc si
  240. cmp si, piles
  241. jb input
  242. ;==========================
  243.  
  244. ;print pile
  245. call print_pile
  246. ;==========================
  247.  
  248. ;prompt user to choose who plays first (machine or user):
  249. call who_turn
  250. ;==========================
  251.  
  252. turn:
  253. cmp first, 0 ;compare to know who played first
  254. jnz computer_next
  255.  
  256. player_next:
  257. call print_pile
  258. call user_turn
  259. cmp win_flag, 0
  260. jz closing
  261. call print_pile
  262. call computer_turn
  263. cmp win_flag, 1
  264. je closing
  265. jmp player_next
  266.  
  267. jmp closing
  268.  
  269. computer_next:
  270. call print_pile
  271. call computer_turn
  272. cmp win_flag, 1
  273. je closing
  274. call print_pile
  275. call user_turn
  276. cmp win_flag, 0
  277. jz closing
  278. jmp computer_next
  279.  
  280.  
  281. closing:
  282. call play_again
  283. jmp exit
  284.  
  285.  
  286. who_turn proc
  287.  
  288. lea dx, who_first
  289. call print_msg
  290.  
  291. call get_ch
  292. sub al,30h
  293. cmp al, 0
  294. mov first, al
  295. jnz label3
  296.  
  297. call computer_turn
  298.  
  299. jmp fin
  300.  
  301. label3:
  302. call user_turn
  303.  
  304. fin:
  305. ret
  306. who_turn endp
  307.  
  308. user_turn proc
  309.  
  310. jmp pick_
  311. wrong_:
  312. lea dx, wrong
  313. call print_msg
  314.  
  315. pick_:
  316. lea dx, pick_pile
  317. call print_msg
  318.  
  319. call get_ch
  320. dec al
  321. sub al, 30h
  322. mov b. pile_choice, al
  323.  
  324. xor di, di
  325. mov di, pile_choice
  326. cmp pile1[di], 0
  327. jz wrong_
  328. cmp al, b. piles
  329. jg wrong_
  330.  
  331.  
  332. mov dx, offset pick_stick_
  333. call print_msg
  334.  
  335. mov si, pile_choice
  336. mov al, pile1[si]
  337. add al, 30h
  338. call print_ch
  339.  
  340.  
  341. lea dx, pick_stick__
  342. call print_msg
  343.  
  344. jmp label2
  345. wrong_choice2:
  346. lea dx, wrong
  347. call print_msg
  348.  
  349. label2:
  350. call get_ch
  351. sub al, 30h
  352. cmp al, pile1[si]
  353. jg wrong_choice2
  354. cmp al, 0
  355. jz wrong_choice2
  356.  
  357. sub pile1[si], al
  358. call get_sum
  359. cmp sum, 0
  360. jz u_win
  361.  
  362. jmp label5
  363. u_win:
  364. mov win_flag, 0
  365. lea dx, uwin
  366. call print_msg
  367.  
  368. label5:
  369. ret
  370. user_turn endp
  371.  
  372. computer_turn proc
  373. lea dx, computer_think
  374. call print_msg
  375. call is_kernel
  376. cmp gState, 0
  377. jz pick
  378.  
  379. call make_kernel
  380. call print_move
  381. call get_sum
  382. cmp sum, 0
  383. jz i_win
  384.  
  385. jmp label1
  386.  
  387. pick:
  388. call pick_stick
  389. call print_move
  390. call get_sum
  391. cmp sum, 0
  392. jz i_win
  393.  
  394. jmp label1
  395.  
  396. i_win:
  397. call print_pile
  398. mov win_flag, 1
  399. lea dx, iwin
  400. call print_msg
  401.  
  402. label1:
  403.  
  404. ret
  405. computer_turn endp
  406.  
  407. print_move proc ;prints computer's move
  408. pusha
  409. lea dx, computer_pick
  410. call print_msg
  411. mov al, computer_move
  412. add al, 30h
  413. call print_ch
  414. lea dx, computer_pick2
  415. call print_msg
  416. mov al, b. rand
  417. inc al
  418. add al, 30h
  419. call print_ch
  420. popa
  421. ret
  422. print_move endp
  423.  
  424. get_sum proc ; get sum of sticks. if there is not more sticks,
  425. ; player who's made the last move is the winner
  426. pusha
  427. mov cx, piles
  428. xor si, si
  429. xor al, al
  430.  
  431. label4:
  432. add al, pile1[si]
  433. inc si
  434. loop label4
  435. mov sum, al
  436. popa
  437. ret
  438. get_sum endp
  439.  
  440. is_empty proc ; check if a pile is empty so user can't choose that one
  441. pusha
  442. mov al, pile1[si]
  443. or al, al
  444. popa
  445. ret
  446. is_empty endp
  447.  
  448. pick_stick proc ;pick one stick from a random pile when status is kernel
  449. pusha
  450. label:
  451. call rand_gen
  452. mov si, rand
  453. call is_empty
  454. jz label
  455. sub pile1[si], 1
  456. mov computer_move, 1
  457. popa
  458. ret
  459. pick_stick endp
  460.  
  461. rand_gen proc ;generates a random number -- not a good one, but does the job
  462. pusha
  463. mov ah, 0h
  464. int 1ah
  465. mov ax, dx
  466. xor dx, dx
  467. mov cx, piles
  468. div cx
  469. mov rand, dx
  470.  
  471. popa
  472. ret
  473. rand_gen endp
  474.  
  475. is_kernel proc ;check if game status is kernel
  476. pusha
  477. mov cx, piles
  478. mov al, 0
  479. xor si, si
  480.  
  481. _xor:
  482. xor al, pile1[si]
  483. inc si
  484. loop _xor
  485.  
  486. mov gState, al
  487. popa
  488. ret
  489. is_kernel endp
  490.  
  491. make_kernel proc ; make game status kernel. (computer's move)
  492.  
  493. jmp xor_
  494. wrong_choice:
  495. xchg bl, pile1[di]
  496.  
  497. xor_:
  498. call rand_gen
  499. mov di, rand
  500.  
  501. cmp pile1[di], 0
  502. jz xor_
  503.  
  504. mov bl, gState
  505. xor bl, pile1[di]
  506. xchg bl, pile1[di]
  507.  
  508. cmp pile1[di], bl
  509. jge wrong_choice
  510.  
  511. sub bl, pile1[di]
  512. mov computer_move, bl
  513.  
  514. ret
  515. make_kernel endp
  516.  
  517.  
  518. print_pile proc ;print the piles. this is where my problem lies.
  519. pusha
  520.  
  521. mov al, nl ; print a new line
  522. call print_ch
  523. lea dx, top ;print top of border
  524. call print_msg
  525. xor si, si
  526. mov cx, piles
  527. xor bl, bl
  528.  
  529. pile_loop: ;print pile number
  530. lea dx, pile_num1
  531. call print_msg
  532. call set_cursor
  533. lea dx, pile_num2
  534. call print_msg
  535. mov temp, si
  536. mov al, b. temp
  537. inc al
  538. add al, 30h
  539. call print_ch
  540.  
  541. mov al, ' ' ;print sticks in each pile in numbers
  542. call print_ch
  543. mov al, '('
  544. call print_ch
  545. mov al, pile1[si]
  546. add al, 30h
  547. call print_ch
  548. mov al, ')'
  549. call print_ch
  550. mov al, ' '
  551. call print_ch
  552.  
  553. lea dx, shape
  554. mov bl, pile1[si]
  555. cmp bl, 0
  556. jz no_print
  557. print_loop: ;prints sticks in each pile using an ascii character
  558. set_color light_red ; here i want to print abovesaid shapes in a color
  559. call print_msg
  560. dec bl
  561. cmp bl, 0
  562.  
  563. jnz print_loop
  564.  
  565. no_print:
  566.  
  567. call set_cursor
  568.  
  569. inc si
  570. dec cx
  571. or cx, cx
  572. jnz pile_loop
  573. lea dx, star2
  574. call print_msg
  575. lea dx, top
  576. call print_msg
  577. popa
  578. ret
  579. print_pile endp
  580.  
  581. ; get and set cursor position in order to print a border
  582. ; around piles
  583. get_cursor proc
  584. mov ah, 03h
  585. int 10h
  586. ret
  587. get_cursor endp
  588.  
  589. set_cursor proc
  590. pusha
  591. call get_cursor
  592. mov temp2, 24h
  593. sub temp2, dl
  594. add dl, temp2
  595. mov ah, 02h
  596. int 10h
  597. mov al, 0b1h
  598. call print_ch
  599. mov al, 0b2h
  600. call print_ch
  601. popa
  602. ret
  603. set_cursor endp
  604. ;==========================
  605.  
  606. print_msg proc
  607. pusha
  608. mov ah, prnt_msg
  609. int 21h
  610. popa
  611. ret
  612. print_msg endp
  613.  
  614.  
  615. print_ch proc
  616. pusha
  617. mov ah, _out
  618. int 10h
  619. popa
  620. ret
  621. print_ch endp
  622.  
  623.  
  624. get_ch proc
  625. lea dx, space
  626. call print_msg
  627. mov ah, _in
  628. int 21h
  629. ret
  630. get_ch endp
  631.  
  632.  
  633. play_again proc ; here i prompt user for a second play
  634. cmp win_flag, 0
  635. jz computer_lost
  636.  
  637. lea dx, again_ul
  638.  
  639. jmp fin2
  640.  
  641. computer_lost:
  642. lea dx, again_cl
  643.  
  644. fin2:
  645. call print_msg
  646.  
  647. call get_ch
  648. cmp al, 'y'
  649. je start_again
  650. cmp al, 'Y'
  651. je start_again
  652. cmp al, 'N'
  653. je exit
  654. cmp al, 'n'
  655. je exit
  656.  
  657. start_again:
  658. mov win_flag, -1
  659. jmp start
  660.  
  661. ret
  662. play_again endp
  663.  
  664. exit:
  665. mov ax, 4c00h
  666. int 21h
  667.  
  668. .EXIT
  669. END
  670.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.asm:8: error: comma, colon, decorator or end of line expected after operand
prog.asm:9: error: comma, colon, decorator or end of line expected after operand
prog.asm:30: error: attempt to define a local label before any non-local labels
prog.asm:30: error: parser: instruction expected
prog.asm:33: error: attempt to define a local label before any non-local labels
prog.asm:35: error: comma expected after operand 1
prog.asm:39: error: attempt to define a local label before any non-local labels
prog.asm:42: error: comma expected after operand 5
prog.asm:59: error: expression syntax error
prog.asm:87: error: comma expected after operand 1
prog.asm:118: error: comma expected after operand 1
prog.asm:151: error: parser: instruction expected
prog.asm:168: error: symbol `set_color' redefined
prog.asm:168: error: parser: instruction expected
prog.asm:205: error: comma, colon, decorator or end of line expected after operand
prog.asm:227: error: comma, colon, decorator or end of line expected after operand
prog.asm:238: error: comma, colon, decorator or end of line expected after operand
prog.asm:286: error: parser: instruction expected
prog.asm:306: error: symbol `who_turn' redefined
prog.asm:306: error: parser: instruction expected
prog.asm:308: error: parser: instruction expected
prog.asm:322: error: comma, colon, decorator or end of line expected after operand
prog.asm:326: error: comma, colon, decorator or end of line expected after operand
prog.asm:328: error: comma, colon, decorator or end of line expected after operand
prog.asm:332: error: comma, colon, decorator or end of line expected after operand
prog.asm:336: error: comma, colon, decorator or end of line expected after operand
prog.asm:352: error: comma, colon, decorator or end of line expected after operand
prog.asm:357: error: comma, colon, decorator or end of line expected after operand
prog.asm:370: error: symbol `user_turn' redefined
prog.asm:370: error: parser: instruction expected
prog.asm:372: error: parser: instruction expected
prog.asm:405: error: symbol `computer_turn' redefined
prog.asm:405: error: parser: instruction expected
prog.asm:407: error: parser: instruction expected
prog.asm:416: error: comma, colon, decorator or end of line expected after operand
prog.asm:422: error: symbol `print_move' redefined
prog.asm:422: error: parser: instruction expected
prog.asm:424: error: parser: instruction expected
prog.asm:432: error: comma, colon, decorator or end of line expected after operand
prog.asm:438: error: symbol `get_sum' redefined
prog.asm:438: error: parser: instruction expected
prog.asm:440: error: parser: instruction expected
prog.asm:442: error: comma, colon, decorator or end of line expected after operand
prog.asm:446: error: symbol `is_empty' redefined
prog.asm:446: error: parser: instruction expected
prog.asm:448: error: parser: instruction expected
prog.asm:455: error: comma, colon, decorator or end of line expected after operand
prog.asm:459: error: symbol `pick_stick' redefined
prog.asm:459: error: parser: instruction expected
prog.asm:461: error: parser: instruction expected
prog.asm:473: error: symbol `rand_gen' redefined
prog.asm:473: error: parser: instruction expected
prog.asm:475: error: parser: instruction expected
prog.asm:482: error: comma, colon, decorator or end of line expected after operand
prog.asm:489: error: symbol `is_kernel' redefined
prog.asm:489: error: parser: instruction expected
prog.asm:491: error: parser: instruction expected
prog.asm:495: error: comma, colon, decorator or end of line expected after operand
prog.asm:501: error: comma, colon, decorator or end of line expected after operand
prog.asm:505: error: comma, colon, decorator or end of line expected after operand
prog.asm:506: error: comma, colon, decorator or end of line expected after operand
prog.asm:508: error: comma, colon, decorator or end of line expected after operand
prog.asm:511: error: comma, colon, decorator or end of line expected after operand
prog.asm:515: error: symbol `make_kernel' redefined
prog.asm:515: error: parser: instruction expected
prog.asm:518: error: parser: instruction expected
prog.asm:536: error: comma, colon, decorator or end of line expected after operand
prog.asm:545: error: comma, colon, decorator or end of line expected after operand
prog.asm:554: error: comma, colon, decorator or end of line expected after operand
prog.asm:558: error: symbol `set_color' redefined
prog.asm:558: error: parser: instruction expected
prog.asm:579: error: symbol `print_pile' redefined
prog.asm:579: error: parser: instruction expected
prog.asm:583: error: parser: instruction expected
prog.asm:587: error: symbol `get_cursor' redefined
prog.asm:587: error: parser: instruction expected
prog.asm:589: error: parser: instruction expected
prog.asm:603: error: symbol `set_cursor' redefined
prog.asm:603: error: parser: instruction expected
prog.asm:606: error: parser: instruction expected
prog.asm:612: error: symbol `print_msg' redefined
prog.asm:612: error: parser: instruction expected
prog.asm:615: error: parser: instruction expected
prog.asm:621: error: symbol `print_ch' redefined
prog.asm:621: error: parser: instruction expected
prog.asm:624: error: parser: instruction expected
prog.asm:630: error: symbol `get_ch' redefined
prog.asm:630: error: parser: instruction expected
prog.asm:633: error: parser: instruction expected
prog.asm:662: error: symbol `play_again' redefined
prog.asm:662: error: parser: instruction expected
ld: cannot find prog.o: No such file or directory
stdout
Standard output is empty