?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /**********************************************************************
2 *
3 * Big Integer Assembly Helpers
4 * Library for Microchip TCP/IP Stack
5 * - Accelerates processing for BigInt functions
6 *
7 **********************************************************************
8 * FileName: BigInt_helper_C32.S
9 * Dependencies: None
10 * Processor: PIC32
11 * Compiler: Microchip C32 v1.05 or higher
12 * Company: Microchip Technology, Inc.
13 *
14 * Software License Agreement
15 *
16 * Copyright (C) 2002-2009 Microchip Technology Inc. All rights
17 * reserved.
18 *
19 * Microchip licenses to you the right to use, modify, copy, and
20 * distribute:
21 * (i) the Software when embedded on a Microchip microcontroller or
22 * digital signal controller product ("Device") which is
23 * integrated into Licensee's product; or
24 * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
25 * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
26 * used in conjunction with a Microchip ethernet controller for
27 * the sole purpose of interfacing with the ethernet controller.
28 *
29 * You should refer to the license agreement accompanying this
30 * Software for additional information regarding your rights and
31 * obligations.
32 *
33 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
34 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
35 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
36 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
37 * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
38 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
39 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
40 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
41 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
42 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
43 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
44 *
45 *
46 * Author Date Comment
47 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 * Abhay Deshmukh 03/04/08 Original
49 *********************************************************************/
50  
51 #define __GENERIC_TYPE_DEFS_H_
52 #define __COMPILER_H
53 #include "HardwareProfile.h"
54 #include "TCPIPConfig.h"
55 #if (defined(STACK_USE_SSL_SERVER) || defined(STACK_USE_SSL_CLIENT)) && !defined(ENC100_INTERFACE_MODE)
56  
57 #include "TCPIP Stack/regdef.h"
58  
59 #define MAX_UNSIGNED_INT_VALUE 0xFFFFFFFF
60  
61 #define ONE_VALUE 0x00000001
62  
63 #define ZERO_VALUE 0x00000000
64  
65 .bss
66 .align 4
67 .global _iA
68 .global _xA
69 .global _iB
70 .global _xB
71 .global _iR
72 .global _wC
73 _iA: .word 4 /* _iA, starting index for A (lower memory address, least significant byte/word) */
74 _xA: .word 4 /* _xA, end index for A (higher memory address, most significant byte/word) */
75 _iB: .word 4 /* _iB, starting index for B (lower memory address, least significant byte/word) */
76 _xB: .word 4 /* _xB, end index for B (higher memory address, most significant byte/word) */
77 _iR: .word 4 /* _iR, starting index for Res (lower memory address, least significant byte/word) */
78 _wC: .word 4 /* value of B for _mas (little endian word) */
79  
80 .set noreorder
81  
82 .text
83  
84 /***************************************************************************
85 ; Function: void _addBI()
86 ;
87 ; PreCondition: _iA and _iB are loaded with the address of the LSB of the BigInt
88 ; _xA and _xB are loaded with the address of the MSB of the BigInt
89 ; a.size >= b.magnitude
90 ;
91 ; Input: A and B, the BigInts to add
92 ;
93 ; Output: A = A + B
94 ;
95 ; Side Effects: None
96 ;
97 ; Overview: Quickly performs the bulk addition of two BigInts
98 ;
99 ; Note: Function works
100 ;***************************************************************************/
101  
102 /* Note :- There is no Carry/Borrow Flag in STATUS Register of PIC32...So Workarounds were done */
103  
104 .global _addBI
105  
106 .ent _addBI
107  
108 _addBI:
109 la t0, _xA # Load adress of _xA
110 lw t0, 0(t0) # Load the Contents of memory pointing by t0 register
111  
112 la t1, _iA
113 addiu t0, t0, 4 # Store the address present in _xA & increment it by 4 ( since every access is 32 bit )
114 lw t1, 0(t1) # Store the address present in __iA
115  
116 la t2, _xB
117 lw t2, 0(t2)
118  
119 la t3, _iB
120 addiu t2, t2, 4 # Store the address present in _xB & increment it by 4
121 lw t3, 0(t3) # Store the address present in _iB
122  
123 la t6, ZERO_VALUE # Store 0x00000000 in t6
124  
125 aDoAdd:
126 lw t4, 0(t3) # Place the LSW of _iB in t4
127 addiu t3, t3, 4 # t3 = t3 + t4
128 lw t5, 0(t1) # Place the LSW of _iA in t5
129 addu t5, t5, t6 # t5 = t5 + t6
130 addu t7, t5, t4 # t7 = t5 + t4
131 sw t7, 0(t1) # 4 bytes of data transfered from address specified by t1 to t5
132 sltu t8, t7, t5 # if ( t7 < t5 ) then ( t8 = 1 )
133 movz t8, t6, t5
134 move t6, t8 # PrevCarry = carry
135 bne t3, t2, aDoAdd # if t3 != t2 then Jump to 'aDoAdd'
136 addiu t1, t1, 4
137 beqz t6, addDone
138 nop
139  
140 AddCarry:
141 beq t0, t1, addDone # if t0 = t1 then Jump to 'nextDoadd'
142 lw t7, 0(t1)
143 addiu t5, t7, 1
144 sw t5, 0(t1) # Store the Contents of t5 in memory pointed by t1 register
145 addiu t1, t1, 4
146 beqz t7, AddCarry
147 nop
148  
149 addDone:
150 jr $ra # return to function from where you are called
151 nop
152  
153 .end _addBI
154  
155  
156 /***************************************************************************
157 ; Function: void _subBI()
158 ;
159 ; PreCondition: _iA and _iB are loaded with the address of the LSB of the BigInt
160 ; _xA and _xB are loaded with the address of the MSB of the BigInt
161 ;
162 ; Input: A and B, the BigInts to subtract
163 ;
164 ; Output: A = A - B
165 ;
166 ; Side Effects: None
167 ;
168 ; Overview: Quickly performs the bulk subtraction of two BigInts
169 ;
170 ; Note: Function works
171 ;***************************************************************************/
172 .global _subBI
173  
174 .ent _subBI
175  
176 _subBI:
177 la t0, _xA
178 lw t0, 0(t0) # Store the address present in __xA & increment it by 4
179  
180 la t1, _iA # Store the address present in __iA
181 addiu t0, t0, 4
182 lw t1, 0(t1)
183  
184 la t2, _xB # Store the address present in __xB & increment it by 4
185 lw t2, 0(t2)
186  
187 la t3, _iB # Store the address present in __iB
188 addiu t2, t2, 4
189 lw t3, 0(t3)
190  
191 la t6, ZERO_VALUE # Store 0x00000000 in t6
192  
193 sDoSub:
194 move t8,$0
195 lw t4, 0(t3) # Place the LSW of __iB in t4
196 addiu t3, t3, 4 # t3 = t3 + t4
197 lw t5, 0(t1) # Place the LSW of __iA in t5
198 movz t8, t6, t5
199 subu t5, t5, t6 # t8 = t5 - t6
200 sltu t7, t5, t4 # if ( t8 < t4 ) then ( t7 = 1 )
201 subu t5, t5, t4 # t5 = t5 - t4
202 sw t5, 0(t1) # 4 bytes of data transfered from address specified by t1 to t5
203 or t7, t7, t8
204 move t6, t7
205 bne t3, t2, sDoSub
206 addiu t1, t1, 4
207 beqz t6, subDone
208 nop
209  
210 SubBorrow:
211 beq t0, t1, subDone
212 lw t7, 0(t1)
213 addiu t5, t7, -1
214 sw t5, 0(t1)
215 addiu t1, t1, 4
216 beq t7,$0, SubBorrow
217 nop
218  
219 subDone:
220 jr $ra
221 nop
222  
223 .end _subBI
224  
225  
226 /***************************************************************************
227 ; Function: void _zeroBI()
228 ;
229 ; PreCondition: _iA is loaded with the address of the LSB of the BigInt
230 ; _xA is loaded with the address of the MSB of the BigInt
231 ;
232 ; Input: None
233 ;
234 ; Output: A = 0
235 ;
236 ; Side Effects: None
237 ;
238 ; Overview: Sets all words from _iA to _xA to zero
239 ;
240 ; Note: Function works
241 ;***************************************************************************/
242 .global _zeroBI
243  
244 .ent _zeroBI
245  
246 _zeroBI:
247  
248 la t0, _xA
249 lw t0, 0(t0) # Store the address present in __xA & increment it by 4
250  
251 la t1, _iA # Store the address present in __iA
252 addiu t0, t0, 4
253 lw t1, 0(t1)
254  
255 zDoZero:
256 sw $0, 0(t1) # The value of $0 register is always 0
257 addiu t1, t1, 4
258 bne t0, t1, zDoZero
259 nop
260 jr $ra
261 nop
262  
263 .end _zeroBI
264  
265  
266 /***************************************************************************
267 ; Function: void _msbBI()
268 ;
269 ; PreCondition: _iA is loaded with the address of the LSB of the BigInt buffer
270 ; _xA is loaded with the address of the right most byte of the BigInt buffer
271 ;
272 ; Input: None
273 ;
274 ; Output: _xA is now pointing to the MSB of the BigInt
275 ;
276 ; Side Effects: None
277 ;
278 ; Overview: Finds the MSB (first non-zero word) of the BigInt, starting
279 ; from the right-most word and testing to the left. This
280 ; function will stop if _iA is reached.
281 ;
282 ; Note: Function works
283 ;***************************************************************************/
284 .global _msbBI
285  
286 .ent _msbBI
287  
288 _msbBI:
289  
290 la t2, _xA
291 lw t0, 0(t2) # Store the address present in __xA & increment it by 4
292  
293 la t1, _iA # Store the address present in __iA
294 lw t1, 0(t1)
295  
296 msbLoop:
297 lw t3, 0(t0)
298 bne t3, $0, msbDone
299 nop
300 addiu t0, t0, -4
301 bne t0, t1, msbLoop
302 nop
303  
304 msbDone:
305 sw t0, 0(t2)
306 jr $ra
307 nop
308  
309 .end _msbBI
310  
311  
312 /***************************************************************************
313 ; Function: void _mulBI()
314 ;
315 ; PreCondition: _iA and _iB are loaded with the address of the LSB of each BigInt
316 ; _xA and _xB are loaded with the address of the MSB of the BigInt
317 ; _iR is loaded with the LSB address of the destination result memory
318 ; _iR memory must be zeroed and have enough space (_xB-_iB+_xA-_iA words)
319 ;
320 ; Input: A and B, the BigInts to multiply
321 ;
322 ; Output: R = A * B
323 ;
324 ; Side Effects: None
325 ;
326 ; Overview: Performs the bulk multiplication of two BigInts
327 ;
328 ; Note: Function works. An Assumption has been made that 'R' is cleared to
329 ; 'Zero' before calling this function.
330 ;***************************************************************************/
331  
332 .global _mulBI
333  
334 .ent _mulBI
335  
336 _mulBI:
337  
338 la t0, _xA
339 lw t0, 0(t0) # Store the address present in _xA & increment it by 4
340  
341 la t1, _iA # Store the address present in _iA
342 addiu t0, t0, 4
343 lw t1, 0(t1)
344  
345 la t2, _xB # Store the address present in _xB & increment it by 4
346 lw t2, 0(t2)
347  
348 la t3, _iB # Store the address present in _iB
349 addiu t2, t2, 4
350 lw t3, 0(t3)
351  
352 la t4, _iR # Store the address present in _iR
353 lw t4, 0(t4)
354  
355 la a0, ONE_VALUE # Store 0x00000001 in a0
356  
357 addiu t6, $0, 0 # Store 0x0 in t6
358  
359 mLoopB:
360 beq t3, t2, mDone
361 lw t7, 0(t3)
362  
363 beq t7, $0, mContinue
364 addu t8, t4, t6
365  
366 mthi $0 # Move 0 to HI Register.
367  
368 addiu t5, t1, 0
369  
370 mLoopA:
371 mfhi a1 # Store the contents from HI Register to 'a1' register
372 lw a3,0(t8)
373 addu a2, a1, a3
374 mtlo a2 # Move the contents from 'a1' register to LO register
375 sltu a1, a2, a1
376 bgtz a1, mHiPutOne
377 nop
378 b mMultAdd
379 mthi $0
380  
381 mHiPutOne:
382 mthi a0 # Move the contents from 'a0' register to HI register
383  
384 mMultAdd:
385 lw a1, 0(t5)
386 maddu a1, t7 # Multiply a1 with t7 then add with HI || LO register ( i.e HI || LO + a1 * t7 )
387 addiu t5, t5, 4
388 mflo a3 # Store the contents from LO Register to 'a3' register
389 sw a3,0(t8)
390 addiu t8, t8, 4
391 bne t5,t0, mLoopA
392 nop
393 mfhi a1 # Store the contents from HI Register to 'a1' register
394 sw a1, 0(t8) # Move the contents from 'a1' register to LO register
395  
396 mContinue:
397 addiu t3, t3, 4
398 b mLoopB
399 addiu t6, t6, 4
400  
401 mDone:
402 jr $ra
403 nop
404  
405 .end _mulBI
406  
407  
408 /***************************************************************************
409 ; Function: void _sqrBI()
410 ;
411 ; PreCondition: _iA is loaded with the address of the LSB of the BigInt
412 ; _xA is loaded with the address of the MSB of the BigInt
413 ; _iR is loaded with the LSB address of the destination result memory
414 ; _iR memory must be zeroed and have enough space (_xB-_iB+_xA-_iA words)
415 ;
416 ; Input: A, the BigInt to square
417 ;
418 ; Output: R = A * A
419 ;
420 ; Side Effects: None
421 ;
422 ; Overview: Squares BigInt A and stores result in R
423 ;
424 ; Note: Function works
425 ;***************************************************************************/
426 .global _sqrBI
427  
428 .ent _sqrBI
429  
430 _sqrBI:
431  
432 la t0, _xA
433 lw t0, 0(t0) # Store the address present in _xA & increment it by 4
434  
435 la t1, _xB # Store the address present in _xB & increment it by 4
436 sw t0, 0(t1)
437  
438 la t0, _iA # Store the address present in _iA
439 lw t0, 0(t0)
440  
441 la t1, _iB # Store the address present in _iB
442  
443 b _mulBI
444 sw t0, 0(t1)
445  
446 .end _sqrBI
447  
448 /***************************************************************************
449 ; Function: void _masBI()
450 ;
451 ; PreCondition: _iB is loaded with the LSB of the modulus BigInt
452 ; _xB is loaded with the MSB of the modulus BigInt
453 ; _wC is loaded with the 32 bit unsigned integer by which to multiply
454 ; _iR is the starting LSB of the decumulator BigInt
455 .end __sqrBI
456 ;
457 ; Input: B (BigInt) and C (16-bit int) to multiply
458 ;
459 ; Output: R = R - (B * C)
460 ;
461 ; Side Effects: None
462 ;
463 ; Overview: Performs a Multiply And Subtract function. This is used in
464 ; the modulus calculation to save several steps. A BigInt (iB/xB)
465 ; is multiplied by a single word and subtracted rather than
466 ; accumulated.
467 ;
468 ; Note: Decumulator is the opposite of an accumulator,
469 ; if that wasn't obvious
470 ;
471 ; Note: Function works
472 ;***************************************************************************/
473 .global _masBI
474  
475 .ent _masBI
476  
477 _masBI:
478  
479 la t2, _xB # Store the address present in _xB & increment it by 4
480 lw t2, 0(t2)
481  
482 la t3, _iB # Store the address present in _iB
483 addiu t2, t2, 4
484 lw t3, 0(t3)
485  
486 la t1, _wC # Store the address present in _wC
487 lw t1, 0(t1)
488  
489 la t4, _iR # Store the address present in _iR
490 lw t4, 0(t4)
491  
492 la a1, ZERO_VALUE
493  
494 subu t8, t2, t3
495 addiu t8, t8, 4
496 addiu t9, t4, 0
497 addiu a0, $0, 1
498  
499 masLoop:
500  
501 mthi $0
502 mfhi t6
503  
504 mMultandSub:
505 mtlo t6
506 mthi $0
507 lw t5, 0(t3)
508 maddu t5, t1
509 nop
510 mflo t6
511  
512 mCalc:
513 move a2, $0
514 lw t5, 0(t4)
515 movz a2, a1, t5
516 subu t5, t5, a1
517 sltu t7, t5, t6 # if ( t5 < t6 ) then ( t7 = 1 )
518 subu t5, t5, t6 # t5 = t5 - t6
519 sw t5, 0(t4) # 4 bytes of data transfered from address specified by t4 to t5
520 or t7, t7, a2
521 move a1, t7
522 addiu t4, t4, 4
523 addiu t3, t3, 4
524 bne t3, t2, mMultandSub
525 mfhi t6
526 move a2, $0
527 lw t5, 0(t4)
528 movz a2, a1, t5
529 subu t5, t5, a1
530 sltu t7, t5, t6 # if ( t5 < t6 ) then ( t7 = 1 )
531 subu t5, t5, t6 # t5 = t5 - t6
532 sw t5, 0(t4) # 4 bytes of data transfered from address specified by t4 to t5
533 or t7, t7, a2
534 move a1, t7
535 addiu t4, t4, 4
536 beqz a1, masDone
537  
538 mSubBorrow:
539 subu t7, t4, t9
540 beq t7, t8, masDone
541 lw t7, 0(t4)
542 addiu t6, t7, -1
543 sw t6, 0(t4)
544 addiu t4, t4, 4
545 beq t7, $0, mSubBorrow
546 nop
547  
548 masDone:
549 jr $ra
550 nop
551  
552 .end _masBI
553  
554  
555 /***************************************************************************
556 ; Function: void _copyBI()
557 ;
558 ; PreCondition: _iA and _iB are loaded with the address of the LSB of each BigInt
559 ; _xA and _xB are loaded with the address of the MSB of each BigInt
560 ;
561 ; Input: A and B, the destination and source
562 ;
563 ; Output: A = B
564 ;
565 ; Side Effects: None
566 ;
567 ; Stack Req:
568 ;
569 ; Overview: Copies a value from one BigInt to another
570 ;
571 ; Note: Function works
572 ;***************************************************************************/
573 .global _copyBI
574  
575 .ent _copyBI
576  
577 _copyBI:
578  
579 la t0, _xA
580 lw t0, 0(t0) # Store the address present in __xA & increment it by 4
581  
582 la t1, _iA # Store the address present in __iA
583 addiu t0, t0, 4
584 lw t1, 0(t1)
585  
586 la t2, _xB # Store the address present in __xB & increment it by 4
587 lw t2, 0(t2)
588  
589 la t3, _iB # Store the address present in __iB
590 addiu t2, t2, 4
591 lw t3, 0(t3)
592  
593 cLoop:
594 lw t4, 0(t3)
595 sw t4, 0(t1)
596 addiu t3, t3, 4
597 beq t3, t2, cZeroLoopTest
598 addiu t1, t1, 4
599 bne t1,t0, cLoop
600 nop
601 jr $ra
602 nop
603  
604 cZeroLoop:
605 sw $0, 0(t1)
606 addiu t1, t1, 4
607  
608 cZeroLoopTest:
609 bne t1,t0, cZeroLoop
610 nop
611  
612 cend:
613 jr $ra
614 nop
615  
616 .end _copyBI
617  
618 .set reorder
619  
620 #endif
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3