Jump to content

Find the first occurrence


Recommended Posts

JohnOne, we (as people knowing about the rule) have unwritten rule that anyone saying something like that must, and this a must must, tell a joke aferward or one angel falls. In the meantime it's said that one whale dies too, or two, I'm not sure.

Save the whales JohnOne. Save the poor whales and the angel, I beg you.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

8B5508 mov edx, dword[ebp+08] ; first parameter is copied to edx ; sString

"first parameter is copied to edx."

If sString is passed as a reference then wouldn't that line go away?

Edited by LaCastiglione
Link to comment
Share on other sites

  • 5 weeks later...

Hi,

maybe this will shrink the code at 3 bytes ...

INT WINAPI ChrInStr (LPCWSTR szString, WCHAR chFind)
{
    if (szString)
    {
        for(INT i = 0; szString[i]; i++) 
        {
           if (szString[i] == chFind)
               return i ;
        }
    }
    return 0 ;
}

Regards

Greenhorn

Edited by Greenhorn
Link to comment
Share on other sites

Hi,

maybe this will shrink the code at 3 bytes ...

INT WINAPI ChrInStr (LPCWSTR szString, WCHAR chFind)
{
    if (szString)
    {
        for(INT i = 0; szString[i]; i++) 
        {
           if (szString[i] == chFind)
               return i ;
        }
    }
    return 0 ;
}

Regards

Greenhorn

I don't see how. What's the logic?

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

You save at least two bytes: "xor eax, eax" at "if (szStrig == 0) return 0 ;".

If you take a look at the generated listing file or the disassembly in the debugger, you'll see. :mellow:

Regards Greenhorn

Could you show me?

I'm kinda noobish.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I'm also rather skeptical that will produce a smaller size. I sincerely hope the compiler is smart enough to realize the following two blocks of code are functionally equivalent and produce the same (optimized) disassembly:

if (!value) 
    return 0;
// code
return 0;
And:

if (value)
    // code
return 0;
Link to comment
Share on other sites

I think I got it. (lol, probably not)

If the function parameters are pushed on the stack then is it necessary to mov the arguments into variables?

In other words is this necessary:

mov edx, dword[ebp+08] ; first parameter is copied to edx ; sString?

Edited by LaCastiglione
Link to comment
Share on other sites

I'm also rather skeptical that will produce a smaller size. I sincerely hope the compiler is smart enough to realize the following two blocks of code are functionally equivalent and produce the same (optimized) disassembly:

if (!value) 
    return 0;
// code
return 0;
And:

if (value)
    // code
return 0;
Yes, I think with the optimization switches on, the compiler should do it in this way ...

I've just tested the function in 64 bit Debug configuration and I had a lot more bytes than 39.

EDIT:

Your right Valik, I've tested it in Release configuration and the generated listing file shows the same ...

Here is trancexx's function...

?ChrInStr@@YAHPEB_W_W@Z PROC                ; ChrInStr, COMDAT

; 225  :     if (sString == NULL) return 0;
; 226  : 
; 227  :     for(INT i = 0; sString[i]; ++i) 

  00000 0f b7 05 00 00
    00 00        movzx   eax, WORD PTR ?szTitle@@3PA_WA
  00007 33 c9        xor     ecx, ecx
  00009 8b d1        mov     edx, ecx
  0000b 66 85 c0     test    ax, ax
  0000e 74 1d        je  SHORT $LN2@ChrInStr
  00010 4c 8d 05 00 00
    00 00        lea     r8, OFFSET FLAT:?szTitle@@3PA_WA ; szTitle
$LL4@ChrInStr:

; 228  :     {
; 229  :         if (sString[i] == sFind) return i;

  00017 66 83 f8 69  cmp     ax, 105            ; 00000069H
  0001b 74 13        je  SHORT $LN9@ChrInStr

; 225  :     if (sString == NULL) return 0;
; 226  : 
; 227  :     for(INT i = 0; sString[i]; ++i) 

  0001d 41 0f b7 44 50
    02       movzx   eax, WORD PTR [r8+rdx*2+2]
  00023 48 ff c2     inc     rdx
  00026 ff c1        inc     ecx
  00028 66 85 c0     test    ax, ax
  0002b 75 ea        jne     SHORT $LL4@ChrInStr
$LN2@ChrInStr:

; 230  :     }
; 231  : 
; 232  :     return 0;

  0002d 33 c0        xor     eax, eax

; 233  : }

  0002f c3       ret     0
$LN9@ChrInStr:

; 228  :     {
; 229  :         if (sString[i] == sFind) return i;

  00030 8b c1        mov     eax, ecx

; 233  : }

  00032 c3       ret     0
?ChrInStr@@YAHPEB_W_W@Z ENDP                ; ChrInStr

...and here is mine...

?ChrInStr_@@YAHPEB_W_W@Z PROC               ; ChrInStr_, COMDAT

; 237  :     if (szString)
; 238  :     {
; 239  :         for(INT i = 0; szString[i]; i++) 

  00000 0f b7 05 00 00
    00 00        movzx   eax, WORD PTR ?szTitle@@3PA_WA
  00007 33 c9        xor     ecx, ecx
  00009 8b d1        mov     edx, ecx
  0000b 66 85 c0     test    ax, ax
  0000e 74 1d        je  SHORT $LN2@ChrInStr_
  00010 4c 8d 05 00 00
    00 00        lea     r8, OFFSET FLAT:?szTitle@@3PA_WA ; szTitle
$LL4@ChrInStr_:

; 240  :         {
; 241  :            if (szString[i] == chFind)

  00017 66 83 f8 6e  cmp     ax, 110            ; 0000006eH
  0001b 74 13        je  SHORT $LN9@ChrInStr_

; 237  :     if (szString)
; 238  :     {
; 239  :         for(INT i = 0; szString[i]; i++) 

  0001d 41 0f b7 44 50
    02       movzx   eax, WORD PTR [r8+rdx*2+2]
  00023 48 ff c2     inc     rdx
  00026 ff c1        inc     ecx
  00028 66 85 c0     test    ax, ax
  0002b 75 ea        jne     SHORT $LL4@ChrInStr_
$LN2@ChrInStr_:

; 243  :         }
; 244  :     }
; 245  :     return 0 ;

  0002d 33 c0        xor     eax, eax

; 246  : }

  0002f c3       ret     0
$LN9@ChrInStr_:

; 242  :                return i ;

  00030 8b c1        mov     eax, ecx

; 246  : }

  00032 c3       ret     0
?ChrInStr_@@YAHPEB_W_W@Z ENDP               ; ChrInStr_

If you want to get it smaller in size (and maybe a few clocks faster), then you should write your own proc in assembly, as Valik already pointed to ...

Regards

Greenhorn

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...