原帖及讨论:http://bbs.bccn.net/thread-227571-1-1.html
偶要写个发电子邮件的程序 用到了base64算法 就用汇编实现了一下 还望各位高手指点下
贴下源码吧
;*****************************************************************************************************************
;函数功能:实现 base64 算法
;参数:_lpszInput 指向输入的缓冲区 _dwInputLen 给出长度 _lpszOutput 指向输入的缓冲区
;返回值:成功返回0 不成功返回 -1
;作者:zklhp Email:zklhp@sina.com
;时间:2008.8.8
;版权所有 转载请保持完整
;*****************************************************************************************************************
.DATA
base64_alphabet db "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",0
.CODE
_base64 proc uses ebx esi edi _lpszInput:DWORD,_dwInputLen:DWORD,_lpszOutput:DWORD
.if (_lpszInput==NULL)||(_lpszOutput==NULL)||(_dwInputLen==0)
xor eax,eax
dec eax
ret
.endif
mov eax,_dwInputLen
xor edx,edx
mov ecx,3
div ecx
push eax
.if eax > 0
dec eax
.if eax > 0
push eax
push ebp
mov esi,_lpszInput
mov edi,_lpszOutput
mov ebp,eax
lea edi,[edi+ebp*4]
neg ebp
align 4
@@:
mov ebx,DWORD ptr [esi]
bswap ebx
mov ecx,ebx
mov edx,ebx
mov eax,ebx
shr ecx,14
shr edx,8
shr eax,26
and ecx,3Fh
shr ebx,20
and edx,3Fh
and eax,3Fh
movzx ecx, BYTE PTR [base64_alphabet+ecx]
and ebx,3Fh
mov ch , BYTE PTR [base64_alphabet+edx]
movzx eax, BYTE PTR [base64_alphabet+eax]
shl ecx,16
mov ah,BYTE PTR [base64_alphabet+ebx]
add esi,3
or ecx,eax
mov [edi+ebp*4],ecx
add ebp,1
jnz @B
pop ebp
pop eax
mov ecx,eax
mov ebx,3
mul ebx
add _lpszInput,eax
shl ecx,2
add _lpszOutput,ecx
.endif
mov esi,_lpszInput
mov edi,_lpszOutput
mov ebx,DWORD ptr [esi]
bswap ebx
mov ecx,ebx
mov edx,ebx
mov eax,ebx
shr ecx,14
shr edx,8
shr eax,26
and ecx,3Fh
shr ebx,20
and edx,3Fh
and eax,3Fh
movzx ecx, BYTE PTR [base64_alphabet+ecx]
and ebx,3Fh
mov ch , BYTE PTR [base64_alphabet+edx]
movzx eax, BYTE PTR [base64_alphabet+eax]
shl ecx,16
mov ah,BYTE PTR [base64_alphabet+ebx]
or ecx,eax
mov [edi],ecx
add _lpszInput,3
add _lpszOutput,4
.endif
pop eax
mov ecx,3
mul ecx
neg eax
add eax,_dwInputLen
;int 3h
.if eax == 1
mov esi,_lpszInput
mov edi,_lpszOutput
movzx ecx,BYTE ptr [esi]
mov ebx,ecx
shr ecx,2
movzx ecx,BYTE ptr [base64_alphabet+ecx]
shl ebx,4
and ebx,03fh
movzx ebx,BYTE ptr [base64_alphabet+ebx]
shl ebx,8
or ecx,ebx
xor edx,edx
mov dl,'='
mov dh,'='
shl edx,16
or ecx,edx
mov [edi],ecx
.elseif eax == 2
mov esi,_lpszInput
mov edi,_lpszOutput
movzx ecx,BYTE ptr [esi]
mov ebx,ecx
shr ecx,2
movzx ecx,BYTE ptr [base64_alphabet+ecx]
movzx eax,BYTE ptr [esi+1]
mov edx,eax
shr eax,4
shl ebx,4
or ebx,eax
and ebx,03fh
movzx ebx,BYTE ptr [base64_alphabet+ebx]
shl edx,2
and edx,03fh
movzx edx,BYTE ptr [base64_alphabet+edx]
xor eax,eax
mov ah,'='
shl eax,16
or ecx,eax
shl ebx,8
or ecx,ebx
shl edx,16
or ecx,edx
mov [edi],ecx
.endif
xor eax,eax
ret
_base64 endp
参考了网上的一篇base64优化的文章 向高手的无私奉献精神表示感谢
程序+源码+参考资料已打包