15213 Introduction to Computer Systems 3 rd Lecture Jan 19 2016 Instructors Franz Franchetti Seth Copen Goldstein Ralf Brown and Brian Railing Autolab accounts You should have an ID: 784399
Download The PPT/PDF document "Bits, Bytes, and Integers – Part 2" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Bits, Bytes, and Integers – Part 215-213: Introduction to Computer Systems3rd Lecture, Jan. 19, 2016
Instructors:
Franz Franchetti, Seth Copen Goldstein, Ralf Brown, and Brian Railing
Slide2Autolab accountsYou should have an autolab account by now
You must be enrolled to get an account
Autolab
is not tied in to the Hub’s
rosters
If you do NOT have an
Autolab
account for 213/513 this semester, please add your name to
the following Google form. The link is available from the course web page.
https
://
docs.google.com/forms/d/1M3dHRvEraM8eCpk9jq46rkqDqeEho_ffhdce7F25rqY/viewform?usp=send_form
We
will update the
autolab
accounts once a day, so check back in 24 hours
.
Slide3First Assignment: Data LabDue: Thursday, Jan 28th 2016, 11:59:00 pm
Last Possible Time to Turn in: Fri, Jan 29, 11:59PM
Read the instructions carefully
You should have started
Seek help (office hours started on Sunday)
Based on Lecture 2, 3 , and 4
After today’s lecture you know everything for the integer problems, float problems covered on Thursday
Slide4Summary From Last LectureRepresenting information as bitsBit-level manipulationsIntegersRepresentation: unsigned and signedConversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Representations in memory, pointers, strings
Summary
Slide5Operations
&
,
|
,
~
,
^
Available in CApply to any “integral” data typelong, int, short, char, unsignedView arguments as bit vectorsArguments applied bit-wiseExamples (Char data type)~0x41 → 0xBE~010000012 → 101111102~0x00 → 0xFF~000000002 → 1111111120x69 & 0x55 → 0x41011010012 & 010101012 → 0100000120x69 | 0x55 → 0x7D011010012 | 010101012 → 011111012
Bit-Level Operations in C
Operations &, |, ~, ^ Available in CApply to any “integral” data typelong, int, short, char, unsignedView arguments as bit vectorsArguments applied bit-wiseExamples (Char data type)~0x41 → 0xBE~0100 00012 → 1011 11102~0x00 → 0xFF~0000 00002 → 1111 111120x69 & 0x55 → 0x410110 10012 & 0101 01012 → 0100 000120x69 | 0x55 → 0x7D0110 10012 | 0101 01012 → 0111 11012
0
0
0000
1
1
0001
2
2
0010
3
3
0011
4
4
0100
5
5
0101
6
6
0110
7
7
0111
8
8
1000
9
9
1001
A
10
1010
B
11
1011
C
12
1100
D
13
1101
E
14
1110
F
15
1111
Hex
Decimal
Binary
Slide6Logic Operations in CLogic Operations: &&, ||, !
View 0 as “False”
Anything nonzero as “True”
Always return 0 or 1
Early termination
Examples (char data type)
!0x41
→
0x00!0x00 → 0x01!!0x41→ 0x010x69 && 0x55 → 0x010x69 || 0x55 → 0x01p && *p (avoids null pointer access)
Slide7Unsigned & Signed Numeric Values
Equivalence
Same encodings for nonnegative values
Uniqueness
Every bit pattern represents unique integer value
Each
representable
integer has unique bit encoding
Expression containing signed and unsigned int:int is cast to unsigned!!XB2T(X)B2U(X)0000000011001020011301004010150110601117–88
–7
9–610–511–412–313–214–1151000100110101011110011011110
1111
0
1
2
3
4
5
6
7
Slide8Sign Extension and TruncationSign Extension
Truncation
Slide9Today: Bits, Bytes, and IntegersRepresenting information as bitsBit-level manipulationsIntegersRepresentation: unsigned and signed
Conversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Representations in memory, pointers, strings
Summary
Slide10Unsigned Addition
Standard Addition Function
Ignores carry output
Implements Modular Arithmetic
s
=
UAdd
w
(u , v) = u + v mod 2w• • •• • •uv+• • •
u
+ v• • •True Sum: w+1 bitsOperands: w bitsDiscard Carry: w bitsUAddw(u , v) 1110 1001+ 1101 0101 1 1011 1110 1011 1110
E9
+ D5
1BE
BE
0
0
0000
1
1
0001
2
2
0010
3
3
0011
4
4
0100
5
5
0101
6
6
0110
7
7
0111
8
8
1000
9
9
1001
A
10
1010
B
11
1011
C
12
1100
D
13
1101
E
14
1110
F
15
1111
Hex
Decimal
Binary
223
+ 213
446
190
u
nsigned
char
Slide11Visualizing (Mathematical) Integer Addition
Integer Addition
4-bit integers
u
,
v
Compute true sum Add
4
(u , v)Values increase linearly with u and vForms planar surfaceAdd4(u , v)uv
Slide12Visualizing Unsigned Addition
Wraps Around
If true sum ≥ 2
w
At most once
0
2
w
2w+1UAdd4(u , v)uvTrue SumModular SumOverflowOverflow
Slide13Two’s Complement AdditionTAdd and
UAdd
have Identical Bit-Level Behavior
Signed vs. unsigned addition in C:
int
s, t, u, v;
s = (
int) ((unsigned) u + (unsigned) v); t = u + vWill give s == t• • •• • •uv+• • •
u +
v• • •True Sum: w+1 bitsOperands: w bitsDiscard Carry: w bitsTAddw(u , v) 1110 1001+ 1101 0101 1 1011 1110 1011 1110
E9
+ D5
1BE
BE
-23
+ -43
446
-66
Slide14TAdd OverflowFunctionalityTrue sum requires
w
+1
bits
Drop off MSB
Treat remaining bits as 2’s comp. integer
–2
w
–1–2w02w –1–12w–1True SumTAdd Result1 000…01 011…1
0
000…00 100…00 111…1100…0000…0011…1PosOverNegOver
Slide15Visualizing 2’s Complement Addition
Values
4-bit two’s comp.
Range from -8 to +7
Wraps Around
If sum
2
w–1Becomes negativeAt most onceIf sum < –2w–1Becomes positiveAt most onceTAdd4(u , v)uvPosOverNegOver
Slide16MultiplicationGoal: Computing Product of w-bit numbers
x
,
y
Either signed or unsigned
But, exact results can be bigger than
w
bitsUnsigned: up to 2w bitsResult range: 0 ≤ x * y ≤ (2w – 1) 2 = 22w – 2w+1 + 1Two’s complement min (negative): Up to 2w-1 bitsResult range: x * y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1Two’s complement max (positive): Up to 2w bits, but only for (TMinw)2Result range: x * y ≤ (–2w–1) 2 = 22w–2So, maintaining exact results…would need to keep expanding word size with each product computedis done in software, if needede.g., by “arbitrary precision” arithmetic packages
Slide17Unsigned Multiplication in CStandard Multiplication Function
Ignores high order
w
bits
Implements Modular Arithmetic
UMult
w
(
u , v) = u · v mod 2w• • •• • •uv*• • •u · v
• • •
True Product: 2*w bitsOperands: w bitsDiscard w bits: w bitsUMultw(u , v)• • • 1110 1001* 1101 0101
1100 0001 1101 0010
1101 1101
E9
*
D5
C1DD
DD
223
*
213
47499
221
Slide18Signed Multiplication in CStandard Multiplication Function
Ignores high order
w
bits
Some of which are different for signed vs. unsigned multiplication
Lower bits are the same
• • •
• • •
uv*• • •u · v
• • •
True Product: 2*w bitsOperands: w bitsDiscard w bits: w bitsTMultw(u , v)• • • -23* -43
16896
-35
1110 1001
* 1101 0101
1100 0001 1101 0010
1101 1101
E9
*
D5
C1DD
DD
Slide19Power-of-2 Multiply with ShiftOperation
u << k
gives
u *
2
k
Both signed and unsigned
Examplesu << 3 == u * 8(u << 5) – (u << 3) == u * 24Most machines shift and add faster than multiplyCompiler generates this code automatically• • •001000•••u2k*u · 2kTrue Product: w+k bitsOperands: w bitsDiscard k
bits: w bits
UMultw(u , 2k)•••k• • •000•••TMultw(u , 2k)000•••
•••
Slide20Unsigned Power-of-2 Divide with ShiftQuotient of Unsigned by Power of 2
u >> k
gives
u /
2
k
Uses logical shift001000•••u2k/u / 2kDivision: Operands:•••k•••
•••
•••000•••••• u / 2k •••Result:.
Binary Point
0
0
0
0
•••
0
Slide21Signed Power-of-2 Divide with ShiftQuotient of Signed by Power of 2
x >> k
gives
x /
2
k
Uses arithmetic shiftRounds wrong direction when u < 0001000•••x2k/x / 2kDivision: Operands:•••k••••••
•••
0••••••RoundDown(x / 2k)•••Result:.Binary Point0
•••
Slide22Correct Power-of-2 DivideQuotient of Negative Number by Power of 2
Want
x /
2
k
(
Round Toward 0)Compute as (x+2k-1)/ 2k In C: (x + (1<<k)-1) >> kBiases dividend toward 0Case 1: No roundingDivisor: Dividend:001000•••u2k/ u / 2k •••k1•••000
•••
1•••011•••.Binary Point1000111•••+2k –1•••1
1
1
•••
1
•••
1
1
1
•••
Biasing has no effect
Slide23Correct Power-of-2 Divide (Cont.)Divisor:
Dividend:
Case 2: Rounding
0
0
1
0
0
0•••x2k/ x / 2k •••k1••••••1•••
0
11•••.Binary Point1000111•••+2k –1•••1•••
•••
Biasing adds 1 to final result
•••
Incremented by 1
Incremented by 1
Slide24Negation: Complement & IncrementNegate through
complement and increase
~x + 1 == -x
Exa
mple
Observation:
~x + x == 1111…111 == -1
1
0010111 x01101000~x+11111
1
11-1x = 15213
Slide25Complement & Increment Examples
x =
TMin
x = 0
Canonical counter example
Slide26Today: Bits, Bytes, and IntegersRepresenting information as bitsBit-level manipulationsIntegersRepresentation: unsigned and signed
Conversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Summary
Representations in memory, pointers, strings
Slide27Arithmetic: Basic RulesAddition:Unsigned/signed: Normal addition followed by truncate,same operation on bit levelUnsigned: addition mod 2w
Mathematical addition + possible subtraction of 2
w
Signed: modified addition mod 2
w
(result in proper range)
Mathematical addition + possible addition or subtraction of 2
w
Multiplication:Unsigned/signed: Normal multiplication followed by truncate, same operation on bit levelUnsigned: multiplication mod 2wSigned: modified multiplication mod 2w (result in proper range)
Slide28Why Should I Use Unsigned?Don’t use without understanding implicationsEasy to make mistakes
unsigned
i
;
for (
i
= cnt-2;
i
>= 0; i--) a[i] += a[i+1];Can be very subtle#define DELTA sizeof(int)int i;for (i = CNT; i-DELTA >= 0; i-= DELTA) . . .
Slide29Counting Down with UnsignedProper way to use unsigned as loop index
unsigned
i
;
for (
i
= cnt-2;
i
< cnt; i--) a[i] += a[i+1];See Robert Seacord, Secure Coding in C and C++C Standard guarantees that unsigned addition will behave like modular arithmetic0 – 1 UMaxEven bettersize_t i;for (i = cnt-2; i < cnt; i--) a[i] += a[i+1];Data type size_t defined as unsigned value with length = word sizeCode will work even if cnt = UMaxWhat if cnt is signed and < 0?
Slide30Why Should I Use Unsigned? (cont.)Do Use When Performing Modular Arithmetic
Multiprecision
arithmetic
Do
Use When Using Bits to Represent Sets
Logical right shift, no sign extension
Do
Use
In System ProgrammingBit masks, device commands,…
Slide31Integer Arithmetic Example
0
0
0000
1
1
0001
2
20010
3
3001144010055
0101
6
6
0110
7
7
0111
8
8
1000
9
9
1001
A
10
1010
B
11
1011
C
12
1100
D
13
1101
E
14
1110
F
15
1111
Hex
Decimal
Binary
1111 0011
+ 0101 0010
1 0100 0101
0101 0101
F3
+ 52
145
45
243
+ 82
325
69
u
nsigned char
0001 1001
*
0000 0010
0 0011 0010
0011 0010
19
*
02
032
32
25
* 2
50
50
u
nsigned char
Slide32Today: Bits, Bytes, and IntegersRepresenting information as bitsBit-level manipulationsIntegers
Representation: unsigned and signed
Conversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Summary
Representations in memory, pointers, strings
Slide33Byte-Oriented Memory OrganizationPrograms refer to data by addressConceptually, envision it as a very
large array of
bytes
In reality, it’s not, but can think of it that way
An address is like an index into that array
and, a pointer variable stores an address
Note: system
provides
private address spaces to each “process”Think of a process as a program being executedSo, a program can clobber its own data, but not that of others• • •00•••0FF•••F
Slide34Machine WordsAny given computer has a “Word Size”Nominal size of integer-valued dataand of addresses
Until recently, most
machines
used
32 bits (4 bytes)
as word size
Limits addresses to
4GB (2
32 bytes)Increasingly, machines have 64-bit word sizePotentially, could have 18 EB (exabytes) of addressable memoryThat’s 18.4 X 1018Machines still support multiple data formatsFractions or multiples of word sizeAlways integral number of bytes
Slide35Word-Oriented Memory OrganizationAddresses Specify Byte LocationsAddress of first byte in wordAddresses of successive words differ by 4 (32-bit) or 8 (64-bit)
0000
0001
0002
0003
0004
0005
0006
000700080009
0010
001132-bitWordsBytesAddr.001200130014
0015
64-bit
Words
Addr
=
??
Addr
=
??
Addr
=??
Addr
=
??Addr
=
??
Addr =
??
0000
0004
0008
0012
0000
0008
Slide36Example Data Representations
C Data Type
Typical 32-bit
Typical 64-bit
x86-64
char
1
1
1short2
2
2int444long4
8
8
float
4
4
4
double
8
8
8
pointer
4
8
8
Slide37Byte OrderingSo, how are the bytes within a multi-byte word ordered in memory?ConventionsBig
Endian
: Sun, PPC Mac,
Internet
Least significant byte has highest address
Little Endian:
x86
, ARM processors running Android,
iOS, and WindowsLeast significant byte has lowest address
Slide38Byte Ordering ExampleExampleVariable x has 4-byte value of 0x01234567
Address given by &
x
is 0x100
0x100
0x101
0x102
0x103
01234567
0x100
0x1010x1020x1036745
23
01
Big Endian
Little Endian
01
23
45
67
67
45
23
01
Slide39Representing Integers
Decimal:
15213
Binary:
0011 1011 0110 1101
Hex:
3 B 6 D
6D
3B0000IA32, x86-643B
6D
0000Sunint A = 15213;93C4
FF
FF
IA32, x86-64
C4
93
FF
FF
Sun
Two’s complement
representation
int B = -15213;
long int C = 15213;
00
00
00
00
6D
3B
00
00
x86-64
3B
6D
00
00
Sun
6D
3B
00
00
IA32
Increasing addresses
Slide40Examining Data RepresentationsCode to Print Byte Representation of DataCasting pointer to unsigned char * allows treatment as a byte array
Printf
directives:
%p
:
Print pointer
%x
:
Print Hexadecimaltypedef unsigned char *pointer;void show_bytes(pointer start, size_t len){ size_t i; for (i = 0; i < len; i++) printf(”%p\t0x%.2x\n",start+i, start[i]); printf("\n");}
Slide41show_bytes Execution Example
int
a = 15213;
printf("int
a = 15213;\n");
show_bytes((pointer
) &a,
sizeof(int
));Result (Linux x86-64):int a = 15213;0x7fffb7f71dbc 6d0x7fffb7f71dbd 3b0x7fffb7f71dbe 000x7fffb7f71dbf 00
Slide42Representing PointersDifferent compilers & machines assign different locations to
objects
Even get different results each time run program
int
B = -15213;
int
*P = &B;
x86-64
SunIA32EFFFFB2CAC28
F5
FF3C1BFE82FD
7F
00
00
Slide43char S[6] = "
18213
";
Representing
Strings
Strings in C
Represented by array of characters
Each character encoded in ASCII format
Standard 7-bit encoding of character setCharacter “0” has code 0x30Digit i has code 0x30+iString should be null-terminatedFinal character = 0CompatibilityByte ordering not an issueIA32Sun31383231
33
003138323133
00
Slide44Address Instruction Code Assembly Rendition
8048365: 5b pop %ebx
8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx
804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx)
Reading Byte-Reversed Listings
Disassembly
Text representation of binary machine code
Generated by program that reads the machine codeExample FragmentDeciphering NumbersValue: 0x12abPad to 32 bits: 0x000012abSplit into bytes: 00 00 12 abReverse: ab 12 00 00
Slide45Integer C Puzzles
x < 0
((x*2) < 0)
ux
>= 0
x & 7 == 7
(x<<30) < 0ux > -1x > y -x < -yx * x >= 0x > 0 && y > 0 x + y > 0x >= 0 -x <= 0x <= 0 -x >= 0(x|-x)>>31 == -1ux >> 3 == ux/8x >> 3 == x/8x & (x-1) != 0int x = foo();int y = bar();unsigned ux = x;unsigned uy = y;Initialization
Slide46SummaryRepresenting information as bitsBit-level manipulationsIntegersRepresentation: unsigned and signedConversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Representations in memory, pointers, strings
Summary