www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/31/23:58:18

Message-ID: <32F2CAE9.DF5@rangenet.com>
Date: Fri, 31 Jan 1997 22:47:37 -0600
From: Dan Hedlund <markiv AT rangenet DOT com>
Reply-To: markiv AT rangenet DOT com
Organization: Range Net
MIME-Version: 1.0
To: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
CC: djgpp AT delorie DOT com
Subject: Re: Possible bug/misfeature in GCC
References: <32F1A438 DOT 5509 AT rangenet DOT com> <w6dv5EAwtk8yEw1j AT talula DOT demon DOT co DOT uk>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019