04 Arithmetic
04 Arithmetic
04 Arithmetic
Addition
0 0 1 1 0 6
0 1 0 1 1 11
1 0 0 0 1 17
1 0 1 1 0 22
0 1 0 1 1 11
1 0 0 0 0 1 33
1
3D1 / Microprocessor Systems I
Carry Binary Arithmetic
1 0 0 1 1 1 0 0 156
1 0 1 0 0 1 1 1 167
1 0 1 0 0 0 0 1 1 323
Condition Code
Flags
Some instructions can optionally update the Condition
Code Flags to provide information about the result of
the execution of the instruction
• e.g. whether the result of an addition was zero, or negative or
whether a carry occurred
N – Negative Z – Zero
V – oVerflow C – Carry
3
3D1 / Microprocessor Systems I
Condition Code Flags Binary Arithmetic
11100000100 S Rn Rd 00000000 Rm
31 21 20 19 16 15 12 11 4 3 0
start
LDR r0, =0xC0000000
LDR r1, =0x70000000
ADDS r0, r0, r1
stop B stop
5
3D1 / Microprocessor Systems I
Negative Numbers Binary Arithmetic
Interpretation!
sign
magnitude
+/-
31 30 0
A 12-hour clock is an 12
11 1
example of modulo-12
arithmetic 10 2
If we add 4 hours to 10
o’ lo k e get o’ lo k 9 3
If we subtract 4 from 2
o’ lo k e get 8 4
o’ lo k ot - o’ lo k! 7 5
6
8
2’s Co ple e t
3D1 / Microprocessor Systems I
Binary Arithmetic
A 4-bit number
system allows us to 1111
0000
0001
represent 16 values 0
15 1
1110 0010
Ignoring carries from 14 2
addition gives us
1101 0011
modulo-16 arithmetic 13 3
1100 12 4 0100
(15 + 1) mod 16 = 0
• and -1 + 1 = 0 11 5
• and -2 + 2 = 0
10 6
1010 0110
(14 + 4) mod 16 = 2
9 7
8
• and -2 +4 = 2 1001
1000
0111
9
2’s Co ple e t
3D1 / Microprocessor Systems I
Binary Arithmetic
0000
1111 0001
0
15
-1 +1
1110 0010
0
14
-2 15 1 +2
14 2
1101 0011
13
-3 +3
13 3
Remember:
1100 12
-4 12 interpretation!! 4 +4 0100
11 5
11
-5 +5
1011 0101
10 6
10
-6 9 7 +6
8
1010 -7
9 +7 0110
-8
8
1001 0111
1000
10
2’s Co ple e t
3D1 / Microprocessor Systems I
Binary Arithmetic
11
2’s Co ple e t Exa ples
3D1 / Microprocessor Systems I
Binary Arithmetic
0 1 1 0 0 0 0 1
1 0 0 1 1 1 1 1
1 0 0 0 0 0 0 0 0
stop B stop
A–B 8 bits
0 0 1 1 0 1 1 0
0 0 1 1 0 1 0 0
0 0 0 0 0 0 1 0
A + (TC(B))
8 bits
0 0 1 1 0 1 1 0
1 1 0 0 1 1 0 0
1 0 0 0 0 0 0 1 0
15
3D1 / Microprocessor Systems I
Subtraction Binary Arithmetic
A–B 8 bits
0 0 0 0 1 0 0 0
0 1 1 1 1 1 1 1
1 0 0 0 1 0 0 1
A + (TC(B)) 8 bits
0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 1
1 0 0 0 1 0 0 1
17
3D1 / Microprocessor Systems I
ARM Instruction Set Reference Binary Arithmetic
18
3D1 / Microprocessor Systems I
ARM Instruction Set Reference Binary Arithmetic
• e.g. SUBtract
19
3D1 / Microprocessor Systems I
Zero and Negative Flags Binary Arithmetic
20
2’s Co ple e t Exa ples
3D1 / Microprocessor Systems I
Binary Arithmetic
0 1 1 0 0 0 0 1
0 0 1 0 1 1 0 1
0 1 0 0 0 1 1 1 0
Condition Code
Flags
22
3D1 / Microprocessor Systems I
Carry and oVerflow Binary Arithmetic
Addition rule (r = a + b)
V = 1 if MSB(a) = MSB(b) and
MSB(r) MSB(a)
• i.e. oVerflow accurs for addition if the operands have the
same sign and the result has a different sign
Subtraction rule (r = a – b)
V = 1 if MSB(a) MSB(b) and
MSB(r) MSB(a)
• i.e. oVerflow occurs for subtraction if the operands have
different signs and the sign of the result is different from the
sign of the first operand
24
3D1 / Microprocessor Systems I
Carry and oVerflow Examples Binary Arithmetic
8 bits
0 1 1 1 0 0 0 0
Carry =1
1 0 1 1 0 0 0 0 oVerflow = 0
1 0 0 1 0 0 0 0 0
8 bits
1 0 1 1 0 0 0 0
Carry =1
1 0 1 1 0 0 0 0 oVerflow = 1
1 0 1 1 0 0 0 0 0
0 0 0 0 0 E 4 4 3 2 A 8 4 F E 6
0 0 0 0 0 0 0 0 F 4 E 0 0 3 2 2
0 0 0 0 0 E 4 5 2 7 8 8 5 3 0 8
; Add A and B
ADDS r0, r3, r5 ; Rlwr = Alwr + Blwr, update Cout
ADC r1, r2, r4 ; Rupr = Aupr + Blwr + Cout
stop B stop