| | |
Why is strlen from string in asm code?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
This makes me slightly curious...
Why is there assembly code for the string class of C++?
Why is there assembly code for the string class of C++?
asm Syntax (Toggle Plain Text)
page ,132 title strlen - return the length of a null-terminated string ;*** ;strlen.asm - contains strlen() routine ; ; Copyright (c) Microsoft Corporation. All rights reserved. ; ;Purpose: ; strlen returns the length of a null-terminated string, ; not including the null byte itself. ; ;******************************************************************************* .xlist include cruntime.inc .list page ;*** ;strlen - return the length of a null-terminated string ; ;Purpose: ; Finds the length in bytes of the given string, not including ; the final null character. ; ; Algorithm: ; int strlen (const char * str) ; { ; int length = 0; ; ; while( *str++ ) ; ++length; ; ; return( length ); ; } ; ;Entry: ; const char * str - string whose length is to be computed ; ;Exit: ; EAX = length of the string "str", exclusive of the final null byte ; ;Uses: ; EAX, ECX, EDX ; ;Exceptions: ; ;******************************************************************************* CODESEG public strlen strlen proc \ buf:ptr byte OPTION PROLOGUE:NONE, EPILOGUE:NONE .FPO ( 0, 1, 0, 0, 0, 0 ) string equ [esp + 4] mov ecx,string ; ecx -> string test ecx,3 ; test if string is aligned on 32 bits je short main_loop str_misaligned: ; simple byte loop until string is aligned mov al,byte ptr [ecx] add ecx,1 test al,al je short byte_3 test ecx,3 jne short str_misaligned add eax,dword ptr 0 ; 5 byte nop to align label below align 16 ; should be redundant main_loop: mov eax,dword ptr [ecx] ; read 4 bytes mov edx,7efefeffh add edx,eax xor eax,-1 xor eax,edx add ecx,4 test eax,81010100h je short main_loop ; found zero byte in the loop mov eax,[ecx - 4] test al,al ; is it byte 0 je short byte_0 test ah,ah ; is it byte 1 je short byte_1 test eax,00ff0000h ; is it byte 2 je short byte_2 test eax,0ff000000h ; is it byte 3 je short byte_3 jmp short main_loop ; taken if bits 24-30 are clear and bit ; 31 is set byte_3: lea eax,[ecx - 1] mov ecx,string sub eax,ecx ret byte_2: lea eax,[ecx - 2] mov ecx,string sub eax,ecx ret byte_1: lea eax,[ecx - 3] mov ecx,string sub eax,ecx ret byte_0: lea eax,[ecx - 4] mov ecx,string sub eax,ecx ret strlen endp end
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?
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
Make love, no war. Cave ab homine unius libri.
Danny
>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.
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.
•
•
•
•
This makes me slightly curious...
Why is there assembly code for the string class of C++?
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).
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.
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
•
•
•
•
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Delphi CustomCheckListBox (Pascal and Delphi)
- Compile errors [help] [ log ] (C++)
Other Threads in the C++ Forum
- Previous Thread: spot the error?
- Next Thread: Search array and edit
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






