summaryrefslogtreecommitdiff
path: root/boot/common-boot.s
blob: a6fb04eb5e5a74698bee2d10428af9f3970315c9 (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

.global _is_alpha, _is_digit, _is_alnum, _is_upper, _is_lower
.global _write_out, _read_file, _write_error, _put_char, _printi
.global _get, _memcmp, _memchr, _memmem, _memcpy
.global _divide_by_zero_error, _exit
.global _strings_index

.section .rodata

.equ SYS_READ, 63
.equ SYS_WRITE, 64
.equ SYS_EXIT, 93
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2

new_line: .ascii "\n"

.section .text

# Write the current token to stderr. Ends the output with a newline.
#
# a0 - String pointer.
# a1 - String length.
.type _write_error, @function
_write_error:
	mv t0, a0
	mv t1, a1

	li a0, STDERR
	mv a1, t0
	mv a2, t1
	li a7, SYS_WRITE
	ecall

	li a0, STDERR
	la a1, new_line
	li a2, 1
	li a7, SYS_WRITE
	ecall

	ret

# a0 - First pointer.
# a1 - Second pointer.
# a2 - The length to compare.
#
# Returns 0 in a0 if memory regions are equal.
.type _memcmp, @function
_memcmp:
	mv t0, a0
	li a0, 0

.Lmemcmp_loop:
	beqz a2, .Lmemcmp_end

	lbu t1, (t0)
	lbu t2, (a1)
	sub a0, t1, t2

	bnez a0, .Lmemcmp_end

	addi t0, t0, 1
	addi a1, a1, 1
	addi a2, a2, -1

	j .Lmemcmp_loop

.Lmemcmp_end:
	ret

# Detects if a0 is an uppercase character. Sets a0 to 1 if so, otherwise to 0.
.type _is_upper, @function
_is_upper:
	li t0, 'A' - 1
	sltu t1, t0, a0 # t1 = a0 >= 'A'

	sltiu t2, a0, 'Z' + 1 # t2 = a0 <= 'Z'
	and a0, t1, t2 # t1 = a0 >= 'A' & a0 <= 'Z'

	ret

# Detects if a0 is an lowercase character. Sets a0 to 1 if so, otherwise to 0.
.type _is_lower, @function
_is_lower:
	li t0, 'a' - 1
	sltu t2, t0, a0 # t2 = a0 >= 'a'

	sltiu t3, a0, 'z' + 1 # t3 = a0 <= 'z'
	and a0, t2, t3 # t2 = a0 >= 'a' & a0 <= 'z'

	ret

# Detects if the passed character is a 7-bit alpha character or an underscore.
# The character is passed in a0.
# Sets a0 to 1 if the character is an alpha character or underscore, sets it to 0 otherwise.
.type _is_alpha, @function
_is_alpha:
	# Prologue.
	addi sp, sp, -16
	sw ra, 12(sp)
	sw s0, 8(sp)
	addi s0, sp, 16

	sw a0, 4(sp)

	call _is_upper
	sw a0, 0(sp)

	lw a0, 4(sp)
	call _is_lower

	lw t0, 4(sp)
	xori t1, t0, '_'
	seqz t1, t1

	lw t0, 0(sp)
	or a0, a0, t0
	or a0, a0, t1

	# Epilogue.
	lw ra, 12(sp)
	lw s0, 8(sp)
	addi sp, sp, 16
	ret

# Detects whether the passed character is a digit
# (a value between 0 and 9).
#
# Parameters:
# a0 - Exemined value.
#
# Sets a0 to 1 if it is a digit, to 0 otherwise.
.type _is_digit, @function
_is_digit:
	li t0, '0' - 1
	sltu t1, t0, a0 # t1 = a0 >= '0'

	sltiu t2, a0, '9' + 1 # t2 = a0 <= '9'

	and a0, t1, t2

	ret

.type _is_alnum, @function
_is_alnum:
	# Prologue.
	addi sp, sp, -16
	sw ra, 12(sp)
	sw s0, 8(sp)
	addi s0, sp, 16

	sw a0, 4(sp)

	call _is_alpha
	sw a0, 0(sp)

	lw a0, 4(sp)
	call _is_digit

	lw a1, 0(sp)
	or a0, a0, a1

	# Epilogue.
	lw ra, 12(sp)
	lw s0, 8(sp)
	addi sp, sp, 16
	ret

.type _write_out, @function
_write_out:
	# Prologue.
	addi sp, sp, -8
	sw ra, 4(sp)
	sw s0, 0(sp)
	addi s0, sp, 8

	mv a2, a1
	mv a1, a0
	li a0, STDOUT
	li a7, SYS_WRITE
	ecall

	# Epilogue.
	lw ra, 4(sp)
	lw s0, 0(sp)
	addi sp, sp, 8
	ret

# Reads standard input into a buffer.
# a0 - Buffer pointer.
# a1 - Buffer size.
#
# Sets s1 to the buffer passed in a0.
#
# Returns the amount of bytes written in a0.
.type _read_file, @function
_read_file:
	# Prologue.
	addi sp, sp, -8
	sw ra, 4(sp)
	sw s0, 0(sp)
	addi s0, sp, 8

	mv s1, a0

	li a0, STDIN
	mv a2, a1
	mv a1, s1
	li a7, SYS_READ
	ecall

	# Epilogue.
	lw ra, 4(sp)
	lw s0, 0(sp)
	addi sp, sp, 8
	ret

# Terminates the program. a0 contains the return code.
#
# Parameters:
# a0 - Status code.
.type _exit, @function
_exit:
	li a7, SYS_EXIT
	ecall
	# ret

.type _divide_by_zero_error, @function
_divide_by_zero_error:
    addi a7, zero, 172 # getpid
    ecall

    addi a1, zero, 8 # SIGFPE
    addi a7, zero, 129 # kill
    ecall
    ret

# a0 - Whole number.
# t1 - Constant 10.
# a1 - Local buffer.
# t2 - Current character.
# t3 - Whether the number is negative.
.type printi, @function
_printi:
    addi sp, sp, -16
    sw s0, 0(sp)
    sw ra, 4(sp)
    addi s0, sp, 16
    addi t1, zero, 10
    addi a1, s0, -1

    addi t3, zero, 0
    bge a0, zero, .digit10
    addi t3, zero, 1
    sub a0, zero, a0

.digit10:
    rem t2, a0, t1
    addi t2, t2, '0'
    sb t2, 0(a1)
    div a0, a0, t1
    addi a1, a1, -1
    bne zero, a0, .digit10

    beq zero, t3, .write_call
    addi t2, zero, '-'
    sb t2, 0(a1)
    addi a1, a1, -1

.write_call:
    addi a0, zero, 1
    addi a1, a1, 1
    sub a2, s0, a1
    addi a7, zero, 64 # write
    ecall

    lw s0, 0(sp)
    lw ra, 4(sp)
    addi sp, sp, 16
    ret

# Writes a character from a0 into the standard output.
.type _put_char, @function
_put_char:
	# Prologue
	addi sp, sp, -16
	sw ra, 12(sp)
	sw s0, 8(sp)
	addi s0, sp, 16

	sb a0, 4(sp)
	li a0, STDOUT
	addi a1, sp, 4
	li a2, 1
	li a7, SYS_WRITE
	ecall

	# Epilogue.
	lw ra, 12(sp)
	lw s0, 8(sp)
	add sp, sp, 16
	ret

# a0 - Pointer to an array to get the first element.
#
# Dereferences a pointer and returns what is on the address in a0.
.type _get, @function
_get:
	lw a0, (a0)
	ret

# Searches for the occurences of a character in the given memory block.
#
# Parameters:
# a0 - Memory block.
# a1 - Needle.
# a2 - Memory size.
#
# Sets a0 to the pointer to the found character or to null if the character
# doesn't occur in the memory block.
.type _memchr, @function
_memchr:
.Lmemchr_loop:
	beqz a2, .Lmemchr_nil # Exit if the length is 0.

	lbu t0, (a0) # Load the character from the memory block.
	beq t0, a1, .Lmemchr_end # Exit if the character was found.

	# Otherwise, continue with the next character.
	addi a0, a0, 1
	addi a2, a2, -1

	j .Lmemchr_loop

.Lmemchr_nil:
	li a0, 0

.Lmemchr_end:
	ret

# Locates a substring.
#
# Parameters:
# a0 - Haystack.
# a1 - Haystack size.
# a2 - Needle.
# a3 - Needle size.
#
# Sets a0 to the pointer to the beginning of the substring in memory or to 0
# if the substring doesn't occur in the block.
.type _memmem, @function
_memmem:
	# Prologue.
	addi sp, sp, -24
	sw ra, 20(sp)
	sw s0, 16(sp)
	addi s0, sp, 24

	# Save preserved registers. They are used to keep arguments.
	sw s1, 12(sp)
	sw s2, 8(sp)
	sw s3, 4(sp)
	sw s4, 0(sp)

	mv s1, a0
	mv s2, a1
	mv s3, a2
	mv s4, a3

.Lmemmem_loop:
	blt s2, s3, .Lmemmem_nil # Exit if the needle length is greater than memory.

	mv a0, s1
	mv a1, s3
	mv a2, s4
	call _memcmp

	mv t0, a0 # memcmp result.
	mv a0, s1 # Memory pointer for the case the substring was found.
	beqz t0, .Lmemmem_end

	addi s1, s1, 1
	add s2, s2, -1

	j .Lmemmem_loop

.Lmemmem_nil:
	li a0, 0

.Lmemmem_end:

	# Restore the preserved registers.
	lw s1, 12(sp)
	lw s2, 8(sp)
	lw s3, 4(sp)
	lw s4, 0(sp)

	# Epilogue.
	lw ra, 20(sp)
	lw s0, 16(sp)
	add sp, sp, 24
	ret

# Copies memory.
#
# Parameters:
# a0 - Destination.
# a1 - Source.
# a2 - Size.
#
# Preserves a0.
.type _memcpy, @function
_memcpy:
	mv t0, a0

.Lmemcpy_loop:
	beqz a2, .Lmemcpy_end

	lbu t1, (a1)
	sb t1, (a0)

	addi a0, a0, 1
	addi a1, a1, 1

.Lmemcpy_end:
	mv a0, t0
	ret

# Searches for a string in a string array.
#
# Parameters:
# a0 - Number of elements in the string array.
# a1 - String array.
# a2 - Needle length.
# a3 - Needle.
#
# Sets a0 to the 1-based index of the needle in the haystack or to 0 if the
# element could not be found.
.type _strings_index, @function
_strings_index:
	# Prologue.
	addi sp, sp, -32
	sw ra, 28(sp)
	sw s0, 24(sp)
	addi s0, sp, 32

	sw s1, 20(sp)
	mv s1, a0
	sw s2, 16(sp)
	mv s2, a1
	sw s3, 12(sp)
	mv s3, a2
	sw s4, 8(sp)
	mv s4, a3
	sw s5, 4(sp)
	li s5, 0 # Index counter.

.Lstrings_index_loop:
	addi s5, s5, 1
	beqz s1, .Lstrings_index_missing

	lw a2, (s2) # Read the length of the current element in the haystack.
	bne a2, s3, .Lstrings_index_next # Lengths don't match, skip the iteration.

	addi a0, s2, 4
	mv a1, s4
	call _memcmp

	beqz a0, .Lstrings_index_end

.Lstrings_index_next:
	addi s2, s2, 4
	add s2, s2, a2
	addi s1, s1, -1
	j .Lstrings_index_loop

.Lstrings_index_missing:
	li s5, 0

.Lstrings_index_end:
	mv a0, s5

	lw s1, 20(sp)
	lw s2, 16(sp)
	lw s3, 12(sp)
	lw s4, 8(sp)
	lw s5, 4(sp)

	# Epilogue.
	lw ra, 28(sp)
	lw s0, 24(sp)
	add sp, sp, 32
	ret