/
gw_prog1.asm
456 lines (379 loc) · 6.78 KB
/
gw_prog1.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
include \Irvine\Irvine32.inc
includelib \Irvine\Irvine32.lib
.DATA
InputMessage DB 'Please enter a sentence up to 50 characters: ', 0
MenuMessage1 DB 13, 10, 'Function 1: Enter a letter and get the position of the first occurrence in the string.', 13, 10,
'Function 2: Enter a letter and get the number of occurrences in the string.', 13, 10,
'Function 3: Get the length of the string.', 13, 10,
'Function 4: Get the number of alphanumeric characters in the string.', 13, 10, 0
MenuMessage2 DB 'Function 5: Enter a letter to be replaced and a letter that will do the replacing in the string.', 13, 10,
'Function 6: Capitalize the letters in the string.', 13, 10,
'Function 7: Make all the letters in the string lower case.', 13, 10,
'Function 8: Toggle the case of each letter in the string.', 13, 10, 0
MenuMessage3 DB 'Function 9: Input a new string.', 13, 10,
'Function 10: Undo the last action that modified the string.', 13, 10,
'Function 100: Output this menu again.', 13, 10,
'Function 0: Exit the program.', 13, 10, 0
MAX_CHARS DB 50
InputString DB 51 DUP (?)
InputStringCopy DB 51 DUP (?)
GetFunctionMessage DB 13, 10, 'Please enter a function code: ', 0
BadFunctionMessage DB 'Invalid function code.', 0
GetCharMessage DB 'Please enter a character: ', 0
GetReplacerMessage DB 13, 10, 'Please enter a character to insert in place of the last entered character: ', 0
NewStringMessage DB 13, 10, 'The new string is: ', 0
OccurencesMessage DB 13, 10, 'Occurences found in string: ', 0
IndexFoundMessage DB 13, 10, 'First occurence found at index: ', 0
CharNotFoundMessage DB 13, 10, 'Character was not found in string.', 0
StringLengthMessage DB 'Length of string: ', 0
AlphaNumericMessage DB 'Number of alphanumerics found in string: ', 0
ErrorMessage DB 13, 10, 'An error has occurred. Please press any key to exit . . . .', 0
.CODE
Main PROC
call Function9
call Function100
GetFunctionCode:
lea edx, GetFunctionMessage
call WriteString
call ReadInt
jo BadFunctionCode
cmp eax, 0
je ExitMain
cmp eax, 1
je CallFunction1
cmp eax, 2
je CallFunction2
cmp eax, 3
je CallFunction3
cmp eax, 4
je CallFunction4
cmp eax, 5
je CallFunction5
cmp eax, 6
je CallFunction6
cmp eax, 7
je CallFunction7
cmp eax, 8
je CallFunction8
cmp eax, 9
je CallFunction9
cmp eax, 10
je CallFunction10
cmp eax, 100
je CallFunction100
jmp BadFunctionCode
CallFunction1:
call Function1
jmp GetFunctionCode
CallFunction2:
call Function2
jmp GetFunctionCode
CallFunction3:
call Function3
jmp GetFunctionCode
CallFunction4:
call Function4
jmp GetFunctionCode
CallFunction5:
call CopyInputString
call Function5
jmp OutputNewString
CallFunction6:
call CopyInputString
call Function6
jmp OutputNewString
CallFunction7:
call CopyInputString
call Function7
jmp OutputNewString
CallFunction8:
call CopyInputString
push 0
call Function8
jmp OutputNewString
CallFunction9:
call CopyInputString
call Function9
jmp OutputNewString
CallFunction10:
call Function10
call CopyInputString
jmp OutputNewString
CallFunction100:
call Function100
jmp GetFunctionCode
OutputNewString:
lea edx, NewStringMessage
call WriteString
lea edx, InputString
call WriteString
jmp GetFunctionCode
BadFunctionCode:
lea edx, BadFunctionMessage
call WriteString
jmp GetFunctionCode
GenericError:
lea edx, ErrorMessage
call WriteString
call ReadChar
ExitMain: invoke ExitProcess, 0
Main ENDP
Function1 PROC NEAR
lea edx, GetCharMessage
call WriteString
call ReadChar
xor cx, cx
xor dx, dx
mov cl, MAX_CHARS
lea ebx, InputString
NextChar:
jcxz EndOfString
mov dl, [ebx]
inc bx
dec cx
cmp dl, al
je Found
jmp NextChar
Found:
sub cx, 50
neg cx
xor eax, eax
mov ax, cx
lea edx, IndexFoundMessage
call WriteString
call WriteInt
jmp Done
EndOfString:
lea edx, CharNotFoundMessage
call WriteString
Done:
RET
Function1 ENDP
Function2 PROC NEAR
lea edx, GetCharMessage
call WriteString
call ReadChar
xor cx, cx
xor dx, dx
mov cl, MAX_CHARS
lea ebx, InputString
NextChar:
jcxz EndOfString
mov dl, [ebx]
inc ebx
dec cx
cmp dl, al
je Match
jmp NextChar
Match:
inc dh
jmp NextChar
EndOfString:
xor eax, eax
mov al, dh
lea edx, OccurencesMessage
call WriteString
call WriteInt
RET
Function2 ENDP
Function3 PROC NEAR
xor cx, cx
xor dx, dx
mov cl, MAX_CHARS
lea ebx, InputString
NextChar:
jcxz EndOfString
mov dl, [ebx]
inc ebx
dec cx
cmp dl, 0
je EndOfString
jmp NextChar
EndOfString:
xor eax, eax
sub cx, 49
neg cx
mov ax, cx
lea edx, StringLengthMessage
call WriteString
call WriteInt
RET
Function3 ENDP
Function4 PROC NEAR
xor cx, cx
xor dx, dx
mov cl, MAX_CHARS
lea ebx, InputString
NextChar:
jcxz EndOfString
mov dl, [ebx]
inc ebx
dec cx
cmp dl, 0
je EndOfString
NumberCheck:
cmp dl, 30h
jl NextChar
cmp dl, 39h
jg UpperCaseCheck
inc dh
jmp NextChar
UpperCaseCheck:
cmp dl, 41h
jl NextChar
cmp dl, 5ah
jg LowerCaseCheck
inc dh
jmp NextChar
LowerCaseCheck:
cmp dl, 61h
jl NextChar
cmp dl, 7ah
jg NextChar
inc dh
jmp NextChar
EndOfString:
xor eax, eax
mov al, dh
lea edx, AlphaNumericMessage
call WriteString
call WriteInt
RET
Function4 ENDP
Function5 PROC NEAR
lea edx, GetCharMessage
call WriteString
call ReadChar
mov bl, al
lea edx, GetReplacerMessage
call WriteString
call ReadChar
mov ah, bl
xor cx, cx
mov cl, MAX_CHARS
lea ebx, InputString
NextChar:
jcxz Done
mov dl, [ebx]
inc ebx
dec cx
cmp dl, 0
je Done
cmp dl, ah
jne NextChar
mov [ebx - 1], al
jmp NextChar
Done:
RET
Function5 ENDP
Function6 PROC NEAR
push 1
call Function8
RET
Function6 ENDP
Function7 PROC NEAR
push -1
call Function8
RET
Function7 ENDP
Function8 PROC NEAR, case:BYTE
mov ah, case
xor cx, cx
mov cl, MAX_CHARS
lea ebx, InputString
NextChar:
jcxz Done
mov al, [ebx]
inc ebx
dec cx
cmp dl, 0
je Done
cmp al, 41h
jl NextChar
cmp al, 5ah
jle FixCase
cmp al, 61h
jl NextChar
cmp al, 7ah
jle FixCase
jmp NextChar
FixCase:
cmp ah, -1
je LowerCase
cmp ah, 1
je UpperCase
xor al, 20h
mov [ebx - 1], al
jmp NextChar
LowerCase:
or al, 20h
mov [ebx - 1], al
jmp NextChar
UpperCase:
shl al, 3
shr al, 3
or al, 40h
mov [ebx - 1], al
jmp NextChar
Done:
RET
Function8 ENDP
Function9 PROC NEAR
GetInputString:
lea edx, InputMessage
call WriteString
lea edx, InputString
xor ecx, ecx
mov cl, MAX_CHARS
call ReadString
cmp InputString, 10
je GetInputString
RET
Function9 ENDP
Function10 PROC NEAR
xor cx, cx
mov cl, MAX_CHARS
inc cl
lea ebx, InputStringCopy
lea edx, InputString
mov al, [ebx]
cmp al, 0
je Done
NextChar:
jcxz Done
mov al, [ebx]
mov [edx], al
inc ebx
inc edx
dec cx
jmp NextChar
Done:
RET
Function10 ENDP
CopyInputString PROC NEAR
xor cx, cx
mov cl, MAX_CHARS
inc cl
lea ebx, InputString
lea edx, InputStringCopy
NextChar:
jcxz Done
mov al, [ebx]
mov [edx], al
inc ebx
inc edx
dec cx
jmp NextChar
Done:
RET
CopyInputString ENDP
Function100 PROC NEAR
lea edx, MenuMessage1
call WriteString
lea edx, MenuMessage2
call WriteString
lea edx, MenuMessage3
call WriteString
RET
Function100 ENDP
END Main