程序代写代做代考 Java algorithm computer architecture SEC204

SEC204
Computer Architecture and Low Level Programming
Dr. Vasilios Kelefouras
Email: v.kelefouras@plymouth.ac.uk
Website:
https://www.plymouth.ac.uk/staff/vasilios -kelefouras
School of Computing (University of Plymouth)
Date
23/09/2019

2
Outline
 Positional Numbering Systems  Signed Integer Representation  Floating Point Representation  Character Codes

3
Basics (1)
 The bit is the most basic unit of information in a computer  Switching activity 0 or 1
 A Byte is a group of 8 bits
 A byte is the smallest possible addressable unit of computer storage
 The term, “addressable,” means that a particular byte can be retrieved according to its location in memory
 A word is a contiguous group of bytes, e.g., an integer uses 4 bytes  Word sizes of 4 or 8 bytes are most common

4
Kilo- (K) = 1 thousand = 103 and 210 Mega- (M) = 1 million = 106 and 220 Giga- (G) = 1 billion = 109 and 230 Tera- (T) = 1 trillion = 1012 and 240 Peta- (P) = 1 quadrillion = 1015 and 250 Exa- (E) = 1 quintillion = 1018 and 260 Zetta- (Z) = 1 sextillion = 1021 and 270 Yotta- (Y) = 1 septillion = 1024 and 280
Normally, powers of 2 are used for measuring capacity
Basics (2)
Milli- (m) = 1 thousandth = 10-3 Micro- (μ) = 1 millionth = 10-6 Nano- (n) = 1 billionth = 10-9 Pico- (p) = 1 trillionth = 10-12

5
Basics (3)
 Hertz = clock cycles per second (frequency)
 1MHz = 1,000,000Hz
 Processor speeds are measured in MHz or GHz
 Byte = a unit of storage
 1KB = 210 = 1024 Bytes
 1MB = 220 = 1,048,576 Bytes
 1GB = 230 = 1,099,511,627,776 Bytes
 Main memory (RAM) is measured in GB
 Disk storage is measured in GB for small systems, TB (240) for large systems

6
POSITIONAL NUMBERING SYSTEMS (1)
 Positional numbering systems are systems in which the placement of a digit in connection to its intrinsic value determines its actual meaning in a numeral string
 The organization of any computer depends considerably on how it represents numbers, characters, and control information
 There are several positional numbering systems such as Decimal, Binary, Octal, Hexadecimal etc
 The positioning system is provided as a subscript, e.g., 1410, 101012, 8216

7
POSITIONAL NUMBERING SYSTEMS (2)
 Our decimal system is the base-10 system. It uses powers of 10 for each position in a number
 The binary system is also called the base-2 system
 The hexadecimal system is the base-16 system
 The Mayan and other Mesoamerican cultures used a number system based in a base-20 system

8
Decimal System
 Decimal system: Our well known and used system.

 It uses 10 different digits: 0,1,2,3,4,5,6,7,8,9
 Our decimal system is the base-10 system. It uses powers of 10 for each position in a number
 For example, the decimal number 947 in powers of 10 is 947 =
=9×100 + 4×10 + 7×1 = =9×102 + 4×101 + 7×100
 70216=7×10000+0x1000+2×100+1×10+6×1= =7×104+0x103+2×102+1×101+6×100
The decimal number 3812.46 in powers of 10 is (3×103 + 8×102 +1×101+2×100+ 4×10-1+ 6×10-2)

9
Binary System
 A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically 0 (zero) and 1 (one)
 The base is 2
 2 different digits are used: 0,1
 For example, 1012= 1×22 +0x21 +1×20
= 1×4+0x2+1×1
= 510
 The binary number 11001 in powers of 2 is: 1×24 + 1×23 + 0x22 +
0x21 + 1×20 = 16 + 8 + 0 + 0 + 1 = 2510
 1011.1012 =
= 1×23 +0x22 +1×21+1×20 +1×2-1 +0x2-2 +1×2-3 =
= 1×8+0x4+1×2+1×1+1×0.5+0x0.25+1×0.125 =11.62510

10
Octal system
 The base is 8
 8 different digits are used only: 0,1,2,3,4,5,6,7
 For example: 4368 = 4×82 +3×81 +6×80
= 4×64+3×8+6×1 = 28610
Convert the following octal number 205.248,to decimal:
205.248=2×82 +0x81 +5×80 + 2×8-1 + 4×8-2
= 2×64 + 0 + 5 + 2×0.125 + 4×0.015625 = 133.312510

11
Hexadecimal system
 The base is 16
 16 different digits are used: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (we do not use numbers with 2 digits like 10,11,12,…), but
Α instead of 10, B instead of 11, C instead of 12, etc)
 Example: 3Β116 = 3×162 +11×161 +1×160
= 3×256+11×16+1 = = 768 + 176 + 1=
= 94510
Convert the following hexadecimal number 20C.216 to decimal
20C.216= 2×162 + 0x161 + 12×160 + 2×16-1= =2×256 + 0 + 12×1 + 2×0.0625= =512 + 12 + 0.125=
=524.12510

12
In the Lab session
 You will learn how to convert from a system to another…

13
Positional Numbering Systems – General case
 Base: r
 Uses r different digits: 0,1,2,3,..r-1
 Νr =An-1 An-2…A1 A0 A-1 A-2…A-(m-1)A-m
Νr =An-1 xrn-1 +An-2 xrn-2 +….+A1 xr1 +A0 xr0 +A-1 xr-1 +A-2 x
r -2 +…+A-(m-1) x r -(m-1) +A-m x r -m
To better understand the above formula consider that if 234.035=?10 then
n=3, m=2 and r=5
 The left most digit (An-1) is called Most Significant Bit-(MSB) while the right most (A-m ) Least Significant Bit-(LSB)

14
Basic arithmetic operations
 The basic arithmetic operations are applied to all the previous numerical systems. There are:
 Addition
 Subtraction  Multiplication  Division
 Examples are provided in the lab session…

15
Signed integer representation
Introduction
 In practice we have to use negative binary numbers too. We need to
define signed binary numbers
 There are three ways in which signed binary integers may be expressed:
1. Signed magnitude
2. One’s complement
3. Two’s complement

16
Signed Magnitude Representation (1)
 Allocate the high-order (leftmost) bit to indicate the sign of a number
 The high-order bit is the leftmost bit. It is also called the most significant bit  0 is used to indicate a positive number; 1 indicates a negative number
 The remaining bits contain the value of the number
 Note that we also pay attention to the number of bits used to represent signed
binary numbers
 i.e. if using 4 bit numbers, then we use 00012 rather than 12
 In an 8-bit word, signed magnitude representation places the absolute value of the number in the 7 bits to the right of the sign bit
For example:
+3 is: 00000011 – 3 is: 10000011

17
Signed Magnitude Representation (2)
 The “binary addition algorithm” does NOT work with sign-magnitude
0 1
0 1 1
0 1 12 = 310 1 0 02 = -410
01 1 + 10 0
1 1 1 this is wrong

18
Signed Magnitude: intuitive for humans, difficult for computers
 Signed magnitude representation is easy for people to understand, but it requires complicated computer hardware
 Also it allows two different representations for zero: positive zero and negative zero
 As such, computer systems employ complement systems for signed number representation

19
Signed Integer Representation Complement Systems
 In binary systems, these are:
 One’s Complement. To represent negative values, invert all the bits in the binary
representation of the number (swapping 0s for 1s and vice versa)  1 becomes 0 and 0 becomes 1
 To represent positive numbers no change is applied
For example, using 8-bit one’s complement representation
+ 3 is: 00000011 – 3 is: 11111100
More examples
Χ=11011100, 1C(X)=00100011
Χ=1011, 1C(X)=?
 One’s complement still has the disadvantage of having two different
representations for zero: positive zero and negative zero
 In addition positive and negative integers need to be processed separately
 Two’s complement solves this problem
 Two’s complement
 One’s Complement add 1

20
Signed Integer Representation Two’s Complement
Two’s complement 2C(X)
 You represent positive numbers, just like the unsigned numbers
 To represent negative values, start with the corresponding positive
number, invert all the bits. Then add 1
 For example, using 8-bit two’s complement representation:
+ 3 is: 00000011
11111100 + 1
– 3 is: 11111101
1.Start with positive number 2.Invert bits
3. Add 1
-3 in 8-bit Two’s Complement Representation is 11111101
Negative numbers must always start with ‘1’
Both positive and negative numbers must have the same number of bits

21
Floating-Point Representation (1)
 To represent real numbers with fractional values, floating-point representation is used
 Floating-point numbers are often expressed in scientific notation  For example: 0.125 = 1.25 × 10-1
 Remember that when a number is multiplied by its base, e.g., 10, then we add a zero or we move the ‘,’ by one position to the right
 235×10 = 2350  1.345×10=13.45
 1102×2 = 11002 (6×2=1210)
 101.112×2=1011.1 (5.75×2=11.510)

22
Floating-Point Representation (2)
 Computers use a form of scientific notation for floating-point representation
 Single Precision floating point format 32-bit
 Double Precision floating point format 64-bit
 Numbers written in scientific notation have three components:

23
Sign-S 1-bit
Exponent-E 8 – bits
Mantissa (Fraction) – F 23 – bits
Single precision Floating-Point format (1)
A binary number is represented in FP format as follows:
1. We write the number using only a single non-zero digit before the radix point : e.g., 1011010010001=1.011010010001 x 212
1101.10111 = 1.10110111 x 23
2. Then we transform the number to the following format using 32 bits Ν = (-1)S (1+F)(2E-127)
S: Sign, 0/1 for positives/negatives, respectively
Ε: Exponent. E-127=exp, where exp is the corresponding exponent F: Significant or Mantissa. We write the fractional part in 23 bits
E=127+exp in order to avoid using negative numbers. exp=[-127,128] and therefore E=[0,255] – 255 needs 8 bits

24
0
10001011
01101001000100000000000
Single precision Floating-Point format (2)
Convert the positive number Ν=1011010010001 in Floating point format
Step1: 1011010010001= 1.011010010001 x 212 Step2: Ν = (-1)S (1+F)(2E-127)
S = 0 (positive number)
Ε – 127 = 12, and thus Ε = 13910 and Ε = 100010112 F = 01101001000100000000000
Therefore Ν in FP format is:

25
1
10010001
10001110001000000000000
Single precision Floating-Point format (3)
Suppose that the 32-bit floating-point representation pattern is the following. Find the binary number
S is 1 and thus the number is negative
Ε is 10010001 = 14510, and thus the exponent is exp=Ε-127=145-
127=18
F= 10001110001000000000000
Ν = (-1)S (1+F)(2E-127)
Ν is (-1)1 x 1.10001110001000000000000×218 or Ν = – 1100011100010000000

26
Floating-Point Representation (1)
 No matter how many bits we use in a FP representation, the model is finite
 The real number system is, of course, infinite, so our models can give nothing
more than an approximation of a real value
 e.g., how to represent 33.33333333333333333333?
 At some point, every model breaks down, introducing errors into our calculations
 By using a greater number of bits in our model, we can reduce these errors, but we can never totally eliminate them

27
Why is 0.1+0.2 not equal to 0.3 in most programming languages?
 computers use a binary floating point format that cannot accurately represent a number like 0.110
 0.110 is already rounded to the nearest number in that format
 0.110 doesn’t exist in the FP representation
 0.110 is already rounded to the nearest number in that format, which results in a small rounding error
 This means that 0.110 is converted to a binary number that’s just very close to 0.110
 The error is tiny since 0.110 is 0.1000000000000000055511151231257827
 The constants 0.210 and 0.310 are also approximations to their true values
 So, 0.110 + 0.210 == 0.30000000000000004440892098500610

28
Character Codes
 So far, we have learnt how to represent numbers. How about text?
 To represent text characters, we use character codes
 Essentially, we assign a number for each character we want to represent
 As computers have evolved, character codes have evolved. Larger computer
memories and storage devices permit richer character codes
 Some of the character codes are
1. BCD
2. ASCII (American Standard Code for Information Interchange) (7 bits)
3. Extended ASCII (8-bits)
4. Unicode
5. and others
 A binary number of n bits gives 2n different codes
 For n=2 there are 22 =4 different codes, i.e., bit combinations {00, 01, 10, 11}

29
Binary Coded Decimal (BCD) code
 when numbers, letters or words are represented by a specific group of symbols, it is said that the number, letter or word is being encoded. The group of symbols is called as a code
 Binary Coded Decimal (BCD) code
 In this code each decimal digit is represented by a 4-bit binary
number
 BCD is a way to express each of the decimal digits with a binary code
 In the BCD, with four bits we can represent sixteen numbers (0000 to 1111)
25610 = 0010 0101 0110BCD And vise versa
0011 1000 1001BCD =38910

30
ASCII Code
 The most widely accepted code is called the American Standard Code for Information Interchange ( ASCII).
 The ASCII code associates an integer value for each symbol in the character set, such as letters, digits, punctuation marks, special characters, and control characters
 The ASCII table has 128 characters, with values from 0 through 127. Thus, 7 bits are sufficient to represent a character in ASCII

ASCII Code
31

32
Extended ASCII Characters
 ASCII was designed in the 1960s for teleprinters and telegraphy, and some computing
 The number of printable characters was deliberately kept small, to keep teleprinters and line printers inexpensive
 When computers and peripherals standardized on eight-bit bytes, it became obvious that computers and software could handle text that uses 256-character sets at almost no additional cost in programming, and no additional cost for storage
 An eight-bit character set (using one byte per character) encodes 256 characters, so it can include ASCII plus 128 more characters
 The extra characters represent characters from foreign languages and special symbols for drawing pictures

A set of codes that extends the basic ASCII set. The extended ASCII character set uses 8 bits, which gives it an additional 128 characters
33

34
UNICODE
 Many of today’s systems embrace Unicode that can encode the characters of every language in the world
 The Java programming language, and some operating systems now use Unicode as their default character code
 UTF-8 (8-bits: essentially the extended ASCII Table)
 UTF-16 (16 bits: Most spoken languages in the world, widely
used)
 UTF-32 (32 bits: includes past languages, space inefficient)

35
Any questions?