memmove.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* $OpenBSD: memmove.S,v 1.7 2014/11/29 18:51:23 tedu Exp $ */
  2. /*-
  3. * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
  4. * Copyright (c) 1990 The Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * William Jolitz.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <machine/param.h>
  35. #include <machine/asm.h>
  36. /*
  37. * Emulate bcopy() by swapping the first two arguments, and jumping
  38. * into memmove(), which handles overlapping regions.
  39. */
  40. ENTRY(bcopy)
  41. pushl %esi
  42. pushl %edi
  43. movl 12(%esp),%esi
  44. movl 16(%esp),%edi
  45. jmp docopy
  46. /*
  47. * memmove(caddr_t dst, caddr_t src, size_t len);
  48. * Copy len bytes, coping with overlapping space.
  49. */
  50. ENTRY(memmove)
  51. pushl %esi
  52. pushl %edi
  53. movl 12(%esp),%edi
  54. movl 16(%esp),%esi
  55. docopy:
  56. movl 20(%esp),%ecx
  57. movl %edi,%eax
  58. subl %esi,%eax
  59. cmpl %ecx,%eax # overlapping?
  60. jb 1f
  61. jmp docopyf # nope
  62. /*
  63. * memcpy() doesn't worry about overlap and always copies forward
  64. */
  65. ENTRY(memcpy)
  66. pushl %esi
  67. pushl %edi
  68. movl 12(%esp),%edi
  69. movl 16(%esp),%esi
  70. movl 20(%esp),%ecx
  71. docopyf:
  72. movl %edi,%eax # setup return value for memcpy/memmove
  73. shrl $2,%ecx # copy by 32-bit words
  74. rep
  75. movsl
  76. movl 20(%esp),%ecx
  77. andl $3,%ecx # any bytes left?
  78. rep
  79. movsb
  80. popl %edi
  81. popl %esi
  82. ret
  83. _ALIGN_TEXT
  84. 1: movl %edi,%eax # setup return value for memmove
  85. addl %ecx,%edi # copy backward
  86. addl %ecx,%esi
  87. std
  88. andl $3,%ecx # any fractional bytes?
  89. decl %edi
  90. decl %esi
  91. rep
  92. movsb
  93. movl 20(%esp),%ecx # copy remainder by 32-bit words
  94. shrl $2,%ecx
  95. subl $3,%esi
  96. subl $3,%edi
  97. rep
  98. movsl
  99. popl %edi
  100. popl %esi
  101. cld
  102. ret