-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS_271-Project_4-Composite_Calulator_9000.asm
357 lines (276 loc) · 12.6 KB
/
CS_271-Project_4-Composite_Calulator_9000.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
; TITLE: Composite Calulator 9000 FILE: (Project4-931810401.asm)
; AUTHOR: Elizabeth Graalum EMAIL: [email protected]
; COURSE / PROJECT ID: CS271 / Project 4 DATE: October 28th, 2018
; Description: this programme will introduce itself and the programmer. Then it
; will ask the user for a positive integer between 1 and 400 and validate the
; user's input. It will then caluclate and display the composite numbers up to the
; inputted place. Finally it will display a goodbye message.
; E.C.: Output columns are aligned
;=====================================================================================
INCLUDE Irvine32.inc
;=====================================================================================
; CONSTANTS
; Constants---------------------------------------------------------------------------
MAX EQU <400> ; max output limit
;=====================================================================================
.data
;-------------------------------------------------------------------------------------
; DATA
;-------------------------------------------------------------------------------------
; Intros------------------------------------------------------------------------------
intro_1 BYTE "Hello! My name is Libby, and this is the Composite Number Calculator 9000! ", 0
intro_2 BYTE "This programme will ask you to imput a positive integer between 1 and 400. ", 0
intro_3 BYTE "It will then display all of the composite numbers up to the inputted place. ", 0
; Prompts-----------------------------------------------------------------------------
prompt_1 BYTE "Please enter an integer between 1 and 400: ", 0
invalid BYTE "That's out of range, I told you the range -like- 10 times. Try again. ", 0
; Spacing Results---------------------------------------------------------------------
space_6 BYTE " ", 0
space_7 BYTE " ", 0
space_8 BYTE " ", 0
; Goodbyes----------------------------------------------------------------------------
goodbye_1 BYTE "Trust my math, I'm a coder ;) ", 0
goodbye_2 BYTE "Tah-tah for now! ", 0
goodbye_3 BYTE "-Libby ", 0
; User Inputs------------------------------------------------------------------------
input DWORD ? ; user input
; Variables---------------------------------------------------------------------------
number DWORD ? ; number to be printed
factor DWORD ? ; factor used for calculations
count DWORD ? ; loop counter
lineCount DWORD ? ; loop counter for line breaks
;=====================================================================================
.code
;-------------------------------------------------------------------------------------
; MAIN
;-------------------------------------------------------------------------------------
main PROC
call introduction
call getUserData
call showComposites
call farewell
exit
main ENDP
;=====================================================================================
;-------------------------------------------------------------------------------------
; INTRODUCTION
;-------------------------------------------------------------------------------------
;Procedure to introduce the programme
;receives: none
;returns: none
;preconditions: none
;registers changed: edx
;-------------------------------------------------------------------------------------
introduction PROC
; Introduce programme and programmer---------------------------------------------
mov edx, OFFSET intro_1
call WriteString
call CrLf
; Explain programme--------------------------------------------------------------
mov edx, OFFSET intro_2
call WriteString
call CrLf
mov edx, OFFSET intro_3
call WriteString
call CrLf
call CrLf
ret
introduction ENDP
;=====================================================================================
;-------------------------------------------------------------------------------------
; GET USER DATA
;-------------------------------------------------------------------------------------
; Procedure to get user data for composite calculation
; receives: none
; returns: validated user input for global variable input
; preconditions: none
; registers changed: eax, ebx, edx
;-------------------------------------------------------------------------------------
getUserData PROC
; Ask user for positive integer-------------------------------------------------------
mov edx, OFFSET prompt_1
call WriteString
;--------------------------------------------------------------------------------
; Validate Input (is an int and greater than zero)
;--------------------------------------------------------------------------------
;Procedure to get user data for composite calculation
; receives: input is a global variable
; returns: validated user input for global variable input
; preconditions: MAX is CONSTANT det to 400
; registers changed: eax, ebx, edx
;--------------------------------------------------------------------------------
validate PROC
; Is it an integer?---------------------------------------------------------
InputStepA:
call ReadInt ; read user input
call IsDigit ; check Zero Flag (ZF)
add eax, 0
jz ValidationError ; ZF = 0 - invalid (not int)
jmp InputStepB ; ZF = 1 - is int and goes on to next step
; Is it between 1 and 400?--------------------------------------------------
InputStepB:
mov ebx, MAX ; move MIN to ebx register
cmp eax, ebx ; compare to MIN CONSTANT (-100)
jg ValidationError ; input is too high
cmp eax, 0
jle ValidationError
jmp ValidInput ; input is valid
; Input is invalid (not int or too low)-------------------------------------
ValidationError:
mov edx, OFFSET invalid ; tell user to input valid integer
call WriteString
call CrLf
mov edx, OFFSET prompt_1
call WriteString
jmp InputStepA ; Start Input Loop over
; Input is valid------------------------------------------------------------
ValidInput:
mov input, eax ; move register value to number hold
;---------------------------------------------------------------------------
ret
validate ENDP
;--------------------------------------------------------------------------------
ret
getUserData ENDP
;=====================================================================================
;-------------------------------------------------------------------------------------
; SHOW COMPOSITES
;-------------------------------------------------------------------------------------
; Procedure to validate and display composite numbers
; receives: input is a global variable
; returns: none
; preconditions: input has been validated (0 < input <= 400)
; registers changed: eax, ebx, ecx, edx
;-------------------------------------------------------------------------------------
showComposites PROC
; Intialize variables-----------------------------------------------------------------
call CrLf ; line break
mov ecx, input ; set ECX register to validated input to track loop
mov number, 3 ; number to start loop (we know 1, 2, 3 are prime)
mov factor, 1 ; factor to start loop (can't divide by zero or 1)
;----------------------------------------------------------------------
;| NOTE: both number and factor are to be incremented at the |
;| very start of the caculation loop so the calculations |
;| to find composities will actually begin with |
;| number = 4 and factor = 2 |
;----------------------------------------------------------------------
mov count, 0 ; start overall counter at 0
mov lineCount, 0 ; start counter used for line breaks at 0
;-------------------------------------------------------------------------------------
; LOOP for composites verification----------------------------------------------------
CompositeLoop:
call isComposite ; procedure for verifying composite numbers
inc count ; increment loop counter
inc lineCount ; increment counter for line breaks
call printComposite ; procedure for printing composites
loop CompositeLoop ; loop until ECX = 0
;================================================================================
;--------------------------------------------------------------------------------
; IS COMPOSITE?
;--------------------------------------------------------------------------------
; Sub-Procedure to find composite numbers
; receives: input, number, and factor are global variables
; returns: validated composite
; preconditions: input has been validated
; registers changed: eax, ebx, edx
;--------------------------------------------------------------------------------
isComposite PROC
; Check is composite?------------------------------------------------------------
IncrementNum:
inc number ; increment number being checked
mov factor, 1 ; reset factor to 1
IncrementFact:
inc factor ; increment factor
mov eax, number ; move number value to EAX
mov ebx, factor ; move factor value to EBX
cmp ebx, eax ; compare factor to number
jge IncrementNum ; if factor greater than number move on
; to next number
mov edx, 0 ; set EDX to 0
div ebx ; divide the number by the factor (EAX/EBX)
cmp edx, 0 ; compare remainder in EDX to 0
jne IncrementFact ; if not equal jump to top to try next factor
; if the remainder is equal to 0 that means the factor is an actual
; factor f the number so the number is not prime, a.k.a. composite
ret
isComposite ENDP
;================================================================================
;--------------------------------------------------------------------------------
; PRINT COMPOSITE
;--------------------------------------------------------------------------------
; Sub-Procedure to print composite numbers
; receives: input, number, and factor are global variables
; returns: validated composite
; preconditions: input has been validated
; registers changed: eax, ebx, edx
;--------------------------------------------------------------------------------
;| NOTE: In this section the extra cedit requirements for aligning all of the |
;| outputted columns. |
;--------------------------------------------------------------------------------
printComposite PROC
; Print composite value----------------------------------------------------------
mov eax, number
call WriteDec
; Make new line?-----------------------------------------------------------------
cmp lineCount, 10
je newLine
jmp numSpace
; Adjust spacing based on value position in line-up------------------------------
numSpace:
cmp number, 9 ; if composite < 9
jle giantSpace ; use giantSpace (space of 8)
cmp number, 99 ; if composite < 99
jle bigSpace ; use bigSpace (space of 7)
jmp regSpace ; if composite => 99 use regSpace (space of 6)
; Spacing Sizes------------------------------------------------------------------
giantSpace:
mov edx, OFFSET space_8
call WriteString
jmp spacingEnd
bigSpace:
mov edx, OFFSET space_7
call WriteString
jmp spacingEnd
regSpace:
mov edx, OFFSET space_6
call WriteString
jmp spacingEnd
; Continue on new line-----------------------------------------------------------
newLine:
call CrLf
mov lineCount, 0
; End of spacing-----------------------------------------------------------------
spacingEnd:
ret
printComposite ENDP
;================================================================================
ret
showComposites ENDP
;=====================================================================================
;-------------------------------------------------------------------------------------
; FAREWELL
;-------------------------------------------------------------------------------------
; Procedure to print departing message
; receives: none
; returns: none
; preconditions: none
; registers changed: edx
;-------------------------------------------------------------------------------------
farewell PROC
; Goodbye Message---------------------------------------------------------------------
call CrLf
call CrLf
mov edx, OFFSET goodbye_1
call WriteString
call CrLf
mov edx, OFFSET goodbye_2
call WriteString
call CrLf
call CrLF
mov edx, OFFSET goodbye_3
call WriteString
call CrLf
ret
farewell ENDP
;=====================================================================================
END main