aboutsummaryrefslogtreecommitdiff
path: root/src/libmp/386/mpvecadd.s
blob: 67519ad4f78430fbeaae967c80db79bffbca6d2c (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
/*	mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum) */
/*		sum[0:alen] = a[0:alen-1] + b[0:blen-1] */
/*	prereq: alen >= blen, sum has room for alen+1 digits */
/* (very old gnu assembler doesn't allow multiline comments) */

.text

.p2align 2,0x90
.globl mpvecadd
	.type mpvecadd, @function
mpvecadd:
	/* Prelude */
	pushl %ebp
	movl %ebx, -4(%esp)		/* save on stack */
	movl %esi, -8(%esp)
	movl %edi, -12(%esp)

	movl	12(%esp), %edx		/* alen */
	movl	20(%esp), %ecx		/* blen */
	movl	8(%esp), %esi		/* a */
	movl	16(%esp), %ebx		/* b */
	subl	%ecx, %edx
	movl	24(%esp), %edi		/* sum */
	xorl	%ebp, %ebp		/* this also sets carry to 0 */

	/* skip addition if b is zero */
	testl	%ecx,%ecx
	je	_add1

	/* sum[0:blen-1],carry = a[0:blen-1] + b[0:blen-1] */
_addloop1:
	movl	(%esi, %ebp, 4), %eax
	adcl	(%ebx, %ebp, 4), %eax
	movl	%eax, (%edi, %ebp, 4)
	incl	%ebp
	loop	_addloop1

_add1:
	/* jump if alen > blen */
	incl	%edx
	movl	%edx, %ecx
	loop	_addloop2

	/* sum[alen] = carry */
_addend:
	jb	_addcarry
	movl	$0, (%edi, %ebp, 4)
	jmp done

_addcarry:
	movl	$1, (%edi, %ebp, 4)
	jmp done

	/* sum[blen:alen-1],carry = a[blen:alen-1] + 0 */
_addloop2:
	movl	(%esi, %ebp, 4),%eax
	adcl	$0, %eax
	movl	%eax, (%edi, %ebp, 4)
	incl	%ebp
	loop	_addloop2
	jmp	_addend

done:
	/* Postlude */
	movl -4(%esp), %ebx		/* restore from stack */
	movl -8(%esp), %esi
	movl -12(%esp), %edi
	movl %esp, %ebp
	leave
	ret