GCC and "struct"

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.
Googles

GCC and "struct"

Post by Googles »

I have defined a structure:

Code: Select all

struct BIOSInfo
{
BYTE MajorVersion;
BYTE MinorVersion;
};
And when I'm trying to use it...

Code: Select all

BIOSInfo *BIOS_Info_Pointer = 0;
...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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:GCC and "struct"

Post 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...

Code: Select all

BIOSInfo *BIOS_Info_Pointer = 0;
...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

Code: Select all

typedef struct X {
...
} X;
. 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"
Googles

Re:GCC and "struct"

Post 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? :D
AR

Re:GCC and "struct"

Post 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.
IRBMe

Re:GCC and "struct"

Post 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 ;)
Googles

Re:GCC and "struct"

Post by Googles »

To be honest IRBMe, I have written an OS in x86 asm. All I'm doing now is porting it to C. ;)
Googles

Re:GCC and "struct"

Post 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?
AR

Re:GCC and "struct"

Post 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?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC and "struct"

Post 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.
Googles

Re:GCC and "struct"

Post by Googles »

I solved by adding a underscore ;D
Googles

Re:GCC and "struct"

Post 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!) ???
AR

Re:GCC and "struct"

Post 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 *.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:GCC and "struct"

Post 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

while(obj && !obj->done) {
    x
}

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.
AR

Re:GCC and "struct"

Post by AR »

The idea was you replace the 0xFF with some unpredictable input, but your example is better
Googles

Re:GCC and "struct"

Post 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.
Post Reply