L14 Logic Instructions
L14 Logic Instructions
AVR Microcontroller:
Logic and Compare Instructions
2
Logic Instructions
AND Rd,Rr ;Rd = Rd AND Rr
OR Rd,Rr ;Rd = Rd OR Rr
EOR Rd,Rr ;Rd = Rd XOR Rr ( immediate are not supported)
COM Rd,Rr ;Rd = 1’s Complement of Rd (11111111 – Rd)
NEG Rd,Rr ;Rd = 2’s Complement of Rd (100000000 – Rd)
Solution:
3
AND: Details and Machine-code
• Can you specify the 16-bit machine code for AND R1,R2 ?
4
Setting and Clearing bits
AND Rd,Rr ;Rd = Rd AND Rr
OR Rd,Rr ;Rd = Rd OR Rr
EOR Rd,Rr ;Rd = Rd XOR Rr ( immediate are not supported)
COM Rd,Rr ;Rd = 1’s Complement of Rd (11111111 – Rd)
NEG Rd,Rr ;Rd = 2’s Complement of Rd (100000000 – Rd)
35H 0 0 1 1 0 1 0 1 04H 0 0 0 0 0 1 0 0
AND 0FH 0 0 1 1 0 1 0 1 OR 30H 0 0 1 1 0 0 0 0
05H 0 0 0 0 0 1 0 1 34H 0 0 1 1 0 1 0 0
5
X OR
• EOR Rd,Rs ;Rd=Rd XOR Rs
6
COM (Compleiment)
• COM Rd ;Rd=1’s compliment Rd
– Example
7
NEG (Negate)
• NEG Rd ;Rd=2’s compliment Rd
8
Branch and CP Instructions
• For example, BRVC can be used to branch when oVerflow is clear to zero
• BRVS is used to branch when oVerflow is set to one
9
ROL instruction
ROL Rd ;Rotate Left
In ROL, as bits are shifted from right to left, the carry flag enters the LSB
and the MSB exits to the carry flag. In other words, in ROL, the C is
moved to the LSB, and the MSB is moved to the C.
SEC ;set C to 1 (C = 1)
LDI R20,0x15 ;R20 = 0001 0101
ROL R20 ;R20 = 0010 1011 C=0
ROL R20 ;R20 = 0101 0110 C=0
ROL R20 ;R20 = 1010 1100 C=0
ROL R20 ;R20 = 0101 1000 C=1
10
ROR instruction
ROR Rd ;Rotate Right
In ROR, as bits are rotated from left to right, the carry flag enters the
MSB and the LSB exits to the carry flag. In other words, in ROR, the
C is moved to the MSB, and the LSB is moved to the C.
11
LSL instruction
LSL Rd ;Logical Shift Left
In LSL, as bits are shifted from right
to left, 0 enters the LSB and the MSB
exits to the carry flag. In other
words, in LSL, 0 is moved to the
LSB, and the MSB is moved to
the C.
this instruction multiplies by 2 the value of the register, but
only when the carry flag is not set after the LSL is executed.
Let’s see what happens to 00100110 after running 3 LSL instructions.
Multiplication
CLC ;clear C (C = 0)
LDI R20 , 0x26 ;R20 = 0010 0110 (38) C=0
LSL R20 ;R20 = 0100 1100 (74) C = 0
Error
LSL R20 ;R20 = 1001 1000 (148) CWhy
= 0?
LSL R20 ;R20 = 0011 0000 (98) C = 1; as
C=1 so content of ;R20 is
not multiplied by 2
12
LSR instruction
LSR Rd ;Logical Shift Right
In LSR, as bits are shifted from left
to right, 0 enters the MSB and the
LSB exits to the carry flag. In other
words, in LSR, 0 is moved to the
MSB, and the LSB is moved to
this instruction divides by 2 the content of the register and
the C.
carry flag contains the remainder of division.
In ASR, as bits are shifted from left to right, MSB is held constant and
the LSB exits to the carry flag. In other words, MSB is not changed
but is copied to D6, D6 is moved to D5, D5 is moved to D4 and
so on. Signed division
What happens to 0010 0110 after running 5 ASR instructions?at work
14
More Conditional Branch Instruction
15
SWAP
• SWAP Rd ;Swap nibbles Rd=R0 to R31
16
Rotate and Shift Instructions: Self-Read
• ROR
• ROL
• LSL
• LSR
• SWAP
17
Reading
• The AVR Microcontroller and Embedded Systems: Using
Assembly and C by Mazidi et al., Prentice Hall
– Chapter-5: Complete
18
THANK YOU