问个memcmp的问题,想修改他最后的返回值,有点牵涉到汇编的,懂的进来看看。

jpsr jpsr 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请问memcmp这个函数他返回的效果和strcmp一样的,如果我想要memcmp来返回2个连续内存中首个不相同的地址,那么应该怎么去修改这个memcmp??全是汇编啊,看不懂 @@
有会的请帮我看看怎么修改??我个人觉得汇编可能比较快一点,所以想改这段汇编代码,请看清问题再回答。
附memcmp的汇编代码:
CODESEG

public memcmp
memcmp proc

.FPO ( 0, 3, 0, 0, 0, 0 )

mov eax,[esp+0ch] ; eax = counter
test eax,eax ; test if counter is zero
jz short retnull ; return 0

mov edx,[esp+4] ; edx = buf1
push esi
push edi
mov esi,edx ; esi = buf1
mov edi,[esp+10h] ; edi = buf2

; Check for dword (32 bit) alignment
or edx,edi
and edx,3 ; edx=0 iff buf1 are buf2 are aligned
jz short dwords

; Strings are not aligned. If the caller knows the strings (buf1 and buf2) are
; different, the function may be called with length like -1. The difference
; may be found in the last dword of aligned string, and because the other
; string is misaligned it may cause page fault. So, to be safe. the comparison
; must be done byte by byte.
test eax,1
jz short main_loop

mov cl,[esi]
cmp cl,[edi]
jne short not_equal
inc esi
inc edi
dec eax
jz short done ; eax is already 0

main_loop:
mov cl,[esi]
mov dl,[edi]
cmp cl,dl
jne short not_equal

mov cl,[esi+1]
mov dl,[edi+1]
cmp cl,dl
jne short not_equal

add edi,2
add esi,2

sub eax,2
jnz short main_loop
done:
pop edi
pop esi
retnull:
ret ; _cdecl return


dwords:
mov ecx,eax
and eax,3 ; eax= counter for tail loop

shr ecx,2
jz short tail_loop_start
; counter was >=4 so may check one dword
rep cmpsd

jz short tail_loop_start

; in last dword was difference
mov ecx,[esi-4] ; load last dword from buf1 to ecx
mov edx,[edi-4] ; load last dword from buf2 to edx
cmp cl,dl ; test first bytes
jne short difference_in_tail
cmp ch,dh ; test seconds bytes
jne short difference_in_tail
shr ecx,10h
shr edx,10h
cmp cl,dl ; test third bytes
jne short difference_in_tail
cmp ch,dh ; they are different, but each one is bigger?
; jmp short difference_in_tail

difference_in_tail:
mov eax,0
; buf1 < buf2 buf1 > buf2
not_equal:
sbb eax,eax ; AX=-1, CY=1 AX=0, CY=0
pop edi ; counter
sbb eax,-1 ; AX=-1 AX=1
pop esi
ret ; _cdecl return

; in tail loop we test last three bytes (esi and edi are aligned on dword
; boundary)
tail_loop_start:

test eax,eax ; eax is counter%4 (number of bytes for tail
; loop)
jz short done ; taken if there is no tail bytes
mov edx,[esi] ; load dword from buf1
mov ecx,[edi] ; load dword from buf2
cmp dl,cl ; test first bytes
jne short difference_in_tail
dec eax ; counter--
jz short tail_done
cmp dh,ch ; test second bytes
jne short difference_in_tail
dec eax ; counter--
jz short tail_done
and ecx,00ff0000h ; test third bytes
and edx,00ff0000h
cmp edx,ecx
jne short difference_in_tail
dec eax
tail_done:
pop edi
pop esi
ret ; _cdecl return

memcmp endp
end


...全文

相关推荐

memcmp函数详解[项目源码]

本文详细介绍了C语言中的内存操作函数memcmp(),包括其核心功能、特点、函数原型、实现原理、使用场景及注意事项。memcmp()函数能够逐字节比较两个内存块的前n个字节,适用于任意类型的数据比较,不依赖终止符,常用于系统编程、数据校验和协议解析等场景。文章还提供了伪代码实现和实际应用示例,帮助读者深入理解其工作原理和优化策略。同时,强调了在使用memcmp()时需要注意的数据类型、字节序、结构体填充字节等问题,以避免潜在的错误和陷阱。

herb5

C语言memcmp函数详解[项目源码]

memcmp函数是C语言标准库中用于比较两个内存块内容的函数,定义在<string.h>头文件中。该函数逐字节比较两个内存块,直到找到不同字节或比较完指定字节数。函数原型为`int memcmp(const void *s1, const void *s2, size_t n)`,其中`s1`和`s2`为待比较内存块指针,`n`为比较字节数。返回值表示比较结果:0表示完全相同,非零值表示首个不同字节的偏移量。文章通过示例代码展示了memcmp的使用方法,包括字符串比较和二进制数据比较的场景,并对比了与strncmp等函数的异同。

e4f5g6h7

strncat strncpy strncmp memcpy memcmp 比较及其原型

strcmp strcpy strcat strlen 的实现 以及与strncat strncpy strncmp memcpy memcmp 原型分析

秦十九行

memcmp与memset详解[项目代码]

本文详细介绍了C/C++标准库中的两个重要函数memcmp和memset。memcmp用于逐字节比较两个内存区域的内容,返回值为负数、零或正数,分别表示第一个内存块小于、等于或大于第二个内存块。文章提供了函数原型、参数说明、使用示例及注意事项,如不考虑数据类型、不检查指针有效性等。memset用于将内存区域设置为特定值,同样提供了函数原型、参数说明、使用示例及注意事项,如使用字节值而非字符值、缓冲区溢出风险等。通过具体代码示例,帮助读者理解这两个函数的正确使用方法和潜在问题。

xxx12

112.memcmp.pdf

112.memcmp

长久的梦
评论 8
收藏 已收藏
分享
返回顶部
jpsr
VC/MFC 16,550 成员 421,606 内容