my objective is to create a simple game called Pig. The rules are
2 players (you and the computer) are racing to reach
100 points. Each turn, the active player faces a decision:
a) Hold, take your turn total and add it to the player's overall total.
The next player becomes active.
b) Roll, generate a random number between
1 and 6.
Results are:
o to 1:
Lose turn, no turn total added to overall total.
2 to 6:
Add this number to the turn total. Player goes again.
when i assemble, link, and run i receive:
rogerfleenor@roger-VirtualBox:~/Downloads$ ./pig
Welcome to the Game of Pig.
Objective and Rules: first to 100 wins. Enter 1 to skip your turn and give the pc your score or 2 to roll the dice. If the outcome is 0 or 1, turn lost. If 2 to 6, it is added to your score. Ready? Go!
Enter 1 to skip turn or 2 to roll.
2
2your score is
the first "2" above is the number i entered, the second 2 is generated from "write_digit2:"
which should actually output the random number that was generated. so that leads me
to beleive that my problem resides in "cmpUserDice"and that i am adding the random digit to an
empty buffer wrong or something similar. Especially since when i reach the end of the program
where "userScore" should print, nothing is outputted in the terminal. is anyone able to identify
my issue? This is my final project and is due in two days! i am very desperate.
section .bss
userScore: resb 4
pcScore: resb 4
counter: resb 4
digiter: resb 4
number: resb 4
digit: resb 4
count: resb 4
userOption: resb 30
intLen: resd 1
section .data
welcomeMsg db "Welcome to the Game of Pig.",13,10
welcomeLen equ $ - welcomeMsg
ruleMsg dd "Objective and Rules: first to 100 wins. Enter 1 to skip your turn and give the pc your score or 2 to roll the dice. If the outcome is 1, turn lost. If 2 to 6, it is added to your score. Ready? Go!",13,10
ruleLen equ $ - ruleMsg
optionMsg db "Enter 1 to skip turn or 2 to roll.",13,10
optionLen equ $ - optionMsg
userScoreIs db "your score is",13,10
userScoreIsLen equ $ - userScoreIs
section .text
global _start:
_start:
nop
mov ax, 0
call printWelcome
call printRules
call printReadOption
exit:
mov eax, 1
mov ebx, 0
int 80h
printWelcome:
mov edx, welcomeLen
mov ecx, welcomeMsg
mov ebx, 1
mov eax, 4
int 80h
printRules:
mov edx, ruleLen
mov ecx, ruleMsg
mov ebx, 1
mov eax, 4
int 80h
printReadOption:
;prints option
mov edx, optionLen
mov ecx, optionMsg
mov ebx, 1
mov eax, 4
int 80h
;reads option & saves as string
mov edx, 30
mov ecx, userOption
mov ebx, 2
mov eax, 3
int 80h
;save size of string input
dec eax
mov dword [intLen], eax
;convert string to int
mov edi, 10
mov ecx, [intLen]
mov esi, userOption
xor eax, eax
xor ebx, ebx
mov bl, [esi]
cmp bl, '-'
jne next_check
inc eax
inc esi
dec ecx
jmp done_sign
next_check:
cmp bl, '+'
jne done_sign
inc esi
dec ecx
done_sign:
push eax
l1:
mov bl, [esi]
cmp bl, 30h
;jb error
cmp bl, 39h
;ja error
sub bl, 30h
mul edi; eax=eax*10
mov bh, 0
add eax, ebx
inc esi
loop l1
pop ebx
cmp ebx, 1
jne compareInput
neg eax
compareInput:
cmp eax, 2
je userRollDice ;jmp to roll dice
cmp eax, 1 ;jmp to pc turn
je exit ;switch to pic turn
userRollDice: ;randnumgen 0-6
;jmp exit
mov eax, 13
int 80h
add eax, 65535
mov ebx, 30903
mul ebx,
mov edx, 0
mov ecx, 7
div ecx
mov dword[number], edx ;# to print
;call write_number
call cmpUserDice ;decides 1 - 6 effect
write_number:
L01:
mov eax, dword[number]
mov ecx, 0Ah
cdq
div ecx ;eax=number/10,edx=number%10
mov dword[number], eax ;number= number/10
add edx, 30h ;add 48 (30h) to make a printable character
push edx ;push edx in to the stack and
inc dword[count] ;increment count of numbers in the stack
cmp dword[number], 0 ;if number != 0, loop again
jne L01
L02:
pop dword[digit] ;pop the digit from the stack and
call write_digit ;write it
dec dword[count] ;decrement the count
cmp dword[count], 0 ; if count != 0, loop again
jne L02
ret
write_digit: ;Print score
mov eax, 4
mov ebx, 1
mov ecx, digit
mov edx, 4
int 80h
cmpUserDice:
cmp edx, 0
je userRollDice
cmp edx, 1
je printReadOption
cmp edx, 2
add dword[userScore], 2
je printUserScore
;call pcTurn
cmp edx, 3
add dword[userScore], 3
je printUserScore
;call pcTurn
cmp edx, 4
add dword[userScore], 4
je printUserScore
;call pcTurn
cmp edx, 5
add dword[userScore], 5
je printUserScore
;call pcTurn
cmp edx, 6
add dword[userScore], 6
je printUserScore
;call pcTurn
printUserScore:
mov eax, dword[userScore]
mov ecx, 0Ah ;hex for 10
cdq
div ecx ;eax=number/10,edx=number%10
mov dword[userScore], eax ;number= number/10
add edx, 30h ;add 48 (30h) to make a printable character
push edx ;push edx in to the stack and
inc dword[counter] ;increment count of numbers in the stack
cmp dword[userScore], 0 ;if number != 0, loop again
jne printUserScore
loop2:
pop dword[digiter] ;pop the digit from the stack and
call write_digit2 ;write it
dec dword[counter] ;decrement the count
cmp dword[counter], 0 ; if count != 0, loop again
jne loop2
ret
write_digit2: ;Print score
mov eax, 4
mov ebx, 1
mov ecx, digiter
mov edx, 4
int 80h
;prints your score is:
;mov dword[userScore], 2
mov eax, 4
mov ebx, 1
mov ecx, userScoreIs
mov edx, userScoreIsLen
int 80h
mov eax, 4
mov ebx, 1
mov ecx, userScore
mov edx, 4
int 80h
call exit;