-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.s
631 lines (595 loc) · 15.4 KB
/
utils.s
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# The functions in this unit are used by more than one of the other units
# Reading files is done by both main
#
#
# Structures and Defintions
# -------------------------
# 1. vec
# 1st 8 bytes: length
# Everything after the length field till the max capacity * unit size, specified in initialization,
# is considered part of the vec
#
# Associated Functions
# --------------------
#
# utils_vec_init
# --------------
# Role
# ----
# allocate space for a vector with a certain capacity
#
# Expected
# --------
# 1. The size of a single item in the vector is in %rdi
# 2. The max number of items the vector could possibly carry is in %rsi
#
# Result
# ------
# 1. In the case of an error, -1 in %rax, pointer to the error string in %rdi, string length in %rsi
# 2. In the case of a success, 0 in %rax, the pointer to the vector in %rdi, with it's length initialized to 0
#
# Modus Operandi
# --------------
# Todo
#
# 2. heap_string
.equ STDOUT, 1
.equ SYS_OPEN, 2
.equ SYS_WRITE, 1
.section .data
.err_msg_file_doesnt_exist:
.equ ERR_MSG_FILE_DOESNT_EXIST_LEN, 30
.asciz "The input file doesn't exist.\n"
.err_msg_open_file_generic:
.equ ERR_MSG_OPEN_FILE_GENERIC_LEN, 79
.asciz "Something went wrong while opening the input file. (It's probably your fault).\n"
.err_msg_alloc_no_memory:
.equ ERR_MSG_ALLOC_NO_MEMORY_LEN, 15
.asciz "Out of memory.\n"
.err_msg_alloc_generic:
.equ ERR_MSG_ALLOC_GENERIC, 74
.asciz "Something went wrong while allocating memory. (It's probably your fault).\n"
.err_msg_read_file_generic:
.equ ERR_MSG_READ_FILE_GENERIC_LEN, 79
.asciz "Something went wrong while reading the input file. (It's probably your fault).\n"
.err_msg_create_file_generic:
.equ ERR_MSG_CREATE_FILE_GENERIC_LEN, 81
.asciz "Something went wrong while creating the output file. (It's probably your fault).\n"
.err_msg_write_file_generic:
.equ ERR_MSG_WRITE_FILE_GENERIC_LEN, 83
.asciz "Something went wrong while writing to the output file. (It's probably your fault).\n"
.newline:
.equ NEWLINE_LEN, 1
.asciz "\n"
.minus:
.equ MINUS_LEN, 1
.asciz "-"
.section .bss
.char_to_print:
.byte 0
.input_file_info_buf:
# Space to store the struct that holds file info when the
# fstat syscall is called to determine the length of the input file
.space 128
.section .text
# Role
# ----
# Prints ascii characters
#
# Expected
# --------
# 1. The address of the first character of the string is in %rdi
# 2. The length of the string is in %rsi
utils_print:
pushq %r8
pushq %r9
pushq %rax
pushq %rdx
pushq %rcx
pushq %r11
movq %rdi, %r8 # Save the string address, because the file descriptor is going into %rdi
movq %rsi, %r9 # Save the string length
movq $1, %rax # The system call to write
movq $STDOUT, %rdi
movq %r8, %rsi
movq %r9, %rdx
syscall
popq %r11
popq %rcx
popq %rdx
popq %rax
popq %r9
popq %r8
ret
utils_print_newline:
pushq %rdi
pushq %rsi
leaq .newline(%rip), %rdi
movq $1, %rsi
call utils_print
popq %rsi
popq %rdi
ret
# Role
# ----
# Finds the length of a string
#
# Expected
# --------
# 1. The address of the string is in %rdi
#
# Result
# ------
# 1. The length of the string in %rax
utils_strlen:
pushq %r8
pushq %r9
movq $0, %r8 # Initializing the string index to 0
jmp utils_strlen_loop
utils_strlen_loop:
movb (%rdi, %r8, 1), %r9b # Load the byte at index %r8
cmp $0, %r9b # Strings are null terminated, so they end with 0s
je utils_end_strlen_loop
incq %r8
jmp utils_strlen_loop
utils_end_strlen_loop:
movq %r8, %rax
popq %r9
popq %r8
ret
# Role
# ----
# Checks if 2 strings are equal
#
# Expected
# --------
# 1. The address of the 1st string is in %rdi
# 2. The address of the 2nd string is in %rsi
#
# Result
# ------
# 1. In the case where the 2 strings are equal, 1 in %rax
# 2. Else, 0 in %rax
utils_streq:
pushq %r8
pushq %r9
pushq %r10
movq $0, %r8 # Initialize the index to 0
movq $1, %rax # Initialize the strings_are_equal value to true
jmp utils_streq_loop
utils_streq_loop:
movb (%rdi, %r8, 1), %r9b # The value of the first string at index %r8
movb (%rsi, %r8, 1), %r10b # The value of the second string at index %r8
cmpb %r9b, %r10b
jne utils_streq_loop_end_fail
cmpb $0, %r9b
je utils_streq_r9_finished
cmpb $0, %r10b
je utils_streq_r10_finished
incq %r8
jmp utils_streq_loop
utils_streq_r9_finished:
cmpb $0, %r10b
je utils_streq_loop_end_success
jmp utils_streq_loop_end_fail
utils_streq_r10_finished:
cmpb $0, %r9b
je utils_streq_loop_end_success
jmp utils_streq_loop_end_fail
utils_streq_loop_end_fail:
popq %r10
popq %r9
popq %r8
movq $0, %rax
ret
utils_streq_loop_end_success:
popq %r10
popq %r9
popq %r8
ret
# Role
# ----
# Prints an integer
#
# Expected
# --------
# 1. The integer to print is in %rdi
#
# Modus Operandi
# --------------
# Given input n,
# Initialize digit count to 0
# Divide n by 10
# Push the remainder on the stack
# Compare the quotient with 0
# If the quotient is 0, break
# If it is not 0, increment digit count by 1 and go back to the Push
# Repeat the following digit-count times
# pop the value off the stack
# Add 48 to the value
# Print the value
# Print a newline
utils_printint:
pushq %r8 # Save whatever was here before
pushq %rdx
pushq %rsi
pushq %rcx
pushq %r11
movq $0, %r8 # Digit count
movq %rdi, %rax # The number to print
movq $10, %rdi # The divisor
jmp utils_printint_divide_loop
utils_printint_divide_loop:
movq $0, %rdx
idiv %rdi
pushq %rdx
incq %r8
cmp $0, %rax
je utils_printint_print_loop
jmp utils_printint_divide_loop
utils_printint_print_loop:
leaq .char_to_print(%rip), %rdi
popq %rsi
addq $48, %rsi
movq %rsi, (%rdi)
movq $1, %rsi
pushq %r8
call utils_print
popq %r8
decq %r8
cmp $0, %r8
je utils_end_printint
jmp utils_printint_print_loop
utils_end_printint:
leaq .char_to_print(%rip), %rdi
movq $10, (%rdi)
movq $1, %rsi
call utils_print
movq $0, %rax
popq %r11 # Restore whatever was here
popq %rcx
popq %rsi
popq %rdx
popq %r8
ret
# Role
# ----
# To write an integer into a file
#
# Expected
# --------
# 1. The file descriptor is in %rdi
# 2. The integer to print is in %rsi
utils_write_int_file:
pushq %r8 # Save whatever was here before
pushq %rdx
pushq %rcx
pushq %r11
pushq %r15
pushq %r9
movq %rdi, %r15 # The file descriptor
movq $0, %r8 # Digit count
movq %rsi, %rax # The number to print
movq $0, %r9 # Is number negative?
call utils_write_int_file_unnegate_number
movq $10, %rdi # The divisor
jmp utils_write_int_file_divide_loop
utils_write_int_file_divide_loop:
movq $0, %rdx
idiv %rdi
pushq %rdx
incq %r8
cmp $0, %rax
je utils_write_int_file_write
jmp utils_write_int_file_divide_loop
utils_write_int_file_write:
cmp $0, %r9
je utils_write_int_file_write_loop
movq %r15, %rdi
leaq .minus(%rip), %rsi
movq $MINUS_LEN, %rdx
call utils_write_file
jmp utils_write_int_file_write_loop
utils_write_int_file_write_loop:
leaq .char_to_print(%rip), %rsi
popq %rdx
addq $48, %rdx
movq %rdx, (%rsi)
movq $1, %rdx
movq %r15, %rdi
call utils_write_file
decq %r8
cmp $0, %r8
je utils_end_write_int_file
jmp utils_write_int_file_write_loop
utils_write_int_file_unnegate_number:
cmp $0, %rax
jge utils_return
imul $-1, %rax
movq $1, %r9
ret
utils_end_write_int_file:
movq $0, %rax
popq %r9
popq %r15
popq %r11 # Restore whatever was here
popq %rcx
popq %rdx
popq %r8
ret
# Role
# ----
# Arbitrary returns
utils_return:
ret
# Role
# ----
# Open a file
#
# Expected
# --------
# 1. The address of the filename is in %rdi
#
# Result
# ------
# 1. In the case of a success, the file descriptor in %rax, and the file size in %rsi
# 2. In the case of an error, -1 in %rax, the error string in %rdi, the error length in %rsi
.equ O_RDONLY, 0
.equ ENOENT, -2
.equ SYS_FSTAT, 5
.equ STAT_FILE_SIZE_OFFSET, 48
utils_open_file:
movq $SYS_OPEN, %rax
movq $O_RDONLY, %rsi
syscall
cmp $0, %rax
jl utils_open_file_err # In the case of an error, %rax holds -errno
pushq %rax # Save the file descriptor
movq %rax, %rdi
movq $SYS_FSTAT, %rax
leaq .input_file_info_buf(%rip), %rsi # The location to store the stat struct
pushq %rsi
syscall
cmp $0, %rax
jl utils_open_file_err
popq %rsi
xor %rdi, %rdi # To remove leading 0s
movl STAT_FILE_SIZE_OFFSET(%rsi), %edi
popq %rax # Restore the file descriptor
ret
utils_open_file_err:
cmp $ENOENT, %rax
je utils_open_file_err_file_doesnt_exist
movq $-1, %rax
leaq .err_msg_open_file_generic(%rip), %rdi
movq $ERR_MSG_OPEN_FILE_GENERIC_LEN, %rsi
ret
utils_open_file_err_file_doesnt_exist:
leaq .err_msg_file_doesnt_exist(%rip), %rdi
movq $ERR_MSG_FILE_DOESNT_EXIST_LEN, %rsi
ret
# Role
# ----
# To read a file's contents into a buffer
#
# Expected
# --------
# 1. The file descriptor is in %rdi
# 2. The file address of the buffer is in %rsi
# 3. The number of bytes to read is in %rdx
#
# Result
# ------
# 1. In the case of a success, 0 is in %rax
# 2. In the case of an error, -1 is in %rax, the error string in %rdi, the error length in %rsi
.equ SYS_READ, 0
utils_read_file:
movq $SYS_READ, %rax
syscall
cmp $0, %rax
jl utils_read_file_err
movq $0, %rax
ret
utils_read_file_err:
leaq .err_msg_read_file_generic(%rip), %rdi
movq $ERR_MSG_READ_FILE_GENERIC_LEN, %rsi
movq $-1, %rax
ret
# Role
# ----
# Create a writeable file
#
# Expected
# --------
# 1. The address of the filename is in %rdi
#
# Result
# ------
# 1. In the case of a success, the file descriptor is in %rax
# 2. In the case of a failure, -1 is in %rax, the error string in %rdi, the error length in %rsi
# .equ SYS_OPEN, 85
.equ OPS, 577
.equ O_PERM, 0666
utils_create_file:
movq $SYS_OPEN, %rax
movq $OPS, %rsi
movq $O_PERM, %rdx
syscall
cmp $0, %rax
jl utils_create_file_err
ret
utils_create_file_err:
leaq .err_msg_create_file_generic(%rip), %rdi
movq $ERR_MSG_CREATE_FILE_GENERIC_LEN, %rsi
movq $-1, %rax
ret
# Role
# ----
# Write to a file
#
# Expected
# --------
# 1. File descriptor of file to write to is in %rdi
# 2. Buffer to write from is in %rsi
# 3. Number of bytes to write is in %rdx
#
# Result
# ------
# 1. On success, 0 is in %rax
# 2. On failure, -1 is in %rax
utils_write_file:
pushq %r11
movq $SYS_WRITE, %rax
syscall
cmp $0, %rax
jl utils_write_file_err
movq $0, %rax
popq %r11
ret
utils_write_file_err:
popq %r11
leaq .err_msg_write_file_generic(%rip), %rdi
movq $ERR_MSG_WRITE_FILE_GENERIC_LEN, %rsi
movq $-1, %rax
ret
# Role
# ----
# Allocate space to store input file content and tokens
# Note: this function is to be called only once
#
# Expected
# --------
# 1. The input file descriptor is in %rdi
# 2. The number of bytes in the file is in %rsi
#
# Result
# ------
# 1. In the case of a success, the addresses of the locations to store the input file content,
# the tokens and the expression structure are in %rax, %rdi and %rsi respectively
# 2. In the case of an error, -1 in %rax, the error string in %rdi, the error length in %rsi
#
# Definitions
# -----------
# In the comments, n means number of bytes in the file
# In the following,
# %r14 holds the input file descriptor
# %r15 holds the number of bytes in the input file
# %rax either holds the syscall number for brk or the top of the data segment
# %r8 temporarily holds the address of the input file content buffer
.equ SYS_BRK, 12
.equ ENOMEM, -12
utils_alloc_main_space:
movq %rdi, %r14 # Saving the input file descriptor in %r14
movq %rsi, %r15 # Saving the number of bytes in the file in %r15
movq $SYS_BRK, %rax
movq $-1, %rdi
syscall # To find the current position of the data segment
cmp $0, %rax
jl utils_alloc_err
movq %rax, %r8 # Saving the address of the data segment top in %r8
incq %r8 # %r8 now contains the address of the data segment top. The next address is the input file content base
addq %r15, %rax # Increase the data segment by file length, to create space for the file
movq %rax, %rdi # To become the new top of data segment
movq $SYS_BRK, %rax # To create space for the file contents
syscall
cmp $0, %rax
jl utils_alloc_err
movq %r8, %rax # The base address of the input file contents
movq $TOKEN_SIZE, %r13
imul %r15, %r13 # Number of bytes to store n tokens (tokens can't be more than that)
movq %rdi, %rsi
incq %rsi # The address to be returned as the base of the token array
addq %r13, %rdi # To become the new top of data segment
movq $SYS_BRK, %rax
syscall
cmp $0, %rax
jl utils_alloc_err
movq %r8, %rax
movq %rsi, %rdi
ret
# Role
# ----
# Allocate space arbitrarily
#
# Expected
# --------
# 1. The amount of space in bytes is in %rdi
#
# Result
# ------
# 1. In the case of a success, the address of the allocated space is in %rax
# 2. In the case of an error, -1 in %rax, the error string in %rdi, the error length in %rsi
utils_alloc:
pushq %r11
pushq %rcx
pushq %r8
pushq %rdi
pushq %rdi # Save the number of bytes to allocate
movq $SYS_BRK, %rax
movq $-1, %rdi # To get the current top of the data segment
syscall
cmp $0, %rax
jl utils_alloc_err
movq %rax, %r8
incq %r8 # The current top of data segment + 1 will become the base of the space allocated
popq %rdi # Restore the number of bytes to allocate
addq %rax, %rdi # To become the new top of data segment
movq $SYS_BRK, %rax
syscall
cmp $0, %rax
jl utils_alloc_err
movq %r8, %rax # The base address of the newly allocated space
popq %rdi
popq %r8
popq %rcx
popq %r11
ret
utils_alloc_err:
cmp $ENOMEM, %rax
je utils_alloc_err_no_memory
movq $-1, %rax
leaq .err_msg_alloc_generic(%rip), %rax
movq $ERR_MSG_ALLOC_GENERIC, %rdi
ret
utils_alloc_err_no_memory:
movq $-1, %rax
leaq .err_msg_alloc_no_memory(%rip), %rax
movq $ERR_MSG_ALLOC_NO_MEMORY_LEN, %rdi
ret
# Role
# ----
# Saves all registers on then stack.
# To be used with utils_restore_regs because they are order dependent
utils_save_regs:
pushq %rax
pushq %rbx
pushq %rcx
pushq %rdx
pushq %rdi
pushq %rsi
pushq %r8
pushq %r9
pushq %r10
pushq %r11
pushq %r12
pushq %r13
pushq %r14
pushq %r15
# Role
# ----
# Restores all registers previously saved with utils_save_regs
utils_restore_regs:
popq %r15
popq %r14
popq %r13
popq %r12
popq %r11
popq %r10
popq %r9
popq %r8
popq %rsi
popq %rdi
popq %rdx
popq %rcx
popq %rbx
popq %rax