substring64.asm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. %include "defs.asm"
  2. ;************************* substring64.asm **********************************
  3. ; Author: Agner Fog
  4. ; Date created: 2011-07-18
  5. ; Last modified: 2011-07-18
  6. ; Source URL: www.agner.org/optimize
  7. ; Project: asmlib.zip
  8. ; Description:
  9. ; Makes a substring of a zero-terminated ASCII string
  10. ;
  11. ; C++ prototype:
  12. ; extern "C"
  13. ; size_t A_substring(char * dest, const char * source, size_t pos, size_t len);
  14. ; Makes a substring from source, starting at position pos (zero-based) and length
  15. ; len and stores it in the array dest. It is the responsibility of the programmer
  16. ; that the size of the dest array is at least len + 1.
  17. ; The return value is the actual length of the substring. This may be less than
  18. ; len if the length of source is less than pos + len.
  19. ;
  20. ; Copyright (c) 2011 GNU General Public License www.gnu.org/licenses/gpl.html
  21. ;******************************************************************************
  22. global A_substring: function ; Function _A_substring
  23. extern A_strlen
  24. extern A_memcpy
  25. SECTION .text
  26. ; extern "C"
  27. ; size_t A_substring(char * dest, const char * source, size_t pos, size_t len);
  28. %ifdef WINDOWS
  29. %define par1 rcx ; dest
  30. %define par2 rdx ; source
  31. %define par3 r8 ; pos
  32. %define par4 r9 ; len
  33. %else ; UNIX
  34. %define par1 rdi
  35. %define par2 rsi
  36. %define par3 rdx
  37. %define par4 rcx
  38. %endif
  39. A_substring:
  40. push par1
  41. push par2
  42. push par3
  43. push par4
  44. mov par1, par2
  45. call A_strlen ; rax = strlen(source)
  46. pop par4
  47. pop par3
  48. pop par2
  49. pop par1
  50. sub rax, par3 ; max length = strlen(source) - pos
  51. jbe empty ; strlen(source) <= pos. Return empty string
  52. cmp rax, par4
  53. cmova rax, par4 ; min(len, maxlen)
  54. add par2, par3 ; source + pos = source for memcpy
  55. mov par3, rax ; length for memcpy
  56. push rax ; new length
  57. call A_memcpy
  58. pop rcx ; new length = return value, rax = dest
  59. mov byte [rcx+rax], 0 ; terminating zero
  60. mov rax, rcx ; return new length
  61. ret
  62. empty: ; return empty string
  63. xor eax, eax ; return 0
  64. mov byte [par1], al
  65. ret
  66. ;A_substring END