strcmp() and drivers

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

strcmp() and drivers

Post by AUsername »

Ok, about 3 weeks ago me and my partner ran into this problem with our GetProcAddress() function. It works perfectly in our third stage bootloader but in the HAL driver it fails. It took us awhile (and I got busy with other things so I had to stop for about a week and a half) and I found that strcmp() fails in the HAL.

This is the test I ran in stage3:

Code: Select all

if (strcmp("test","tes")){
	_asm{
		mov		eax, 0x1337
		cli
		hlt
	}
}

This works correctly it sets eax to 0x1337 then halts the system.
Now I commented this out in stage three went to the HAL and recompiled and ran again and the HAL halts with eax=0x2 and ebx=0x1 (error code from GetProcAddress)

I have no idea why at first I thought it was the strcmp() function itself that was failing so I replaced it with a strcmp() that has been proven to work in an older OS of mine and it still fails.

I'm completely stumped the only thing that would make sense is the address space or that it's a DLL instead of an EXE.

strcmp():

Code: Select all

int strcmp(const char* str1, const char* str2){
	int res=0;
	while(!(res=*(unsigned char*)str1-*(unsigned char*)str2) && *str2){
		++str1;
		++str2;
	}
	
	if (res<0) res=-1;
	if (res>0) res=1;

	return res;
}
Any ideas? :S
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Re: strcmp() and drivers

Post by hailstorm »

Maybe, just maybe, it might be the order of evaluation. I am just trying to think outside the box here.

Normally, boolean operator precedence is from left to right. So the subtraction is executed first, and
second the check if *str2 is zero. Should (in this case) the first evaluation be false, then the second statement
isn't executed. Maybe, in this case, it is the other way around. Maybe *str2 is evaluated first and at the fourth
character it is evaluated false. This means the other statement isn't executed at all and res is still zero.

This, however, would be a very odd situation, but again, I am just trying to think outside the box...

Hope this helps anyway.

Greetz.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: strcmp() and drivers

Post by ru2aqare »

hailstorm wrote:Maybe, in this case, it is the other way around. Maybe *str2 is evaluated first and at the fourth
character it is evaluated false. This means the other statement isn't executed at all and res is still zero.
In the C (and C++) language logical expressions are always evaluated from left to right, and short-circuit evaluation rules are applied (meaning that at any point where the result of the complete expression can be determined, evaluation of the rest of the expression is/may be skipped) - contrary to Pascal, where the evaluation order is not specified (although Borland Pascal compilers did use LTR evaluation). I somewhat doubt that this is the cause of the anomaly.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: strcmp() and drivers

Post by geppyfx »

The thread belongs in "General Programming" or better yet, some outside C/C++ forum where you can get considerably better explanation. Once you determined that C is not your problem you are welcome to post. This includes no posting of strcmp code.
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

Re: strcmp() and drivers

Post by AUsername »

geppyfx wrote:The thread belongs in "General Programming" or better yet, some outside C/C++ forum where you can get considerably better explanation. Once you determined that C is not your problem you are welcome to post. This includes no posting of strcmp code.
I don't think C is my problem. I know the problem is with pointer.

Some more tests showed that it returns properly but pointers seem to be messing up. I'm assuming this is because of the changed address space. Putting the strcmp() function inside of the file that calls it (renaming it strcmpp()) also fails. This leads me to believe the pointers are at fault, as for why, I'm not sure.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: strcmp() and drivers

Post by frank »

Whats the problem with GetProcAddress? You just talk about it with no real substance. Also ask yourself the question, Whats different between my third stage bootloader and my hal? Then start debugging.
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

Re: strcmp() and drivers

Post by AUsername »

Yeah sorry about not really explaining GetProcAddress().
It fails because it uses strcmp(). I need it so that drivers can call drivers. (For testing I want the HAL to call the VGA driver)

Now the only difference between stage3 and the HAL is its place in memory which further confirms my suspicion of pointers failing. I still don't see how they could be unless they're pointing to some random place in memory.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: strcmp() and drivers

Post by geppyfx »

AUsername wrote:I'm assuming this is because of the changed address space.
But you haven't posted anything regarding than. This is still c/c++ problems as far as posted code concerned.

Now if you about to post your entire ring0/ring3 switch or task switch inside kernel:
keep in mind that it has to be very small, very well commented, and most importantly illustrate only one single problem where you think there is problem. We must not make assumptions what what you constans mean or state of registers before your code starts.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: strcmp() and drivers

Post by frank »

Do you have paging enabled? Do you have a working printf so that you can output the address of the pointers? Are you encountering a processor fault or is the error routine for GetProcAddress just halting and printing the state?

Try using a simpler strcmp. Also maybe you should look at the asm code of the strcmp function to see if the compiler is messing it up.
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

Re: strcmp() and drivers

Post by AUsername »

frank wrote:Do you have paging enabled?
No.
Do you have a working printf so that you can output the address of the pointers?
Yes, but I need GetProcAddress to work in order to call it from the TVGA driver, and GetProcAddress used strcmp().
Are you encountering a processor fault or is the error routine for GetProcAddress just halting and printing the state?
It halts printing its status. If I remove that it GPFs.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: strcmp() and drivers

Post by Solar »

As for the strcmp() part, this one's tested OK. Doesn't help you with your address space issues, but takes away the doubt that it might be the strcmp() implementation.

From PDCLib, more verbose than need be I know:

Code: Select all

int strcmp( const char * s1, const char * s2 )
{
    while ( ( *s1 ) && ( *s1 == *s2 ) )
    {
        ++s1;
        ++s2;
    }
    return ( *s1 - *s2 );
}
Every good solution is obvious once you've found it.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: strcmp() and drivers

Post by Velko »

Solar wrote:As for the strcmp() part, this one's tested OK.
You may want to add && ( *s2 ). Result is undefined if s1 is longer than s2 otherwise.
If something looks overcomplicated, most likely it is.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: strcmp() and drivers

Post by Solar »

Are you sure? 8)

^^ trick question...
Every good solution is obvious once you've found it.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: strcmp() and drivers

Post by Velko »

Oops... nevermind :oops:

It's just not obvious at a first glance.
If something looks overcomplicated, most likely it is.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: strcmp() and drivers

Post by Solar »

No problem. Actually you had me sweating for a second there. 8)
Every good solution is obvious once you've found it.
Post Reply