-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proj6_fonbergi.asm
468 lines (386 loc) · 14.1 KB
/
Proj6_fonbergi.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
TITLE String Primitives and Macros (Proj6_fonbergi.asm)
; Author: Ian Fonberg
; Last Modified: 6/4/2021
; OSU email address: [email protected]
; Course number/section: CS271 Section 400
; Project Number: 6 Due Date: 6/6/2021
; Description: Implements the macros mGetString and mDisplayString to get user
; input strings and display strings using Irvine procedures ReadString and
; WriteString, respectively. These macros are used to write the low-level
; procedures ReadVal and WriteVal. ReadVal prompts the user for a signed
; integer, validates it, and stores it as an SDWORD. WriteVal takes a stored
; SDWORD and converts it to a string that is displayed to output. All of
; these macros and procedures are used to write a test script within main
; that greets the user, gets 10 signed integers from the user calculating the
; sum and displaying a subtotal along the way. Next, it displays the numbers
; back to the user along with the final sum and average of the numbers.
; Finally, the test script says goodbye to the user and exits.
INCLUDE Irvine32.inc
; ---------------------------------------------------------------------------------
; Name: mGetString
;
; Display prompt and get the user's input into a memory location.
;
; Preconditions: Do not use EAX, ECX, EDX, or EDI as arguments
;
; Postconditions: none
;
; Receives:
; getPrompt = input, offset of prompt to display
; getLength = input, value of maximum length of user input string
; getDest = output, offset of user input string
; getBytes = output, offset of number of bytes read
;
; Returns:
; getDest = raw user input
; getBytes = bytes written
; ---------------------------------------------------------------------------------
mGetString MACRO getPrompt:REQ, getLength:REQ, getDest:REQ, getBytes:REQ
PUSH EAX
PUSH ECX
PUSH EDX
PUSH EDI
; Display Prompt.
mDisplayString getPrompt
; Store Raw Input
MOV ECX, getLength ; Max limit of readable characters.
MOV EDX, getDest
CALL ReadString
MOV EDI, getBytes
MOV [EDI], EAX ; Store bytes read.
POP EDI
POP EDX
POP ECX
POP EAX
ENDM
; ---------------------------------------------------------------------------------
; Name: mDisplayString
;
; Print the string which is stored in a specified memory location.
;
; Preconditions: Do not use EDX as an argument.
;
; Postconditions: none
;
; Receives:
; displayStr = input, offset of string to display
;
; Returns: none
; ---------------------------------------------------------------------------------
mDisplayString MACRO displayStr:REQ
PUSH EDX
; Display stored string.
MOV EDX, displayStr
CALL WriteString
POP EDX
ENDM
MAX_DIGITS = 12
ARRAY_SIZE = 10
.data
firstAttempt BYTE ". Please enter a signed number: ",0
secondAttempt BYTE ". Please try again: ",0
emptyMsg BYTE "ERROR: a value is required.",13,10,0
errorMsg BYTE "ERROR: You did not enter a signed number or your number was too big.",13,10,0
rawNumString BYTE MAX_DIGITS DUP(0)
validNum SDWORD ?
intro1 BYTE "PROGRAMMING ASSIGNMENT 6: Designing low-level I/O procedures",13,10,
"Written by: Ian Fonberg",13,10,
"**EC: Number each line of user input and display a running subtotal of the user's valid numbers.",13,10,13,10,
"Please provide ",0
intro2 BYTE " signed decimal integers.",13,10,
"Each number needs to be small enough to fit inside a 32 bit register. After you have ",
"finished inputting the raw numbers I will display a list of the integers, their sum, ",
"and their average value.",13,10,13,10,0
testArr SDWORD ARRAY_SIZE DUP(?)
subtotal BYTE "Subtotal: ",0
numsEntered BYTE "You entered the following numbers: ",13,10,0
comma BYTE ", ",0
sumNums BYTE "The sum of these numbers is: ",0
avgNums BYTE "The rounded average is: ",0
goodbye BYTE "Thanks for playing!",0
lineNo BYTE 1
.code
main PROC
; -------------------------
; Test Script
; -------------------------
; Introduce test script.
mDisplayString OFFSET intro1
PUSH ARRAY_SIZE
CALL WriteVal
mDisplayString OFFSET intro2
; -------------------------
; Add user input to array.
; -------------------------
MOV EAX, 0 ; Initialize sum.
MOV ECX, ARRAY_SIZE ; Initialize counter to ARRAY_SIZE.
MOV EDI, OFFSET testArr
_readLoop:
PUSH OFFSET lineNo
PUSH OFFSET firstAttempt
PUSH OFFSET emptyMsg
PUSH OFFSET errorMsg
PUSH OFFSET secondAttempt
PUSH OFFSET rawNumString
PUSH OFFSET validNum
CALL ReadVal
; Insert signed integer using register indirect addressing.
MOV EBX, validNum
MOV [EDI], EBX
; -------------------------
; Display Subtotal.
; -------------------------
ADD EAX, EBX ; Add valid number to total.
mDisplayString OFFSET subtotal
PUSH EAX
CALL WriteVal
CALL CrLf
ADD EDI, TYPE testArr ; Move to next element.
LOOP _readLoop
CALL CrLf
; -------------------------
; Display numbers entered.
; -------------------------
mDisplayString OFFSET numsEntered
MOV ESI, OFFSET testArr
MOV ECX, ARRAY_SIZE
PUSH [ESI]
CALL WriteVal ; Display first number
DEC ECX ; Decrement counter.
_writeLoop:
; Comma separate the numbers
mDisplayString OFFSET comma
ADD ESI, TYPE testArr ; Access next number using register indirect addressing.
PUSH [ESI]
CALL WriteVal ; Display the current number.
LOOP _writeLoop
CALL CrLf
; -------------------------
; Display sum of numbers entered.
; -------------------------
mDisplayString OFFSET sumNums
PUSH EAX ; EAX holds final sum.
CALL WriteVal
CALL CrLf
; -------------------------
; Calculate and display average of numbers entered.
; -------------------------
MOV EBX, ARRAY_SIZE
CDQ
IDIV EBX
; Floor negative numbers with remainders.
CMP EDX, 0
JGE _alreadyFloored
DEC EAX
_alreadyFloored:
mDisplayString OFFSET avgNums
PUSH EAX ; EAX contains floored average.
CALL WriteVal
CALL CrLf
CALL CrLf
; Say goodbye to user.
mDisplayString OFFSET goodbye
Invoke ExitProcess,0 ; exit to operating system
main ENDP
; ---------------------------------------------------------------------------------
; Name: ReadVal
;
; Gets user input in the form of a string of digits, then attempts to convert the
; string of ASCII digits to its numeric value representation. If the input is
; invalid, the user is prompted to try again with an appropriate value, otherwise
; the value is stored in memory. Numbers the prompt with the current number
; of valid guesses.
;
; Preconditions: none
;
; Postconditions: none
;
; Receives:
; [EBP+32] = input/output, offset of current number of valid guesses.
; [EBP+28] = input, offset of first attempt prompt
; [EBP+24] = input, offset of empty error message
; [EBP+20] = input, offset of invalid error message
; [EBP+16] = input, offset of second attempt prompt
; [EBP+12] = output, offset of user input string
; [EBP+8] = output, offset of number storage
;
; Returns:
; [EBP+32] = Incremented by one.
; [EBP+8] = validated number
; ---------------------------------------------------------------------------------
ReadVal PROC USES EAX EBX ECX EDI ESI
LOCAL valid:BYTE, numInt:SDWORD, sign:SDWORD, bytesRead:DWORD
; -------------------------
; Get Integer Digits.
; -------------------------
MOV valid, 1 ; Initialize valid to true.
; Display initial prompt for signed number.
MOV ESI, [EBP+32]
PUSH [ESI]
CALL WriteVal ; Display line number.
LEA EBX, bytesRead
mGetString [EBP+28], MAX_DIGITS, [EBP+12], EBX
_validateDigits:
MOV numInt, 0 ; Initialize integer aggregator to 0.
MOV sign, 1 ; Value is unsigned.
CMP valid, 0
JNE _endTryAgain
; Prompt for new signed integer.
MOV ESI, [EBP+32]
PUSH [ESI]
CALL WriteVal ; Display line number.
LEA EBX, bytesRead
mGetString [EBP+16], MAX_DIGITS, [EBP+12], EBX
_endTryAgain:
MOV ECX, bytesRead ; Initialize counter.
INC ECX
; -------------------------
; Assert input is non-empty.
; -------------------------
; Assert a value was read.
CMP bytesRead, 0
JNE _notEmpty
; Set error state.
MOV valid, 0 ; Valid is false.
mDisplayString [EBP+24]
JMP _validateDigits
_notEmpty:
MOV ESI, [EBP+12] ; Move user input to ESI.
; -------------------------
; Assert sign is either first character or absent.
; -------------------------
CLD
LODSB
DEC ECX ; Load first character and adjust counter position.
CMP AL, 45 ; is the character a '-' sign?
JE _setNegative
CMP AL, 43 ; is the character a '+' sign?
JE _checkLength
JMP _aggregateNum ; Proceed with calculation.
_setNegative:
MOV sign, -1
_checkLength:
CMP bytesRead, 1 ; Assert sign is not the only component.
JNE _loadNext
; Set error state.
MOV valid, 0 ; Valid is false.
mDisplayString [EBP+20]
JMP _validateDigits
_loadNext:
LODSB
DEC ECX ; Load second character and adjust counter position.
_aggregateNum:
; -------------------------
; Aggregate digits into integer.
; -------------------------
_readDigit:
CMP AL, 48
JB _notDigitOrTooLarge
CMP AL, 57
JA _notDigitOrTooLarge
; ASCII value - 48 will result in an integer between 0 and 9, inclusive.
MOVZX EBX, AL
SUB EBX, 48
IMUL EBX, sign ; Calculated signed amount to add to total.
; Multiply 10x current numInt.
MOV EAX, 10
IMUL numInt
JO _notDigitOrTooLarge ; Invalid if overflow.
; Add both to get current integer value.
ADD EAX, EBX
JO _notDigitOrTooLarge ; Invalid if overflow.
MOV numInt, EAX
JMP _continueRead
_notDigitOrTooLarge:
MOV valid, 0 ; Valid is false.
mDisplayString [EBP+20]
JMP _validateDigits
_continueRead:
LODSB
LOOP _readDigit
; -------------------------
; Store number.
; -------------------------
MOV EDI, [EBP+8]
MOV EAX, numInt
MOV [EDI], EAX
; Increment line number.
MOV ESI, [EBP+32]
INC BYTE PTR [ESI]
RET 28
ReadVal ENDP
; ---------------------------------------------------------------------------------
; Name: WriteVal
;
; Convert a numeric SDWORD value to a string of ASCII digits and displays it to
; the user.
;
; Preconditions: none
;
; Postconditions: none
;
; Receives:
; [EBP+8] = input, numeric SDWORD value
;
; Returns: none
; ---------------------------------------------------------------------------------
WriteVal PROC USES EAX EBX ECX EDX EDI ESI
LOCAL signed:BYTE, digits[MAX_DIGITS]:BYTE, strNum[MAX_DIGITS]:BYTE
MOV signed, 0
LEA EDI, digits
MOV ECX, 0 ; Counter for string length
CLD
; -------------------------
; Check sign.
; -------------------------
MOV EAX, [EBP+8]
CMP EAX, 0
JGE _storeDigits
MOV signed, 1 ; Number is signed.
NEG EAX ; Use the absolute value of the number.
; -------------------------
; Store digits in reverse order
; -------------------------
_storeDigits:
MOV EDX, 0
MOV EBX, 10
DIV EBX
MOV EBX, EAX ; Store current quotient
MOV AL, DL
ADD AL, 48 ; Add 48 to remainder to get ASCII character value.
STOSB
INC ECX
MOV EAX, EBX ; Restore quotient
CMP EAX, 0
JNE _storeDigits
; Append sign if number is signed.
CMP signed, 1
JNE _terminateDigits
MOV AL, '-'
STOSB
INC ECX
; Null terminate the string.
_terminateDigits:
MOV EBX, 0
MOV [EDI], EBX
; -------------------------
; Reverse stored digits
; -------------------------
MOV ESI, EDI ; EDI currently points to end of digits which is now our source.
DEC ESI
LEA EDI, strNum
_reverseDigits:
STD
LODSB ; Move backwards through digit and load result in AL
CLD
STOSB ; Move forwards through strNum and store the result from AL
LOOP _reverseDigits
; Null terminate the string.
MOV EBX, 0
MOV [EDI], EBX
; Display string.
LEA EBX, strNum
mDisplayString EBX
RET 4
WriteVal ENDP
END main