Mail Archives: djgpp/1997/01/31/17:50:44
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?
/*
* Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
* Ghoti: 'gh' as in 'enough', 'o' as in 'women', and 'ti' as in 'nation'.
*/
- Raw text -