aboutsummaryrefslogtreecommitdiff
path: root/boot/common-boot.s
blob: bfd1dbb57c763488978aa8359d5a6c1d9b8c786b (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
.global is_alpha, is_digit, is_alnum, write_out, read_file, exit

.section .rodata

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

.section .text

# 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:
	li t0, 'A' - 1
	sltu t1, t0, a0 # t1 = a0 >= 'A'

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

	li t0, 'a' - 1
	sltu t2, t0, a0 # t2 = a0 >= 'a'

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

	xori t3, a0, '_'
	seqz t3, t3

	or a0, t1, t2
	or a0, a0, t3
	ret

.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, @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.
#
# Returns the result 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 a2, a1
	mv a1, a0
	li a0, STDIN
	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.
exit:
	li a7, SYS_EXIT
	ecall