Mail Archives: djgpp/1997/01/31/23:58:18
Shawn Hargreaves wrote:
>
> Dan Hedlund writes:
> >The following lines compile wrong with the -O3 option.
> >
> >unsigned char inportb (int port)
> >{
> > unsigned char c;
> > asm ("inb %%dx, %%al"
> > : "=a" (c)
> > : "d" (port));
> > return c;
> >}
> [...]
> >GCC reads a byte from port 0 only once, and then compars that byte to 0
> >in an endless loop. Apparently GCC thinks the byte retrieved from the
> >port doesn't change.
>
> This is correct behaviour on the part of gcc. In most cases that is a
> valid optimisation, since you've told it what registers you change in
> your asm routine, and it has no way of knowing that you are waiting for
> an external hardware event. To make the optimiser leave your function
> alone, you need to add the 'volatile' keyword. Look in
> djgpp\include\inlines\pc.h for an example. In fact, why don't you just
> use the inportb() from that header?
I'm writing an OS, like every one else..., and it also happens when
accessing cr0 and cr3. Thanks for refrenceing pc.h, I was putting the
'volatile' in the function definition.
--
***** *** ** ** Dan M. Hedlund
** ** ***** *** ** <markiv AT rangenet DOT com>
** ** ** ** **** ** http://www.rangenet.com/markiv
** ** ** ** ** ** **
** ** ******* ** ****
** ** ** ** ** ***
** ** ** ** ** **
***** ** ** ** **
- Raw text -