3: Storing Values - School of Computer Science

advertisement
From last time
Describe in detail what happens when the following ARM
program is obeyed. At each step, clearly describe the
movement of information (both numbers and instructions)
between the memory (RAM) and the processor, and how the
values in the memory and in registers R0, R1, R2 and R15
(PC) change. Assume that the program starts at memory
location 0. (5 marks)
a
b
c
d
B
DEFW
DEFW
DEFW
LDR
STR
LDR
ADD
STR
SVC
COMP15111 Lecture 3
d
23
45
10
R0, a
R0, c
R1, b
R2, R1, R0
R2, a
2; terminate program
2/42
Answer
COMP15111 Lecture 3
3/42
COMP15111: Introduction to Architecture
Lecture 3: Storing Values
Dr. Javier Navaridas
School of Computer Science, University of Manchester
Autumn 2013
COMP15111 Lecture 3
4/42
Overview & Learning Outcomes
How are values represented inside a computer?
What different kinds of values can be used in a computer?
How big can a number get?
COMP15111 Lecture 3
5/42
How do computers work?
Answer depends on what we are talking about:
• Electronics Model → voltages, currents
• Physics Model → atoms, electrons
• Computational Model → more abstract meanings
Everything in a computer is composed of bits (binary digits)
Each bit can hold one of two values: 0 or 1
We use binary representations for values
COMP15111 Lecture 3
How are values represented inside a computer?
7/42
e.g. “Is today a Monday?”
2 answers: “Yes” or “No”
Represent using a single bit:
• “Yes” by 1
• “No” by 0
Why not swap them?
Convention: “Yes” or “True” is 1, “No” or “False” is 0
(Unless there is a good reason to change!)
COMP15111 Lecture 3
Representing 2 or 4 or ... different values
9/42
e.g. “What colour is this traffic light?”
Rl
Rl
Al
l
l
l
l
l
l
Al
Gl
l
4 answers: “Red”, “Red and amber”, “Green”, “Amber”
2 bits can take 4 different patterns: 00, 01, 10, 11
Here is one representation (from 4! = 24 possibilities):
• 00 is “Green”,
• 01 is “Amber”,
• 10 is “Red”,
• 11 is “Red and amber”.
(Can you see any reason why I might choose this
representation?)
COMP15111 Lecture 3
Representing 2 or 4 or ... different values
11/42
e.g. “What day of the week is it?”
7 answers: “Monday”, “Tuesday”, “Wednesday”, “Thursday”,
“Friday”, “Saturday”, “Sunday”
3 bits can take 8 patterns:
{000, 001, 010, 011, 100, 101, 110, 111}
Which seven patterns we are going to use?
Which pattern represents each day?
• 001 is “Monday”,
• 010 is “Tuesday”,
• 011 is “Wednesday”,
• 100 is “Thursday”,
• 101 is “Friday”,
• 110 is “Saturday”,
• 111 is “Sunday”.
• 000 is unused (error?)
COMP15111 Lecture 3
Representing 2 or 4 or ... different values
12/42
e.g. “What day of the month is it?”
Answers: 1 to 28, or 1 to 29, or 1 to 30, or 1 to 31
Assume 1 to 31 will do.
• 1 bit gives us 2 different patterns,
• 2 bits allow up to 4 different patterns,
• 3 bits allow up to 8 different patterns.
• n bits allow up to 2n different patterns.
24 = 16, 25 = 32
so 5 bits can represent 31 different days (1 pattern unused)
(e.g. represent day 1 by integer number 1 etc. – later)
COMP15111 Lecture 3
Representing 2 or 4 or ... different values
13/42
Question
Our student registration numbers used to be a year number
(e.g. “009” for 2009) followed by 5 more decimal digits.
What was the maximum number of students that we could
register in one year?
How many bits are needed to number this many students?
How many bits are needed to count the current Human
population of Earth?
COMP15111 Lecture 3
Representing 2 or 4 or ... different values
14/42
Characters
How many different characters are there?
A to Z is 26
a to z is another 26
0 to 9 is 10
+ - * / ( ) < = > is 9
. , : ; ? ! ‘ ’ is 8
space and newline make 2 more,
etc. – that is already over 80 characters
26 = 64, 27 = 128, so try using 7 bits
COMP15111 Lecture 3
Representing Characters
17/42
ASCII
An early 7-bit standard: 128 English and American characters
• A is represented by 1000001
• B is represented by 1000010
• Z is represented by 1011010
• a is represented by 1100001
• b is represented by 1100010
• z is represented by 1111010
• 0 is represented by 0110000
• 9 is represented by 0111001
• + is represented by 0101011
• . is represented by 0101110
(There have been others, e.g. EBCDIC.)
COMP15111 Lecture 3
Representing Characters
18/42
More characters!
 o o̧ o. o o̊
e.g. European accents: ò ó ô ö õ ō ȯ ŏ ǒ ő o
¯
e.g. Greek letters α β γ δ etc.
e.g. Japanese, Chinese, Arabic etc.
byte = 8 bits
ISO 8859: ASCII + 128 extra (mainly European) characters
Alternatives: 8859-1, 8859-2 . . . 8859-15
(Linux uses 8859-1 – Latin 1)
ISO 8859-1 extended to Unicode (ISO 10646) used by Java
(various) 16-bit and longer encodings
COMP15111 Lecture 3
Representing Characters
19/42
Question
“FAT” filesystems use 3 characters to show type of file
(e.g. .txt, .htm, .mp3).
Each character can be a digit or a letter (ignoring case).
How many different file types can be encoded?
How many bits would be needed to record this information?
As each character is a byte, how much memory is wasted?
COMP15111 Lecture 3
Representing Characters
20/42
Integers
How many bits?
• Mathematically: no maximum size
• Efficiency: pick some standard size(s)
Tradeoff between useful range and available space:
8
16
24
32
64
bits: (e.g. 0 to 255)
256
bits:
65,536
bits:
16,777,216
bits:
4,294,967,296
bits: 18,446,744,073,709,551,616
values
values
values
values
values
8, 16, 24, (and many other sizes) have been used.
Nowadays, mostly 32 bits (some 64 bits)
Java: integers = 32 bits
COMP15111 Lecture 3
Representing Numbers
23/42
Pick a bit pattern for each (positive) integer value
Theoretically, anything possible
In practice, only one representation: base 2
• can encode arbitrary numbers
• can build simple arithmetic hardware
0000000000000000 = zero
0000000000000001 = one
0000000000000010 = two
0000000000000011 = three
0000000000000100 = four
0000000000001010 = ten
0000000001100100 = one hundred
0000001111101000 = one thousand
0010011100010000 = ten thousand
COMP15111 Lecture 3
Representing Numbers
24/42
Positional significance
Decimal
123 = (1 × 100) + (2 × 10) + (3 × 1)
= (1 × 102 ) + (2 × 101 ) + (3 × 100 )
Where 10 is the base.
The N th digit – counting from 0 on the right – is ×10N
Binary
011010 = (0 × 32) + (1 × 16) + (1 × 8) + (0 × 4) + (1 × 2) + (0 × 1)
= (0 × 25 ) + (1 × 24 ) + (1 × 23 ) + (0 × 22 ) + (1 × 21 ) + (0 × 20 )
Where 2 is the base.
The N th digit – counting from 0 on the right – is ×2N
With N bits normally represent 0 to (2N − 1)
COMP15111 Lecture 3
Representing Numbers
(e.g. 0-255)
25/42
Decimal → Binary
e.g. Binary for 10010
Repeatedly divide by 2 and remember each remainder:
100 ÷ 2 = 50 remainder 0 →
0
50 ÷ 2 = 25 remainder 0 →
00
25 ÷ 2 = 12 remainder 1 →
100
12 ÷ 2 = 6 remainder 0 →
0100
6 ÷ 2 = 3 remainder 0 → 00100
3 ÷ 2 = 1 remainder 1 → 100100
1 ÷ 2 = 0 remainder 1 → 1100100
10010 = 11001002
(last remainder = left-most bit)
For more bits: 0 ÷ 2 = 0 remainder 0
1100100 = 01100100 = 0000000001100100 etc.
COMP15111 Lecture 3
Representing Numbers
26/42
Binary → Decimal
e.g. Decimal for 11001002
Multiply each bit (0 or 1) by its power of two and sum:
0 × 20 = 0× 1 = 0
0 × 21 = 0× 2 = 0
1 × 22 = 1× 4 = 4
0 × 23 = 0× 8 = 0
0 × 24 = 0×16 = 0
1 × 25 = 1×32 = 32
1 × 26 = 1×64 = 64
total = 100
COMP15111 Lecture 3
Representing Numbers
27/42
Question
Without using a calculator, convert the decimal number 4710 to
binary.
COMP15111 Lecture 3
Representing Numbers
28/42
Octal and Hexadecimal
Make binary numbers easier to read and write:
Group the bits:
threes: 1 100 100
fours: 110 0100
or
or
001 100 100
0110 0100
Then replace the groups of bits by a single digit:
threes → octal: 1448
fours → hexadecimal (hex): 6416
A or a = 1010 (ten)
C or c = 1100 (twelve)
E or e = 1110 (fourteen)
B or b = 1011 (eleven)
D or d = 1101 (thirteen)
F or f = 1111 (fifteen)
e.g. 1A5E16 = 0001 1010 0101 1110
= 0 001 101 001 011 110 = 0151368 = 151368
COMP15111 Lecture 3
Representing Numbers
29/42
Question
Without using a calculator, convert the binary number 1011112
to octal and hexadecimal.
COMP15111 Lecture 3
Representing Numbers
30/42
Question
Without using a calculator, convert the hexadecimal number 47
to octal and binary,
and then to decimal.
COMP15111 Lecture 3
Representing Numbers
31/42
Notations
The lab system assumes numbers are decimal
unless told otherwise:
• Hexadecimal: 0x1A5E or &1A5E or $1A5E
• Binary: 0b0001101001011110 or
:0001101001011110
• Octal: @777
Java:
• Hexadecimal: 0x1A5E
• Octal: 0777
COMP15111 Lecture 3
Representing Numbers
32/42
Negative numbers
For integers, most computers use two’s-complement
(see COMP12111)
e.g. 8-bit: 28 = 256 values
unsigned: 0 to 255
signed: –128 to +127
Essentially modulo 256 arithmetic: represent (-x) by (256 – x)
So the left-most bit is: 0=positive, 1=negative
So every positive number must start with at least one 0 bit!
COMP15111 Lecture 3
Representing Numbers
33/42
Powers of 10 and of 2
kilo
mega
giga
tera
1k = 1000=103
1M =
106
1G =
109
1T = 1012
milli
micro
nano
pico
1m = .001 = 10−3
1µ
= 10−6
1n
= 10−9
1p
= 10−12
COMP15111 Lecture 3
= 1 thousand
= 1 million
= 1 US billion
= 1 UK billion
≈ 210
≈ 220
≈ 230
≈ 240
=
1 024
=
1 048 567
=
1 073 741 824
= 1 099 511 627 776
= 1 thousandth
= 1 millionth
= 1 US billionth
= 1 UK billionth (= 1 US trillionth)
Representing Numbers
34/42
Other representations
Remember: bit patterns can be used to represent anything you
choose.
Integers – signed and unsigned – are one common use
The other common representation is floating point –
the details of which are outside the scope of this module.
Quick (decimal) illustration:
‘1234’ may represent:
‘one thousand two hundred and thirty four’
but could be: 1.23×104
(It’s all a matter of interpretation.)
COMP15111 Lecture 3
Representing Numbers
35/42
Question
In Britain, new car registration numbers are of the form
“LLDD LLL”
– L = any capital letter except “I”,
– D = any decimal digit (“DD” changes every 6 months)
What is the maximum number of cars that can be registered in
each 6 months?
How many bits would be needed to number this many cars?
COMP15111 Lecture 3
Representing Numbers
36/42
Summary of key points
Binary values:
– 1 bit can represent a 0 or a 1
– n bits can represent any one of 2n different values
– Octal (base 8) & Hexadecimal (base 16) notations
Character sets:
– 7-bit (ASCII) and 8-bit (ISO-8859)
– indeterminate-sized (Unicode), many encodings (e.g. UTF-8)
Numbers: representation & arithmetic
– don’t look-up, calculate: base-2
– signed (int): top bit 0=+ve, 1=–ve [2’s-complement (modulo)]
[fractions/decimals (float): IEEE-754]
Powers of 10; SI prefixes (k, M, G, T ...; m, µ, n, p ...)
Powers of 2; IEC prefixes (Ki, Mi, Gi, Ti, ...)
COMP15111 Lecture 3
Representing Numbers
37/42
Your Questions
COMP15111 Lecture 3
Representing Numbers
38/42
Glossary
Bit
Byte
Representation
Character-set
ASCII
ISO 8859
Unicode, UTF-8
Number
Integer
Notation
Decimal
Binary, Octal, Hexadecimal (Hex)
Two’s-complement
pico, nano, micro, milli . . . kilo, mega, giga, tera, peta
Floating-point
COMP15111 Lecture 3
Representing Numbers
39/42
For next time
How is a bit used to represent a simple decision, like the
answer to “Is this question hard”? (1 mark)
Without using a calculator, and briefly explaining how you do it,
convert the decimal number 97 to binary, and then from binary
to octal and to hexadecimal. (3 marks)
COMP15111 Lecture 3
Representing Numbers
40/42
Exam Questions
Without using a calculator, & briefly explaining how, convert:
– binary 0101 1010 to hexadecimal & octal (2 marks)
– binary 010 010 011 to decimal (1 mark)
– decimal 165 (or 123, 90, 121, 101) to binary, octal &
hexadecimal (3 marks)
– hexadecimal 12A to binary, octal & decimal (3 marks)
How many address bits are required to address 32 Mbyte of
byte-addressable memory? (2 marks)
A printer has an ARM processor with both ROM and RAM
memory. The 8 Mbyte ROM starts at address 0. The 64 Mbyte
RAM is located immediately after the ROM. What are the
lowest and highest (hexadecimal) RAM address? (2 marks)
Describe and compare different ways that have been used to
represent characters as values in a computer. (8 marks)
COMP15111 Lecture 3
Representing Numbers
41/42
Reading
man pages for ASCII, ISO 8859 1, unicode, UTF-8 etc.
(e.g. run the command man ascii on Linux)
http://unicode.org/
http://en.wikipedia.org/wiki/SI prefix
http://en.wikipedia.org/wiki/Binary prefix
COMP15111 Lecture 3
Representing Numbers
42/42
Download