But how can I access these 4 chars within 1 run? I mean I like to compare these 4 chars with an constant string and in assembly I´m comparing the register where I loaded the first 4 bytes with this constant, but how to do that in C?
Edit::
I use a pointer of the structure which points to the memory where the structure is.
Check out the "union" data type. What you are looking for is a union of char[4] with int (or rather, uint32_t - remember that "int" doesn't have to mean 4 bytes on all platforms).
Every good solution is obvious once you've found it.
GCC wont optimize the usual comparisons into one 32bit comparison, but I took your first solution and gcc is compiling it right, but I still have to test if it works.
@Solar
I took a look at unions, but I don´t see how they can help here!?
@all
I could also use a "strncmp", but this would be overhead for only searching a 4byte String.
Edit::
The Solution of Korona doesn´t work, it only works if I write 4 normal comparisons
I use the "fno-builtin" option for gcc, so he has to call my strncmp function, but if I don´t use it, he inserts a "rep cmpsb" opcode. Why is he so dump to use the byte compare and not the dword compare and why doesn´t he recognize that I only want to compare 4bytes and makes a compare with a constant?
Another question is, can I use gcc w/o "fno-builtin" when I write my kernel? I mean he is only optimizing the functions away, isn´t he?!
Last edited by FlashBurn on Fri May 02, 2008 8:09 am, edited 1 time in total.
Thanks, the problem was the byte swapping I forgot Now it works.
Edit::
I dislike the union solution. I switched over to C because I wanted to write my code faster and I wanted to write code which is easier to read (before I´ve written my code in assembly).
Also, a decent compiler should create machine code in such a way that it will optimally fit in with the function and the code surrounding it. You're also pointlessly overriding the compiler (which is also most likely better at assembly than a majority of so-called 'assembly programmers').
And I don't see your problem with unions. They conserve memory as well as enabling the compiler to optimize memory accesses.
Note that a union does not have to be located inside a struct, and that in 'strict' C that it cannot be anonymous.
Good luck
"Sufficiently advanced stupidity is indistinguishable from malice."