TITLE Craps Simulation ; This is a 32-bit MASM Monte Carlo simulation program to estimate ; the probability of winning the dice game called craps. INCLUDE Irvine32.inc .data iterReq BYTE "Craps Simulation - Enter number of iterations: ",0 first10 BYTE "First 10 Games - Detail:",0 oneSpace BYTE " ",0 iterStr BYTE " Iterations = ",0 winStr BYTE " Wins = ",0 winFirstStr BYTE " Wins on First Roll = ",0 loseFirstStr BYTE " Losses on First Roll = ",0 winsPointStr BYTE " Wins with Point = ",0 losesPointStr BYTE " Losses with Point = ",0 gameRollsStr BYTE " Rolls per Game. Total Rolls = ",0 iterations DWORD ? die1 DWORD ? die2 DWORD ? diceTotal DWORD ? point DWORD ? wins DWORD 0 firstRollWins DWORD 0 firstRollLoses DWORD 0 winsWithPoint DWORD 0 losesWithPoint DWORD 0 totalRolls DWORD 0 .code Main PROC call Randomize ;Initialize random number seed 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 mov edx,OFFSET first10 call WriteString call crlf mov ecx,iterations cmp iterations,10 jb FirstPlays mov ecx,10 FirstPlays: ;display first 10 games (or less, is fewer iterations requested) call PlayTheGame loop FirstPlays cmp iterations,11 jb DonePlaying mov ecx,iterations PlayGame: ;play the rest of the games call PlayTheGames loop PlayGame DonePlaying: call PrintStatistics ;display statistics call crlf exit main ENDP ;========================================================================================================== RollDice PROC ;Roll dice one time mov eax,6 call RandomRange mov die1,eax mov eax,6 call RandomRange mov die2,eax mov eax,1 add eax,die1 add eax,die2 mov diceTotal,eax call WriteDec call PrintOneSpace inc totalRolls ret RollDice ENDP ;========================================================================================================== PlayTheGame PROC call RollDice ;initial roll cmp diceTotal,7 je FirstWins cmp diceTotal,11 je FirstWins cmp diceTotal,2 je FirstLoses cmp diceTotal,3 je FirstLoses cmp diceTotal,12 je FirstLoses mov eax,diceTotal mov point,eax jmp RollAgain FirstWins: inc firstRollWins inc wins jmp GameOver FirstLoses: inc firstRollLoses jmp GameOver RollAgain: ;continue rolling if point call RollDice cmp diceTotal,7 je PointLoses mov eax,point cmp diceTotal,eax je PointWins jmp RollAgain PointWins: inc winsWithPoint inc wins jmp GameOver PointLoses: inc losesWithPoint GameOver: call crlf ret PlayTheGame ENDP ;========================================================================================================== RollDices PROC ;Roll dice one time mov eax,6 call RandomRange mov die1,eax mov eax,6 call RandomRange mov die2,eax mov eax,1 add eax,die1 add eax,die2 mov diceTotal,eax inc totalRolls ret RollDices ENDP ;========================================================================================================== PlayTheGames PROC call RollDices ;initial roll cmp diceTotal,7 je FirstWins cmp diceTotal,11 je FirstWins cmp diceTotal,2 je FirstLoses cmp diceTotal,3 je FirstLoses cmp diceTotal,12 je FirstLoses mov eax,diceTotal mov point,eax jmp RollAgain FirstWins: inc firstRollWins inc wins jmp GameOver FirstLoses: inc firstRollLoses jmp GameOver RollAgain: ;continue rolling if point call RollDices cmp diceTotal,7 je PointLoses mov eax,point cmp diceTotal,eax je PointWins jmp RollAgain PointWins: inc winsWithPoint inc wins jmp GameOver PointLoses: inc losesWithPoint GameOver: ret PlayTheGames ENDP ;========================================================================================================== PrintOneSpace PROC mov edx,OFFSET oneSpace call WriteString ret PrintOneSpace ENDP ;========================================================================================================== PrintStatistics PROC mov edx,OFFSET iterStr call WriteString mov eax,iterations call WriteDec call crlf call crlf mov eax,wins call PrintQuotient mov edx,OFFSET winStr call WriteString mov eax,wins call WriteDec call crlf call crlf mov eax,firstRollWins call PrintQuotient mov edx,OFFSET winFirstStr call WriteString mov eax,firstRollWins call WriteDec call crlf call crlf mov eax,firstRollLoses call PrintQuotient mov edx,OFFSET loseFirstStr call WriteString mov eax,firstRollLoses call WriteDec call crlf call crlf mov eax,winsWithPoint call PrintQuotient mov edx,OFFSET winsPointStr call WriteString mov eax,winsWithPoint call WriteDec call crlf call crlf mov eax,losesWithPoint call PrintQuotient mov edx,OFFSET losesPointStr call WriteString mov eax,losesWithPoint call WriteDec call crlf call crlf mov eax,totalRolls call PrintQuotient mov edx,OFFSET gameRollsStr call WriteString mov eax,totalRolls call WriteDec call crlf ret PrintStatistics ENDP ;========================================================================================================== PrintQuotient PROC cmp eax,iterations jb NoDivide1 cdq div iterations call WriteDec mov eax,edx jmp Done1 NoDivide1: push eax mov eax,0 call WriteDec pop eax Done1: mov ebx,10 mul ebx push eax mov al,2Eh call WriteChar pop eax mov ecx,6 Quotient: cmp eax,iterations jb NoDivide cdq div iterations call WriteDec mov eax,edx jmp Done NoDivide: push eax mov eax,0 call WriteDec pop eax Done: mov ebx,10 mul ebx loop Quotient ret PrintQuotient ENDP END main
|