1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
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
| |