我的代码为用户提供了两个数字,他们应该猜测可以组合的数量。目前,该程序总是告诉用户他们得到了正确的答案。此外,当我问他们是否想再次播放时,程序会崩溃。
INCLUDE Irvine32.inc
;MACRO FOR writeString
my_write_string MACRO buffer
push edx
mov edx, OFFSET buffer
call WriteString
pop edx
ENDM
; (insert constant definitions here)
MAX_SIZE=15
YES='Y'
NO='N'
.data
prompt_title BYTE "Welcome to the Combinations Calculator by Rachael Philpott.", 0
prompt_description BYTE "I'll give you a combinations problem. You enter your answer, and I'll let you know if you're right", 0
prompt_1 BYTE "Problem: ", 0
prompt_2 BYTE "Number of elements in the set: ", 0
prompt_3 BYTE "Number of elements to choose from the set: ", 0
prompt_4 BYTE "How many ways can you choose?: ", 0
result_1 BYTE "There are : ", 0
result_2 BYTE " combinations of ", 0
result_3 BYTE " items from a set of ", 0
result_4 BYTE "Good job!", 0
result_5 BYTE "You need more practice!", 0
error_msg BYTE "Invalid response!", 0
play_msg BYTE "Do you want to play again? (Y/N): ", 0
goodbye_1 BYTE "Thank you for playing Combinations Calculator! Goodbye!", 0
user_str BYTE MAX_SIZE DUP (?)
str_len DWORD ?
n_rand DWORD ?
r_rand DWORD ?
combos DWORD ?
play_again DWORD ?
n_lo DWORD 3
n_hi DWORD 12
r_lo DWORD 1
r_hi DWORD ?
count DWORD ?
ans DWORD ?
divisor DWORD ?
user_num DWORD ?
; (insert variable definitions here)
.code
main PROC
call Randomize
call introduction
call showProblem
;Get and validate user input
push OFFSET user_str
push MAX_SIZE
call getData
;Get combinations
push OFFSET divisor
push n_rand
push r_rand
push OFFSET combos
call combinations
;Show results
call showResults
;Say goodbye
push YES
call goodbye
;exit to operating system
exit
main ENDP
;INTRODUCTION
introduction PROC
;display title
my_write_string prompt_title
call Crlf
call Crlf
;Print the description of program
my_write_string prompt_description
call Crlf
call Crlf
ret
introduction ENDP
;SHOW PROBLEM
showProblem PROC
;get random number for n
nRand:
mov eax, n_hi
sub eax, n_lo
inc eax
call RandomRange
add eax, n_lo
mov n_rand, eax
rRand:
mov eax, n_rand
sub eax, r_lo
inc eax
call RandomRange
add eax, r_lo
mov r_rand, eax
displayProblem:
my_write_string prompt_1
call Crlf
my_write_string prompt_2
mov eax, n_rand
call WriteDec
call Crlf
my_write_string prompt_3
mov eax, r_rand
call WriteDec
call Crlf
ret
showProblem ENDP
;GET DATA
getData PROC
push ebp
mov ebp,esp
mov edx,[ebp+12]
mov ecx, [ebp+8]
user_input:
my_write_string prompt_4
call ReadString
call Crlf
mov esi, edx
mov str_len, eax
cmp eax, 3
ja to_large
cld
validate:
lodsb
cmp al, 48
jb out_of_range
cmp al, 57
ja out_of_range
jmp done
to_large:
my_write_string error_msg
call Crlf
call Crlf
jmp again
out_of_range:
my_write_string error_msg
call Crlf
call Crlf
jmp again
again:
my_write_string prompt_1
call Crlf
my_write_string prompt_2
mov eax, n_rand
call WriteDec
call Crlf
my_write_string prompt_3
mov eax, r_rand
call WriteDec
Call Crlf
jmp user_input
done:
pop ebp
ret 8
getData ENDP
;COMBINATIONS
combinations PROC
push ebp
mov ebp,esp
mov eax, [ebp+16]
sub eax, [ebp+12]
mov ebx, eax
push ebx
call factorial
mov edx,[ebp+20]
mov [edx],eax
mov ebx, [ebp+12]
push ebx
call factorial
mov edx,[ebp+20]
mov ebx, [edx]
mul ebx
mov ebx, [ebp+20]
mov [ebx], eax
mov ebx, [ebp+16]
push ebx
call factorial
mov edx,[ebp+20]
mov ebx,[edx]
mov edx, 0
div ebx
mov ebx, [ebp+8]
mov [ebx],eax
pop ebp
ret 16
combinations ENDP
;FACTORIAL
factorial PROC
mov eax,dword ptr [esp+4]
cmp eax,1
jle end_recursive
dec eax
push eax
call factorial
mov esi,dword ptr [esp+4]
mul esi
end_recursive:
ret 4
factorial ENDP
;SHOW RESULTS
showResults PROC
my_write_string result_1
mov eax, combos
call WriteDec
my_write_string result_2
mov eax, r_rand
call WriteDec
my_write_string result_3
mov eax, n_rand
call WriteDec
call Crlf
;TRYING TO TELL USER IF GOOD JOB OR IF NEED TO PRACTICE - NOT WORKING CORRECTLY
;ALWAYS JUMPS TO DONE AND SAYS GOOD JOB
mov eax, combos
cmp eax, edx
jl wrong_guess
cmp eax, edx
jg wrong_guess
cmp eax, edx
je done
wrong_guess:
my_write_string result_4
call Crlf
ret
done:
my_write_string result_5
call Crlf
ret
showResults ENDP
;GOODBYE/PLAY_AGAIN
goodbye PROC
;TRYING TO DO PLAY AGAIN - CAN'T GET IT TO WORK
my_write_string play_msg
call ReadString
mov esi, edx
mov ebp, eax
cmp eax, edx
je go_again
jmp done
go_again:
call showProblem
done:
my_write_string goodbye_1
call Crlf
ret
goodbye ENDP
; (insert additional procedures here)
END main