CPS 104 Computer Organization and Programming Lecture-2: Data Representations
CPS 104 Computer Organization and Programming Lecture-2: Data Representations
CPS104 Lec2.1
GK Spring 2004
Circuit Design
Hardware
CPS104 Lec2.2
GK Spring 2004
Levels of Representation
temp = v[k]; High Level Language Program Compiler Assembly Language Program Assembler Machine Language Program
0000 1010 1100 0101 1001 1111 0110 1000
RegDst
Rw Ra Rb 32 32-bit Registers
ALU
Mux
32
Mux
Extender
imm16
32 ALUSrc
16
Data In 32 Clk
Data Memory
ExtOp
GK Spring 2004
SOFTWARE
software
instruction set
hardware
Interface
time
GK Spring 2004
Instruction Categories Load/Store Computational Jump and Branch Floating Point Memory Management Special
R0 - R31
PC HI LO
rs rs
rt rt
rd
sa
funct
immediate
jump target
GK Spring 2004
Summary
Goal Understand basic operation of a computer Why? Software performance is affected/determined by HW capabilities Future Computer Architects (Processor or System)
CPS104 Lec2.7
GK Spring 2004
Summary (Continued)
Agenda Map high-level software to instructions Instructions are composed of hardware primitives how to use them how to implement them why a particular primitive Memory for storing instructions and data Main memory Caches interaction with operating system Input/Output
CPS104 Lec2.8
GK Spring 2004
Summary (Continued)
All computers consist of five components Processor: (1) datapath and (2) control (3) Memory (4) Input devices and (5) Output devices Not all memory created equally Cache: fast (expensive, small) memory close to the processor Main memory: slower, cheaper, larger memory farther from processor Input and output (I/O) devices has the messiest organization Wide range of speed: graphics vs. keyboard Wide range of requirements: speed, standard, cost ... etc.
CPS104 Lec2.9
GK Spring 2004
Data Representation
Computers store variables (data) Typically Numbers and Characters or composition of these We reason about numbers many different ways The key is to use a representation that is amenable to hardware implementation and is efficient
CPS104 Lec2.10
GK Spring 2004
Data Representation
CPS104 Lec2.11
GK Spring 2004
Number Systems
A number is a mathematical concept 10 Many ways to represent a number 10, ten, 2x5, X, 100/10, |||| |||| Symbols are used to create a representation Which representation is best for addition and subtraction? Which representation is best for multiplication and division?
CPS104 Lec2.12
GK Spring 2004
Humans use decimal (base 10) digits 0-9 are composed to make larger numbers 11 = 1*101 + 1*100 weighted positional notation Addition and Subtraction are straightforward carry and borrow (today called regrouping) Multiplication and Division less so can use logarithms and then do adds and subtracts
CPS104 Lec2.13
GK Spring 2004
CPS104 Lec2.14
GK Spring 2004
Todays computers are built from transistors (Electronic Switches) A switch is either off or on. Need to represent numbers using only off and on
Use
Computer memory is organize in words. Each computer word has a fixed number of binary bits. Typical desktop computer uses 32-bit words. 32 bit computers operate on Bytes (8 bits), half words (16 bits), words (32-bits) and double words (64 bits).
GK Spring 2004
CPS104 Lec2.15
Binary representation
weighted positional notation using base 2 1110 = 1*23 + 1*21 + 1*20 = 10112 1110 = 8 + 2 + 1 What is largest number that can be represented, given 4 bits?
CPS104 Lec2.16
GK Spring 2004
N is a positive Integer (in decimal representation) bi i=0,...,k are the bits (binary digits) for the binary representation of N N = bk*2k ++b2*22 + b1*2 + b0 binary representation: bkb3b2b1b0 How do I compute b0? Compute binary representation of 11?
CPS104 Lec2.17
GK Spring 2004
Replace 2 by A and you have an algorithm that computes the base A representation for N
CPS104 Lec2.18
GK Spring 2004
Powers of 2
N
0 1 2 3 4 5 6 7 8 9 10
CPS104 Lec2.19
2n
1 2 4 8 16 32 64 128 256 512 1024 (1K)
Binary
00000000001 00000000010 00000000100 00000001000 00000010000 00000100000 00001000000 00010000000 00100000000 01000000000 10000000000
GK Spring 2004
Computers can input and output decimal numbers but they convert them to internal binary representation. Binary is good for computers, hard for us to read
Use numbers easily computed from binary Example: 120010 = 00000100101100002 Example: 120010 = 042608 Example: 120010 = 04B016 = 0x04B0 does not distinguish between upper and lower case
CPS104 Lec2.20
GK Spring 2004
Easy to convert Binary numbers To/From Octal. Group the binary digits in groups of three bits and convert each group to an Octal digit. 23 = 8
Bin. Oct. 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7
Example: 11 000 010 011 001 110 100 111 101 010 1012 3 0 2 3 1 6 4 7 5 2 58
CPS104 Lec2.21
GK Spring 2004
To convert to and from hex: group binary digits in groups of four and convert according to table 24 = 16
5 16
GK Spring 2004
CPS104 Lec2.23
GK Spring 2004
Binary Integers
Unsigned Integers:
CPS104 Lec2.24
GK Spring 2004
Add a sign bit Example: 0101102 = 2210 ; 1101102 = -2210 Advantages: Simple extension of unsigned numbers. Same number of positive and negative numbers. Disadvantages: Two representations for 0: 0=000000; -0=100000. Algorithm (circuit) for addition depends on the arguments signs.
CPS104 Lec2.25
GK Spring 2004
Key idea is to use largest positive binary numbers to represent negative numbers Obtain negative number by subtracting large constant i = -an-1*2n-1 + an-2*2 n-2 + ... a0*20
6-bit examples: 0101102 = 2210 ; 1010102 = -2210 010 = 0000002; 110 = 0000012; -110 = 1111112
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1
CPS104 Lec2.26
GK Spring 2004
2s Complement
Advantages: Only one representation for 0: 0 = 000000 Addition algorithm independent of sign bits. Disadvantage: One more negative number than positive : Example: 6-bit 2s complement number. 1000002 = -3210; but 3210 could not be represented
CPS104 Lec2.27
GK Spring 2004
CPS104 Lec2.28
GK Spring 2004
2s Complement (cont.)
CPS104 Lec2.29
GK Spring 2004
64-bit using gcc is long long 64-bit using Digital/Compaq compiler is long
To extend precision do sign bit extension precision is number of bits used to represent a number
Example 1410 = 0011102 in 6-bit representation. 1410 = 0000000011102 in 12-bit representation -1410 = 1100102 in 6-bit representation -1410 = 1111111100102 in 12-bit representation.
CPS104 Lec2.30
GK Spring 2004