-
Notifications
You must be signed in to change notification settings - Fork 0
/
mips1.asm
368 lines (330 loc) · 8.34 KB
/
mips1.asm
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
.data
array: .word 0x61707865, 0x3320646E, 0x79622D32, 0x6B206574
.word 0x79ffb088, 0xfa11cc82, 0xf9a9baad, 0xbb629754
.word 0xf5965531, 0x917a363d, 0x7a673c4f, 0x65c12667
.word 0x00000001, 0x00000000, 0x855f416a, 0x2fbc070a
orig: .word 0x61707865, 0x3320646E, 0x79622D32, 0x6B206574
.word 0x79ffb088, 0xfa11cc82, 0xf9a9baad, 0xbb629754
.word 0xf5965531, 0x917a363d, 0x7a673c4f, 0x65c12667
.word 0x00000001, 0x00000000, 0x855f416a, 0x2fbc070a
len: .word 16
hdr: .ascii "\nExample program to encrypt using"
.asciiz " chacha20\n\n"
newLine: .asciiz "\n"
Li: .byte 0x01 # i = 0
Lround: .byte 0x14 #19, i.e. number of rounds
Lsize: .byte 0x04
#
# text/code segment
# The main procedure to be named "main".
.text
main:
# This program will use pointers.
# Display header
# Uses print string system call
la $a0, hdr
li $v0, 4
syscall # print header
#
la $s0, array # set $s0 addr of array
lb $s1, Li #store value of i
lb $s2, Lround #store limit of i
li $s6, 0
lw $s7, len
Loop: bgt $s1, $s2, EXIT #if $s1 <= maximum $s2,continue
addi $s6, $s0, 0 #store copy of array address
andi $t8, $s1, 1 #If loop index is even, $t8=0, else 1
beq $t8, 1, Lcalc #If loop index is odd, jump to Lcalc
li $t9, 5 #first diagonal
add $t9, $t9, $t9
add $t9, $t9, $t9
add $t5, $t9, $s6 #address of array[5]
add $t6 , $t9, $t9
add $t6, $t6, $s6 #address of array[10]
add $t7, $t9, $t9
add $t7, $t7, $t9
add $t7, $t7, $s6 #address of array[15]
lw $t0, 0($s6) #get array[0]
lw $t1, 0($t5) #get array[5]
lw $t2, 0($t6) #get array[10]
lw $t3, 0($t7) #get array[15]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[0]
sw $t1, 0($t5) #store at array[5]
sw $t2, 0($t6) #store at array[10]
sw $t3, 0($t7) #store at array[15]
li $s6, 1 #second diagonal, second quarter round
add $s6, $s6, $s6
add $s6, $s6, $s6
add $s6, $s6, $s0 #address of array[1]
li $t5, 6
add $t5, $t5, $t5
add $t5, $t5, $t5
add $t5, $t5, $s0 #address of array[6]
li $t6, 11
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[11]
li $t7, 12
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[12]
lw $t0, 0($s6) #get array[1]
lw $t1, 0($t5) #get array[6]
lw $t2, 0($t6) #get array[11]
lw $t3, 0($t7) #get array[12]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[1]
sw $t1, 0($t5) #store at array[6]
sw $t2, 0($t6) #store at array[11]
sw $t3, 0($t7) #store at array[12]
li $s6, 2 #third diagonal, third quarter round
add $s6, $s6, $s6
add $s6, $s6, $s6
add $s6, $s6, $s0 #address of array[2]
li $t5, 7
add $t5, $t5, $t5 #address of array[7]
add $t5, $t5, $t5
add $t5, $t5, $s0
li $t6, 8
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[8]
li $t7, 13
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[13]
lw $t0, 0($s6) #get array[2]
lw $t1, 0($t5) #get array[7]
lw $t2, 0($t6) #get array[8]
lw $t3, 0($t7) #get array[13]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[2]
sw $t1, 0($t5) #store at array[7]
sw $t2, 0($t6) #store at array[8]
sw $t3, 0($t7) #store at array[13]
li $s6, 3 #fourth diagonal, last quarter round
add $s6, $s6, $s6
add $s6, $s6, $s6
add $s6, $s6, $s0 #address of array[3]
li $t5, 4
add $t5, $t5, $t5
add $t5, $t5, $t5
add $t5, $t5, $s0 #address of array[4]
li $t6, 9
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[9]
li $t7, 14
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[14]
lw $t0, 0($s6) #get array[3]
lw $t1, 0($t5) #get array[4]
lw $t2, 0($t6) #get array[9]
lw $t3, 0($t7) #get array[14]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[3]
sw $t1, 0($t5) #store at array[4]
sw $t2, 0($t6) #store at array[9]
sw $t3, 0($t7) #store at array[14]
j Ldone #end round
Lcalc: li $s6, 0 #first column
add $s6, $s6, $s0 #address of array[0]
li $t5, 4
add $t5, $t5, $t5
add $t5, $t5, $t5
add $t5, $t5, $s0 #address of array[4]
li $t6, 8
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[8]
li $t7, 12
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[12]
lw $t0, 0($s6) #get array[0]
lw $t1, 0($t5) #get array[4]
lw $t2, 0($t6) #get array[8]
lw $t3, 0($t7) #get array[12]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[0]
sw $t1, 0($t5) #store at array[4]
sw $t2, 0($t6) #store at array[8]
sw $t3, 0($t7) #store at array[12]
li $s6, 1 #second column, second quarter round
add $s6, $s6, $s6
add $s6, $s6, $s6
add $s6, $s6, $s0 #address of array[1]
li $t5, 5
add $t5, $t5, $t5
add $t5, $t5, $t5
add $t5, $t5, $s0 #address of array[5]
li $t6, 9
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[9]
li $t7, 13
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[13]
lw $t0, 0($s6) #get array[1]
lw $t1, 0($t5) #get array[5]
lw $t2, 0($t6) #get array[9]
lw $t3, 0($t7) #get array[13]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[1]
sw $t1, 0($t5) #store at array[5]
sw $t2, 0($t6) #store at array[9]
sw $t3, 0($t7) #store at array[13]
li $s6, 2 #third column, third quarter round
add $s6, $s6, $s6
add $s6, $s6, $s6
add $s6, $s6, $s0 #address of array[2]
li $t5, 6
add $t5, $t5, $t5
add $t5, $t5, $t5
add $t5, $t5, $s0 #address of array[6]
li $t6, 10
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[10]
li $t7, 14
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[14]
lw $t0, 0($s6) #get array[2]
lw $t1, 0($t5) #get array[6]
lw $t2, 0($t6) #get array[10]
lw $t3, 0($t7) #get array[14]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[2]
sw $t1, 0($t5) #store at array[6]
sw $t2, 0($t6) #store at array[10]
sw $t3, 0($t7) #store at array[14]
li $s6, 3 #fourth column, last quarter round
add $s6, $s6, $s6
add $s6, $s6, $s6
add $s6, $s6, $s0 #address of array[3]
li $t5, 7
add $t5, $t5, $t5
add $t5, $t5, $t5
add $t5, $t5, $s0 #address of array[7]
li $t6, 11
add $t6, $t6, $t6
add $t6, $t6, $t6
add $t6, $t6, $s0 #address of array[11]
li $t7, 15
add $t7, $t7, $t7
add $t7, $t7, $t7
add $t7, $t7, $s0 #address of array[15]
lw $t0, 0($s6) #get array[3]
lw $t1, 0($t5) #get array[7]
lw $t2, 0($t6) #get array[11]
lw $t3, 0($t7) #get array[15]
addu $t0, $t0, $t1 #begin quarter round operations
xor $t3, $t3, $t0
sll $t3, $t3, 16
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 12
addu $t0, $t0, $t1
xor $t3, $t3, $t0
sll $t3, $t3, 8
addu $t2, $t2, $t3
xor $t1, $t1, $t2
sll $t1, $t1, 7
sw $t0, 0($s6) #store at array[3]
sw $t1, 0($t5) #store at array[7]
sw $t2, 0($t6) #store at array[11]
sw $t3, 0($t7) #store at array[15]
Ldone: addiu $s1, $s1, 1 #increment loop index
j Loop
EXIT: li $s4, 0
LPrint: bgt $s4, 15, QUIT #print elements of the final array
lw $t0, 0($s0)
li $v0, 1
move $a0, $t0
syscall
addi $s4, $s4, 1
addi $s0, $s0, 4
j LPrint
QUIT: li $v0, 10 #exit code
syscall