汇编的大数运算核心算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
mov ecx, t; lea esi, dword ptr pL lea ebx, dword ptr pR lea edi, dword ptr pA clc cld
} mbinsub1:
asm {
mov eax, dword ptr [esi] lea esi, [esi + 4] sbb eax, [ebx] lea ebx, [ebx + 4] mov dword ptr [edi], eax lea edi, [edi + 4] loop mbinsub1 } mbinsub2: asm { setc al xor ah, ah } return _EAX; } //双字串减双字,t 为长度,pL – pR = PA,小头表示低位,返回借位 long AsmSubL(unsigned long *pL, unsigned long pR,
汇编的大数运算核心算法
加法
//相同长度的双字串相加,t 为长度,pL + pR = PA,小头表示低位,返回进位 long AsmAddLL(unsigned long *pL, unsigned long *pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0; asm {
mov ecx, t lea esi, dword ptr pL lea ebx, dword ptr pR lea edi, dword ptr pA clc cld } mbinadd1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] adc eax, [ebx] lea ebx, [ebx + 4] mov dword ptr [edi], eax lea edi, [edi + 4] loop mbinadd1 } mbinadd2: asm { setc al xor ah, ah } return _EAX; } //双字串加双字,t 为长度,pL + pR = PA,小头表示低位,返回进位 long AsmAddL(unsigned long *pL, unsigned long pR,
adc edx, 0 add eax, Temp adc edx, 0 mov [ebx], eax mov Temp, edx lea ebx, [ebx + 4] loop mbinmul2 mov [ebx], edx lea esi, [esi + 4] pop edi pop ebx lea ebx, [ebx + 4] pop ecx loop mbinmul1 } } //双字串乘双字,t 为长度,pL * pR = PA,小头表示低位 void AsmMulL(unsigned long *pL, unsigned long pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0; asm {
mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, pR clc cld } mbinsub1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] sbb eax, ebx mov ebx, 0 mov dword ptr [edi], eax lea edi, [edi + 4]
lodsd shrd edx, eax, cl xchg edx, eax stosd dec ebx jnz AsmShr1 } AsmShr2: asm { shr edx, cl mov [edi], edx pop ds } } //比较 假设 es = ds //比较 pL 和 pR 大小,返回值 1 表示大于 0 表示等于-1 表示小于 long AsmCmpLL(unsigned long *pL, unsigned long *pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0;
asm {
mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, pR clc cld } mbinadd1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] adc eax, ebx mov ebx, 0 mov dword ptr [edi], eax lea edi, [edi + 4] loop mbinadd1 } mbinadd2: asm { setc al xor ah, ah } return _EAX; }
push ds mov ebx, t mov ecx, s lea esi, dword ptr pL
lea edi, dword ptr pA mov edx, 0 cld } AsmShl1: asm { lodsd push eax shld eax, edx, cl pop edx stosd dec ebx jnz AsmShl1 mov bl, 32 sub bl, cl mov cl, bl shr edx, cl mov [edi], edx pop ds } } //右移 pL >> s = pA(s < 32) void AsmShr(unsigned long *pL, unsigned long *pA,
unsigned long *pA, unsigned long tL, unsigned long tR) { if ((tL == 0) || (tR == 0)) return; unsigned long Temp; asm { mov ecx, tL lea esi, dword ptr pL lea edi, dword ptr pR lea ebx, dword ptr pA } mbinmul1: asm { push ecx mov ecx, tR push ebx push edi mov eax, 0 mov Temp, eax } mbinmul2: asm { mov eax, [edi] lea edi, [edi + 4] mul dword ptr es:[si] add eax, [ebx]
lea edi, dword ptr pA; mov ecx, t mov eax, 0 cld rep stosd } } //拷贝 void AsmCopy(unsigned long *pL, unsigned long *pR,
unsigned long t) {
if (t == 0) return; asm {
unsigned long *pA, unsigned long t) {
if (t == 0) return; asm {
mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, 0 clc cld } AsmMulL1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] mul dword ptr pR add eax, ebx adc edx, 0 mov ebx, edx mov dword ptr [edi], eax lea edi, [edi + 4] loop AsmMulL1 mov [edi], ebx
{ if (t == 0) return 0; asm { mov ecx, t mov edx, 0 lea esi, dword ptr pL mov ebx, ecx dec ebx shl ebx, 2 add esi, ebx mov ebx, pR std }
unsigned long t) {
if (t == 0) return 0; asm {
push ds mov ecx, t mov eax, 0 std lea esi, dword ptr pL lea edi, dword ptr pR mov ebx, ecx shl ebx, 2 add esi, ebx add edi, ebx repe cmpsd jz AsmCmpLL1 add edi, 4 add esi, 4 mov eax, [esi] cmp eax, [edi] ja AsmCmpLL2 mov eax, 0xffffffff jmp AsmCmpLL1 }
AsmCmpLL2: asm { mov eax, 1 }
AsmCmpLL1: asm { pop ds } return _EAX;
} //假设 es = ds //清零 void AsmZero(unsigned long *pA, unsigned long t) {
if (t == 0) return; asm {
unsigned long t, unsigned long s) {
if (t == 0) return; asm {
push ds mov ebx, t mov ecx, s lea esi, dword ptr pL lea edi, dword ptr pA mov edx, [esi] add esi, 4 dec ebx jz AsmShr2 cld } AsmShr1: asm {
loop mbinsub1 } mbinsub2: asm {
setc al xor ah, ah } return _EAX; }
乘法
//不同长度的双字串相乘,tL 为 pL 长度,tR 为 pR 长度,pL * pR = PA void AsmMulLL(unsigned long *pL, unsigned long *pR,
lea esi, dword ptr pR lea edi, dword ptr pL mov ecx, t cld rep movsd } } //假设 es = ds //求余,返回余数,未检测除数为 0
unsigned long AsmModL(unsigned long *pL, unsigned long pR, unsigned long t)
AsmMulLS1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] mul dword ptr pR add eax, ebx adc edx, 0 mov ebx, edx mov dword ptr [edi], eax lea edi, [edi + 4] loop AsmMulLS1 }
减法
//相同长度的双字串相减,t 为长度,pL – pR = PA,小头表示低位,返回借位 long AsmSubLL(unsigned long *pL, unsigned long *pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0; asm {
Hale Waihona Puke Baidu }
void AsmMulLS(unsigned long *pL, unsigned long pR, unsigned long *pA, unsigned long t)
{ if (t == 0) return; asm { mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, 0 clc cld }
} //移位 假设 es = ds //左移 pL << s = pA(s < 32) void AsmShl(unsigned long *pL, unsigned long *pA,
unsigned long t, unsigned long s) {
if (t == 0) return; asm {
} mbinsub1:
asm {
mov eax, dword ptr [esi] lea esi, [esi + 4] sbb eax, [ebx] lea ebx, [ebx + 4] mov dword ptr [edi], eax lea edi, [edi + 4] loop mbinsub1 } mbinsub2: asm { setc al xor ah, ah } return _EAX; } //双字串减双字,t 为长度,pL – pR = PA,小头表示低位,返回借位 long AsmSubL(unsigned long *pL, unsigned long pR,
汇编的大数运算核心算法
加法
//相同长度的双字串相加,t 为长度,pL + pR = PA,小头表示低位,返回进位 long AsmAddLL(unsigned long *pL, unsigned long *pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0; asm {
mov ecx, t lea esi, dword ptr pL lea ebx, dword ptr pR lea edi, dword ptr pA clc cld } mbinadd1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] adc eax, [ebx] lea ebx, [ebx + 4] mov dword ptr [edi], eax lea edi, [edi + 4] loop mbinadd1 } mbinadd2: asm { setc al xor ah, ah } return _EAX; } //双字串加双字,t 为长度,pL + pR = PA,小头表示低位,返回进位 long AsmAddL(unsigned long *pL, unsigned long pR,
adc edx, 0 add eax, Temp adc edx, 0 mov [ebx], eax mov Temp, edx lea ebx, [ebx + 4] loop mbinmul2 mov [ebx], edx lea esi, [esi + 4] pop edi pop ebx lea ebx, [ebx + 4] pop ecx loop mbinmul1 } } //双字串乘双字,t 为长度,pL * pR = PA,小头表示低位 void AsmMulL(unsigned long *pL, unsigned long pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0; asm {
mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, pR clc cld } mbinsub1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] sbb eax, ebx mov ebx, 0 mov dword ptr [edi], eax lea edi, [edi + 4]
lodsd shrd edx, eax, cl xchg edx, eax stosd dec ebx jnz AsmShr1 } AsmShr2: asm { shr edx, cl mov [edi], edx pop ds } } //比较 假设 es = ds //比较 pL 和 pR 大小,返回值 1 表示大于 0 表示等于-1 表示小于 long AsmCmpLL(unsigned long *pL, unsigned long *pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0;
asm {
mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, pR clc cld } mbinadd1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] adc eax, ebx mov ebx, 0 mov dword ptr [edi], eax lea edi, [edi + 4] loop mbinadd1 } mbinadd2: asm { setc al xor ah, ah } return _EAX; }
push ds mov ebx, t mov ecx, s lea esi, dword ptr pL
lea edi, dword ptr pA mov edx, 0 cld } AsmShl1: asm { lodsd push eax shld eax, edx, cl pop edx stosd dec ebx jnz AsmShl1 mov bl, 32 sub bl, cl mov cl, bl shr edx, cl mov [edi], edx pop ds } } //右移 pL >> s = pA(s < 32) void AsmShr(unsigned long *pL, unsigned long *pA,
unsigned long *pA, unsigned long tL, unsigned long tR) { if ((tL == 0) || (tR == 0)) return; unsigned long Temp; asm { mov ecx, tL lea esi, dword ptr pL lea edi, dword ptr pR lea ebx, dword ptr pA } mbinmul1: asm { push ecx mov ecx, tR push ebx push edi mov eax, 0 mov Temp, eax } mbinmul2: asm { mov eax, [edi] lea edi, [edi + 4] mul dword ptr es:[si] add eax, [ebx]
lea edi, dword ptr pA; mov ecx, t mov eax, 0 cld rep stosd } } //拷贝 void AsmCopy(unsigned long *pL, unsigned long *pR,
unsigned long t) {
if (t == 0) return; asm {
unsigned long *pA, unsigned long t) {
if (t == 0) return; asm {
mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, 0 clc cld } AsmMulL1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] mul dword ptr pR add eax, ebx adc edx, 0 mov ebx, edx mov dword ptr [edi], eax lea edi, [edi + 4] loop AsmMulL1 mov [edi], ebx
{ if (t == 0) return 0; asm { mov ecx, t mov edx, 0 lea esi, dword ptr pL mov ebx, ecx dec ebx shl ebx, 2 add esi, ebx mov ebx, pR std }
unsigned long t) {
if (t == 0) return 0; asm {
push ds mov ecx, t mov eax, 0 std lea esi, dword ptr pL lea edi, dword ptr pR mov ebx, ecx shl ebx, 2 add esi, ebx add edi, ebx repe cmpsd jz AsmCmpLL1 add edi, 4 add esi, 4 mov eax, [esi] cmp eax, [edi] ja AsmCmpLL2 mov eax, 0xffffffff jmp AsmCmpLL1 }
AsmCmpLL2: asm { mov eax, 1 }
AsmCmpLL1: asm { pop ds } return _EAX;
} //假设 es = ds //清零 void AsmZero(unsigned long *pA, unsigned long t) {
if (t == 0) return; asm {
unsigned long t, unsigned long s) {
if (t == 0) return; asm {
push ds mov ebx, t mov ecx, s lea esi, dword ptr pL lea edi, dword ptr pA mov edx, [esi] add esi, 4 dec ebx jz AsmShr2 cld } AsmShr1: asm {
loop mbinsub1 } mbinsub2: asm {
setc al xor ah, ah } return _EAX; }
乘法
//不同长度的双字串相乘,tL 为 pL 长度,tR 为 pR 长度,pL * pR = PA void AsmMulLL(unsigned long *pL, unsigned long *pR,
lea esi, dword ptr pR lea edi, dword ptr pL mov ecx, t cld rep movsd } } //假设 es = ds //求余,返回余数,未检测除数为 0
unsigned long AsmModL(unsigned long *pL, unsigned long pR, unsigned long t)
AsmMulLS1: asm { mov eax, dword ptr [esi] lea esi, [esi + 4] mul dword ptr pR add eax, ebx adc edx, 0 mov ebx, edx mov dword ptr [edi], eax lea edi, [edi + 4] loop AsmMulLS1 }
减法
//相同长度的双字串相减,t 为长度,pL – pR = PA,小头表示低位,返回借位 long AsmSubLL(unsigned long *pL, unsigned long *pR,
unsigned long *pA, unsigned long t) {
if (t == 0) return 0; asm {
Hale Waihona Puke Baidu }
void AsmMulLS(unsigned long *pL, unsigned long pR, unsigned long *pA, unsigned long t)
{ if (t == 0) return; asm { mov ecx, t lea esi, dword ptr pL lea edi, dword ptr pA mov ebx, 0 clc cld }
} //移位 假设 es = ds //左移 pL << s = pA(s < 32) void AsmShl(unsigned long *pL, unsigned long *pA,
unsigned long t, unsigned long s) {
if (t == 0) return; asm {