-
Notifications
You must be signed in to change notification settings - Fork 0
/
Primality_Test.asm
280 lines (232 loc) · 7.07 KB
/
Primality_Test.asm
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
TITLE Program Template (template.asm)
; Description: Program asks user for input between 1-200 --> [1...200]
; If user enters number between 1-200 then that many primes will be displayed up to an unspecified "n" value
; If user enters number outside range of 1-200 then the user will be prompted again to enter a number in the range 1-200
INCLUDE Irvine32.inc
MIN_NUMBER = 1
MAX_NUMBER = 200
.data
programmersName BYTE "Haris Hambasic", 0
programTitle BYTE "Project 4", 0
programIntroduction BYTE "Welcome! This progrm allows you to enter a number for which you want to view all primes up to it!", 0
programFarewell BYTE "Thanks for playing!", 0
promptNumberOfPrimesToDisplay BYTE "Enter the number of primes to be displayed: ", 0
numberOfPrimesToDisplay DWORD 0
numberToDetermineIfPrime DWORD ?
primeWasFound WORD 0 ; 0=FALSE, 1=TRUE
numberOfPrimesToDisplayIsLess BYTE "The integer you entered is less than 1. The value must must be greater than or equal to 1. Please enter another number now: ", 0
numberOfPrimesToDisplayIsGreater BYTE "The integer you entered is greater than 200. The value must be less than or equal to 200. Please enter another number now: ", 0
.code
main PROC
CALL introduction
CALL getUserData
CALL showPrimes
CALL farewell
Invoke ExitProcess,0
main ENDP
introduction PROC
; ---------------------------------------------------------------------------------
; Name: introduction
;
; Introduces the program by printing the programmer's name, the program title,
; and the program introduction statement
;
; Preconditions: values at memory locations programmersName, programTitle, and programIntroduction
;
; Postconditions: none.
;
; returns: no return value
; ---------------------------------------------------------------------------------
MOV EDX, OFFSET programmersName
CALL WriteString
CALL CrLf
MOV EDX, OFFSET programTitle
CALL WriteString
CALL CrLf
CALL CrLf
MOV EDX, OFFSET programIntroduction
CALL WriteString
CALL CrLf
CALL CrLf
RET
introduction ENDP
getUserData PROC
; ---------------------------------------------------------------------------------
; Name: getUserData
;
; Gets the user's input data, specifically the number of primes to display
;
; Preconditions: none
;
; Postconditions:
; - memory location "numberOfPrimesToDisplay" is populated with the number
; of prime numbers to display
;
; returns: no return value
; ---------------------------------------------------------------------------------
_setNumberOfPrimesToDisplay:
MOV EDX, OFFSET promptNumberOfPrimesToDisplay
CALL WriteString
CALL ReadInt
MOV numberOfPrimesToDisplay, EAX
CALL validate
CALL CrLf
RET
getUserData ENDP
validate PROC
; ---------------------------------------------------------------------------------
; Name: validate
;
; Validates the user's input
;
; Preconditions:
; - values at memory locations:
; - MIN_NUMBER
; - MAX_NUMBER
;
; Postconditions:
; - validated user input at memory location "numberOfPrimesToDisplay"
;
; returns: no return value
; ---------------------------------------------------------------------------------
_validateNumberOfPrimesToDisplay:
CMP numberOfPrimesToDisplay, OFFSET MIN_NUMBER
JL _isLess?
CMP numberOfPrimesToDisplay, OFFSET MAX_NUMBER
JG _isGreater?
JMP _endValidation
_isLess?:
MOV EDX, OFFSET numberOfPrimesToDisplayIsLess
CALL WriteString
CALL ReadInt
MOV numberOfPrimesToDisplay, EAX
JMP _validateNumberOfPrimesToDisplay
_isGreater?:
MOV EDX, OFFSET numberOfPrimesToDisplayIsGreater
CALL WriteString
CALL ReadInt
MOV numberOfPrimesToDisplay, EAX
JMP _validateNumberOfPrimesToDisplay
_endValidation:
RET
validate ENDP
showPrimes PROC
; ---------------------------------------------------------------------------------
; Name: showPrimes
;
; Prints to the console a prime number if the current number is a prime.
;
; Preconditions:
; - "numberOfPrimesToDisplay" to use as the counter for loop
;
; Postconditions: none... this procedure only prints the number if is prime and
; keeps track of the loop constraints
;
; returns: no return value
; ---------------------------------------------------------------------------------
; -----------------------------------
; Checks if the user want only to
; to print one prime number
; -----------------------------------
CMP numberOfPrimesToDisplay, 1
JE _displayOnePrime
CMP numberOfPrimesToDisplay, 2
JGE _main
_displayOnePrime:
MOV EAX, 2
CALL WriteInt
_farewell:
CALL farewell
_main:
MOV EAX, 2
CALL WriteInt
MOV EAX, 3 ; EAX will hold the current number being tested
MOV EBX, 2 ; The current divisor
SUB numberOfPrimesToDisplay, 1
MOV ECX, numberOfPrimesToDisplay ; ECX will hold the total number of primes to compute
_primeLoop:
PUSH ECX
PUSH EAX
CALL isPrime
CMP primeWasFound, 1
JE _continueToNextIteration
; --------------------------
; Adds 1 to ECX before the next loop decrements
; ECX causing an incorrect counter
; --------------------------
_adjustECX:
POP EAX
POP ECX
ADD ECX, 1 ; Adjust ECX so that when the next loop iteration decrements ECX, ECX will store the correct value
INC EAX
MOV EBX, 2
LOOP _primeLoop
JMP _endShowPrimes
_continueToNextIteration:
POP EAX
POP ECX
call CrLf
CALL WriteInt
MOV primeWasFound, 0
INC EAX
MOV EBX, 2
LOOP _primeLoop
_endShowPrimes:
RET
showPrimes ENDP
isPrime PROC
; ---------------------------------------------------------------------------------
; Name: isPrime
;
; Determines if the number is a prime
;
; Preconditions: the number to check, in register EAX
;
; Postconditions:
; - memory location "primeWasFound" is set (1) if the current number is
; prime and subsequently procedure "showPrimes" prints number to console
; otherwise number is not prime and next number is loaded
;
; returns: no return value
; ---------------------------------------------------------------------------------
DEC EAX
DEC EAX
MOV ECX, EAX
_primeInnerLoop:
CMP EAX, 1
JLE _jumpEarly
PUSH EAX
MOV EDX, 0
DIV EBX
POP EAX
CMP EDX, 0
JE _endIsPrime
DEC EAX
INC EBX
LOOP _primeInnerLoop
_jumpEarly:
MOV primeWasFound, 1
_endIsPrime:
RET
isPrime ENDP
farewell PROC
; ---------------------------------------------------------------------------------
; Name: farewell
;
; Displays a farewell message to the user
;
; Preconditions: none
;
; Postconditions: none
;
; returns: no return value
; ---------------------------------------------------------------------------------
CALL CrLf
CALL CrLf
CALL CrLf
MOV EDX, OFFSET programFarewell
CALL WriteString
CALL CrLf
CALL CrLf
farewell ENDP
END main