ee3376-information · Electronic Representation of a Bit ! Relies only on approximate physical...
Transcript of ee3376-information · Electronic Representation of a Bit ! Relies only on approximate physical...
-
Information
EE3376
-
Topics to Cover…
l Binary Digital System l Data Types l Conversions l Binary Arithmetic l Overflow l Logical Operations l Fixed Point l Floating Point l ASCII Characters
Adapted from notes from BYU ECE124
2
-
What are Decimal Numbers?
l “Decimal” means that we have ten digits to use in our representation
– the symbols 0 through 9 l What is 3,546?
– 3 thousands + 5 hundreds + 4 tens + 6 ones. – 3,54610 = 3×103 + 5×102 + 4×101 + 6×100
l How about negative numbers? – Use two more symbols to distinguish positive and
negative, namely, + and -.
Adapted from notes from BYU ECE124 Adapted from notes from BYU ECE124
3
-
What are Binary Numbers?
l “Binary” means that we have two digits to use in our representation
– the symbols 0 and 1 l What is 1011?
– 1 eights + 0 fours + 1 twos + 1 ones – 10112 = 1×23 + 0×22 + 1×21 + 1×20
l How about negative numbers? – We don’t want to add additional symbols – So…
Adapted from notes from BYU ECE124
4
-
Electronic Representation of a Bit
l Relies only on approximate physical values. – A logical ‘1’ is a relatively high voltage (2.4V - 5V). – A logical ‘0’ is a relatively low voltage (0V - 1V).
n Analog processing relies on exact values which are affected by temperature, age, etc. n Analog values are never quite the same. n Each time you play a vinyl album, it will sound a bit different. n CDs sound the same no matter how many times you play them.
Adapted from notes from BYU ECE124
5
-
The Power of the Bit…
l Bits rely on approximate physical values that are not affected by age, temperature, etc. – Music that never degrades. – Pictures that never get dusty or scratched.
l By using groups of bits, we can achieve high precision.
– 8 bits => each pattern represents 1/256. – 16 bits => each pattern represents 1/65,536 – 32 bits => each pattern represents 1/4,294,967,296 – 64 bits => each pattern represents 1/18,446,744,073,709,550,000
l Disadvantage: bits only represent discrete values l Digital = Discrete
Adapted from notes from BYU ECE124 6
-
Binary Nomenclature l Binary Digit: 0 or 1 l Bit (short for binary digit): A single binary digit l LSB (least significant bit): The rightmost bit l MSB (most significant bit): The leftmost bit l Data sizes
– 1 Nibble = 4 bits – 1 Byte = 2 nibbles = 8 bits – 1 Kilobyte (KB) = 1024 bytes – 1 Megabyte (MB) = 1024 kilobytes = 1,048,576 bytes – 1 Gigabyte (GB) = 1024 megabytes = 1,073,741,824
bytes Adapted from notes from BYU ECE124
7
-
What Kinds of Data? l All kinds…
– Numbers – signed, unsigned, integers, floating point, complex, rational, irrational, …
– Text – characters, strings, … – Images – pixels, colors, shapes, … – Sound – pitch, amplitude, … – Logical – true / false, open / closed, on / off, … – Instructions – programs, … – …
l Data type: – representation and operations within the computer
l We’ll start with numbers… Adapted from notes from BYU ECE124
8
-
Some Important Data Types
l Unsigned integers – only non-negative numbers – 0, 1, 2, 3, 4, …
l Signed integers – negative, zero, positive numbers – …, -3, -2, -1, 0, 1, 2, 3, …
l Floating point numbers – numbers with decimal point – PI = 3.14159 x 100
l Characters – 8-bit, unsigned integers – ‘0’, ‘1’, ‘2’, … , ‘a’, ‘b’, ‘c’, … , ‘A’, ‘B’, ‘C’, … , ‘@’, ‘#’,
Adapted from notes from BYU ECE124
9
-
Unsigned Integers
329 102 101 100
101 22 21 20
3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5
most significant
least significant
l What do these unsigned binary numbers represent? 0000 0110
1111 1010
0001 1000
0111 1100
1011 1001
n Weighted positional notation n “3” is worth 300, because of its position, while “9” is only worth 9
Adapted from notes from BYU ECE124
10
-
Unsigned Integers (continued…)
22 21 20 0 0 0 0 0 0 1 1 0 1 0 2 0 1 1 3 1 0 0 4 1 0 1 5 1 1 0 6 1 1 1 7
Data Types
Adapted from notes from BYU ECE124
11
-
Unsigned Binary Arithmetic
l Base 2 addition – just like base 10! – add from right to left, propagating carry
10010 10010 1111 + 1001 + 1011 + 1
11011 11101 10000
10111 + 111
carry
0 1 1 1 1 Adapted from notes from BYU ECE124
12
-
Signed Integers
l With n bits, we have 2n distinct values. – assign about half to positive integers (1 through 2n-1)
and about half to negative (- 2n-1 through -1) – that leaves two values: one for 0, and one extra
l Positive integers – just like unsigned – zero in most significant (MS) bit
00101 = 5 l Negative integers
– sign-magnitude – set MS bit to show negative 10101 = -5
– one’s complement – flip every bit to represent negative 11010 = -5
– MS bit indicates sign: 0=positive, 1=negative Adapted from notes from BYU ECE124
13
-
2’s Complement
l Problems with sign-magnitude and 1’s complement – two representations of zero (+0 and –0) – arithmetic circuits are complex
l How to add two sign-magnitude numbers? e.g., try 2 + (-3)
l How to add to one’s complement numbers? e.g., try 4 + (-3)
l Two’s complement representation developed to make circuits easy for arithmetic.
Adapted from notes from BYU ECE124
14
-
2’s Complement (continued…)
l Simplifies logic circuit construction because – addition and subtraction are always done using the
same circuitry. – there is no need to check signs and convert. – operations are done same way as in decimal
l right to left l with carries and borrows
l Bottom line: simpler hardware units!
Adapted from notes from BYU ECE124
15
-
2’s Complement (continued…)
l If number is positive or zero, – normal binary representation
l If number is negative, – start with positive number – flip every bit (i.e., take the one’s complement) – then add one
00101 (5) 01001 (9) 11010 (1’s comp) (1’s comp)
+ 1 + 1 11011 (-5) (-9)
10110
10111 Adapted from notes from BYU ECE124
16
-
2’s Complement (continued…) l Positional number representation with a twist
– the most significant (left-most) digit has a negative weight
– n-bits represent numbers in the range -2n-1 … 2n-1 - 1 l What are these?
0110 = 22 + 21 = 6
1110 = -23 + 22 + 21 = -2
0000 0110 1111 1010 0001
1000 0111 1100 1011 1001
0121 2222 −−− nn
Adapted from notes from BYU ECE124
17
-
Number Decimal Value Negated Binary Value011001110000111101001000
2’s Complement Negation
To negate a number, invert all the bits and add 1
6 1010
7 1001
0 0000
-1 0001
4 1100
-8 1000 (??)
Adapted from notes from BYU ECE124
18
-
Two’s complement – simple conversion
0001102 = 610 0001102 Find first “1” from the right 0000102 invert each digit to the left 0010102 0110102 1110102 = -610 until you reach the left end
To obtain negative 6, start with positive magnitude and negate
Adapted from notes from BYU ECE124
19
-
l Continually divide the number by 2 and track the remainders.
1 × 25 + 0 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 32 + 0 + 8 + 0 + 2 + 1 = 43
43 2
2
2
2
2
2
Decimal to Binary Conversion
n For negative numbers, do above for positive number and negate result
5 R 0
0
2 R 1
1 21 R 1
1
10 R 1
1
1 R 0
0
0 R 1
1
Adapted from notes from BYU ECE124
20
-
Number Binary Value56
12335
-351007
Decimal to Binary Conversion
0101
0110
01111011
00100011
11011101
01111101111
Adapted from notes from BYU ECE124
21
-
Sign-Extension in 2’s Complement
l You can make a number wider by simply replicating its leftmost bit as desired.
0110 = 000000000000000110 = 1111 = 11111111111111111 = 1 =
6
6
-1
-1 -1
n What do these represent?
Adapted from notes from BYU ECE124
22
-
Hexadecimal Notation
l Binary is hard to read and write by hand l Hexadecimal is a common alternative
– 16 digits are 0123456789ABCDEF
0100 0111 1000 1111 = 0x478F 1101 1110 1010 1101 = 0xDEAD 1011 1110 1110 1111 = 0xBEEF 1010 0101 1010 0101 = 0xA5A5
Binary Hex
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
0x is a common prefix for writing numbers which mean hexadecimal
1. Separate binary code into groups of 4 bits (starting from the right)
2. Translate each group into a single hex digit
Adapted from notes from BYU ECE124
23
-
Why Hexadecimal? l One Hexadecimal digit
represents four binary digits l Hexadecimal is more human
readable l Can quickly convert large
binary numbers to hexadecimal and back by inspection
l Nomenclature – Motorola = $64 – C = 0x64 – Intel = 64H – TI = 64h
Decimal Hexadecimal Binary 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
10 a 1010 11 b 1011 12 c 1100 13 d 1101 14 e 1110 15 f 1111 Adapted from notes from BYU ECE124 24
-
Binary to Hexadecimal – Easy!
1011
B
0001
1
1100
C
0101
5
So $b1c5 equals %1011000111000101.
25
-
Decimal to Hex Examples
12decimal = 1100 = 0xc
21decimal = 0001 0101 = 0x15
55decimal = 0011 0111 = 0x37
256decimal = 0001 0000 0000 = 0x100
47decimal = 0010 1111 = 0x2f
3decimal = 0011 = 0x3
127decimal = 0111 1111 = 0x7f
1029decimal = 0100 0000 0101 = 0x405
Adapted from notes from BYU ECE124
26
-
Word Sizes
l In the preceding slides, every bit pattern was a different length (15 was represented as 01111).
l Every real computer has a base word size – our machine (MPS430) is 16-bits
l Memory fetches are word-by-word – even if you only want 8 bits (a byte)
l Instructions are packed into words l Numeric representations are word-sized
– 15 is represented as 0000000000001111
Conversions
Adapted from notes from BYU ECE124
27
-
Number Wheel for 3 bit hardware
000
001
010
011
100
101
110
111
0 unsigned or signed
1 unsigned or signed
2 unsigned or signed
3 unsigned or signed
4 unsigned / -4 signed 5 unsigned / -3 signed
6 unsigned / -2 signed
7 unsigned / -1 signed
28
-
Number Wheel – Unsigned Overflow
000
001
010
011
100
101
110
111
0 unsigned or signed
1 unsigned or signed
2 unsigned or signed
3 unsigned or signed
4 unsigned / -4 signed 5 unsigned / -3 signed
6 unsigned / -2 signed
7 unsigned / -1 signed
6 + 3 = 9 (too large for 3 bits)
29
-
Number Wheel – Signed Overflow
000
001
010
011
100
101
110
111
0 unsigned or signed
1 unsigned or signed
2 unsigned or signed
3 unsigned or signed
4 unsigned / -4 signed 5 unsigned / -3 signed
6 unsigned / -2 signed
7 unsigned / -1 signed
3+2 = -5 (too large for 3 bits)
30
-
Condition Code Register
Bit Name Meaning after arithmetic N negative result is negative
Z zero result is zero
V overflow signed overflow
C carry carry or unsigned overflow
Condition code (CC) register inherently contains information about previous arithmetic or logical operation. Specific bits in the CC register are set under specific conditions.
31
-
Examples of 4 bit overflow
1100 0110 +
(1)0010 = 210
= 1210 = 610
= 210
= - 410 = 610
0100 0100 +
(0)1000 = 810
= 410 = 410
= - 810
= 410 = 410
1000 1000 +
(1)0000 = 010
= 810 = 810
= - 810 = - 810 = 010
0001 0001 +
(0)0010 = 210
= 110 = 110
= 110 = 110 = 210
Adapted from notes from BYU ECE124
32
-
Binary Multiplication
Multiplication Table 0 * 0 = 0 0 * 1 = 0 1 * 0 = 0 1 * 1 = 1
1210 = 610 =
1100 0110 * * 0000
1100 1100
0000 + 1001000 =7210
Adapted from notes from BYU ECE124
33
-
Introduction to moving data
l CPU can load and store data into internal registers for faster operations
l Done with first assembly instructions – mov.b and mov.w
mov.b #FFH, R5 mov.b R5, &P1OUT mov.w 2(R5), R6 mov.w @R5, 3(R6)
Adapted from notes from BYU ECE124
34
-
Logic review – basic gates
A Z 0 1 1 0
A B Z 0 0 0 0 1 0 1 0 0 1 1 1
A B Z 0 0 0 0 1 1 1 0 1 1 1 1
INV.W R5 R5 = ~R5;
BIS.B R6, R7 R7 = R6 || R7;
AND.W R6, R7 R7 = R6 && R7;
A B Z 0 0 0 0 1 1 1 0 1 1 1 0
XOR.W R6, R7 R7= R7 ^ R6;
A B Z 0 0 0 0 1 0 1 0 0 1 1 1
BIC.W R6, R7 R7 = !R6 && R7;
35
-
Logic review – example
BIS.B #08h, R5
BIC.B #08h, R5
C and Assembly code to set bit 3 of local variable - var1 in R5
C and Assembly code to clear bit 3 of local variable - var1 in R5
Var1 = Var1 && $F7;
Var1 = Var1 || $08;
36
-
Shift Operations
c
c 0
c
c
RRA.B
RLA.B
RRC.B
RLC.B
divide by 2 – signed
multiply by 2 – signed or unsigned C command X = Y
-
Shift Operations
Multiplying by a power of 2 is shifting to the left. M • 2n will shift binary number M left by N bits M / 2n = M • 2-n will shift binary number M right by N bits
When dividing, be careful about the most significant bit. If signed, the existing bit must be replicated for sign extension. If unsigned, the most significant bit must always be a zero
38
-
Encoders / Decoders
3 to 8 line decode
input 0 1 2 3 4 5 6 7 000 1 0 0 0 0 0 0 0 001 0 1 0 0 0 0 0 0 010 0 0 1 0 0 0 0 0 011 0 0 0 1 0 0 0 0 100 0 0 0 0 1 0 0 0 101 0 0 0 0 0 1 0 0 110 0 0 0 0 0 0 1 0 111 0 0 0 0 0 0 0 1
Encoder Decoder
39
-
Read Only Memories - ROM
Address Data Out 1 2 2 3 4 5 6 7
000 1 0 1 0 1 0 0 0 001 0 1 0 0 0 0 0 1 010 0 0 1 0 0 1 0 0 011 0 0 1 1 0 0 0 0 100 0 0 1 0 0 0 1 1 101 1 1 1 0 0 1 1 1 110 1 1 0 0 0 0 1 0 111 1 1 1 1 0 0 0 1
n
m
ROM
2n addresses addressed by n bit m output data bits
40
-
Read Only Memories - ROM
n
m
Memory array n to 2n decoder
2n
Address input word lines
data output
41
-
ROM memory array
pd pd pd pd pd pd pd pd pd pd word line 2N-1
word line 2N-2
word line 2
word line 1
word line 0
data m-1 data m-2 data0 data1 data 2
programmed “one”
programmed “zero”
bit lines
weak pull down
42
-
Random Access Memory (RAM)
wordline
bitline bitlineN
6 Transistor SRAM cell
n
m
sram array of 6-t cells n to 2n decoder
2n Address input
word lines m
rnw
data in
data out 43
-
Creating Fractions
l Fractions are created by using extra bits below your whole numbers.
l The programmer is responsible for knowing where the “decimal place” is.
l Move the decimal place by using the shift operator (>). – Shifting is multiplying by powers of 2. – Example:
l x > 5 = x × 2-5
44
-
Fractional Example
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 = 5
A/D Sample (10-bit)
Fractional part
Shift left by 6 (i.e. A2D
-
Fractional Example l We know 5/2 = 2.5
– If we used pure integers, 5/2 = 2 – Using a fixed-point fractional portion can recover the
lost decimal portion.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 = 5/2 = 2
A/D Sample (10-bit)
Fractional part
Whole part
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 = 5.0/2 = 2.5 1
46
-
Fixed Point
Fractional part
Whole part
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
§ By using a fixed-point fractional part, we can have 5/2 = 2.5 § The more bits you use in your fractional part, the more accuracy you will
have.
§ Accuracy is 2^-(fraction bits). § For example, if we have 6 bits in our fractional part (like the above example),
our accuracy is 2^-6 (2-6 = 0.015625. In other words, every bit is equal to 0.015625
47
-
Fixed Point Arithmetic
Adding 2.5 + 2.5 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
§ Fixed point addition:
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
+
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 = 5 0
§ Shift right to regain original resolution and data position.
§ Without fixed point math the result would have been 4 due to the truncation of the integer division. 48
-
Fixed Point Example #1
Cruise Control Specification range 0 – 100 MPH, resolution of 1 MPH two values: measured speed and target speed algorithm constantly calculates difference Format determination Use binary fixed point for ease of math (subtraction) Select range of 0 to 127 (27) to just exceed requirements Select resolution of 0.5 (2-1) to just exceed requirements 7 bits for integer and 1 bit for fractional = 8 bits precision
0 1 0 0 1 1 0 0
implied point
integer part fractional part
= 70.0 = K • Δp = 140 • 2-1
49
-
Fixed Point Example #2
A/D converter Specification range 0 – 5 V, resolution of 20 millivolts Format determination Select range of 0 to 7 (23) to just exceed requirements Select resolution of 0.015625 (2-6) to just exceed rqmts 3 bits for integer and 6 bit for fractional = 9 bits precision for 9 bits precision, need 16 bit register in HCS12
0 1 1 1 1 0 0 0
implied point
integer part fractional part
= 3.75 = K • Δp = 240 • 2-6 0
50
-
Floating Point Numbers
l Binary scientific notation l 32-bit floating point
l Exponent is biased l Implied leading 1 in mantissa
s exponent mantissa 1 8 23
1272.11 −××−= exponents fractionN
51
-
Floating Point Numbers
l What does this represent?
Positive number
Exponent is 128 which means the real exponent is 1
Mantissa is to be interpreted as 1.1 This is 20 + 2-1 = 1 + 1/2 = 1.5
The final number is 1.5 x 21 = 3
0 10000000 10000000000000000000000
52
-
ASCII Characters
NUL DLE SP 0 @ P ` p SOH DC1 ! 1 A Q a q STX DC2 “ 2 B R b r ETX DC3 # 3 C S c s EOT DC4 $ 4 D T d t ENQ NAK % 5 E U e u ACK SYN & 6 F V f v BEL ETB ‘ 7 G W g w BS CAN ( 8 H X h x HT EM ) 9 I Y i y LF SUB * : J Z j z VT ESC + ; K [ k { FF FS , < L \ l | CR GS - = M ] m } SO RS . > N ^ n ~ SI US / ? O _ o DEL
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8-9 a-f
53
-
Properties of ASCII Code l What is relationship between a decimal digit ('0', '1', …)
and its ASCII code? l What is the difference between an upper-case letter
('A', 'B', …) and its lower-case equivalent ('a', 'b', …)? l Given two ASCII characters, how do we tell which
comes first in alphabetical order? l What is significant about the first 32 ASCII codes? l Are 128 characters enough? (http://www.unicode.org/)
54
-
Data Representation – endian
l Byte Ordering – Little endian
l refers to system where bytes start in memory from small to large
l Intel and HP – Big endian
l refers to system where bytes start in memory from large to small
l IBM and Motorola (this class) – Biendian - goes either way – PowerPC
l Bit Ordering $00
$01
$02
$03
$12
$34
$56
$78
$12345678 $00
$01
$02
$03
$78
$56
$34
$12 big endian little endian
55
-
Data representation – BCD
1 2 : 4 5 0001 0010 : 0100 0101
l Binary Coded Decimal – useful for human readable displays (7 segment) – half carry bit for BCD arithmetic – values 1010 through 1111 are not legal – H CCR bit and DAA instructions used with BCD
56