Computer Systems Architecture: Thorsten Altenkirch and Liyang Hu
Computer Systems Architecture: Thorsten Altenkirch and Liyang Hu
Signed Numbers
Binary Addition
Signed Numbers
Long Addition
Long Addition in Decimal
6
+
2
0 1
9
2
8
1
1
9
1
1
1
5
8
0
3
1
2
1
4
4
8
0
2
1 3
1 7
1
Carry
3 0
0
0
1
1
1
1
0
0
1 0 7616
0 1 D516
0
1 1 14B16
0
1
1 1
1 0
1
1
1
1
1
0
1
0
1
1
0
0
= 118
= 213
Carry
= 331
Binary Addition
Signed Numbers
Single-Bit Adders
Half-Adder
A
0
0
1
1
S =AB
C =AB
B
0
1
0
1
C
0
0
0
1
S
0
1
1
0
Cout
0
0
0
1
0
1
1
1
S
0
1
1
0
1
0
0
1
Full-Adder
S = A B Cin
Cout = (AB)(B Cin )(ACin )
A
0
0
0
0
1
1
1
1
B
0
0
1
1
0
0
1
1
Cin
0
1
0
1
0
1
0
1
Binary Addition
Signed Numbers
Binary Addition
Signed Numbers
Simulating addition in C
Inputs x, y ;
ci=0;
z=0;
for(i=0;i<sizeof(x)*8;i++) {
xi=x&(1<<i);
yi=y&(1<<i);
zi=xi^yi^ci;
csi=(xi&yi)|(yi&ci)|(xi&ci);
z=z|zi;
ci=csi;
}
Output: z.
For illustration only!
Nobody would implement addition in software!
Binary Addition
Signed Numbers
Ones Complement
Excess-n
Twos Complement
Binary Addition
Signed Numbers
7
6
5
4
3
2
1
0
+0
+1
+2
+3
+4
+5
+6
+7
Ones Comp.
1000
1001
1010
1011
1100
1101
1110
1111
0000
0001
0010
0011
0100
0101
0110
0111
7
6
5
4
3
2
1
0
+0
+1
+2
+3
+4
+5
+6
+7
Twos Comp.
Excess-3
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
3
2
1
0
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
1000
1001
1010
1011
1100
1101
1110
1111
0000
0001
0010
0011
0100
0101
0110
0111
8
7
6
5
4
3
2
1
0
+1
+2
+3
+4
+5
+6
+7
Binary Addition
Signed Numbers
Method of Complements
In base b with n digits, represent x using b n x
But theres a quicker way to calculate b n x:
Binary Addition
Signed Numbers
Twos Complements
In base 2 with n bits, represent x using 2n x
But theres a quicker way to calculate 2n x:
Binary Addition
Signed Numbers
Alternative View
8-Bit Twos Complement (128 x < 127)
MSB
Bit 7th 6th
Weight 27 26
th
5
25
th
4
24
rd
3
23
nd
2
22
st
1
21
LSB
0th
20
Exercises
Calculate 81 42 in 8-bit twos complement
We can negate $s0 using nor $s0, $zero, $s0
addi $s0, $s0, 1
For what number will this not work?
Binary Addition
Signed Numbers
Thursday quiz
Binary Addition
Signed Numbers
Thursday quiz
Binary Addition
Signed Numbers
Thursday quiz
Binary Addition
Signed Numbers
Thursday quiz
Binary Addition
Signed Numbers
Thursday quiz
3. What does the following program fragment do?
num:
a
b
c
d
e
.word 0
...
la $s0,num
li $t0,1
sb $t0,1($s0)
lw $t1,($s0)
It
It
It
It
It
Binary Addition
Signed Numbers
Thursday quiz
d
e
Binary Addition
Signed Numbers
Thursday quiz
A machine instruction.
A pseudoinstruction.
A conditional jump.
An unconditional jump.
A data transfer instruction.