-
Notifications
You must be signed in to change notification settings - Fork 0
/
256bitsmul.s
356 lines (249 loc) · 6.64 KB
/
256bitsmul.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
.section .data
values:
// from the least significant to the most
.word 0x88888888, 0x77777777, 0x66666666, 0x12345678, 0x44444444, 0x33333333, 0x22222222, 0x11111111
.word 0x99999999, 0xaaaaaaaa, 0xbbbbbbbb, 0xfedcba98, 0xdddddddd, 0xeeeeeeee, 0xffffffff, 0xffffffff
.align 4
result: .space 256
.section .text
.global _start
_start:
ldr r0, =values // Load the address of values into r0
add r1, r0, #32 // Calculate the address of the second 128-bit value
ldr r2, =result // Load the address of result into r2
bl _256mul // Call the 256-bit multiplication function
b _exit // Exit the program
_256mul:
// Save registers that will be used
push {r0-r2, lr}
// Call _128mul with the first pair of 128-bit values
bl _128mul
// Calculate the next addresses for 128-bit multiplication
add r0, r0, #16
add r2, r2, #128
bl _128mul
sub r0, r0, #16
add r1, r1, #16
add r2, r2, #64
bl _128mul
add r0, r0, #16
sub r2, r2, #128
bl _128mul
// Restore registers
pop {r0-r2, lr}
// Post processing addition (similar to the 128-bit post-processing)
add r3, r2, #64
add r4, r3, #64
add r5, r4, #64
// r2, #16
ldr r8, [r2, #16]
ldr r9, [r4]
ldr r10, [r5]
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #16] //carry in r11
// r2, #20
ldr r8, [r2, #20]
ldr r9, [r4, #4]
ldr r10, [r5, #4]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #20]
// r2, #24
ldr r8, [r2, #24]
ldr r9, [r4, #8]
ldr r10, [r5, #8]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #24]
// r2, #28
ldr r8, [r2, #28]
ldr r9, [r4, #12]
ldr r10, [r5, #12]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #28]
// r2, #32
ldr r8, [r3]
ldr r9, [r4, #16]
ldr r10, [r5, #16]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #32]
// r2, #36
ldr r8, [r3, #4]
ldr r9, [r4, #20]
ldr r10, [r5, #20]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #36]
// r2, #40
ldr r8, [r3, #8]
ldr r9, [r4, #24]
ldr r10, [r5, #24]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #40]
// r2, #44
ldr r8, [r3, #12]
ldr r9, [r4, #28]
ldr r10, [r5, #28]
adds r8, r8, r11 //add carry
mov r11, #0 //clear carry
adc r11, r11, #0
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #44]
// r2, #48
ldr r8, [r3, #16]
add r8, r8, r11
mov r11, #0
str r8, [r2, #48]
// Shifting
ldr r8, [r3, #20]
str r8, [r2, #52]
ldr r8, [r3, #24]
str r8, [r2, #56]
ldr r8, [r3, #28]
str r8, [r2, #60]
bx lr
_128mul:
// Save registers that will be used
push {r0-r2, lr}
// Call _64mul with the first pair of 64-bit values
bl _64mul
add r0, r0, #8
add r2, r2, #32
bl _64mul
sub r0, r0, #8
add r1, r1, #8
add r2, r2, #16
bl _64mul
add r0, r0, #8
sub r2, r2, #32
// Call _64mul with the second pair of 64-bit values
bl _64mul
// Restore registers
pop {r0-r2, lr}
// 後處理加法:
add r3, r2, #16
add r4, r3, #16
add r5, r4, #16
// r2, #8
ldr r8, [r2, #8]
ldr r9, [r4]
ldr r10, [r5]
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #8] //carry in r11
// r2, #12
ldr r8, [r2, #12]
ldr r9, [r4, #4]
ldr r10, [r5, #4]
add r8, r8, r11 //把進位加上去
mov r11, #0 //進位歸零
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #12]
// r2, #16
ldr r8, [r3]
ldr r9, [r4, #8]
ldr r10, [r5, #8]
add r8, r8, r11 //把進位加上去
mov r11, #0 //進位歸零
adds r8, r8, r9
adc r11, r11, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #16]
// r2, #20
ldr r8, [r3, #4]
ldr r9, [r4, #12]
ldr r10, [r5, #12]
add r8, r8, r11 //把進位加上去
mov r11, #0 //進位歸零
adds r8, r8, r9
adc r11, r12, #0
adds r8, r8, r10
adc r11,r11,#0
str r8, [r2, #20]
// r2, #24
ldr r8, [r3, #8]
add r8, r8, r11
mov r11, #0 //clear carry
str r8, [r2, #24]
bx lr
_64mul:
// Load the lower and upper parts of the first 64-bit number
ldr r3, [r0] // Lower part of the first number
ldr r4, [r0, #4] // Upper part of the first number
// Load the lower and upper parts of the second 64-bit number
ldr r5, [r1] // Lower part of the second number
ldr r6, [r1, #4] // Upper part of the second number
// Perform the multiplication
// r3:r4 * r5:r6 -> (r7:r8:r9:r10)
// r3 * r5 (lower * lower)
umull r7, r8, r3, r5
// r3 * r6 (lower * upper)
umull r9, r10, r3, r6
// Add the result to the upper part
adds r8, r8, r9
adc r10, r10, #0
// r4 * r5 (upper * lower)
umull r9, r3, r4, r5
// Add the result to the upper part
adds r8, r8, r9
adc r10, r10, r3
// r4 * r6 (upper * upper)
umull r9, r3, r4, r6
// Add the result to the upper part
adds r10, r10, r9
adc r3, r3, #0
// Store the result
str r7, [r2] // Store lower part
str r8, [r2, #4] // Store lower-middle part
str r10, [r2, #8] // Store upper-middle part
str r3, [r2, #12] // Store upper part
bx lr // Return from subroutine
_exit:
mov r7, #1 // System call for exit
mov r0, #0 // Exit status
svc 0 // Trigger system call