Why is strlen from string in asm code?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Why is strlen from string in asm code?

 
0
  #1
Oct 29th, 2008
This makes me slightly curious...

Why is there assembly code for the string class of C++?

  1.  
  2. page ,132
  3. title strlen - return the length of a null-terminated string
  4. ;***
  5. ;strlen.asm - contains strlen() routine
  6. ;
  7. ; Copyright (c) Microsoft Corporation. All rights reserved.
  8. ;
  9. ;Purpose:
  10. ; strlen returns the length of a null-terminated string,
  11. ; not including the null byte itself.
  12. ;
  13. ;*******************************************************************************
  14.  
  15. .xlist
  16. include cruntime.inc
  17. .list
  18.  
  19. page
  20. ;***
  21. ;strlen - return the length of a null-terminated string
  22. ;
  23. ;Purpose:
  24. ; Finds the length in bytes of the given string, not including
  25. ; the final null character.
  26. ;
  27. ; Algorithm:
  28. ; int strlen (const char * str)
  29. ; {
  30. ; int length = 0;
  31. ;
  32. ; while( *str++ )
  33. ; ++length;
  34. ;
  35. ; return( length );
  36. ; }
  37. ;
  38. ;Entry:
  39. ; const char * str - string whose length is to be computed
  40. ;
  41. ;Exit:
  42. ; EAX = length of the string "str", exclusive of the final null byte
  43. ;
  44. ;Uses:
  45. ; EAX, ECX, EDX
  46. ;
  47. ;Exceptions:
  48. ;
  49. ;*******************************************************************************
  50.  
  51. CODESEG
  52.  
  53. public strlen
  54.  
  55. strlen proc \
  56. buf:ptr byte
  57.  
  58. OPTION PROLOGUE:NONE, EPILOGUE:NONE
  59.  
  60. .FPO ( 0, 1, 0, 0, 0, 0 )
  61.  
  62. string equ [esp + 4]
  63.  
  64. mov ecx,string ; ecx -> string
  65. test ecx,3 ; test if string is aligned on 32 bits
  66. je short main_loop
  67.  
  68. str_misaligned:
  69. ; simple byte loop until string is aligned
  70. mov al,byte ptr [ecx]
  71. add ecx,1
  72. test al,al
  73. je short byte_3
  74. test ecx,3
  75. jne short str_misaligned
  76.  
  77. add eax,dword ptr 0 ; 5 byte nop to align label below
  78.  
  79. align 16 ; should be redundant
  80.  
  81. main_loop:
  82. mov eax,dword ptr [ecx] ; read 4 bytes
  83. mov edx,7efefeffh
  84. add edx,eax
  85. xor eax,-1
  86. xor eax,edx
  87. add ecx,4
  88. test eax,81010100h
  89. je short main_loop
  90. ; found zero byte in the loop
  91. mov eax,[ecx - 4]
  92. test al,al ; is it byte 0
  93. je short byte_0
  94. test ah,ah ; is it byte 1
  95. je short byte_1
  96. test eax,00ff0000h ; is it byte 2
  97. je short byte_2
  98. test eax,0ff000000h ; is it byte 3
  99. je short byte_3
  100. jmp short main_loop ; taken if bits 24-30 are clear and bit
  101. ; 31 is set
  102.  
  103. byte_3:
  104. lea eax,[ecx - 1]
  105. mov ecx,string
  106. sub eax,ecx
  107. ret
  108. byte_2:
  109. lea eax,[ecx - 2]
  110. mov ecx,string
  111. sub eax,ecx
  112. ret
  113. byte_1:
  114. lea eax,[ecx - 3]
  115. mov ecx,string
  116. sub eax,ecx
  117. ret
  118. byte_0:
  119. lea eax,[ecx - 4]
  120. mov ecx,string
  121. sub eax,ecx
  122. ret
  123.  
  124. strlen endp
  125.  
  126. end
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,985
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 289
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Why is strlen from string in asm code?

 
0
  #2
Oct 29th, 2008
You could write a strlen function in any language but assembly is the fastest you can get, perhaps thats why.
The only problem is the way the fastest is implemented on a microprocessor. It makes it less portable. How can you be sure the listing is how the C++ compiler designers implemented it that way?
Last edited by ddanbe; Oct 29th, 2008 at 4:20 pm. Reason: Added a question.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why is strlen from string in asm code?

 
2
  #3
Oct 29th, 2008
>Why is there assembly code for the string class of C++?
On your implementation, you mean. The answer is probably optimization, especially if you read the code and understand what the algorithm is trying to achieve.

>How can you be sure the listing is how the C++ compiler designers implemented it that way?
I'd say that code is hand crafted, or it's tweaked assembly output. Either way, the implementation is extremely low level and clearly written with performance in mind.
Last edited by Narue; Oct 29th, 2008 at 4:29 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,414
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why is strlen from string in asm code?

 
0
  #4
Oct 29th, 2008
Originally Posted by Alex Edwards View Post
This makes me slightly curious...

Why is there assembly code for the string class of C++?
To me that doesn't look like the std::string that seems implied by "the string class of C++", but that's silly pedantry.

Why wouldn't library implementations be written in assembly? I would imagine that the folks providing a language implementation have a pretty solid understanding of the implementation they are providing (in general, QoI issues aside).

Originally Posted by ddanbe View Post
It makes it less portable.
Notsomuch, really. A language implementation must conform to the standard, thereby providing portability. The particulars of an implementation will most likely vary from one to the next. That is to say, on almost every platform the "under the hood" stuff will look different, but the functions will still do as specified in the standard.

Originally Posted by ddanbe View Post
How can you be sure the listing is how the C++ compiler designers implemented it that way?
I'm not quite sure what you are asking, but I'd say that it's either a compliant C++ compiler or it isn't. If it doesn't do what the standard states, it's broken.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,985
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 289
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Why is strlen from string in asm code?

 
0
  #5
Oct 29th, 2008
How can you be sure the listing is how the C++ compiler designers implemented it that way?
I'm not quite sure what you are asking, but I'd say that it's either a compliant C++ compiler or it isn't. If it doesn't do what the standard states, it's broken.
I was talking about how it is implemented. The outcome may be standard ISO whatever...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC