This article from Keil's support knowledge database shows that one can directly write to ACC from C51 code. http://www.keil.com/support/docs/1619.htm I would like to know if there are any rules when writing to ACC from C code. I have noticed that the following line in my code was entirely commented out when I chose Code Optimization Level-9.
unsigned char data uDataIndex, uDataArray[5]; void main() { . . ACC=uDataArray[uDataIndex%5]; . }
"Why would a compiler do this?" Why would a 'C' programmer want to do this? You would not be able to guarantee that your value would still be there for use by any other 'C' statement! In general, if you just write to a location and never read from that location, what's the point in writing to it in the 1st place?! This is precisely what the compiler thinks - so it optimises-out the apparently useless code. This is one of the commonest optimisations found in almost every optimising compiler. To prevent it, try the volatile keyword.
"can directly write to ACC from C51 code." Of course you can - ACC is just another SFR!
Why would a 'C' programmer want to do this? To get the parity bit. In fact, this is the only purpose for writing to ACC directly from 'C'. You would not be able to guarantee that your value would still be there for use by any other 'C' statement! Well said!
With ACC being just another SFR and SFR's being implicitly volatile (I thought they were, but now after a brief search didn't find that characteristic explicitly mentioned), shouldn't the compiler avoid optimizing out the access, regardless of optimization level?
I Write to ACC directly from C, and use Optimization Level-9 with no problem! I do this for same reason, parity check.
You will not have any problem if you check P right after. The mistake I made was I had another if statement placed before if (P == ...) {}. Then again, I think since the only legitimate reason to write to ACC directly is to get the parity bit, Keil perhaps should consider running a get_parity function in its intrinsic function library.
"a get_parity function" I wrote my parity() function in assembler precisely to avoid any possible doubt about what the compiler might get up to... ;-)
I use the following to get the parity after loading the accumulator.
// Disable Interrupts parity = (ACC=0x12,P); // Restore Interrupts