DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   asm (http://www.daniweb.com/code/asm.html)
-   -   Wheel of Fortune (http://www.daniweb.com/code/snippet545.html)

cscgal asm syntax
Aug 28th, 2006
This is a program I wrote for my x86 assembly class which revolves a "roulette wheel" around. Wait about a minute and it will eventually slow down to a stop. It uses Irvine32.inc which came with the textbook.

  1. title Wheel of Fortune (fortune.asm)
  2.  
  3. ; Dani Horowitz
  4. ; CSC111 x86 Assembly Programming
  5.  
  6. ; This program plays wheel of fortune
  7.  
  8. INCLUDE Irvine32.inc
  9.  
  10. ;--------------------------------------------------
  11. .stack ; begin stack segment
  12.  
  13. ;--------------------------------------------------
  14. .data ; begin data segment
  15.  
  16. xcoord byte 5, 10, 15, 20, 25, 30,
  17. 30, 30, 30, 30, 30, 30,
  18. 30, 25, 20, 15, 10, 5,
  19. 5, 5, 5, 5, 5, 5
  20.  
  21. ycoord byte 5, 5, 5, 5, 5, 5,
  22. 10, 15, 20, 25, 30, 35,
  23. 40, 40, 40, 40, 40, 40,
  24. 35, 30, 25, 20, 15, 10
  25.  
  26. numbers dword 0,1,2,3,4,5,6,
  27. 8,6,5,4,3,2,
  28. 1,2,3,4,5,6,
  29. 8,6,5,4,3,2
  30.  
  31. wheel_slot dword 0
  32. rotation_count dword 0
  33. countdown dword 1
  34. ;--------------------------------------------------
  35. .code ; begin code segment
  36.  
  37. ;--------------------------------------------------
  38. printnumber PROC
  39. ; prints numbers[wheel_slot*4] at coordinates
  40. ; xcoord[wheel_slot], ycoord[wheel_slot]
  41. ; where wheel_slot is from 0 thru n
  42. ;--------------------------------------------------
  43. ; ## position cursor ##
  44. mov esi, OFFSET xcoord
  45. add esi, wheel_slot
  46. mov dl, [esi]
  47. mov esi, OFFSET ycoord
  48. add esi, wheel_slot
  49. mov dh, [esi]
  50. call Gotoxy
  51.  
  52. ; ## get value ##
  53. mov esi, OFFSET numbers
  54. mov eax, wheel_slot
  55. inc eax
  56. mov ebx, 4
  57. mul ebx
  58. add esi, eax
  59. mov eax, [esi]
  60.  
  61. ; ## write value ##
  62. call WriteChar
  63. ret
  64. ;--------------------------------------------------
  65. printnumber ENDP
  66. ;--------------------------------------------------
  67.  
  68. ;--------------------------------------------------
  69. printwheel proc
  70. ; calls printnumber for each of the n wheel_slots
  71. ;--------------------------------------------------
  72. mov wheel_slot, 0
  73. NextNum:
  74. ;; ## print each number of the array once ##
  75. call printnumber
  76. inc wheel_slot
  77. cmp wheel_slot, 24 ; XX edit XX
  78. jl NextNum
  79. ret
  80. ;--------------------------------------------------
  81. printwheel ENDP
  82. ;--------------------------------------------------
  83.  
  84. ;--------------------------------------------------
  85. shiftleft proc
  86. ;--------------------------------------------------
  87. ;; ## shift each number of the array left
  88. push esi
  89. mov eax, [esi]
  90. sub esi, 4
  91. mov [esi], eax
  92. pop esi
  93. ret
  94. ;--------------------------------------------------
  95. shiftleft ENDP
  96. ;--------------------------------------------------
  97.  
  98. ;--------------------------------------------------
  99. shiftaround proc
  100. ;--------------------------------------------------
  101. ;; ## loop first number of array to last position
  102. push esi
  103. mov esi, OFFSET numbers
  104. mov eax, [esi]
  105. add esi, 96 ;; XX edit*4 XX
  106. mov [esi], eax
  107. pop esi
  108. ret
  109. ;--------------------------------------------------
  110. shiftaround ENDP
  111. ;--------------------------------------------------
  112.  
  113. ;--------------------------------------------------
  114. spinwheel proc
  115. ;--------------------------------------------------
  116. ; ## find array ##
  117. mov rotation_count, 0
  118. mov esi, OFFSET numbers
  119. add esi, 4
  120.  
  121. MoveNumber:
  122. ; ## shift the wheel one iteration
  123. call shiftleft
  124. add esi, 4
  125. inc rotation_count
  126. cmp rotation_count, 24 ;; XX edit XX
  127. jl MoveNumber
  128. call shiftaround
  129. ret
  130. ;--------------------------------------------------
  131. spinwheel ENDP
  132. ;--------------------------------------------------
  133.  
  134. ;--------------------------------------------------
  135. printwin proc
  136. ;--------------------------------------------------
  137. .data
  138. points byte " points", 0
  139. .code
  140. ; ## print winning character
  141. mov dl, 18
  142. mov dh, 20
  143. call Gotoxy
  144. mov esi, OFFSET numbers
  145. add esi, 8
  146. mov eax, [esi]
  147. call WriteChar
  148. mov ebx, eax
  149. mov eax, '!'
  150. call WriteChar
  151. ; ## print winning points
  152. mov dl, 15
  153. mov dh, 25
  154. call Gotoxy
  155. mov eax, ebx
  156. call WriteDec
  157. mov edx, OFFSET points
  158. call WriteString
  159. ; ## reposition cursor
  160. mov dl, 5
  161. mov dh, 45
  162. call Gotoxy
  163. ret
  164. ;--------------------------------------------------
  165. printwin ENDP
  166. ;--------------------------------------------------
  167.  
  168. ;--------------------------------------------------
  169. randomval proc
  170. ;--------------------------------------------------
  171. mov eax, 5 ; generate random #
  172. call RandomRange
  173. add countdown, eax ; increase countdown
  174. mov eax, countdown
  175. call Delay ; pause the wheel
  176. ret
  177. ;--------------------------------------------------
  178. randomval ENDP
  179. ;--------------------------------------------------
  180.  
  181. ;--------------------------------------------------
  182. main proc
  183. ;--------------------------------------------------
  184. call Clrscr ; clear screen
  185. call Randomize ; randomize seed
  186. Spin:
  187. call spinwheel ; shift wheel left
  188. call printwheel ; print current wheel values
  189. call randomval ; wait increasing amount of time
  190. cmp countdown, 350 ; if too slow, stop wheel
  191. jl Spin ; otherwise, spin again
  192. call printwin ; print winning character
  193. call Waitmsg
  194. call Clrscr
  195. exit
  196. main endp
  197. end main
  198. ;--------------------------------------------------