Author Topic: find$() returns -1 if strings are identical  (Read 3595 times)

jj2007

  • Member
  • *****
  • Posts: 10548
  • Assembler is fun ;-)
    • MasmBasic
find$() returns -1 if strings are identical
« on: December 12, 2013, 04:27:32 AM »
Not sure if this is intentional, but hlhelp.chm doesn't document this behaviour:
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
mov esi, chr$("Masm32:")
print "esi=", 9, 9, 9, 9
print esi, 13, 10
print "find$(1, esi, 'Masm32:')=", 9
push find$(1, esi, "Masm32:")
print str$(eax), 13, 10
pop eax
exit eax
end start

Output:
esi=                            Masm32:
find$(1, esi, 'Masm32:')=       -1


MasmBasic Instr_(esi, "Masm32:") would return 1, and that seems to be a standard BASIC behaviour; but C/C++ might be different, of course.

I also had a weird problem with exit eax, being translated to invoke ExitProcess, 0 (with plain Masm32rt.inc), but I can't reproduce it now that I am on the other puter. Will check tomorrow if it's the same macros.asm on both machines 8)

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: find$() returns -1 if strings are identical
« Reply #1 on: December 12, 2013, 05:53:39 AM »
The documentation for InString, called by find$, is in masmlib.chm under Zero Terminated String Functions.
Well Microsoft, here’s another nice mess you’ve gotten us into.

jj2007

  • Member
  • *****
  • Posts: 10548
  • Assembler is fun ;-)
    • MasmBasic
Re: find$() returns -1 if strings are identical
« Reply #2 on: December 12, 2013, 06:47:45 AM »
Thanks, Michael.

Quote
Error Values

If the function fails, the following error values apply.
-1 = substring same length or longer than main string

There is an overview for Find$ at Wikipedia, but no info about the special case of identical strings.

GfaBasic and MasmBasic return 1. There is apparently no Instr() in C, but this workaround returns 0:
  char* s1="abcd";
  char* s2="abcd";
  char* result=strstr(s1, s2);
  int res=result-s1;
  printf("Instr identical=%i\n", res);

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: find$() returns -1 if strings are identical
« Reply #3 on: December 12, 2013, 02:52:06 PM »
In addition to strstr I also tested FreeBASIC, QuickBASIC, and PB3 and they all returned 1, but given the problem with breaking existing code I doubt that this behavior is going to change.
Well Microsoft, here’s another nice mess you’ve gotten us into.