Page 1 of 2
GCC and "struct"
Posted: Tue Dec 21, 2004 2:38 pm
by Googles
I have defined a structure:
Code: Select all
struct BIOSInfo
{
BYTE MajorVersion;
BYTE MinorVersion;
};
And when I'm trying to use it...
...GCC replies:
error: 'BIOSInfo' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: Parse error before "BIOS_Info_Pointer"
What is GCC trying to say?
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 2:44 pm
by Candy
Googles wrote:
I have defined a structure:
Code: Select all
struct BIOSInfo
{
BYTE MajorVersion;
BYTE MinorVersion;
};
And when I'm trying to use it...
...GCC replies:
error: 'BIOSInfo' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: Parse error before "BIOS_Info_Pointer"
What is GCC trying to say?
GCC is trying to tell you that it didn't see a declaration for "BIOSInfo" at that point, and that it's not going to say that again (unlike sun's javac...). It will not go further.
You declared "struct BIOSInfo". You did not declare "BIOSInfo".
Hint: that's why most people use
. The first X defines the name of the struct, the second one defines the name of the typedef. You can leave the first one out even, but that leaves you without "struct X"
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 4:23 pm
by Googles
Thanks candy, but now I have another problem insted
What I have is an asm function:
Code: Select all
Get_ASM_Params:
lea eax,[BIOS_Params]
ret
that I'm trying to call:
Code: Select all
extern BIOSInfo Get_ASM_Params(void);
BIOSInfo *BIOS_Info_Pointer = Get_ASM_Params();
But GCC complains about "incompatible types" any hints?
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 5:06 pm
by AR
The extern declaration of the function says it returns "BIOSInfo" but the variable is "BIOSInfo*", you need to change the function to return a pointer.
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 5:53 pm
by IRBMe
Not to sound nasty or anything, but it helps slightly when writing an OS if you at least know the basics of the language you are using
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 6:13 pm
by Googles
To be honest IRBMe, I have written an OS in x86 asm. All I'm doing now is porting it to C.
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 6:30 pm
by Googles
AR: thanks for the help but when I change:
Code: Select all
extern BIOSInfo Get_ASM_Params(void);
into
Code: Select all
extern BIOSInfo* Get_ASM_Params(void);
ld complains about not finding the reference to "_Get_ASM_Params". For some reason ld doesn't complain when I'm using "extern BIOSInfo Get_ASM_Params(void);"?
Any pointers?
Re:GCC and "struct"
Posted: Tue Dec 21, 2004 6:54 pm
by AR
Are you compiling to COFF? All variables and data that will be used in C need to prefixed with underscores for some reason if you are. You'll need to put an underscore on the start of the name in the Assembly.
BTW. Is there a reason you can't just use inline Assembly, or a direct pointer?
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 3:02 am
by Pype.Clicker
Googles wrote:
ld complains about not finding the reference to "_Get_ASM_Params". For some reason ld doesn't complain when I'm using "extern BIOSInfo Get_ASM_Params(void);"?
You may wish to use -fno-leading-underscore when compiling ... The COFF-base ABI try to prepend an underscore to every generated symbol to maintain compatibility with DOS/Windows linking environment.
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 6:16 am
by Googles
I solved by adding a underscore ;D
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 3:27 pm
by Googles
How can people say "asm is harder than C". Okey, in asm you have to take care of everything, but in C you have to use alot of cryptic operators to describe what you don't know (like memory!) ???
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 3:57 pm
by AR
The reasons that first come to mind are; ASM is harder to read later and lacks the ability to structure it easily and you have to write a lot of operators for something that you would think is simple, for example:
Code: Select all
unsigned int MyVar = (0xFF << 8) * 6 + 9;
Code: Select all
push eax
mov eax, 0xFF
shl eax, 8
mul eax, 6
add eax, 9
mov [MyVar], eax
pop eax
[SECTION .bss]
MyVar: resd 1
C has pretty much the same concepts as ASM though, like a pointer is an address which you can dereference, in Intel ASM you dereference with [ ] where in C it's *.
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 4:27 pm
by Candy
AR wrote:
The reasons that first come to mind are; ASM is harder to read later and lacks the ability to structure it easily and you have to write a lot of operators for something that you would think is simple, for example:
Code: Select all
unsigned int MyVar = (0xFF << 8) * 6 + 9;
Code: Select all
push eax
mov eax, 0xFF
shl eax, 8
mul eax, 6
add eax, 9
mov [MyVar], eax
pop eax
[SECTION .bss]
MyVar: resd 1
C has pretty much the same concepts as ASM though, like a pointer is an address which you can dereference, in Intel ASM you dereference with [ ] where in C it's *.
Code: Select all
unsigned int MyVar = (0xFF << 8) * 6 + 9;
Code: Select all
[SECTION .data]
MyVar dd 0FFh * 256 * 6 + 9
Don't make overly complex what can be easy.
However:
Code: Select all
n_wh:
mov eax, [obj]
cmp eax, 0
je end_wh
mov ebx, [eax+done_offs]
cmp ebx, 0
je end_wh
x;
jmp n_wh
end_wh:
In this case, the C code tells me that "while the object exists and its done is not true" X will be done. I can't read that from the asm version even though it does function the same. Also, if some of the symbolic constants aren't given clear names, they're less clear.
This example can use structs, but it can't be enhanced much.
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 4:38 pm
by AR
The idea was you replace the 0xFF with some unpredictable input, but your example is better
Re:GCC and "struct"
Posted: Wed Dec 22, 2004 5:33 pm
by Googles
with the help of "define" (insted of C's struc) things can get organized:
Code: Select all
struc C_Struc
{
int a;
int b;
int c;
};
Code: Select all
C_Struc_a equ 0
C_Struc_b equ 1
C_Struc_c equ 2
NASM doesn't add padding as long as you don't add align, NASM compiles what you have written, not more not less... IMHO ASM is cleaner.