Convert library asm files to GAS syntax

This commit is contained in:
Arun Thomas
2010-03-03 14:27:30 +00:00
parent bf7397b64e
commit cbd276e4ce
302 changed files with 2544 additions and 2451 deletions
+25 -25
View File
@@ -2,28 +2,28 @@
.PATH: ${.CURDIR}/arch/${ARCH}/string
SRCS+= \
_memmove.s \
_strncat.s \
_strncmp.s \
_strncpy.s \
_strnlen.s \
bcmp.s \
bcopy.s \
bzero.s \
index.s \
memchr.s \
memcmp.s \
memcpy.s \
memmove.s \
memset.s \
rindex.s \
strcat.s \
strchr.s \
strcmp.s \
strcpy.s \
strlen.s \
strncat.s \
strncmp.s \
strncpy.s \
strnlen.s \
strrchr.s
_memmove.S \
_strncat.S \
_strncmp.S \
_strncpy.S \
_strnlen.S \
bcmp.S \
bcopy.S \
bzero.S \
index.S \
memchr.S \
memcmp.S \
memcpy.S \
memmove.S \
memset.S \
rindex.S \
strcat.S \
strchr.S \
strcmp.S \
strcpy.S \
strlen.S \
strncat.S \
strncmp.S \
strncpy.S \
strnlen.S \
strrchr.S
+60
View File
@@ -0,0 +1,60 @@
/* _memmove() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void *_memmove(void *s1, const void *s2, size_t n) */
/* Copy a chunk of memory. Handle overlap. */
/* */
.text
.globl __memmove, __memcpy
.balign 16
__memmove:
push %ebp
movl %esp, %ebp
push %esi
push %edi
movl 8(%ebp), %edi /* String s1 */
movl 12(%ebp), %esi /* String s2 */
movl 16(%ebp), %ecx /* Length */
movl %edi, %eax
subl %esi, %eax
cmpl %ecx, %eax
jb downwards /* if (s2 - s1) < n then copy downwards */
__memcpy:
cld /* Clear direction bit: upwards */
cmpl $16, %ecx
jb upbyte /* Don't bother being smart with short arrays */
movl %esi, %eax
orl %edi, %eax
testb $1, %al
jne upbyte /* Bit 0 set, use byte copy */
testb $2, %al
jne upword /* Bit 1 set, use word copy */
uplword:
shrdl $2, %ecx, %eax /* Save low 2 bits of ecx in eax */
shrl $2, %ecx
rep movsl /* Copy longwords. */
shldl $2, %eax, %ecx /* Restore excess count */
upword:
shrl $1, %ecx
rep movsw /* Copy words */
adcl %ecx, %ecx /* One more byte? */
upbyte:
rep movsb /* Copy bytes */
done:
movl 8(%ebp), %eax /* Absolutely noone cares about this value */
pop %edi
pop %esi
pop %ebp
ret
/* Handle bad overlap by copying downwards, don't bother to do word copies. */
downwards:
std /* Set direction bit: downwards */
leal -1(%esi,%ecx,1), %esi
leal -1(%edi,%ecx,1), %edi
rep movsb /* Copy bytes */
cld
jmp done
-58
View File
@@ -1,58 +0,0 @@
! _memmove() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void *_memmove(void *s1, const void *s2, size_t n)
! Copy a chunk of memory. Handle overlap.
!
.sect .text
.define __memmove, __memcpy
.align 16
__memmove:
push ebp
mov ebp, esp
push esi
push edi
mov edi, 8(ebp) ! String s1
mov esi, 12(ebp) ! String s2
mov ecx, 16(ebp) ! Length
mov eax, edi
sub eax, esi
cmp eax, ecx
jb downwards ! if (s2 - s1) < n then copy downwards
__memcpy:
cld ! Clear direction bit: upwards
cmp ecx, 16
jb upbyte ! Don't bother being smart with short arrays
mov eax, esi
or eax, edi
testb al, 1
jnz upbyte ! Bit 0 set, use byte copy
testb al, 2
jnz upword ! Bit 1 set, use word copy
uplword:shrd eax, ecx, 2 ! Save low 2 bits of ecx in eax
shr ecx, 2
rep
movs ! Copy longwords.
shld ecx, eax, 2 ! Restore excess count
upword: shr ecx, 1
rep
o16 movs ! Copy words
adc ecx, ecx ! One more byte?
upbyte: rep
movsb ! Copy bytes
done: mov eax, 8(ebp) ! Absolutely noone cares about this value
pop edi
pop esi
pop ebp
ret
! Handle bad overlap by copying downwards, don't bother to do word copies.
downwards:
std ! Set direction bit: downwards
lea esi, -1(esi)(ecx*1)
lea edi, -1(edi)(ecx*1)
rep
movsb ! Copy bytes
cld
jmp done
+41
View File
@@ -0,0 +1,41 @@
/* _strncat() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* char *_strncat(char *s1, const char *s2, size_t edx) */
/* Append string s2 to s1. */
/* */
.text
.globl __strncat
.balign 16
__strncat:
push %ebp
movl %esp, %ebp
push %esi
push %edi
movl 8(%ebp), %edi /* String s1 */
movl $-1, %ecx
xorb %al, %al /* Null byte */
cld
repne scasb /* Look for the zero byte in s1 */
decl %edi /* Back one up (and clear 'Z' flag) */
push %edi /* Save end of s1 */
movl 12(%ebp), %edi /* edi = string s2 */
movl %edx, %ecx /* Maximum count */
repne scasb /* Look for the end of s2 */
jne no0
incl %ecx /* Exclude null byte */
no0:
subl %ecx, %edx /* Number of bytes in s2 */
movl %edx, %ecx
movl 12(%ebp), %esi /* esi = string s2 */
pop %edi /* edi = end of string s1 */
rep movsb /* Copy bytes */
stosb /* Add a terminating null */
movl 8(%ebp), %eax /* Return s1 */
pop %edi
pop %esi
pop %ebp
ret
-41
View File
@@ -1,41 +0,0 @@
! _strncat() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *_strncat(char *s1, const char *s2, size_t edx)
! Append string s2 to s1.
!
.sect .text
.define __strncat
.align 16
__strncat:
push ebp
mov ebp, esp
push esi
push edi
mov edi, 8(ebp) ! String s1
mov ecx, -1
xorb al, al ! Null byte
cld
repne
scasb ! Look for the zero byte in s1
dec edi ! Back one up (and clear 'Z' flag)
push edi ! Save end of s1
mov edi, 12(ebp) ! edi = string s2
mov ecx, edx ! Maximum count
repne
scasb ! Look for the end of s2
jne no0
inc ecx ! Exclude null byte
no0: sub edx, ecx ! Number of bytes in s2
mov ecx, edx
mov esi, 12(ebp) ! esi = string s2
pop edi ! edi = end of string s1
rep
movsb ! Copy bytes
stosb ! Add a terminating null
mov eax, 8(ebp) ! Return s1
pop edi
pop esi
pop ebp
ret
+35
View File
@@ -0,0 +1,35 @@
/* strncmp() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* int strncmp(const char *s1, const char *s2, size_t ecx) */
/* Compare two strings. */
/* */
.text
.globl __strncmp
.balign 16
__strncmp:
push %ebp
movl %esp, %ebp
push %esi
push %edi
testl %ecx, %ecx /* Max length is zero? */
je done
movl 8(%ebp), %esi /* esi = string s1 */
movl 12(%ebp), %edi /* edi = string s2 */
cld
compare:
cmpsb /* Compare two bytes */
jne done
cmpb $0, -1(%esi) /* End of string? */
je done
decl %ecx /* Length limit reached? */
jne compare
done:
seta %al /* al = (s1 > s2) */
setb %ah /* ah = (s1 < s2) */
subb %ah, %al
movsbl %al, %eax /* eax = (s1 > s2) - (s1 < s2), i.e. -1, 0, 1 */
pop %edi
pop %esi
pop %ebp
ret
-35
View File
@@ -1,35 +0,0 @@
! strncmp() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! int strncmp(const char *s1, const char *s2, size_t ecx)
! Compare two strings.
!
.sect .text
.define __strncmp
.align 16
__strncmp:
push ebp
mov ebp, esp
push esi
push edi
test ecx, ecx ! Max length is zero?
je done
mov esi, 8(ebp) ! esi = string s1
mov edi, 12(ebp) ! edi = string s2
cld
compare:
cmpsb ! Compare two bytes
jne done
cmpb -1(esi), 0 ! End of string?
je done
dec ecx ! Length limit reached?
jne compare
done: seta al ! al = (s1 > s2)
setb ah ! ah = (s1 < s2)
subb al, ah
movsxb eax, al ! eax = (s1 > s2) - (s1 < s2), i.e. -1, 0, 1
pop edi
pop esi
pop ebp
ret
+23
View File
@@ -0,0 +1,23 @@
/* _strncpy() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* char *_strncpy(char *s1, const char *s2, size_t ecx) */
/* Copy string s2 to s1. */
/* */
.text
.globl __strncpy
.balign 16
__strncpy:
movl 12(%ebp), %edi /* edi = string s2 */
xorb %al, %al /* Look for a zero byte */
movl %ecx, %edx /* Save maximum count */
cld
repne scasb /* Look for end of s2 */
subl %ecx, %edx /* Number of bytes in s2 including null */
xchgl %edx, %ecx
movl 12(%ebp), %esi /* esi = string s2 */
movl 8(%ebp), %edi /* edi = string s1 */
rep movsb /* Copy bytes */
ret
-24
View File
@@ -1,24 +0,0 @@
! _strncpy() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *_strncpy(char *s1, const char *s2, size_t ecx)
! Copy string s2 to s1.
!
.sect .text
.define __strncpy
.align 16
__strncpy:
mov edi, 12(ebp) ! edi = string s2
xorb al, al ! Look for a zero byte
mov edx, ecx ! Save maximum count
cld
repne
scasb ! Look for end of s2
sub edx, ecx ! Number of bytes in s2 including null
xchg ecx, edx
mov esi, 12(ebp) ! esi = string s2
mov edi, 8(ebp) ! edi = string s1
rep
movsb ! Copy bytes
ret
+28
View File
@@ -0,0 +1,28 @@
/* _strnlen() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* size_t _strnlen(const char *s, size_t ecx) */
/* Return the length of a string. */
/* */
.text
.globl __strnlen
.balign 16
__strnlen:
push %ebp
movl %esp, %ebp
push %edi
movl 8(%ebp), %edi /* edi = string */
xorb %al, %al /* Look for a zero byte */
movl %ecx, %edx /* Save maximum count */
cmpb $1, %cl /* 'Z' bit must be clear if ecx = 0 */
cld
repne scasb /* Look for zero */
jne no0
incl %ecx /* Don't count zero byte */
no0:
movl %edx, %eax
subl %ecx, %eax /* Compute bytes scanned */
pop %edi
pop %ebp
ret
-28
View File
@@ -1,28 +0,0 @@
! _strnlen() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! size_t _strnlen(const char *s, size_t ecx)
! Return the length of a string.
!
.sect .text
.define __strnlen
.align 16
__strnlen:
push ebp
mov ebp, esp
push edi
mov edi, 8(ebp) ! edi = string
xorb al, al ! Look for a zero byte
mov edx, ecx ! Save maximum count
cmpb cl, 1 ! 'Z' bit must be clear if ecx = 0
cld
repne
scasb ! Look for zero
jne no0
inc ecx ! Don't count zero byte
no0: mov eax, edx
sub eax, ecx ! Compute bytes scanned
pop edi
pop ebp
ret
+28
View File
@@ -0,0 +1,28 @@
/* bcmp() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* int bcmp(const void *s1, const void *s2, size_t n) */
/* Compare two chunks of memory. */
/* This is a BSD routine that escaped from the kernel. Don't use. */
/* (Alas it is not without some use, it reports the number of bytes */
/* after the bytes that are equal. So it can't be simply replaced.) */
/* */
.text
.globl _bcmp
.balign 16
_bcmp:
push %ebp
movl %esp, %ebp
push 16(%ebp)
push 12(%ebp)
push 8(%ebp)
call _memcmp /* Let memcmp do the work */
testl %eax, %eax
je equal
subl 8(%ebp), %edx /* Memcmp was nice enough to leave "esi" in edx */
decl %edx /* Number of bytes that are equal */
movl 16(%ebp), %eax
subl %edx, %eax /* Number of bytes that are unequal */
equal:
leave
ret
-28
View File
@@ -1,28 +0,0 @@
! bcmp() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! int bcmp(const void *s1, const void *s2, size_t n)
! Compare two chunks of memory.
! This is a BSD routine that escaped from the kernel. Don't use.
! (Alas it is not without some use, it reports the number of bytes
! after the bytes that are equal. So it can't be simply replaced.)
!
.sect .text
.define _bcmp
.align 16
_bcmp:
push ebp
mov ebp, esp
push 16(ebp)
push 12(ebp)
push 8(ebp)
call _memcmp ! Let memcmp do the work
test eax, eax
jz equal
sub edx, 8(ebp) ! Memcmp was nice enough to leave "esi" in edx
dec edx ! Number of bytes that are equal
mov eax, 16(ebp)
sub eax, edx ! Number of bytes that are unequal
equal: leave
ret
+15
View File
@@ -0,0 +1,15 @@
/* bcopy() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void bcopy(const void *s1, void *s2, size_t n) */
/* Copy a chunk of memory. Handle overlap. */
/* This is a BSD routine that escaped from the kernel. Don't use. */
/* */
.text
.globl _bcopy
.balign 16
_bcopy:
movl 4(%esp), %eax /* Exchange string arguments */
xchgl 8(%esp), %eax
movl %eax, 4(%esp)
jmp __memmove /* Call the proper routine */
-16
View File
@@ -1,16 +0,0 @@
! bcopy() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void bcopy(const void *s1, void *s2, size_t n)
! Copy a chunk of memory. Handle overlap.
! This is a BSD routine that escaped from the kernel. Don't use.
!
.sect .text
.define _bcopy
.align 16
_bcopy:
mov eax, 4(esp) ! Exchange string arguments
xchg eax, 8(esp)
mov 4(esp), eax
jmp __memmove ! Call the proper routine
+19
View File
@@ -0,0 +1,19 @@
/* bzero() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void bzero(void *s, size_t n) */
/* Set a chunk of memory to zero. */
/* This is a BSD routine that escaped from the kernel. Don't use. */
/* */
.text
.globl _bzero
.balign 16
_bzero:
push %ebp
movl %esp, %ebp
push 12(%ebp) /* Size */
push $0 /* Zero */
push 8(%ebp) /* String */
call _memset /* Call the proper routine */
leave
ret
-20
View File
@@ -1,20 +0,0 @@
! bzero() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void bzero(void *s, size_t n)
! Set a chunk of memory to zero.
! This is a BSD routine that escaped from the kernel. Don't use.
!
.sect .text
.define _bzero
.align 16
_bzero:
push ebp
mov ebp, esp
push 12(ebp) ! Size
push 0 ! Zero
push 8(ebp) ! String
call _memset ! Call the proper routine
leave
ret
+12
View File
@@ -0,0 +1,12 @@
/* index() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* char *index(const char *s, int c) */
/* Look for a character in a string. Has suffered from a hostile */
/* takeover by strchr(). */
/* */
.text
.globl _index
.balign 16
_index:
jmp _strchr
-13
View File
@@ -1,13 +0,0 @@
! index() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *index(const char *s, int c)
! Look for a character in a string. Has suffered from a hostile
! takeover by strchr().
!
.sect .text
.define _index
.align 16
_index:
jmp _strchr
+30
View File
@@ -0,0 +1,30 @@
/* memchr() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void *memchr(const void *s, int c, size_t n) */
/* Look for a character in a chunk of memory. */
/* */
.text
.globl _memchr
.balign 16
_memchr:
push %ebp
movl %esp, %ebp
push %edi
movl 8(%ebp), %edi /* edi = string */
movb 12(%ebp), %al /* The character to look for */
movl 16(%ebp), %ecx /* Length */
cmpb $1, %cl /* 'Z' bit must be clear if ecx = 0 */
cld
repne scasb
jne failure
leal -1(%edi), %eax /* Found */
pop %edi
pop %ebp
ret
failure:
xorl %eax, %eax
pop %edi
pop %ebp
ret
-30
View File
@@ -1,30 +0,0 @@
! memchr() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void *memchr(const void *s, int c, size_t n)
! Look for a character in a chunk of memory.
!
.sect .text
.define _memchr
.align 16
_memchr:
push ebp
mov ebp, esp
push edi
mov edi, 8(ebp) ! edi = string
movb al, 12(ebp) ! The character to look for
mov ecx, 16(ebp) ! Length
cmpb cl, 1 ! 'Z' bit must be clear if ecx = 0
cld
repne
scasb
jne failure
lea eax, -1(edi) ! Found
pop edi
pop ebp
ret
failure:xor eax, eax
pop edi
pop ebp
ret
+58
View File
@@ -0,0 +1,58 @@
/* memcmp() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* int memcmp(const void *s1, const void *s2, size_t n) */
/* Compare two chunks of memory. */
/* */
.text
.globl _memcmp
.balign 16
_memcmp:
cld
push %ebp
movl %esp, %ebp
push %esi
push %edi
movl 8(%ebp), %esi /* String s1 */
movl 12(%ebp), %edi /* String s2 */
movl 16(%ebp), %ecx /* Length */
cmpl $16, %ecx
jb cbyte /* Don't bother being smart with short arrays */
movl %esi, %eax
orl %edi, %eax
testb $1, %al
jne cbyte /* Bit 0 set, use byte compare */
testb $2, %al
jne cword /* Bit 1 set, use word compare */
clword:
shrdl $2, %ecx, %eax /* Save low two bits of ecx in eax */
shrl $2, %ecx
repe cmpsl /* Compare longwords */
subl $4, %esi
subl $4, %edi
incl %ecx /* Recompare the last longword */
shldl $2, %eax, %ecx /* And any excess bytes */
jmp last
cword:
shrdl $1, %ecx, %eax /* Save low bit of ecx in eax */
shrl $1, %ecx
repe cmpsw /* Compare words */
subl $2, %esi
subl $2, %edi
incl %ecx /* Recompare the last word */
shldl $1, %eax, %ecx /* And one more byte? */
cbyte:
testl %ecx, %ecx /* Set 'Z' flag if ecx = 0 */
last:
repe cmpsb /* Look for the first differing byte */
seta %al /* al = (s1 > s2) */
setb %ah /* ah = (s1 < s2) */
subb %ah, %al
movsbl %al, %eax /* eax = (s1 > s2) - (s1 < s2), i.e. -1, 0, 1 */
movl %esi, %edx /* For bcmp() to play with */
pop %edi
pop %esi
pop %ebp
ret
-56
View File
@@ -1,56 +0,0 @@
! memcmp() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! int memcmp(const void *s1, const void *s2, size_t n)
! Compare two chunks of memory.
!
.sect .text
.define _memcmp
.align 16
_memcmp:
cld
push ebp
mov ebp, esp
push esi
push edi
mov esi, 8(ebp) ! String s1
mov edi, 12(ebp) ! String s2
mov ecx, 16(ebp) ! Length
cmp ecx, 16
jb cbyte ! Don't bother being smart with short arrays
mov eax, esi
or eax, edi
testb al, 1
jnz cbyte ! Bit 0 set, use byte compare
testb al, 2
jnz cword ! Bit 1 set, use word compare
clword: shrd eax, ecx, 2 ! Save low two bits of ecx in eax
shr ecx, 2
repe
cmps ! Compare longwords
sub esi, 4
sub edi, 4
inc ecx ! Recompare the last longword
shld ecx, eax, 2 ! And any excess bytes
jmp last
cword: shrd eax, ecx, 1 ! Save low bit of ecx in eax
shr ecx, 1
repe
o16 cmps ! Compare words
sub esi, 2
sub edi, 2
inc ecx ! Recompare the last word
shld ecx, eax, 1 ! And one more byte?
cbyte: test ecx, ecx ! Set 'Z' flag if ecx = 0
last: repe
cmpsb ! Look for the first differing byte
seta al ! al = (s1 > s2)
setb ah ! ah = (s1 < s2)
subb al, ah
movsxb eax, al ! eax = (s1 > s2) - (s1 < s2), i.e. -1, 0, 1
mov edx, esi ! For bcmp() to play with
pop edi
pop esi
pop ebp
ret
+23
View File
@@ -0,0 +1,23 @@
/* memcpy() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void *memcpy(void *s1, const void *s2, size_t n) */
/* Copy a chunk of memory. */
/* This routine need not handle overlap, so it does not handle overlap. */
/* One could simply call __memmove, the cost of the overlap check is */
/* negligible, but you are dealing with a programmer who believes that if */
/* anything can go wrong, it should go wrong. */
/* */
.text
.globl _memcpy
.balign 16
_memcpy:
push %ebp
movl %esp, %ebp
push %esi
push %edi
movl 8(%ebp), %edi /* String s1 */
movl 12(%ebp), %esi /* String s2 */
movl 16(%ebp), %ecx /* Length */
/* No overlap check here */
jmp __memcpy /* Call the part of __memmove that copies up */
-24
View File
@@ -1,24 +0,0 @@
! memcpy() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void *memcpy(void *s1, const void *s2, size_t n)
! Copy a chunk of memory.
! This routine need not handle overlap, so it does not handle overlap.
! One could simply call __memmove, the cost of the overlap check is
! negligible, but you are dealing with a programmer who believes that
! if anything can go wrong, it should go wrong.
!
.sect .text
.define _memcpy
.align 16
_memcpy:
push ebp
mov ebp, esp
push esi
push edi
mov edi, 8(ebp) ! String s1
mov esi, 12(ebp) ! String s2
mov ecx, 16(ebp) ! Length
! No overlap check here
jmp __memcpy ! Call the part of __memmove that copies up
+11
View File
@@ -0,0 +1,11 @@
/* memmove() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void *memmove(void *s1, const void *s2, size_t n) */
/* Copy a chunk of memory. Handle overlap. */
/* */
.text
.globl _memmove
.balign 16
_memmove:
jmp __memmove /* Call common code */
-12
View File
@@ -1,12 +0,0 @@
! memmove() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void *memmove(void *s1, const void *s2, size_t n)
! Copy a chunk of memory. Handle overlap.
!
.sect .text
.define _memmove
.align 16
_memmove:
jmp __memmove ! Call common code
+46
View File
@@ -0,0 +1,46 @@
/* memset() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* void *memset(void *s, int c, size_t n) */
/* Set a chunk of memory to the same byte value. */
/* */
.text
.globl _memset
.balign 16
_memset:
push %ebp
movl %esp, %ebp
push %edi
movl 8(%ebp), %edi /* The string */
movzbl 12(%ebp), %eax /* The fill byte */
movl 16(%ebp), %ecx /* Length */
cld
cmpl $16, %ecx
jb sbyte /* Don't bother being smart with short arrays */
testl $1, %edi
jne sbyte /* Bit 0 set, use byte store */
testl $2, %edi
jne sword /* Bit 1 set, use word store */
slword:
movb %al, %ah
movl %eax, %edx
sall $16, %edx
orl %edx, %eax /* One byte to four bytes */
shrdl $2, %ecx, %edx /* Save low two bits of ecx in edx */
shrl $2, %ecx
rep stosl /* Store longwords. */
shldl $2, %edx, %ecx /* Restore low two bits */
sword:
movb %al, %ah /* One byte to two bytes */
shrl $1, %ecx
rep stosw /* Store words */
adcl %ecx, %ecx /* One more byte? */
sbyte:
rep stosb /* Store bytes */
done:
movl 8(%ebp), %eax /* Return some value you have no need for */
pop %edi
pop %ebp
ret
-44
View File
@@ -1,44 +0,0 @@
! memset() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! void *memset(void *s, int c, size_t n)
! Set a chunk of memory to the same byte value.
!
.sect .text
.define _memset
.align 16
_memset:
push ebp
mov ebp, esp
push edi
mov edi, 8(ebp) ! The string
movzxb eax, 12(ebp) ! The fill byte
mov ecx, 16(ebp) ! Length
cld
cmp ecx, 16
jb sbyte ! Don't bother being smart with short arrays
test edi, 1
jnz sbyte ! Bit 0 set, use byte store
test edi, 2
jnz sword ! Bit 1 set, use word store
slword: movb ah, al
mov edx, eax
sal edx, 16
or eax, edx ! One byte to four bytes
shrd edx, ecx, 2 ! Save low two bits of ecx in edx
shr ecx, 2
rep
stos ! Store longwords.
shld ecx, edx, 2 ! Restore low two bits
sword: movb ah, al ! One byte to two bytes
shr ecx, 1
rep
o16 stos ! Store words
adc ecx, ecx ! One more byte?
sbyte: rep
stosb ! Store bytes
done: mov eax, 8(ebp) ! Return some value you have no need for
pop edi
pop ebp
ret
+12
View File
@@ -0,0 +1,12 @@
/* rindex() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* char *rindex(const char *s, int c) */
/* Look for the last occurrence a character in a string. Has suffered */
/* from a hostile takeover by strrchr(). */
/* */
.text
.globl _rindex
.balign 16
_rindex:
jmp _strrchr
-13
View File
@@ -1,13 +0,0 @@
! rindex() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *rindex(const char *s, int c)
! Look for the last occurrence a character in a string. Has suffered
! from a hostile takeover by strrchr().
!
.sect .text
.define _rindex
.align 16
_rindex:
jmp _strrchr
+12
View File
@@ -0,0 +1,12 @@
/* strcat() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* char *strcat(char *s1, const char *s2) */
/* Append string s2 to s1. */
/* */
.text
.globl _strcat
.balign 16
_strcat:
movl $-1, %edx /* Unlimited length */
jmp __strncat /* Common code */
-13
View File
@@ -1,13 +0,0 @@
! strcat() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *strcat(char *s1, const char *s2)
! Append string s2 to s1.
!
.sect .text
.define _strcat
.align 16
_strcat:
mov edx, -1 ! Unlimited length
jmp __strncat ! Common code
+42
View File
@@ -0,0 +1,42 @@
/* strchr() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* char *strchr(const char *s, int c) */
/* Look for a character in a string. */
/* */
.text
.globl _strchr
.balign 16
_strchr:
push %ebp
movl %esp, %ebp
push %edi
cld
movl 8(%ebp), %edi /* edi = string */
movl $16, %edx /* Look at small chunks of the string */
next:
shll $1, %edx /* Chunks become bigger each time */
movl %edx, %ecx
xorb %al, %al /* Look for the zero at the end */
repne scasb
pushf /* Remember the flags */
subl %edx, %ecx
negl %ecx /* Some or all of the chunk */
subl %ecx, %edi /* Step back */
movb 12(%ebp), %al /* The character to look for */
repne scasb
je found
popf /* Did we find the end of string earlier? */
jne next /* No, try again */
xorl %eax, %eax /* Return NULL */
pop %edi
pop %ebp
ret
found:
pop %eax /* Get rid of those flags */
leal -1(%edi), %eax /* Address of byte found */
pop %edi
pop %ebp
ret
-41
View File
@@ -1,41 +0,0 @@
! strchr() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *strchr(const char *s, int c)
! Look for a character in a string.
!
.sect .text
.define _strchr
.align 16
_strchr:
push ebp
mov ebp, esp
push edi
cld
mov edi, 8(ebp) ! edi = string
mov edx, 16 ! Look at small chunks of the string
next: shl edx, 1 ! Chunks become bigger each time
mov ecx, edx
xorb al, al ! Look for the zero at the end
repne
scasb
pushf ! Remember the flags
sub ecx, edx
neg ecx ! Some or all of the chunk
sub edi, ecx ! Step back
movb al, 12(ebp) ! The character to look for
repne
scasb
je found
popf ! Did we find the end of string earlier?
jne next ! No, try again
xor eax, eax ! Return NULL
pop edi
pop ebp
ret
found: pop eax ! Get rid of those flags
lea eax, -1(edi) ! Address of byte found
pop edi
pop ebp
ret
+12
View File
@@ -0,0 +1,12 @@
/* strcmp() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* int strcmp(const char *s1, const char *s2) */
/* Compare two strings. */
/* */
.text
.globl _strcmp
.balign 16
_strcmp:
movl $-1, %ecx /* Unlimited length */
jmp __strncmp /* Common code */
-13
View File
@@ -1,13 +0,0 @@
! strcmp() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! int strcmp(const char *s1, const char *s2)
! Compare two strings.
!
.sect .text
.define _strcmp
.align 16
_strcmp:
mov ecx, -1 ! Unlimited length
jmp __strncmp ! Common code
+21
View File
@@ -0,0 +1,21 @@
/* strcpy() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* char *strcpy(char *s1, const char *s2) */
/* Copy string s2 to s1. */
/* */
.text
.globl _strcpy
.balign 16
_strcpy:
push %ebp
movl %esp, %ebp
push %esi
push %edi
movl $-1, %ecx /* Unlimited length */
call __strncpy /* Common code */
movl 8(%ebp), %eax /* Return s1 */
pop %edi
pop %esi
pop %ebp
ret
-22
View File
@@ -1,22 +0,0 @@
! strcpy() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *strcpy(char *s1, const char *s2)
! Copy string s2 to s1.
!
.sect .text
.define _strcpy
.align 16
_strcpy:
push ebp
mov ebp, esp
push esi
push edi
mov ecx, -1 ! Unlimited length
call __strncpy ! Common code
mov eax, 8(ebp) ! Return s1
pop edi
pop esi
pop ebp
ret
+12
View File
@@ -0,0 +1,12 @@
/* strlen() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* size_t strlen(const char *s) */
/* Return the length of a string. */
/* */
.text
.globl _strlen
.balign 16
_strlen:
movl $-1, %ecx /* Unlimited length */
jmp __strnlen /* Common code */
-13
View File
@@ -1,13 +0,0 @@
! strlen() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! size_t strlen(const char *s)
! Return the length of a string.
!
.sect .text
.define _strlen
.align 16
_strlen:
mov ecx, -1 ! Unlimited length
jmp __strnlen ! Common code
+12
View File
@@ -0,0 +1,12 @@
/* strncat() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* size_t strncat(char *s1, const char *s2, size_t n) */
/* Append string s2 to s1. */
/* */
.text
.globl _strncat
.balign 16
_strncat:
movl 12(%esp), %edx /* Maximum length */
jmp __strncat /* Common code */
-13
View File
@@ -1,13 +0,0 @@
! strncat() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! size_t strncat(char *s1, const char *s2, size_t n)
! Append string s2 to s1.
!
.sect .text
.define _strncat
.align 16
_strncat:
mov edx, 12(esp) ! Maximum length
jmp __strncat ! Common code
+12
View File
@@ -0,0 +1,12 @@
/* strncmp() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* int strncmp(const char *s1, const char *s2, size_t n) */
/* Compare two strings. */
/* */
.text
.globl _strncmp
.balign 16
_strncmp:
movl 12(%esp), %ecx /* Maximum length */
jmp __strncmp /* Common code */
-13
View File
@@ -1,13 +0,0 @@
! strncmp() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! int strncmp(const char *s1, const char *s2, size_t n)
! Compare two strings.
!
.sect .text
.define _strncmp
.align 16
_strncmp:
mov ecx, 12(esp) ! Maximum length
jmp __strncmp ! Common code
+24
View File
@@ -0,0 +1,24 @@
/* strncpy() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* char *strncpy(char *s1, const char *s2, size_t n) */
/* Copy string s2 to s1. */
/* */
.text
.globl _strncpy
.balign 16
_strncpy:
push %ebp
movl %esp, %ebp
push %esi
push %edi
movl 16(%ebp), %ecx /* Maximum length */
call __strncpy /* Common code */
movl %edx, %ecx /* Number of bytes not copied */
rep stosb /* strncpy always copies n bytes by null padding */
movl 8(%ebp), %eax /* Return s1 */
pop %edi
pop %esi
pop %ebp
ret
-25
View File
@@ -1,25 +0,0 @@
! strncpy() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *strncpy(char *s1, const char *s2, size_t n)
! Copy string s2 to s1.
!
.sect .text
.define _strncpy
.align 16
_strncpy:
push ebp
mov ebp, esp
push esi
push edi
mov ecx, 16(ebp) ! Maximum length
call __strncpy ! Common code
mov ecx, edx ! Number of bytes not copied
rep
stosb ! strncpy always copies n bytes by null padding
mov eax, 8(ebp) ! Return s1
pop edi
pop esi
pop ebp
ret
+12
View File
@@ -0,0 +1,12 @@
/* strnlen() Author: Kees J. Bot */
/* 1 Jan 1994 */
/* size_t strnlen(const char *s, size_t n) */
/* Return the length of a string. */
/* */
.text
.globl _strnlen
.balign 16
_strnlen:
movl 8(%esp), %ecx /* Maximum length */
jmp __strnlen /* Common code */
-13
View File
@@ -1,13 +0,0 @@
! strnlen() Author: Kees J. Bot
! 1 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! size_t strnlen(const char *s, size_t n)
! Return the length of a string.
!
.sect .text
.define _strnlen
.align 16
_strnlen:
mov ecx, 8(esp) ! Maximum length
jmp __strnlen ! Common code
+36
View File
@@ -0,0 +1,36 @@
/* strrchr() Author: Kees J. Bot */
/* 2 Jan 1994 */
/* char *strrchr(const char *s, int c) */
/* Look for the last occurrence a character in a string. */
/* */
.text
.globl _strrchr
.balign 16
_strrchr:
push %ebp
movl %esp, %ebp
push %edi
movl 8(%ebp), %edi /* edi = string */
movl $-1, %ecx
xorb %al, %al
cld
repne scasb /* Look for the end of the string */
notl %ecx /* -1 - ecx = Length of the string + null */
decl %edi /* Put edi back on the zero byte */
movb 12(%ebp), %al /* The character to look for */
std /* Downwards search */
repne scasb
cld /* Direction bit back to default */
jne failure
leal 1(%edi), %eax /* Found it */
pop %edi
pop %ebp
ret
failure:
xorl %eax, %eax /* Not there */
pop %edi
pop %ebp
ret
-36
View File
@@ -1,36 +0,0 @@
! strrchr() Author: Kees J. Bot
! 2 Jan 1994
.sect .text; .sect .rom; .sect .data; .sect .bss
! char *strrchr(const char *s, int c)
! Look for the last occurrence a character in a string.
!
.sect .text
.define _strrchr
.align 16
_strrchr:
push ebp
mov ebp, esp
push edi
mov edi, 8(ebp) ! edi = string
mov ecx, -1
xorb al, al
cld
repne
scasb ! Look for the end of the string
not ecx ! -1 - ecx = Length of the string + null
dec edi ! Put edi back on the zero byte
movb al, 12(ebp) ! The character to look for
std ! Downwards search
repne
scasb
cld ! Direction bit back to default
jne failure
lea eax, 1(edi) ! Found it
pop edi
pop ebp
ret
failure:xor eax, eax ! Not there
pop edi
pop ebp
ret