-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalin.asm
More file actions
153 lines (144 loc) · 2.38 KB
/
palin.asm
File metadata and controls
153 lines (144 loc) · 2.38 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
# palin.asm — Hexadecimal palindrome on 32-bit word (RV32I, RARS).
# In: a0/x10 = value. Out: 1 if hex digits palindrome, 0 if not, -1 if input is 0.
.globl palin
palin:
addi sp, sp, -4
sw ra, 0(sp)
beqz a0, invalid
mv t0, a0
li t1, 0xfffffff0
li t2, 0
hexlenloop:
beqz t0, evenorodd
and t0, t0, t1
addi t2, t2, 1
slli t1, t1, 4
j hexlenloop
evenorodd:
mv a3, t2
li t5, 1
li t3, 2
beq t2, t5, palindrometrue
beq t2, t3, notpalindrome
rem t3, t2, t3
beqz t3, evensplit
j oddsplit
evensplit:
mv t0, a0
li t1, 0xffffffff
li t3, 2
div t2, t2, t3 #how many bits to shift the mask to get the first half
li t3, 0
forloop1:
beq t3, t2, maskcomplete
slli t1, t1, 4
addi t3, t3, 1
j forloop1
maskcomplete:
and t5, t0, t1 #set t5 to first half
li t4, 4
mul t2, t2, t4
srl t5, t5, t2
div t2, t2, t4
li t3, 0
li t1, 0xffffffff
forloop2:
beq t3, t2, maskcomplete2
slli t1, t1, 4
addi t3, t3, 1
j forloop2
maskcomplete2:
not t1, t1
and t4, t0, t1 #set t4 to second half
mv a1, t4
mv a2, t5
mv t1, a1
li t0, 0
li t5, 1
mv t6, a3
li t4, 2
div t6, t6, t4
addi t6, t6, -1
li t4, 0x0000000f
and t2, t1, t4
slli t2, t2, 4
forloop3:
slli t4, t4, 4
and t3, t1, t4
addi t0, t0, 4
srl t3, t3, t0
or t2, t2, t3
bgeu t5, t6, nowcompare
addi t5, t5, 1
bgeu t6, t5, shiftleft
shiftleft:
slli t2, t2, 4
beq t5, t5, forloop3
oddsplit:
mv t0, a0
li t1, 0xffffffff
li t3, 2
div t2, t2, t3 #how many bits to shift the mask to get the first half
addi t2, t2, 1
li t3, 0
forloop4:
beq t3, t2, maskcomplete3
slli t1, t1, 4
addi t3, t3, 1
j forloop4
maskcomplete3:
and t5, t0, t1 #set t5 to first half
li t4, 4
mul t2, t2, t4
srl t5, t5, t2
div t2, t2, t4
addi t2, t2, -1
li t3, 0
li t1, 0xffffffff
forloop5:
beq t3, t2, maskcomplete4
slli t1, t1, 4
addi t3, t3, 1
j forloop5
maskcomplete4:
not t1, t1
and t4, t0, t1 #set t4 to second half
mv a1, t4
mv a2, t5
mv t1, a1
li t0, 0
li t5, 1
mv t6, a3
li t4, 2
div t6, t6, t4
addi t6, t6, -1
li t4, 0x0000000f
and t2, t1, t4
slli t2, t2, 4
forloop6:
slli t4, t4, 4
and t3, t1, t4
addi t0, t0, 4
srl t3, t3, t0
or t2, t2, t3
bgeu t5, t6, nowcompare
addi t5, t5, 1
bgeu t6, t5, shiftleft2
shiftleft2:
slli t2, t2, 4
beq t5, t5, forloop6
nowcompare:
beq t2, a2, palindrometrue
j notpalindrome
palindrometrue:
li a0, 1
j exit
notpalindrome:
li a0, 0
j exit
invalid:
li a0, -1
exit:
lw ra, 0(sp)
addi sp, sp, 4
ret