strcmp()的源码

int strcmp(const char *s1, const char*s2)
{
char* p1;
char* p2;
p1=s1;
p2=s2;
while((*p1)&&(*p2))
{
if(*p1==*p2)
{
p1++;p2++;
}else{
return (*p1-*p2);
}
}
return (*p1-*p2);
}


int strcmp(const char *s1, const char*s2)
{
for(;*s1++==*s2++;)
if (*s1=='\0')
break;
return (*s1-*s2);
}


以下为strcmp.asm 的内容:
page ,132
title strcmp.asm - compare two strings
;***
;strcmp.asm - routine to compare two strings (for equal, less, or greater)
;
; Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
;
;Purpose:
; STRCMP compares two strings and returns an integer
; to indicate whether the first is less than the second, the two are
; equal, or whether the first is greater than the second, respectively.
; Comparison is done byte by byte on an UNSIGNED basis, which is to
; say that Null (0) is less than any other character (1-255).
;
;*******************************************************************************

.xlist
include cruntime.inc
.list

page
;***
;strcmp - compare two strings, returning less than, equal to, or greater than
;
;Purpose:
; Compares two string, determining their lexical order. Unsigned
; comparison is used.
;
; Algorithm:
; int strcmp ( src , dst )
; unsigned char *src;
; unsigned char *dst;
; {
; int ret = 0 ;
;
; while( ! (ret = *src - *dst) && *dst)
; ++src, ++dst;
;
; if ( ret < 0 )
; ret = -1 ;
; else if ( ret > 0 )
; ret = 1 ;
;
; return( ret );
; }
;
;Entry:
; const char * src - string for left-hand side of comparison
; const char * dst - string for right-hand side of comparison
;
;Exit:
; AX < 0, 0, or >0, indicating whether the first string is
; Less than, Equal to, or Greater than the second string.
;
;Uses:
; CX, DX
;
;Exceptions:
;
;*******************************************************************************

CODESEG

public strcmp
strcmp proc

.FPO ( 0, 2, 0, 0, 0, 0 )

mov edx,[esp + 4] ; edx = src
mov ecx,[esp + 8] ; ecx = dst

test edx,3
jnz short dopartial

align 4
dodwords:
mov eax,[edx]

cmp al,[ecx]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 1]
jne short donene
or ah,ah
jz short doneeq

shr eax,16

cmp al,[ecx + 2]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 3]
jne short donene
add ecx,4
add edx,4
or ah,ah
jnz short dodwords

align 4
doneeq:
xor eax,eax
ret

align 4
donene:
; The instructions below should place -1 in eax if src < dst,
; and 1 in eax if src > dst.

sbb eax,eax
sal eax,1
inc eax
ret

align 4
dopartial:
test edx,1
jz short doword

mov al,[edx]
inc edx
cmp al,[ecx]
jne short donene
inc ecx
or al,al
jz short doneeq

test edx,2
jz short dodwords


align 4
doword:
mov ax,[edx]
add edx,2
cmp al,[ecx]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 1]
jne short donene
or ah,ah
jz short doneeq
add ecx,2
jmp short dodwords

strcmp endp

end
其实在C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC目录下有库函数的源代码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gcc strcmp源码如下: ```c int strcmp(const char *s1, const char *s2) { while (*s1 && (*s1 == *s2)) { s1++; s2++; } return *(unsigned char*)s1 - *(unsigned char*)s2; } ``` 这段代码是一个常见的strcmp函数的实现,用于比较两个字符串的大小。它通过逐个比较字符串中的字符来确定它们的大小关系。当遇到不同的字符时,函数会返回它们的差值。如果两个字符串完全相同,则返回0。 这个实现与微软方法和另外一种方法略有不同,但实现的功能是相同的。它使用了指针来遍历字符串,并通过比较每个字符的ASCII值来确定它们的大小关系。在比较字符时,将字符转换为无符号字符,以确保比较的结果是正确的。 需要注意的是,这只是gcc中strcmp函数的一种实现方式,不同的编译器可能会有不同的实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [strcmp源码实现](https://blog.csdn.net/weixin_34107739/article/details/85760564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [你必须知道的495个C语言问题](https://download.csdn.net/download/regandu/9186455)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值