Lecture2
Lecture2
Action Items:
• Get CSUG account
• Make sure you have VPN setup!!!!
Carnegie Mellon
Announcement
• Make sure you can access CSUG machines!!!
• Programming assignment 1 will be posted today.
• It is in C language. Seek help from TAs.
• TAs are best positioned to answer your questions about
programming assignments!!!
• Programming assignments do NOT repeat the lecture
materials. They ask you to synthesize what you have
learned from the lectures and work out something new.
2
Carnegie Mellon
Previously in 252…
Problem
Algorithm
Program
Instruction Set
Architecture (ISA)
Microarchitecture
Circuit
3
Carnegie Mellon
Previously in 252…
Problem
Algorithm
Program
ISA is the contract
Instruction Set
between software and
Architecture (ISA) hardware.
Microarchitecture
Circuit
3
Carnegie Mellon
Previously in 252…
Problem
Algorithm
Renting Computing
Program
Service provider Landlord Hardware
ISA is the contract
Service receiver Instruction
YOU Set Software
between software and
Contract Architecture
Lease (ISA) ISA
hardware.
Contract’s language Natural language (e.g., Assembly programming
Microarchitecture
English) language
Circuit
3
Carnegie Mellon
Previously in 252…
Problem
• How is a human-
readable program Algorithm
translated to a
representation that
computers can Program
understand?
ISA is the contract
Instruction Set
between software and
Architecture (ISA) hardware.
Microarchitecture
Circuit
3
Carnegie Mellon
Previously in 252…
C Program Assembly program
4
Carnegie Mellon
Previously in 252…
C Program Assembly program
4
Carnegie Mellon
5
Carnegie Mellon
Everything is bits
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
6
Carnegie Mellon
Everything is bits
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
Power
(Voltage)
6
Carnegie Mellon
Why Bits?
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
• Use high voltage to represent 1
• Use low voltage to represent 0
0 1 0
1.1V
0.9V
0.2V
0.0V
7
Carnegie Mellon
Why Bits?
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
• Use high voltage to represent 1
• Use low voltage to represent 0
0 1 0
1.1V
0.9V
Low voltage
0.2V
0.0V
7
Carnegie Mellon
Why Bits?
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
• Use high voltage to represent 1
• Use low voltage to represent 0
0 1 0
1.1V
0.9V
Low voltage High voltage
0.2V
0.0V
7
Carnegie Mellon
0 1 2 1 0
1.1V
0.9V
0.2V
0.0V
8
Carnegie Mellon
0 1 2 1 0
1.1V
0.9V
0.2V
0.0V
8
Carnegie Mellon
Binary Notation
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23
9
Carnegie Mellon
Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23
• Binary Arithmetic
9
Carnegie Mellon
Binary Notation
Decimal Binary
• Base 2 Number Representation (Binary) 0 0000
• C.f., Base 10 number representation (Decimal) 1 0001
• 32110 = 1*100 + 2*101 + 3*102 = 321 2 0010
• Weighted Positional Notation 3
4
0011
0100
• Each bit has a weight depending on its position 5 0101
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110 6 0110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23 7 0111
• Binary Arithmetic 8
9
1000
1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
9
Carnegie Mellon
Binary Notation
Decimal Binary
• Base 2 Number Representation (Binary) 0 0000
• C.f., Base 10 number representation (Decimal) 1 0001
• 32110 = 1*100 + 2*101 + 3*102 = 321 2 0010
• Weighted Positional Notation 3
4
0011
0100
• Each bit has a weight depending on its position 5 0101
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110 6 0110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23 7 0111
• Binary Arithmetic 8
9
1000
1001
10 1010
0110 11 1011
+ 0101 12 1100
13 1101
1011
14 1110
15 1111
9
Carnegie Mellon
Binary Notation
Decimal Binary
• Base 2 Number Representation (Binary) 0 0000
• C.f., Base 10 number representation (Decimal) 1 0001
• 32110 = 1*100 + 2*101 + 3*102 = 321 2 0010
• Weighted Positional Notation 3
4
0011
0100
• Each bit has a weight depending on its position 5 0101
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110 6 0110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23 7 0111
• Binary Arithmetic 8
9
1000
1001
10 1010
0110 6 11 1011
+ 0101 + 5 12 1100
13 1101
1011 11
14 1110
15 1111
9
Carnegie Mellon
10110111
MSb LSb
11
Carnegie Mellon
10110111
MSb LSb
• Word = 4 Bytes (32-bit machine) / 8 Bytes (64-bit machine)
• Least Significant Byte (LSB) vs. Most Significant Byte (MSB)
11
Carnegie Mellon
12
Carnegie Mellon
Boolean Algebra
• Developed by George Boole in 19th Century
• Algebraic representation of logic
• Encode “True” as 1 and “False” as 0
And Or
n A&B = 1 when both A=1 and B=1 n A|B = 1 when either A=1 or B=1
14
Carnegie Mellon
14
Carnegie Mellon
14
Carnegie Mellon
14
Carnegie Mellon
14
Carnegie Mellon
Bit-Level Operations in C
• Operations &, |, ~, ^ Available in C
• Apply to any “integral” data type
• long, int, short, char, unsigned
• View arguments as bit vectors
• Arguments applied bit-wise
• Examples (Char data type)
• ~0x41 ➙ 0xBE
• ~010000012 ➙ 101111102
• ~0x00 ➙ 0xFF
• ~000000002 ➙ 111111112
• 0x69 & 0x55 ➙ 0x41
• 011010012 & 010101012 ➙ 010000012
• 0x69 | 0x55 ➙ 0x7D
• 011010012 | 010101012 ➙ 011111012
15
Carnegie Mellon
16
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
Shift Operations
• Left Shift: x << y Argument x 01100010
• Shift bit-vector x left y positions << 3 00010000
• Throw away extra bits on left
Log. >> 2 00011000
• Fill with 0’s on right
17
Carnegie Mellon
18
Carnegie Mellon
… -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 …
19
Carnegie Mellon
20
Carnegie Mellon
20
Carnegie Mellon
20
Carnegie Mellon
0 1 2 3 4 5 6 7
20
Carnegie Mellon
0 1 2 3 4 5 6 7
000 001 010 011 100 101 110 111
20
Carnegie Mellon
-3 -2 -1 0 1 2 3
20
Carnegie Mellon
Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
-2 110
-3 111
21
Carnegie Mellon
Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
010 -2 110
+) 101 -3 111
111
21
Carnegie Mellon
Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
010 2 -2 110
+) 101 +) -1 -3 111
111 -3
21
Carnegie Mellon
Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
010 2 -2 110
+) 101 +) -1 -3 111
111 -3
21
Carnegie Mellon
22
Carnegie Mellon
0 1 2 3 4 5 6 7
Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
22
Carnegie Mellon
-4 -3 -2 -1 0 1 2 3
Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
22
Carnegie Mellon
-4 -3 -2 -1 0 1 2 3
22
Carnegie Mellon
-4 -3 -2 -1 0 1 2 3
22
Carnegie Mellon
-4 -3 -2 -1 0 1 2 3
22
Carnegie Mellon
-4 -3 -2 -1 0 1 2 3
22
Carnegie Mellon
23
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
24
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
24
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
24
Carnegie Mellon
Numeric Ranges
25
Carnegie Mellon
Numeric Ranges
• Unsigned Values
• UMin = 0
000…0
• UMax = 2w – 1
111…1
25
Carnegie Mellon
Numeric Ranges
• Unsigned Values • Two’s Complement Values
• UMin = 0 TMin = –2w–1
000…0 100…0
• UMax = 2w – 1 TMax = 2w–1 – 1
111…1 011…1
25
Carnegie Mellon
Numeric Ranges
• Unsigned Values • Two’s Complement Values
• UMin = 0 TMin = –2w–1
000…0 100…0
• UMax = 2w – 1 TMax = 2w–1 – 1
111…1 011…1
Values for W = 16
25
Carnegie Mellon
Numeric Ranges
• Unsigned Values • Two’s Complement Values
• UMin = 0 TMin = –2w–1
000…0 100…0
• UMax = 2w – 1 TMax = 2w–1 – 1
111…1 011…1
• Other Values
Minus 1
111…1
Values for W = 16
25
Carnegie Mellon
char 1 1
short 2 2
int 4 4
long 4 8
26
Carnegie Mellon
char 1 1
short 2 2
int 4 4
long 4 8
27
Carnegie Mellon
27
Carnegie Mellon
28
Carnegie Mellon
29
Carnegie Mellon
• Implicit casting
• e.g., assignments, function calls
tx = ux;
uy = ty;
30
Carnegie Mellon
31
Carnegie Mellon
31
Carnegie Mellon
Conversion Visualized
• Signed → Unsigned UMax
• Ordering Inversion UMax – 1
• Negative → Big Positive
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
33
Carnegie Mellon
Conversion Visualized
• Signed → Unsigned UMax
• Ordering Inversion UMax – 1
• Negative → Big Positive
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
33
Carnegie Mellon
Conversion Visualized
• Signed → Unsigned UMax
• Ordering Inversion UMax – 1
• Negative → Big Positive
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
33