绝对经典的汇编源代码

inurlcn 2014-02-07 05:28:55
        page    ,132
title strcmp.asm - compare two strings
;***
;strcmp.asm - routine to compare two strings (for equal, less, or greater)
;
; Copyright (c) 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 \
str1:ptr byte, \
str2:ptr byte

OPTION PROLOGUE:NONE, EPILOGUE:NONE

.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
add eax,1
ret

align 4
dopartial:
test edx,1
jz short doword

mov al,[edx]
add edx,1
cmp al,[ecx]
jne short donene
add ecx,1
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
...全文
256 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
inurlcn 2014-02-12
  • 打赏
  • 举报
回复
222
inurlcn 2014-02-07
  • 打赏
  • 举报
回复
VS2008或更高级的VS工具目录下面,有标准C库的实现方法,还有很详细的解释 C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\intel

21,458

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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