-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment6.asm
More file actions
418 lines (335 loc) · 8.82 KB
/
assignment6.asm
File metadata and controls
418 lines (335 loc) · 8.82 KB
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
; Author: Kristy Nguyen
; Section: 1001
; Date Last Modified: 10/9/2020
; Program Description: This program will explore creating and using functions in assembly.
; * Attempted function 1, could not do function 2, completed functions 3 & 4
; Determines the number of characters (including null) of the provided string
; Argument 1: Address of string
; Returns length in rdx
%macro findLength 1
push rcx
mov rdx, 1
%%countLettersLoop:
mov cl, byte[%1 + rdx - 1]
cmp cl, NULL
je %%countLettersDone
inc rdx
loop %%countLettersLoop
%%countLettersDone:
pop rcx
%endmacro
; Outputs error message and stops program execution
; Argument 1: Address of error message
%macro endOnError 1
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, %1
findLength %1 ; rdx set by macro findLength
syscall
jmp endProgram
%endMacro
section .data
; System Service Call Constants
SYSTEM_EXIT equ 60
SUCCESS equ 0
SYSTEM_WRITE equ 1
SYSTEM_READ equ 0
STANDARD_OUT equ 1
STANDARD_IN equ 0
; ASCII Values
NULL equ 0
LINEFEED equ 10
; Program Constraints
MINIMUM_ARRAY_SIZE equ 2
MAXIMUM_ARRAY_SIZE equ 10000
MINIMUM_RANGE_SIZE equ 1
MAXIMUM_RANGE_SIZE equ 100000
INPUT_LENGTH equ 20
; OUTPUT_LENGTH equ 11
; VALUES_PER_LINE equ 5
; Labels / Useful Strings
labelHeader db "Sorted Random Number Generator", LINEFEED, LINEFEED, NULL
labelSorted db "Sorted Random Numbers", LINEFEED, LINEFEED, NULL
; Prompts
promptCount db "Number of values to generate (2-10,000): ", NULL
promptRange db "Maximum Value (1-100,000): ", NULL
; Error Messages
; Array Length
errorArrayMinimum db LINEFEED, "Error - Program can only generate at least 2 value.", LINEFEED, LINEFEED, NULL
errorArrayMaximum db LINEFEED, "Error - Program can only generate at most 10,000 values.", LINEFEED, LINEFEED, NULL
; Range Value
errorRangeMinimum db LINEFEED, "Error - Program can only generate maximum value of at least 1.", LINEFEED, LINEFEED, NULL
errorRangeMaximum db LINEFEED, "Error - Program can only generate minimum value of at most 100,000.", LINEFEED, LINEFEED, NULL
; Decimal String Conversion
errorStringUnexpected db LINEFEED,"Error - Unexpected character found in input." , LINEFEED, LINEFEED, NULL
errorStringNoDigits db LINEFEED,"Error - Value must contain at least one numeric digit." , LINEFEED, LINEFEED, NULL
; Input Length
errorStringTooLong db LINEFEED, "Error - Input can be at most 20 characters long." , LINEFEED, LINEFEED, NULL
; Other
arrayLength dd 0
rangeValue dd 0
; Function 1 Constants
multiplierValue dd 48271
modulusValue dd 2147483647
seedValue dd 1
section .bss
; Array of integer values, not all will necessarily be used
array resd 1000
inputString resb 21
outputString resb 11
section .text
global _start
_start:
; Output Header
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, labelHeader
findLength labelHeader ; rdx set by function findLength
syscall
; Output Array Length Prompt
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, promptCount
findLength promptCount ; rdx set by function findLength
syscall
; Read in Array Length - one character at a time
mov r8, inputString
mov r9, INPUT_LENGTH
call readString
; Convert Array Length
mov rdi, inputString
mov esi, arrayLength
call convertDecimalToInteger
; Check that Array Length is Valid - output error message and end program if not
cmp dword[arrayLength], MINIMUM_ARRAY_SIZE
jl arrayLengthErrorMinimum
cmp dword[arrayLength], MAXIMUM_ARRAY_SIZE
jg arrayLengthErrorMaximum
jmp arrayLengthVerified
arrayLengthErrorMinimum:
endOnError errorArrayMinimum
arrayLengthErrorMaximum:
endOnError errorArrayMaximum
arrayLengthVerified:
; Output Range Prompt
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, promptRange
findLength promptRange
syscall
; Read in Range Value - one character at a time
mov r8, inputString
mov r9, INPUT_LENGTH
call readString
; Convert Range Value
mov rdi, inputString
mov esi, rangeValue
call convertDecimalToInteger
; Check that Range Value is Valid - output error message and end program if not
cmp dword[rangeValue], MINIMUM_RANGE_SIZE
jl rangeErrorMinimum
cmp dword[rangeValue], MAXIMUM_RANGE_SIZE
jg rangeErrorMaximum
jmp rangeVerified
rangeErrorMinimum:
endOnError errorRangeMinimum
rangeErrorMaximum:
endOnError errorRangeMaximum
rangeVerified:
; Output Sorted Random Numbers
mov rax, SYSTEM_WRITE
mov rdi, STANDARD_OUT
mov rsi, labelSorted
findLength labelSorted
syscall
; mov edi, seedValue
; call function1
endProgram:
; Ends program with success return value
mov rax, SYSTEM_EXIT
mov rdi, SUCCESS
syscall
; ; Function 1: Random number generator using a linear congruential generator
; ; rdi: previous lcg value
; global function1
; function1:
; push rbx
; push r12
; mov r13, array
; push r13
; mov eax, dword[rdi] ; Copy doubleword value (seed = 1) into eax ( lcg(0) )
; mov r10, 0 ; Copy 0 into r10 (counter)
; ; mov r13, array ; Copy address of array into r13
; mov r11d, dword[rangeValue] ; Copy doubleword value (range) into r11d
; lcgLoop:
; mov rbx, multiplierValue ; Copy constant a into rbx
; mul rbx ; Multiply previous lcg value ( lcg(n-1) ) by multiplier (a)
; ; and store value in eax ( lcg(n) )
; mov r12, modulusValue
; div r12 ; Take modulus of eax and prime value (m)
; mov eax, edx ; and store value in edx to eax ( lcg(n) )
; mov dword[rdi], eax ; return current lcg / update lcg value
; inc r11d ; Add 1 to range
; mov edx, 0
; div r11d ; Take modulus of eax ( lcg(n) ) and r11d (range+1)
; mov eax, edx
; mov dword[r13 + r10*4], eax ; and store value in edx to array
; inc r10
; cmp r10, rangeValue ; If counter = range
; je lcgDone ; Jump to lcgDone
; jmp lcgLoop ; Otherwise, go to lcgLoop
; lcgDone:
; pop r13
; pop r12
; pop rbx
; ret
; Function 3: Modifies the decimal to integer macro from assignments 4/5
; and turn it into a function.
; rdi: null terminated string (byte array)
; rsi: dword integer variable
global convertDecimalToInteger
convertDecimalToInteger:
push rbx
mov eax, 0
mov rbx, rdi
mov r9d, 1 ; sign
mov r8d, 10 ; base
mov r10, 0 ; digits processed
checkForSpaces1:
mov cl, byte[rbx]
cmp cl, " "
jne nextCheck1
inc rbx
jmp checkForSpaces1
nextCheck1:
cmp cl, "+"
je checkForSpaces2Adjust
cmp cl, "-"
jne checkNumerals
mov r9d, -1
checkForSpaces2Adjust:
inc rbx
checkForSpaces2:
mov cl, byte[rbx]
cmp cl, " "
jne nextCheck2
inc rbx
jmp checkForSpaces2
nextCheck2:
checkNumerals:
movzx ecx, byte[rbx]
cmp cl, NULL
je finishConversion
cmp cl, " "
je checkForSpaces3
cmp cl, "0"
jb errorUnexpectedCharacter
cmp cl, "9"
ja errorUnexpectedCharacter
jmp convertCharacter
errorUnexpectedCharacter:
endOnError errorStringUnexpected
convertCharacter:
sub cl, "0"
mul r8d
add eax, ecx
inc r10
inc rbx
jmp checkNumerals
checkForSpaces3:
mov cl, byte[rbx]
cmp cl, " "
jne checkNull
inc rbx
jmp checkForSpaces3
checkNull:
cmp cl, NULL
je finishConversion
endOnError errorStringUnexpected
finishConversion:
cmp r10, 0
jne applySign
endOnError errorStringNoDigits
applySign:
mul r9d
mov dword[rsi], eax
pop rbx
ret
; Function 4: Modifies the integer to hexadecimal string macro from assignments 4/5
; and turn it into a function.
; edi: dword integer variable rdi
; rsi: string (11 byte array) rsi
global convertIntegerToHexadecimal
convertIntegerToHexadecimal:
push rbx
mov byte[rsi], "0"
mov byte[rsi+1], "x"
mov byte[rsi+10], NULL
mov rbx, rsi
add rbx, 9
mov r8d, 16 ;base
mov rcx, 8
mov eax, dword[edi]
convertHexLoop:
mov edx, 0
div r8d
cmp dl, 10
jae addA
add dl, "0" ; Convert 0-9 to "0"-"9"
jmp nextDigit
addA:
add dl, 55 ; 65 - 10 = 55 to convert 10 to "A"
nextDigit:
mov byte[rbx], dl
dec rbx
dec rcx
cmp eax, 0
jne convertHexLoop
addZeroes:
cmp rcx, 0
je endConversion
mov byte[rbx], "0"
dec rbx
dec rcx
jmp addZeroes
endConversion:
pop rbx
ret
; Clears input buffer
; No arguments
global clearInputBuffer
clearInputBuffer:
clearBufferLoop:
mov rax, SYSTEM_READ
mov rdi, STANDARD_IN
lea rsi, byte[inputString]
mov rdx, 1
syscall
cmp byte[inputString], LINEFEED
jne clearBufferLoop
ret
; Reads a string in from standard in
; r8: Address of location to place string
; r9: Integer max length of string
global readString
readString:
push rbx
mov rbx, 0
readLengthLoop:
mov rax, SYSTEM_READ
mov rdi, STANDARD_IN
lea rsi, byte[r8 + rbx]
mov rdx, 1
syscall
inc rbx
cmp byte[r8 + rbx - 1], LINEFEED
je readLengthDone
inc r9
cmp rbx, r9
jb readLengthLoop
call clearInputBuffer
endOnError errorStringTooLong
readLengthDone:
mov byte[r8 + rbx - 1], NULL
pop rbx
ret