您的位置:首页 > 其它

几种CRT函数的汇编实现

2013-01-18 14:38 218 查看

.data
.set ZERO , 0b00000000000000000000000000000000


strlen:

在这里利用scasb命令,scasb将di指向的数据与al比较,repne表示重复扫描,如果不相等,则di递增指向下一个数据,cx也递减,如此重复,知道遇到结束符‘\0’为止。代码中利用eax存储常值0与数据比较,利用ecx累计长度,由于累计后是负值,所以将其取正返回(拉长32位取反减一)

#asm_strlen(const char *data)
__asm_strlen:

pushl %ebp
movl %esp , %ebp

movl 8(%ebp) , %edi
movl $-1 , %ecx
movl $0 , %eax

cld
repne scasb

orl $ZERO , %ecx
notl %ecx
decl %ecx

movl %ebp , %esp
popl %ebp

movl %ecx , %eax

ret


strncpy:

这里利用lodsb和stosb两个指令,前者将数据从si载入al,后者再将数据再从al存到di中,所以,将参数source的地址放入esi中,将参数dest的地址放入edi,然后进入循环,累减ecx(len),直到len复制完了跳出。

#asm_strncpy(char *dest , char *source , int len)
.global __asm_strncpy
__asm_strncpy:

pushl %ebp
movl %esp , %ebp

movl 16(%ebp) , %ecx
movl 12(%ebp) , %esi
movl 8(%ebp) , %edi

movl %ecx , %ebx

1:
lodsb
stosb
decl %ecx
jne 1b

movl %ebp , %esp
popl %ebp

orl %ecx , %ecx
jne 2

ret
2:
subl %ecx , %ebx
movl %ebx , %eax
ret


strchr:

这里大部分和strlen一样,只是将比较的内容改为参数__c。
#asm_strchr( char *__s, int __c )
.global __asm_strchr
__asm_strchr:

pushl %ebp
movl %esp , %ebp

movl 12(%ebp) , %eax
movl 8(%ebp) , %edi
movl %edi , %ebx
movl $-1 , %ecx

cld
repne scasb

orl $ZERO , %ecx
notl %ecx
decl %ecx

movl %ebp , %esp
popl %ebp

addl %ecx , %ebx
#movl (%ebx) , %eax #return current char
movl %ebx , %eax # return string

ret
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: