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

jpsr 2004-11-24 03:56:55
请问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


...全文
234 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jpsr 2004-11-25
  • 打赏
  • 举报
回复
我把memcmp的代码搬到程序里面了,memcmp.asm我没修改
就是
__asm
{
……
}
双杯献酒 2004-11-25
  • 打赏
  • 举报
回复
我的建议是不要修改它。
jpsr 2004-11-25
  • 打赏
  • 举报
回复
我要用rep cmpsb,不用for语句。我听别人说用cmpsd比较快,不过这里有个对齐的问题,还没搞明白。
riverboat 2004-11-24
  • 打赏
  • 举报
回复
自己用字符串操作写一个吧,应该也不太难啊。

size_t memcmp1(const void *p1, const void * p2, size_t size){
size_t i;
void *q1=p1, *q2=p2;

for(i=0; i<size; i++, q1++, q2++){
if((*q1)!=(*q2)) return i; // 一个个字节比较,不一样就返回该位置
}
return size; // 所有字节都一样,表明相等
}
jpsr 2004-11-24
  • 打赏
  • 举报
回复
高手都不愿意回答,我们自己学习asm,问题已经自己解决了,有会的,请贴一下你修改后的memcmp,好让我结帐。
jpsr 2004-11-24
  • 打赏
  • 举报
回复
谢谢,我自己up
try876 2004-11-24
  • 打赏
  • 举报
回复
UP
Wenxy1 2004-11-24
  • 打赏
  • 举报
回复
晕倒,Win 32 的偶不熟,帮你UP

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧