Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer does not move GCC

Status
Not open for further replies.

sseitz

Programmer
Jun 15, 2003
20
0
0
DE
Hi!

A few people and me are currently working on a very small i86-based OS just to improve our skills.
It's coded from scratch without any use of the standard c-libs. (It also lacks of a memory management...)
By now, i've got a very weired problem:
Code:
char dummy[255]="";
char something[255]="";
int result;

...

int own_strcmp(char *s1, char *s2)
{
while (*s1 && *s2)
  {
  if (*s1++ != *s2++) { return 0; break; }
  }
return 1;
}

...

result = own_strcmp("abc","abc");
does not work. I monitored the function (which has been re-written in 20 different ways) with following result:

a vs a MATCH
b vs a -
c vs a -

The pointer of the 2nd. argument does not move. It only worked with a *(s2+i)...
I don't know why.
Does anyone have a suggestion?

Thanks in advance (and sorry for my english)

sseitz
 
Oh, i forgot: it doesn't matter if the function is called with ("static","arguments") or with
char something[255]="abc";
char anything[255]="abc"; ... (something,anything)

the problem is always the same.
 
Seems OK here

Have you looked at the assembler that produces
Code:
gcc -S prog.c
produces prog.s containing the generated asm

Also, which version of gcc
Code:
$ gcc --version
gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
seems to produce the correct answer here

One small point - the break following return is not necessary
 
From MSDN :
The operands of logical-AND and logical-OR expressions are evaluated from left to right. If the value of the first operand is sufficient to determine the result of the operation, the second operand is not evaluated.

Everything works fine - except for your algorithm.

/JOlesen
 
There is something bizzare then, I tried your code - more or less on my linux system using gcc version 3.2.2

The code I entered is as follows - the coding is VERY rough so no comments on style as I was in a hurry.

int own_cmp(char * a, char * b);
void main(void)
{
int rc;
char x[255]="abc";
char y[255]="abc";
rc=own_cmp(x,y);
printf("RC from function=%d\n",rc);
return;
}
int own_cmp(char * a,char * b)
{
printf("On entry a = %X (%c), b = %X (%c)\n",a,*a,b,*b);
while(*a && *b)
{
printf("Before if check - a = %X (%c), b = %X (%c)\n",a,*a,b,*b);
if(*a++ != *b++)
return 0;
printf("After if check - a = %X (%c), b = %X (%c)\n",a,*a,b,*b);
}
return 1;
}

The output from a test run was as follows and seems to show it worked.

[e66875@playpen testcmp]$ ./a.out
On entry a = BFFFED00 (a), b = BFFFEC00 (a)
Before if check - a = BFFFED00 (a), b = BFFFEC00 (a)
After if check - a = BFFFED01 (b), b = BFFFEC01 (b)
Before if check - a = BFFFED01 (b), b = BFFFEC01 (b)
After if check - a = BFFFED02 (c), b = BFFFEC02 (c)
Before if check - a = BFFFED02 (c), b = BFFFEC02 (c)
After if check - a = BFFFED03 (), b = BFFFEC03 ()
RC from function=1


So it seems to me it works. Comments ?

Cheers - Gavin
 
> So it seems to me it works. Comments ?
Same results here
 
Perhaps as a start sseitz could copy my code and compile it using his gcc then run it and post the results here....

Cheers - Gavin
 
thanks for your answers. i'll try this these days. by now, i'm under heavy pressure :(

but, as said, this is used in an very own environment, so the problem might be forced by an underlaying structure.
i will report my results with your code.

btw. shame on me, i didn't run the code under my std-env.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top