- 论坛徽章:
- 0
|
寒假的时候用汇编写了个处理字符串的函数库,参考了linux/include/string.h,但是这个函数库里,少了个strok函数,呵呵,感觉有点复杂,就没写了,如果以后开发内核需要它的时候在把它补上了。
#
# String Handle Function With ASM
#
# (C) 2007 W.Z.T
#
# 功能: 取得一个字符串的长度
# 输入寄存器: edi:源字符串指针
# al:0
# ecx:0xffff
# 输出寄存器: ecx:字符串的长度
.type strlen,@function
strlen:
pushl %edi
movb $0,%al
movl $0xffff,%ecx
cld
repne scasb
#subw $0xffff,%cx
#neg %cx
not %cx
dec %cx
popl %edi
ret
# 功能: 将一个字符串复制到另一个字符串中,直到遇到空字符为止
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# 输出寄存器: 无
.type strcpy,@function
strcpy:
l1:
pushl %esi
pushl %edi
movsb
cmpl $0,(%esi)
jne l1
popl %edi
popl %esi
ret
# 功能: 复制源字符串前n个字节到目的字符串
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ecx:要复制的字节数
# 输出寄存器: 无
.type strncpy,@function
strncpy:
pushl %esi
pushl %edi
cld
for: movsb
cmpl $0,(%esi)
je next
loop for
next:
popl %edi
popl %esi
ret
# 功能: 将源字符串复制到目的字符串的末尾处
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# al: 0
# ecx;0xffff
# 输出寄存器: 无
.type strcat,@function
strcat:
pushl %esi
pushl %edi
movb $0,%al
movl $0xffff,%ecx
cld
repne scasb
decl %edi
l1: lodsb
stosb
testb %al,%al
jne l1
popl %edi
popl %esi
ret
# 功能: 将源字符串的前n个字节复制到目的字符串的末尾处
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# al: 0
# ecx;0xffff
# edx:要复制的字符串长度
# 输出寄存器: 无
.type strncat,@function
strncat:
pushl %esi
pushl %edi
movb $0,%al
movl $0xffff,%ecx
cld
repne scasb
decl %edi
movl %edx,%ecx
rep movsb
movl $0,(%edi)
popl %edi
popl %esi
ret
# 功能: 比较源字符串和目的字符串的大小
# 输入寄存器: esi:源字符串指针
# ediD康淖址??刚?
# 输出寄存器: eax: 1,源字符串>目的字符串
# 0,源字符串=目的字符串
# -1,源字符串目的字符串
.type strcmp,@function
strcmp:
pushl %esi
pushl %edi
cld
l1: lodsb
scasb
jne l2
testb %al,%al
jne l1
movl $0,%eax
jmp l4
l2: ja l3
movl $-1,%eax
jmp l4
l3: movl $1,%eax
l4: popl %edi
popl %esi
ret
# 功能: 比较源字符串和目的字符串的前n个字节大小
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ecx:前n个字节
# 输出寄存器: eax: 1,源字符串>目的字符串
# 0,源字符串=目的字符串
# -1,源字符串目的字符串
.type strncmp,@function
strncmp:
pushl %esi
pushl %edi
cld
repe cmpsb
cmpsb
je equ
ja big
jb small
equ: movl $0,%eax
jmp next
big: movl $1,%eax
jmp next
small: movl $-1,%eax
next: popl %edi
popl %esi
ret
# 功能: 在字符串中寻找第一个要匹配的字符
# 输入寄存器: esi:源字符串指针
# bl: 要匹配的字符
# 输出寄存器: esi:保存字符串中第一次出现匹配字符的指针,没找到则返回空指针
.type strchr,@function
strchr:
pushl %esi
pushl %ebx
cld
l1: lodsb
testb %al,%al
je n1
cmpb %bl,%al
jne l1
decl %esi
jmp n2
n1: xorl %esi,%esi
n2: popl %ebx
popl %esi
ret
# 功能: 在字符串中寻找最后一个要匹配的字符
# 输入寄存器: esi:源字符串指针
# edi:0
# bl: 要匹配的字符
# 输出寄存器: esi:保存字符串中最后一次出现匹配字符的指针,没找到则返回空指针
.type strrchr,@function
strrchr:
pushl %esi
pushl %edi
pushl %ebx
xorl %edi,%edi
cld
l1: lodsb
testb %al,%al
je n1
cmpb %bl,%al
je n2
jmp l1
n2: decl %esi
movl %esi,%edi
incl %esi
jmp l1
n1: movl %edi,%esi
pushl %ebx
pushl %edi
pushl %esi
ret
# 功能: 在字符串1中寻找首个包含在字符串2中的任何字符
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ebx:0
# ecx:0xffffffff
# al:0
# 输出寄存器: esi:保存字符串1中首个含字符串2的指针,没找到返回空指针
.type strpbrk,@function
strpbrk:
pushl %esi
pushl %edi
pushl %ebx
movb $0,%ebx
movl $0xffffffff,%ecx
cld
repne
scasb
notl %ecx
decl %ecx
movl %ecx,%edx
l1:
lodsb
testb %al,%al
je l3
movl $str2,%edi
movl %edx,%ecx
repne
scasb
je l2
jmp l1
l2: decl %esi
jmp next
l3: xor %esi,%esi
next: popl %ebx
popl %edi
popl %esi
ret
# 功能: 寻找字符串1中不包含字符串2中任何字符的第1个字符序列
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ebx:0
# ecx:0xffffffff
# al:0
# 输出寄存器: ebx:返回字符串1中不包含字符串2中任何字符的第1个字符序列的长度
.type strcspn,@function
strcspn:
pushl %esi
pushl %edi
pushl %ebx
movb $0,%ebx
movl $0xffffffff,%ecx
cld
repne
scasb
notl %ecx
decl %ecx
movl %ecx,%edx
l1:
lodsb
testb %al,%al
je next
movl $str2,%edi
movl %edx,%ecx
repne
scasb
je l3
incl %ebx
jmp l1
jmp next
l3: testl %ebx,%ebx
je l1
next: popl %ebx
popl %edi
popl %esi
ret
# 功能: 寻找字符串1中包含字符串2中任何字符的第1个字符序列
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ebx:0
# ecx:0xffffffff
# al:0
# 输出寄存器: ebx:返回字符串1中包含字符串2中任何字符的第1个字符序列的长度
.type strspn,@function
strspn:
pushl %esi
pushl %edi
pushl %ebx
movb $0,%ebx
movl $0xffffffff,%ecx
cld
repne
scasb
notl %ecx
decl %ecx
movl %ecx,%edx
l1:
lodsb
testb %al,%al
je next
movl $str2,%edi
movl %edx,%ecx
repne
scasb
jne l3
incl %ebx
jmp l1
jmp next
l3: testl %ebx,%ebx
je l1
next: popl %ebx
popl %edi
popl %esi
ret
# 功能: 在字符串1中寻找第1个匹配整个字符串2的字符串
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ebx:0
# ecx:0xffffffff
# al:0
# 输出寄存器: esi:在字符串1中寻找第1个匹配整个字符串2的字符串的指针
.type strstr,@function
strstr:
pushl %esi
pushl %edi
pushl %ebx
movb $0,%ebx
movl $0xffffffff,%ecx
cld
repne
scasb
notl %ecx
decl %ecx
movl %ecx,%edx
l1:
lodsb
testl %eax,%eax
je l4
decl %esi
cmpsb
jne l3
incl %ebx
cmpl %edx,%ebx
je l4
jmp l1
l3: cmpl $1,%ebx
jb l2
decl %esi
decl %esi
l2: movl $str2,%edi
xorl %ebx,%ebx
jmp l1
l4:
subl %edx,%esi
popl %ebx
popl %edi
popl %esi
ret
# 功能: 在n字节大小的内存块中寻找指定字符
# 输入寄存器: edi:目的字符串指针
# ecx:n字节大小
# al:寻找的字符
# 输出寄存器: 返回第一个匹配字符的指针,没找到就返回空指针
.type memchr,@function
memchr:
pushl %edi
cld
repne scasb
je next
xorl %edi,%edi
next: decl %edi
popl %edi
ret
# 功能: 比较两个内存块前n个字节大小
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# ecx:前n个字节
# 输出寄存器: eax: 1,源字符串>目的字符串
# 0,源字符串=目的字符串
# -1,源字符串目的字符串
.type memcmp,@function
memcmp:
pushl %esi
pushl %edi
cld
repe cmpsb
cmpsb
je equ
ja big
jb small
equ: movl $0,%eax
jmp next
big: movl $1,%eax
jmp next
small: movl $-1,%eax
next: popl %edi
popl %esi
ret
# 功能: 将一个内存块复制到另一个内存块中,直到遇到空字符为止
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# 输出寄存器: 无
.type memcpy,@function
memcpy:
pushl %esi
pushl %edi
cld
rep
movsb
popl %edi
popl %esi
ret
# 功能: 将一个内存块的前n个字节移动到另一个内存块中
# 输入寄存器: esi:源字符串指针
# edi:目的字符串指针
# 输出寄存器: 无
.type memmove,@function
memmove:
pushl %esi
pushl %edi
cld
rep movsb
popl %edi
popl %esi
ret
# 功能: 用指定字符填写指定长度的内存块
# 输入寄存器: edi:目的字符串指针
# al:指定字符
# ecx:填写n个字节
# 输出寄存器: 无
.type memset,@function
memset:
pushl %edi
cld
rep
stosb
popl %edi
ret
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15780/showart_258246.html |
|