Given the short example below, is there any way to reduce the amount of registers used in the code between [x1] and [x2]?
I read Input from PINA and generate output for PORTB according to some logic table. This is the most straightforward way to do this for me, but it seems kind of roundabout. Is there a more elegant way that I am not aware of?
loop:
;Set Masks for Inputpins [x1]
ldi r17, 0b00000010
ldi r18, 0b00000100
;Read Input from PINA
in r16, PINA
;Apply Masks and normalize
and r17, r16
lsl r17
and r18, r16
lsl r18
lsl r18
;Start with actual bitwise operations e.g [x2]
mov r16, r17
xor r16, r18
and r16, r17
...
...
out PORTB, r16
rjmp loop
Note that I am not as much interested in perfomance (space, speed) as in reducing the amount of mask and shift operations which introduce (sloppy / nasty) errors easily on a bigger scale.
Also Note that I am not sure if Codereview.Stackechange is the more appropiate place for this question.
I don't think you can cut down on the number of registers used in this particular case because it looks like you want to keep the contents of r16
pristine.
If you didn't need to do that, you could use the andi
instruction to load the bitmasks as immediate data.