Main Menu

Home
MASM
C
C/MASM
Java
Google
 
Home arrow MASM arrow Monte Hall Simulation
Monte Hall Simulation
TITLE Monte Hall Simulation

INCLUDE Irvine32.inc
.data
 iterReq    BYTE "Please enter the number of iterations:  ",0
 prizeDoorCounts  BYTE "Prize door counts:  ",0
 initialDoorCounts BYTE "Initial door selection counts:  ",0
 initialDoorWins  BYTE "Number of initialDoor wins:  ",0
 switchDoorWins  BYTE "Number of switchDoor wins:  ",0
 tryAgain   BYTE "Try again? (Y/N) ",0
 threeSpaces   BYTE "   ",0
 iterations   DWORD ?
 prizeDoor   DWORD ?
 contestantDoor  DWORD ?
 montesDoor   DWORD ?
 switchDoor   DWORD ?
 contestantDoorWon DWORD ?
 switchDoorWon  DWORD ?
 prizeDoorOne  DWORD ?
 prizeDoorTwo  DWORD ?
 prizeDoorThree  DWORD ?
 contestantDoorOne DWORD ?
 contestantDoorTwo DWORD ?
 contestantDoorThree DWORD ?

.code
main PROC
 call Randomize      ;Initialize random number seed
 
 PlayAgain:
 mov edx,OFFSET iterReq    ;Request number of iterations from user
 call WriteString

 call ReadInt      ;Get number of iterations entered by user
 mov iterations, eax
 call crlf

 call ResetGame      ;Reset count variables
 
 PlayGame:
  mov eax,3      ;Select prize door
  call RandomRange
  mov prizeDoor,eax
  inc prizeDoor
 
  mov eax,3      ;contestant selects door
  call RandomRange
  mov contestantDoor,eax
  inc contestantDoor
 
  mov eax,prizeDoor    ;Monte selects door
  cmp eax,contestantDoor
  jne PickLast     
  mov eax,2
  call RandomRange    ;Contestant selected prize door
  mov montesDoor,eax
  add montesDoor,1
  cmp prizeDoor,1
  je IsDoorOne
  cmp prizeDoor,2
  je IsDoorTwo
  cmp montesDoor,1
  je DoorIsOne
  mov switchDoor,1
  jmp Picked
  DoorIsOne:
  mov switchDoor,2
  jmp Picked
  IsDoorTwo:
  cmp montesDoor,2
  je IsTwo
  mov switchDoor,3
  jmp Picked
  OneIt:
  mov switchDoor,3
  jmp Picked
  IsTwo:
  add montesDoor,1
  mov switchDoor,1
  jmp Picked
  IsDoorOne:
  add montesDoor,1
  cmp montesDoor,2
  je DoorIsTwo
  mov switchDoor,2
  jmp Picked
  DoorIsTwo:
  mov switchDoor,3
  jmp Picked
  PickLast:      ;Contestant didn't selected prize door
  cmp prizeDoor,1
  je NotOne
  cmp prizeDoor,2
  je NotOneOrTwo
  cmp contestantDoor,1
  je SelectDoorTwo
  cmp contestantDoor,2
  je SelectDoorOne
  NotOne:
  cmp contestantDoor,2
  je SelectDoorThree
  jmp SelectDoorTwo
  NotOneOrTwo:
  cmp contestantDoor,1
  je SelectDoorTwo
  jmp SelectDoorOne
  SelectDoorOne:
  mov montesDoor,1
  jmp montePicked
  SelectDoorTwo:
  mov montesDoor,2
  jmp montePicked
  SelectDoorThree:
  mov montesDoor,2
  montePicked:
  mov eax,prizeDoor
  mov switchDoor,eax
  Picked:
 
  mov eax,prizeDoor    ;Which selection won?
  cmp contestantDoor,eax
  je Winner
  cmp switchDoor,eax
  je Winner2
  jmp Added
  Winner:
  inc contestantDoorWon
  jmp Added
  Winner2:
  inc switchDoorWon
  Added:
 
  cmp prizeDoor,1     ;Where was the prize?
  je IncPrizeDoorOne
  cmp prizeDoor,2
  je IncPrizeDoorTwo
  inc prizeDoorThree
  jmp Added2
  IncPrizeDoorTwo:
  inc prizeDoorTwo
  jmp Added2
  IncPrizeDoorOne:
  inc prizeDoorOne
  Added2:
 
  cmp contestantDoor,1   ;Which door did the contestant select?
  je IncContestantDoorOne
  cmp contestantDoor,2
  je IncContestantDoorTwo
  inc contestantDoorThree
  jmp Added3
  IncContestantDoorTwo:
  inc contestantDoorTwo
  jmp Added3
  IncContestantDoorOne:
  inc contestantDoorOne
  Added3:
 
  cmp iterations,1    ;Check for end of loop
  je DonePlaying
  dec iterations
 jmp PlayGame
 DonePlaying:

 mov edx,OFFSET prizeDoorCounts  ;Print number of prize door counts
 call WriteString
 mov eax,prizeDoorOne
 call WriteDec
 call PrintThreeSpaces
 mov eax,prizeDoorTwo
 call WriteDec
 call PrintThreeSpaces
 mov eax,prizeDoorThree
 call WriteDec
 call crlf
 
 mov edx,OFFSET initialDoorCounts ;Print number of initial door selection counts
 call WriteString
 mov eax,contestantDoorOne
 call WriteDec
 call PrintThreeSpaces
 mov eax,contestantDoorTwo
 call WriteDec
 call PrintThreeSpaces
 mov eax,contestantDoorThree
 call WriteDec
 call crlf
 
 mov edx,OFFSET initialDoorWins  ;Print number of initial door wins
 call WriteString
 mov eax,contestantDoorWon
 call WriteDec
 call crlf
 
 mov edx,OFFSET switchDoorWins  ;Print number of switch door wins
 call WriteString
 mov eax,switchDoorWon
 call WriteDec
 call crlf
 call crlf
 
 mov edx,OFFSET tryAgain    ;Try again
 call WriteString
 call ReadChar
 cmp al,59h
 jne NotAgain
 call crlf
 call crlf
 jmp PlayAgain
 NotAgain:
 call crlf
 
 exit
main ENDP

ResetGame PROC
 mov contestantDoorWon,0
 mov switchDoorWon,0
 mov prizeDoorOne,0
 mov prizeDoorTwo,0
 mov prizeDoorThree,0
 mov contestantDoorOne,0
 mov contestantDoorTwo,0
 mov contestantDoorThree,0
 ret
ResetGame ENDP

PrintThreeSpaces PROC
 mov edx,OFFSET threeSpaces
 call WriteString
 ret
PrintThreeSpaces ENDP

END main
 
< Prev   Next >
© 2009 SourceRip
Joomla! is Free Software released under the GNU/GPL License.