Page 1 of 1

GCC optimizing my code away

Posted: Tue Nov 11, 2008 11:35 am
by FlashBurn
I compile all my source files with the -O2 flag.

I want to switch on the A20 gate and have the following code:

Code: Select all

uint32t enableA20Gate() {
	struct x86Regs_t regs= { 0x2401, 0, 0, 0, 0, 0, 0, 0 };
	uint32t flags, tmp;
	uint16t *test1= (uint16t *)0x500, *test2= (uint16t *)0x100500;
	
	*test1= 0;
	*test2= 1;	

	if(*test1 != *test2)
		return 1;
	
	flags= callBIOS(0x15,&regs);
	
	if(flags & FLAG_CF) {
		tmp= inb(0x92);
		tmp|= 2;
		outb(0x92,tmp);
	}
	
	*test2= 1;
	
	if(*test1 != *test2)
		return 1;

	waitKbd();
	outb(0x64,0xad);
	
	waitKbd();
	outb(0x64,0xd0);
	
	waitKbdData();
	tmp= inb(0x60);
	
	waitKbd();
	outb(0x64,0xd1);
	
	waitKbd();
	outb(0x60,tmp | 2);
	
	waitKbd();
	outb(0x64,0xae);
	
	waitKbd();
	
	*test2= 1;
	
	if(*test1 != *test2)
		return 1;
	
	return 0;
}
I use gcc 4.3.1 for compiling and if I compile this code gcc only makes assembly code so that I write 0 at 0x500 and 1 at 0x100500 and because for it the if statement is always true it doesn´t compile the rest of the function. How can I say to gcc (w/o creating an extra entry in my makefile for this source file) that it has not to optimize this code away?

Re: GCC optimizing my code away

Posted: Tue Nov 11, 2008 1:01 pm
by Combuster
have you told GCC that you need the operation when you do inline assembly? (hint: try __asm__ __volatile__)

Re: GCC optimizing my code away

Posted: Tue Nov 11, 2008 2:02 pm
by FlashBurn
Yes, I use it and I also tried the "#pragma optimize("",off)", but gcc ignores it. I think the best and easiest way to solve it, is to write the code in assembly or is here anyone who has another solution I can try?

Re: GCC optimizing my code away

Posted: Tue Nov 11, 2008 2:23 pm
by ru2aqare
Try declaring the variables as volatile, that hints the compiler that for example their value may change anytime.

Re: GCC optimizing my code away

Posted: Tue Nov 11, 2008 2:48 pm
by FlashBurn
Thanks, that did it. Now it compiles to the code I need!