Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

EE-222: Microprocessor Systems

AVR Microcontroller:
Arithmetic and Flags

Instructor: Dr. Rehan Ahmed [rehan.ahmed@seecs.edu.pk]


Modeling Finite State Machines

Instructions Grouping

2
Instructions Grouping
• Data Movement/Transfer
• Arithmetic and Logical
• Control flow (Branch)

3
Data Movement/Transfer
• MOV – Move between registers
• LD/LDI – Load / Load Immediate
• ST/STI – Store / Store Immediate
• LPM – Load Program Memory
– Hardwired to load R0 with (Z) in code.
• IN/OUT – In and Out Ports
• PUSH/POP – On and off stack

4
Math and Logical
• Add
• Subtract
• Logical AND
• Logical OR
• Exclusive OR
• One’s Complement
• Two’s Complement
• Increment/Decrement
• Set/Clear Registers and Bits in Registers

5
Branch Instructions: Control Flow
• RJMP/RCALL – Relative Jmp (+/-k)
• IJMP/ICALL – Indirect Jmp (Z Reg)
• RET/RETI – Return from call/interrupt
• CP* - Compare
• SB* - Skip if Bit in Register or I/O is set/clr
• BR* - Branch if condition is met

6
Modeling Finite State Machines

AVR Status Register

7
AVR Status Register: SReg
• Status Register (SReg) aka Flag Register:
– To indicate arithmetic conditions like carry bit
– is an 8-bit register

8
I/O Register $3F : SREG
• All Bits are R/W:
– I – Global Interrupt Enable
– T – Bit Copy Storage
– H – Half Carry Flag
▪ set when there is a carry from D3 to D4 during an ADD or SUB
– S – Sign Bit
▪ XOR of N and V flags
– V – Two’s Complement Overflow Flag
▪ set whenever the signed number result is too large
– N – Negative Flag
▪ N = 0; arithmetic operation result is positive
▪ N = 1; arithmetic operation result is negative
– Z – Zero Flag
▪ If an arithmetic or logic operation result is ZERO
– C – Carry Flag
▪ set whenever there is a carry from the D7 bit

9
Example: ADD Instruction and Z Flag
• Show the status of Z flag during the execution of the
following program:

LDI R20,4
DEC R20
DEC R20
DEC R20
DEC R20

10
Example: ADD Instruction and C,H and Z
Flag
• Show the status of C, H and Z flags after the addition of
0x38 and 0x2F:

LDI R16, 0x38


LDI R17, 0x2F
ADD R16, R17

11
Example: ADD Instruction and C,H and Z
Flag
• Show the status of C, H and Z flags after the addition of
0x9C and 0x64:

LDI R16, 0x9C


LDI R17, 0x64
ADD R16, R17

12
NOT all instructions affect the flags

13
Reading
• The AVR Microcontroller and Embedded Systems: Using
Assembly and C by Mazidi et al., Prentice Hall
– Chapter-2: 2.1 – 2.4

14
THANK YOU

You might also like