Bitwise shifts on assignments to ports shouldn't be applied to port registers, as they may not be able to set/read some of they bits.
Example;
On pic16f877, PORTE register has only 3 bits (RE3:RE0), the rest (RE7:RE4) are non-writable, readed as 0.
On attached exaple program, compiled this way:
sdcc -mpic14 -p16f877a -V --debug -c shifted.c
The C code:
unsigned char tmp;
while(1){
tmp = PORTA;
PORTE = tmp >> 6;
}
Gets compiled to .asm code:
SWAPF r0x1001,W
ANDLW 0x0f
BANKSEL _PORTE
MOVWF _PORTE
BCF STATUS,0
RRF _PORTE,F
BCF STATUS,0
RRF _PORTE,F
Wich gets a wrong value in PORTE, for example:
if tmp = 0xff; tmp>>6 shoud be 0x03. but with that code:
SWAPF r0x1001,W ; W = 0xff
ANDLW 0x0f; W = 0x0f
BANKSEL _PORTE
MOVWF _PORTE ; PORTE = 0x07!! as RE7:RE3 are non-writable!!
BCF STATUS,0
RRF _PORTE,F ; PORTE = 0x03
BCF STATUS,0
RRF _PORTE,F; PORTE = 0x01!! should be 0x03!!
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
Example program with various approaches getting the same problem
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
I tried this example with the freshest version of sdcc and I get almost the same result. This file has three version about bit shifting. The first is compiled well for me. The second and third gave the same result as I can read above. I tried some compiler options with no effect ( one by one and combined together also ):
--nogcse
--no-pcode-opt
--noinvariant
SDCC : pic16/pic14 3.0.2 #6420 (Apr 11 2011) (Linux)
View and moderate all "bugs Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Bugs"
Sorry, I forgot to mention the version i used.
$ sdcc -v
SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.0 #5416 (Jan 10 2010) (UNIX)