Day 8
I. Last Time:
Hw #2 Due Tomorrow by 2pm in my Mailbox (CS342)
A. Binary Numbers & Sign
1. Memory is an array of bytes
1 byte = 8 bits = 2 hex digits
= 2 nybbles
2. Sign representation:
a. Sign Bit
b. Binary Offset / Biased Binary
c. 2's Compliment
2's comp. is used because
we only need the same 4 rules
used to add unsigned numbers
(Also gives us subtraction)
Later we'll worry about rules for
detecting overflow.
II. New Stuff:
A. Finish Type Promotion / Sign Extension
Sign Extension of Unsigned: Pre-Pend zeros
Sign Extension of Signes: Pre-Pend "sign" bit
B. 2's compliment short cut:
1. Long way: Invert ALL bits and add 1.
(How the computer will do it)
2. Short Cut: (For us)
Find the rightmost ONE and flip
everything to its LEFT (don't flip it)
C. ASCII - American Standard Code for Information Interchange
1. A format for storing characters/symbols/language
Basically a set of symbols is assigned to each unique 8-bit
(byte) number.
Basicaly you can think of ASCII as a simple substuition code...
Where you merly substitute every 8-bit number with the
appropriate symbol.
Ex: What is: 0x48656c6c 0x6f20576f 0x726c6400 = "Hello World\0"
Convert each BYTE = 0x48 = 72d = 'H', 0x65 = 101d = 'e' ...
The string is terminated by a NULL (a 0) - Often used as a
special character to represent the "end" of the string.
Other Special Characters:
Carriage Return: 0x0d, Line Feed: 0x0A, Form Feed, Bell, etc.
2. Neat ASCII Tricks/Features
a. Cases differ by 32d = 0x20h
I.e. by "ignoring" a bit, we can ignore case.
(Or adding 32 to make uppers into lowers,
subtract 32 to make lowers into uppers)
b. Numbers and letters are sequential with no gaps
'5'-'0' = 53d-48d = 5d.
This is a commonly used "conversion" trick
(Esp. in functions like "atoi")
3. Numbers in ASCII are difficult to work with
1. Math is difficult
2. Storage requires more space
A 32-bit number stores 0-9999 in ASCII
and 0-4 BILLION in the (unsigned) integer format
4. Alternatives: EBCIDIC, Unicode, etc.
D. Endian-ness
We think of computer memory as an array of bytes!!!
(I can't emphasize this enough!)
1. How does a computer memory work:
a. Can retrieve whole words - working with numbers
b. Can also retrieve individual bytes - working with ASCII
If we store a word and retrieve a byte, which end is which
2. BIG-Endian: The "BIG" end of the word comes first in memory
Many Workstations
3. LITTLE-Endian: The "Little" end of the word comes first in memory
Intel Based Machines
4. SPIM - Conforms to the endianess of the underlying machine
5. What this REALLY means to US - Sometimes Memory Dumps look BACKWARDS
(As in one of the homework problems)
E. IEEE-754 Floating Point Numbers
1. Why FP?
a. Represent "Big" and "Small" Numbers
b. Problems: Limited numbers of "significant" digits
2. How's it work:
32-bit representation, range from +/- 2^127->2^(-127)
(Very LARGE to Very Small numbers of both signs)
(But NOT EVERY Number - some numbers just can't be represented...
In reality computers aren't all that good at math!)
1. How do we represent fractional values in Binary?
Just like in decimal (essentially)
1.5 = 1 1/2 = 1*2^0+1*2^(-1) = 1.1
Conversion: Convert Integer Part, then Convert fractional Part
Ex: 4.125 = 100.001
.125*2 = 0.25
.25*2 = 0.5
.5*2 = 1.0
Much like the division technique, we can use a repreated
multiplication technique to determine the binary decimal.
(Repeatedly multiply by two until there is no decimal remaining,
If use the "1's" digit going down as the exponent)
Ex. 0.6875
2*.6875 = 1.375
2*.375 = 0.750
2*.750 = 1.50
2*.50 = 1.0
0.6875d=0.1011b = 2^-1+2^-3+2^-4
This is one of the vital steps in finding IEEE 754 format.
2. IEEE-754 Components
Bits 31 30-23 22-0 (position of the bit from right)
# bits 1 8 23 (based on "power of two" idea)
Mean a b c
a. Sign Bit: Is the number positive or negative: 1 bit 1=neg
b. Exponent: An exponent to determine the magnitude of the numebr: 8-bits
c. Significand: The "digits" of the number: 23 bits
3. Conversion to IEEE-754
Uses Scientific Notation 11.01 = 1.101 *2^1
By this "normalizatin" process, we ALWAYS end up with a 1 before the decimal
(except for 0 - special case) and we therefore always assume a 1, but don't
bother to actually store it.
1. Sign bit - Sign of the number, 0=pos, 1=neg
2. Significand:
Convert value into binary and scientific notation
Store all but the leading one from the previous part
3. Exponent - use the exp from the preceeding part
Stored Exp = Real Exp + 127
Note that this is offset binary, but with a little different
"center" than the form we discussed earlier.
4. Convert bits to hex.
Ex: Convert -24.625 to IEEE-754
1. s = 1
2. 24 = 1 1000
.625 = .101
24.625=11000.101
Convert to sci not: 1.1000101*2^4
Significand=1000101
3. Stored Exp = 127+4 = 131 = 1000 0011
1 1000 0011 1000 1010 0000 0000 0000 0000...
1100 0001 1100 0101 0000 0000 0000 0000 0...
0xC1C50000
4. Conversion FROM IEEE-754:
Real Exp = Stored Exp-127
Value = -1^(sign bit)*(1+significand)*2^Real Exp
Ex: Convert 0xBF400000 to Decimal from IEEE-754
1 0111 1110 100 0000...
Sign = 1 = neg
Stored Exp = 0111 1110b = 126
Real Exp = 126-127=-1
Significand = 1.1b = 1.5
1.5^2^-1 = 0.75.
Final Result=-0.75
NOTE: This is an approximation - many numbers can NOT be
exactly represented - they are "rounded" or "truncated"
(This is one of the sources of greif in 228 - numerical methods)
(A lot like 1/3 in decimal, many numbers don't "terminate")
Ex: What is 0.1 in binary?
0.1*2 = 0.2
*2 = 0.4
*2 = 0.8
*2 = 1.6
*2 = 1.2
*2 = 0.4 (Look - we're repeating)
F. MIPS and Data Types:
MIPS uses a 32-bit word (MIPS is built to work with 32 bits of
data at a time)
Question: What is the meaning of 0x476F6F64
Answer: What type of data is it?
1. ASCII: "Good"
2. Unsigned 32-bit Big Endian:
= 4+6*16+15*16^2+6*16^3+15+16^4+6*16^5*7*16^6+4*16^7
= 738,872,887,701,363
3. 32-bit Signed 2's Compliment Big Endian:
It's positive - Same as above
4. Unsigned 32-bit Little Endian:
REAL order: 0x646F6F47
= 7+4*16+15*16^2+6*16^3+15+16^4+6*16^5*4*16^6+6*16^7
= 422,214,075,772,758
5. 32-bit Signed 2's Compliment Little Endian:
It's positive - Same as above
6. As IEEE-754 Floating Point (Big Endian):
0100 0111 0110 1111 0110 1111 0110 0100
Sign = 0 = Positive
Stored Exponent = 1000 1110 = 8*16+14 = 142.
Real Exponent = Stored - 127 = 15
Significand = 1.110 1111 0110 1111 0110 0100
1+2^-1+2^-2+2^-4+2^-5+2^-6+2^-7+2^-9+2^-10+2^-12+
2^-13+2^-14+2^-15+2^-17+2^-18+2^-21
= 1.87058687210083007812
Result = significand*2^Real Exponent =
= 1.87058687210083007812*2^15
= 61295.39062499999999983616
III. Next Time:
A. Chapter Review
B. Quiz #1