L13-Arithmetic Instructions
L13-Arithmetic Instructions
AVR Microcontroller:
Arithmetic Instructions
2
Arithmetic
3
UnSigned and Signed Numbers (for humans)
• Unsigned numbers
– all bits represent the magnitude of a positive integer
bn – 1 b1 b0
Magnitude
MSB
• Signed numbers
– left-most bit represents the sign of a number
– And the remaining bits represent the magnitude
bn – 1 bn – 2 b1 b0
Magnitude
Sign
0 denotes + MSB
1 denotes –
4
ALU doesn’t know about Signed/Unsigned
• The ALU just does the binary math and sets the flags
appropriately:
– It's up to you, the programmer, to know which flag to check
after the math is done!
5
Unsigned Arithmetic
6
Addition of Unsigned Numbers
ADD Rd, Rr ; Rd = Rd + Rr
7
Machine Code of ADD
• Can you specify the 16-bit machine code for ADD R1,R2 ?
8
ADD instruction
ADD Rd, Rr ;Rd = Rd + Rr ( Direct or immediate are not supported)
Show how the status register (SREG) is affected by the following instructions.
Solution:
F5H 1111 0101
+ 0BH + 0000 1011
100H 0000 0000
After the addition, register R21 contains 00 and the flags are as follows:
C = 1 because there is a carry out from D7.
Z = 1 because the result in destination register (R21) is zero.
H = 1 because there is a carry from D3 to D4.
9
ADD instruction
Assume that RAM location 400H has the value of 33H. Write a program to find the su
of location 400H of RAM and 55H. At the end of the program, R21 should contain the
Solution:
10
ADC instructions: Addition of 16-bit Numbers
1
3C
+3B
+3B
E7 8D
78 74
Write a program to add two 16-bit numbers. The numbers are 3CE7H and 3B8DH.
Place the sum in R3 and R4; R3 should have the lower byte.
Solution:
Notice the use of ADD for the lower byte and ADC for the higher byte.
11
Subtraction
Micro-processor performs subtraction with digital circuits already
designed for addition
A B A (B)
B B 1
(Invert all bits of B and add 1 to LSB)
A B A + (2’s Compliment
B)
Steps for Subtraction
12
SUB instructions
SUB Rd, Rr ; Rd = Rd – Rr (immediate values are not
supported)
SUBI Rd, K ; Rd = Rd – K
Solution:
The flags would be set as follows: N = 0, C = 0. (Notice that there is a carry but C = 0
See next slide.)
The programmer must look at the N (or C) flag to determine if the result is positive or n
13
Subtraction: Key Take-Aways
• In AVR, the CPU inverts the C flag after the SUB
instruction,
– Notice that the CPU does not invert the carry flag after the
ADD instruction
– If C=0 (N=0), the result is positive
14
SUBI- Subtract Immediate
SUBI Rd, K ;Rd = Rd – K 16 ≤ d ≤ 31,
15
SBIW- Subtract Immediate from Word
16
SBIW: Details and Op-code
17
SBC instruction
SBC Rd, Rr ;Rd = Rd – Rr – C ( immediate are not supported)
SBCI Rd, K ;Rd = Rd – K – C
28 62 (H)
- 12 96 (H)
---------------
15 CC (H)
After the SUB, R26 has 62(H) – 96(H) = CC(H) and the carry flag is set to 1,
indicating there is a borrow (notice, N = 1).
Because C = 1, after SBC is executed then R27 has 28(H) – 12(H) - 1 = 15H.
Therefore, we have 2862(H) – 1296(H) = 15CC(H).
18
Use of SBC for Multibyte Subtraction
Already done !
19
Multiplication
Multiplication: performs 8-bit × 8-bit → 16-bit multiplication.
MUL Rd,Rr ; R1:R0 = Rd × Rr (Multiply Unsigned)
MULS Rd,Rr ; R1:R0 = Rd × Rr (Multiply signed)
MULSU Rd,Rr ; R1:R0 = Rd × Rr (Multiply signed with
unsigned)
Result of
multiplication is
placed in R1 and R0
registers !
20
Division
• AVR has no instruction for division:
– implemented by repeated subtraction
Numerator is placed in a register and the denominator is
subtracted from it repeatedly
The quotient is the number of times we subtract
Remainder is in the register upon completion
.DEF NUM = R20
.DEF DENOMINATOR = R21
.DEF QUOTIENT = R22
21
Signed Arithmetic
22
Signed Number
D7 D6 D5 D4 D3 D2 D1 D0
sign magnitude
23
Representation of Negative Numbers
• Negative numbers can be represented in three different
ways:
2. 1’s complement
3. 2’s complement
24
Overall: Interpretation of 4-bit
Signed Numbers
25
Byte-sized Negative Numbers
26
Overflow Flag V
• In 8 bit signed arithmetic, Vis set to 1 if:
27
Overflow problem: Example
ndicates error by raising V flag, it is up to programmer to take care of erroneous
S – Sign Bit
XOR of N and V flags
28
Example
S – Sign Bit
XOR of N and V flags
29
Example
1111
1011
S – Sign Bit
XOR of N and V flags
30
Example
31
Lessons to Remember
32
Important Take-away – (1)
• If your program treats the bits in a word as unsigned
numbers:
– you must watch to see if your arithmetic sets the carry flag
(C) on, indicating the result is wrong.
– You don't care about the overflow flag when doing unsigned
math.
The overflow flag is only relevant to signed numbers, not
unsigned!
33
Important Take-away – (2)
• If your program treats the bits in a word as two's
complement signed values:
– you must watch to see if your arithmetic sets the overflow
(V) flag on, indicating the result is wrong.
– You don't care about the carry flag when doing signed, two's
complement math.
The carry flag is only relevant to unsigned numbers, not
signed!
34
Sum-up: C vs V Flags
• In unsigned arithmetic, watch the carry flag to detect
errors:
– In unsigned arithmetic, the overflow flag does NOT tell you
anything interesting.
35
Carry Flag (C): When is it set?
• There are two rules for turning on the carry flag in
binary/integer math:
1. The carry flag is set if the addition of two numbers causes
a carry out of the most significant (leftmost) bits added.
1111 + 0001 = 0000 (carry flag is turned on)
36
Overflow Flag (V): When is it set?
• There are two rules for turning on the overflow flag in
binary/integer math :
1. If the sum of two numbers with the sign bits off yields a
result number with the sign bit on, the "overflow" flag is
turned on.
0100 + 0100 = 1000 (overflow flag is turned on)
38
Reading
• The AVR Microcontroller and Embedded Systems: Using
Assembly and C by Mazidi et al., Prentice Hall
– Chapter-5: 5.1 and 5.2
39
THANK YOU