I'm starting to learn Assembler and I have this doubt. I have the following segment of code
LDI R16, 0
OUT DDRB, R16 ; Configures port B as an input port
SBIC PORTB, 0
LDI R18, 0xAA
OUT PORTC, R18
RETI
I've read that what SBIC
does is checking (in this case) if the bit 0 of port B is clear. If it is, then it skips the next instruction.
My questions are:
1) Does OUT DDRB, R16
write 0's in all of the bits of port B or is it just a configuration instruction but it doesn't write anything? In other words: are the bits of port B all set to 0?
2) If they are all set to 0, then the instruction after the SBIC
will be skipped. But is only the first instruction that comes next that is skipped, or everything until RETI
is reached?
The first instruction writes 0's in all bits of the DDRB register. I assume from the comment and the name of the register this makes all the pins of port B inputs. This wouldn't explicitly output anything, but depending on how the device configured electrically, any external device trying to read any of the port B pins at that point might see a change in value since they're no longer configured for reading.
The next instruction tests the 0th bit of PORTB register, the value of which will depend on whatever is connected the corresponding pin. If it's clear the next instruction LDI R18, 0xAA
is skipped. If it's set the next instruction is executed.
This means that the value stored in the PORTC register by OUT PORTC, R18
depends on both the state of the first pin of port B and the value contained in R18 at the start of the execution of your example code.