Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
8/13/2014
1
1
Chapter 2Data Representation in
Computer Systems
2
Chapter 2 Objectives
• เขาใจการคานวณและการแทนขอมลตวเลขใน digitalcomputers.
•
•และการตดทอนตวเลข(overflow and truncation.)
3
Chapter 2 Objectives
•
• เขาใจความแตกตางระหวางการเกบขอมลในหนวยความจาและการสงขอมลออกไปตามสายสญญาณและการเกบในฮารดดสก
• เขาใจแนวคดการตรวจจบขอผดพลาดและรวบรวมรหส
4
2.1 Introduction
• bit (1) computer.– “on” หรอ “off” ในความหมายของ digital
circuit.– หรออาจใช “high” หรอ “low” voltage แทน “on” หรอ
“off..”• byte (1) 8 bits.
– A byteของคอมฯ( computer storage)
– คาวา “addressable” หมายถง การอาน/เขยนขอมลbyte ใดๆ ในหนวยความจาโดยการระบตาแหนงในหนวยความจา
5
2.1 Introduction
• word (1) เปนกลมของ byte– Word อาจอประกอบดวยจานวน bits หรอ bytes ตางๆกนได– ปกตนยมใช Word sizes of 16, 32 หรอ 64 bits
– word-addressable system จะใช 1 wordเปนหนวยเลกสดในการอางองในหนวยเกบ( unit of storage.)
• กลมของ 4 bits เรยกวา nibble ( nybble).– 1 Byte ประกอบดวย 2 nibbles โดยแบงเปนนบเบลสง
(high-order nibble) (low-order” nibble)
6
2.2 Positional Numbering Systems
• คาของBytes จะเปนตวเลขถามการคดคาโดยคาของแตละบตเปนผลคณประกอบกบเลข 2– binary system (base-2
system.)
– เลขฐานสบ เปนผลคณประกอบกบเลข 10 ของแตละหลก.
– จานวนใดๆสามารถแทนดวยเลขฐานbase (or radix)ใดๆกได
8/13/2014
2
7
2.2 Positional Numbering Systems
• decimal number 947 เปนผลคณประกอบกบ 10 คอ
• decimal number 5836.47 เปนผลคณประกอบกบ 10 คอ :
5 10 3 + 8 10 2 + 3 10 1 + 6 10 0
+ 4 10 -1 + 7 10 -2
9 10 2 + 4 10 1 + 7 10 0
8
2.2 Positional Numbering Systems
• binary number 11001 เปนผลคณประกอบกบ 2 คอ :
• เรากาหนดเลขฐานใดๆโดยใชสญลกษณตวหอย(subscript)
– 10 เนนเลขฐาน:
110012 = 2510
1 2 4 + 1 2 3 + 0 2 2 + 0 2 1 + 1 2 0
= 16 + 8 + 0 + 0 + 1 = 25
9
2.3 Decimal to Binary Conversions
• เพราะ binary numbers dataใน digital computer systems
• จะทาใหคณเขาใจการทางานของอปกรณประกอบตางๆรวมถง(instruction set
architectures)
10
2.3 Decimal to Binary Conversions
• การ ฐานโดยอาศยการหาร
•
• 0•
11
• Converting 190 to base3...–
– 3 มาหาร 190 ได63 1.
– ใสผลหารและเศษ
2.3 Decimal to Binary Conversions
12
• Converting 190 to base3...– หาร 63 ตอดวย 3.
– เหลอเศษ 0 ผลลพธ 21.
2.3 Decimal to Binary Conversions
8/13/2014
3
13
• Converting 190 to base3...––
คาตอบคอ:
19010 = 210013
2.3 Decimal to Binary Conversions
14
2.3 Decimal to Binary Conversions
• คาของเศษสวน(ทศนยม)จะใชการประมาณในการ• เชน ½ 2 และ 10 ได
3 ตองใชการประมาณ
15
2.3 Decimal to Binary Conversions• เลขทศนยมฐาน 10 คอเลขฐาน 10 0 ทางขวาของ
จดทศนยม• 0 ทางขวาของจด
ทศนยม• จานวนทางขวามอของจดทศนยมคอคาคณประกอบกบคาฐานยกกาลง
:
0.4710 = 4 10 -1 + 7 10 -2
0.112 = 1 2 -1 + 1 2 -2
= ½ + ¼= 0.5 + 0.25 = 0.75
16
• การใช multiplicationmethoddecimal 0.8125 เปนbinary ใชการคณดวยฐาน 2–
2.3 Decimal to Binary Conversions
17
• Converting 0.8125 tobinary . . .–
เตมหลกเรายงตงใชเศษสวนมาทาการคณดวยฐาน 2
2.3 Decimal to Binary Conversions
18
• Converting 0.8125 tobinary . . .–
ตองการแลว– :
0.812510 = 0.11012
–
2.3 Decimal to Binary Conversions
8/13/2014
4
19
2.3 Decimal to Binary Conversions
•digital computers.
• อยางไรกตาม จะอานยากมากเพราะหลกของตวเลขเยอะมากเชนคาของstring หรอแมคาตวเลขฐาน 10ฐาน 2 กจะยาวมาก– เชน: 110101000110112 = 1359510
• 16 มาแสดงแทนฐาน2
20
2.3 Decimal to Binary Conversions
• ระบบตวเลขฐาน 16 จะประกอบไปดวยตวเลข 0 ถง 9 และตวอกษร A ถง F– decimal number 12 = B16.– decimal number 26 = 1A16.
• 2 เปน ฐาน 16 เพราะ 16 = 24.• 2 เปน ฐาน 16 จะใชการ group ฐาน 2 digits
4 bit
กลม 4 bit จะเรยกวา “hextet”
21
2.3 Decimal to Binary Conversions
• groups of hextets, the binary number110101000110112 (= 1359510) in hexadecimal is:
• Octal (base 8) values are derived from binary byusing groups of three bits (8 = 23):
Octal computers ใชขนาดของword เปน 6 bit.22
2.4 Signed Integer Representation
•• ในการแสดงคาตวเลขลบ computer systems จะ
สารอง high-order bit– high-order bit byte หรอเรยกวา
most significant bit.
• สวนbit
23
2.4 Signed Integer Representation
• ม 3 วธในการแสดง :– Signed magnitude,– One’s complement– Two’s complement.
• เชน ใน 8-bit word คาของ signedmagnitude จะหมายถงคาของ 7 bits ทางขวามอของ sign bit.
24
2.4 Signed Integer Representation
• เชน 8-bit signed magnitude คาของ 3เปนบวก คอ: 00000011
• - 3 คอ : 10000011
• Computers จะทาการคานวณคาของ signedmagnitude numbersบนกระดาษ–
8/13/2014
5
25
2.4 Signed Integer Representation
• การบวกเลข Binary :0 + 0 = 0 0 + 1 = 11 + 0 = 1 1 + 1 = 10
•2 ไปยงวงจรดจตอล(digital circuit)ได– digital circuit 3
26
2.4 Signed Integer Representation
• Example:– การใช signed magnitude
binary arithmetic หาผลบวกของ 75 และ 46.
• 75 และ 46 เปนฐาน2 โดยแยก bitbitของจานวน.
27
2.4 Signed Integer Representation
• Example:– การใช signed magnitude
binary arithmetic หาผลบวกของ 75 และ 46.
• การบวกจะทาเหมอนการบวกฐาน 10 คอ
28
2.4 Signed Integer Representation
• Example:– การใช signed magnitude
binary arithmetic หาผลบวกของ 75 และ 46.
• Bitขางบน bit
29
2.4 Signed Integer Representation
• Example:– การใช signed
magnitude binaryarithmetic หาผลบวกของ75 และ 46
• Bit
30
2.4 Signed Integer Representation
• Example:– การใช signed magnitude
binary arithmetic หาผลบวกของ 75 และ 46.
• 8
8/13/2014
6
31
2.4 Signed Integer Representation
• Example:– การใช signed magnitude
binary arithmetic หาผลบวกของ 107 และ 46.
– เราจะเหนวามการทดออกจาก bit 7
107 + 46 = 25
32
2.4 Signed Integer Representation
•
กระดาษ– เชน: การใช signed
magnitude binaryarithmetic หาผลบวกของ -46 และ - 25.
•
33
2.4 Signed Integer Representation
• .– Example: การใช signed
magnitude binaryarithmetic หาผลบวกของ46 และ - 25.
•– Note มการยมการ bit 2 และ 7
34
2.4 Signed Integer Representation
•สรางตองสราง computer hardware
• 2แทนคา ศนย คอ: positive zero และ negativezero.
• computer hardware งายและไมสลบซบซอน
35
2.4 Signed Integer Representation
• ในระบบ complement systems คาลบ(negativevalues)
• ในการหาcomplement systems ของเลขฐานจานวนลบ จะ
ฐานอย 1• binary system การหา one’s
complement จงเสมอนเปนการกลบ bit เปนคาตรงขาม
36
2.4 Signed Integer Representation
• For example, in 8-bit one’s complement, + 3= 00000011
• - 3 = 11111100– ในการหา one’s complement
ดวย 1 high order bit.• Complement systems are useful because they
eliminate the need for special circuitry forsubtraction. The difference of two values is foundby adding the minuend to the complement of thesubtrahend.
8/13/2014
7
37
2.4 Signed Integer Representation
• ในการบวกคาของone’scomplement คาตวทดจะถก
– เชน : ใชการบวก one’scomplement หาผลบวกของ 48และ - 19
19 = 00010011
-19 = 11101100
38
2.4 Signed Integer Representation
• ถงแมจะมการนาตวทดวนกลบมาบวก ระบบ one’s complementกยงงายในการสรางวงจรคานวณมากวา signed magnitude
• แตกยงคงมความแตกตางระหวาง +0 และ -0• จงใชวธTwo’s complement
• Two’s complement is the radix complement ofthe binary numbering system.
39
2.4 Signed Integer Representation
• การหาจานวนในรป two’s complement:– ถาจานวนเปน positive .– ถาจานวนเปน negative ตองหา one’s complement กอนแลว
บวกดวย 1.• Example:
– In 8-bit one’s complement, positive 3 is:00000011
– Negative 3 in one’s complement is:11111100
– Adding 1 gives us -3 in two’s complement form:11111101.
40
2.4 Signed Integer Representation
• ในการคานวณแบบtwo’s complement เราจะบวก binary2 high order bit.
19 = 00010011
-19 in one’s complement = 11101100
-19 in two’s complement = 11101101
– เชน: การใช two’scomplement binaryarithmetic หาผลบวกของ 48และ - 19.
41
2.4 Signed Integer Representation
•
“overflow”• แตเราสามารถตรวจจบการเกดoverflow ได
• โดยใชวธของ complement arithmeticoverflow งายตอการตรวจจบ
42
2.4 Signed Integer Representation
• Example:– ใช two’s complement binary
arithmetic หาผลรวมของ 107 และ46.
• จะเหนวาเกดการทดจากbit 7 ไปยง signbit (bit 8) overflowsและใหผลลพธผดพลาด: 107 + 46 = -103.กฎการหา two’s complement overflow: “carry in” และ“carry out” ของ the sign bit แตกตางกน overflow .
8/13/2014
8
43
2.5 Floating-Point Representation
• one’scomplement และ two’s complement
•ทางดานscientific หรอ businessกบจานวนจรง( real number)
•
44
2.5 Floating-Point Representation
• floating-point จะใชinteger formatfloating-point emulation
• computer จะมอปกรณ hardware สาหรบการคานวณfloating-point arithmetic โดยไมตองprogram
45
2.5 Floating-Point Representation
• Floating-point numbersขวามอของจดทศนยม เชน: 0.5 0.25 = 0.125
• สามารถเขยนในรปแบบของ scientific notation.– เชน:
0.125 = 1.25 10-1
5,000,000 = 5.0 106
46
2.5 Floating-Point Representation
• Computers จะใชรปแบบ scientific notation สาหรบการแสดง floating-point
• scientific notation จะมสวนประกอบ 3สวน คอ:
47
• Computer จะแสดง floating-point number ดวยfields 3 :
2.5 Floating-Point Representation
รปแบบการจดเรยงของแตละ field
48
• one-bit sign field• ขนาด exponent field• ขนาด significant ใชกาหนดความแนนอนของตองการแสดง
2.5 Floating-Point Representation
8/13/2014
9
49
• มาตรฐาน IEEE-754 single precision floating point ใช 8-bit exponent และ a 23-bit significant.
• มาตรฐาน IEEE-754 double precision standard ใช 11-bitexponent และ 52-bit significant.
2.5 Floating-Point Representation
14-bit model 5-bit exponent และ 8-bitsignificant.
50
• นยสาคญของ floating-point number มกจะเขยนนาหนาดวย binary point.
• binary value.• สวนของ exponent แสดงกาลงของ 2 ของคานยสาคญ
2.5 Floating-Point Representation
51
• ตวอยาง:– แสดง 3210 โดยใช 14-bit floating-point model.
• 32 = 25. 32 = 1.0 x 25 = 0.1 x26.
• 110 (= 610) ในสวนของ exponent field และ1 ในสวนของนยสาคญ
2.5 Floating-Point Representation
52
•เปนการแสดงคา 32ใช simplifiedmodel.
•แตยงสบสน
2.5 Floating-Point Representation
53
•สามารถแสดงคา 0.5 (=2 -1)! (เพราะไมสามารถม
exponent field ได)
2.5 Floating-Point Representation
54
• IEEE-754 จงใหสวนของ significantทศนยมตองเปน 1–
2.5 Floating-Point Representation
ใน simple instructional model เราไมไดใช bit 1 หลงทศนยม.
8/13/2014
10
55
•(biased exponent)
• (bias number)25 = 16
– 16( excess-16representation)
• 16
2.5 Floating-Point Representation
56
• ตวอยางเชน:– การแทนคา 3210 ในรปแบบ 14-bit floating-point
model.
• จาก 32 = 1.0 x 25 = 0.1 x 26.• ใช excess 16 biased exponent เราจะทาการบวก 16
ดวย 6 ไดผลลพธ 2210 (=101102).• แสดงไดดงรปขางลาง
2.5 Floating-Point Representation
57
• Example:– จงแสดงคาของ 0.062510 ในรปแบบของ 14-bit floating-
point model.• จาก 0.0625 = 2-4. จะได 0.0625 = 1.0 x 2-4 = 0.1 x
2 -3.• ใช excess 16 biased exponent โดยบวก 16 กบ -3,
ได 1310 (=011012).
2.5 Floating-Point Representation
58
• Example:– จงแสดง -26.62510 ในรปของ 14-bit floating-point
model.• 26.62510 = 11010.1012. ทา Normalizing จะได
26.62510 = 0.11010101 x 2 5.• ใช excess 16 biased exponent เราจงบวก16 กบ 5, ได
2110 (=101012). และใสคา 1 ลงใน sign bit.
2.5 Floating-Point Representation
59
• มาตรฐานของ IEEE-754 single precision floating point จะใช(bias) ของ 127 ของคายกกาลง 8-bit
– คายกกาลงของ 255 แสดงใหถงคา.• infinity.• NaN หมายถง “ไมใชตวเลข” ใชแสดงการ
เกด error
• double precision standard 1023 ของคายกกาลง11-bit– “special” exponent สาหรบ a double precision number เปน
2047 255 single precision standard.
2.5 Floating-Point Representation
60
• 14-bit model และ the IEEE-754 floatingpoint standard– 0 แสดงเปนคา 0 ในสวนของ exponent และ significant แต
0 หรอ 1.
• programmersเปรยบเทยบคา floating-point value กบคาศนย(zero)– -0 ไมเทากบ +0.
2.5 Floating-Point Representation
8/13/2014
11
61
• การบวกและการลบเลข Floating-point จะใชวธเหมอนคานวณดวยมอ
•แลวบวก สวนของ significantยงคงเดม
• ถากาลงไมเทากน จาเปนตองปรบใหเทากน
2.5 Floating-Point Representation
62
• Example:– การหาผลบวก 1210 และ 1.2510 โดยใช 14-bit floating-
point model.• จาก 1210 = 0.1100 x 2 4. และ 1.2510 = 0.101 x 2 1 =
0.000101 x 2 4.
2.5 Floating-Point Representation
• ผลรวมคอ 0.110101x 2 4.
63
• การคณ Floating-point กคลายกบการคณดวยมอ• โดยการคณสองจานวนและบวกคาของ exponents.• ถาสวนของ exponent ไมเทากนตองปรบใหเทากน
2.5 Floating-Point Representation
64
• Example:– จงหาผลคณของ 1210 และ 1.2510 โดยใช 14-bit floating-point
model.• จาก 1210 = 0.1100 x 2 4. และ 1.2510 = 0.101 x 2 1.
2.5 Floating-Point Representation
• ผลลพธ 0.0111100 x 2 5
= 0.1111 x 2 4.• การทา normalized ผลคณ
2010 = 101102.
65
• ไมสาคญวาเราจะใชจานวน bitfloating-point ตองมความแนนอน.
• สาหรบจานวนจรง model•
bit ในmodel
2.5 Floating-Point Representation
66
• error หรอไมกพยายามทราบคาของerror .
• เชน ใน 14-bit model128.5 ไดอยางลงตว เพราะ
128.5 = 10000000.12 = 128.510
2.5 Floating-Point Representation
8/13/2014
12
67
•
• 0.5 ใหกบ 128.5เกอบๆ 2% หลงจากทา 4 รอบ
2.5 Floating-Point Representation
128.5 - 128128
0.39%
68
• Floating-point errors operands .• ถาเราทาการบวก0.5 เขากบ 128.5 0.5 กบตวมนเองแลวนา
128.5 มาบวกดวย• error จะเกดจากการสญเสย the low-order bit.• แตการสญเสย high-order จะยงยากกวามาก
2.5 Floating-Point Representation
69
• การเกด Floating-point overflow และ underflow จะทาใหprograms หยดทางาน (crash)
• Overflow high-orderbit จากการคานวณ
• Underflowจากการหารดวย 0
2.5 Floating-Point Representation
70
•
computer bitpatterns
2.6 Character Codes
71
• computers พฒนาไป charactercodes พฒนาไปกพฒนาไปดวย
• โดย computercharacter codes ไดมาก
• ในสมยกอน computer ใชระบบรหสตวอกษร 6 bits.• Binary-coded decimal (BCD)
IBM mainframes ประมาณป1950s -1960s.
2.6 Character Codes
72
• ตอมาในป 1964, BCD ไดขยายเปน 8-bit code เรยกวาExtended Binary-Coded DecimalInterchange Code (EBCDIC).
• EBCDICupper และ lowercase alphabetic characters
punctuation และ controlcharacters.
• EBCDIC และ BCD ยงคงถกใชในIBMmainframes
2.6 Character Codes
8/13/2014
13
73
• ผผลต computer บางรายเลอกใช 7-bit ASCII (AmericanStandard Code for Information Interchange) มา
6-bit• BCD และEBCDIC
ASCII(telecommunications (Telex) codes.)
• ณ. ASCII ไดรบความนยมเหนอกวารหสของIBMmainframe
2.6 Character Codes
74
• Unicodeใชถง16-bit สามารถใชแทนรหสของตวอกษรเกอบทกภาษาบน
– Java programming language และบางoperating systems ใช Unicode เปนรหสอกษรมาตรฐาน
• รหสUnicode แบงออกเปน 6 สวน สวนแรกใชเปนรหสของตวอกษรตะวนตกเชน English, Greek, และ Russian.
2.6 Character Codes
75
• การแบงรหส Unicodeสามารถแสดงไดทางขวามอ
•ASCII code.
•กาหนดเอง(user-defined codes)
2.6 Character Codes
76
• computer memory คาของมนอาจจะกากวม
• disk หรอถกสงไปตามสาย–
• Data errors จะสามารถลดลงไดโดยการหาวธ codingmethods (error-detection techniques)
2.7 Codes for Data RecordingAnd Transmission
77
• ในการสงขอมล กระแสฟาในรปแบบของสญญาณสง-(communications media)
•– flux
reversals.
• ระหวางเวลาในการ bitเกบขอมล 1 bit เรยกวา bit cell.
2.7 Codes for Data RecordingAnd Transmission
78
• รหสการบนทกและสงขอมลอยางงาย(simplest data recording andtransmission code) คอวธe non-return-to-zero (NRZ)
• NRZ เขารหส 1 เปน “high” และ 0 เปน “low.”• การเขารหสขอมลคาวาOK (in ASCII) แสดงไดดงขางลาง
2.7 Codes for Data RecordingAnd Transmission
8/13/2014
14
79
• ปญหาของ NRZ 0 และ 1จานวนมาก
• วธ Non-return-to-zero-invert (NRZI) จะชวยลดการbit 1
2.7 Codes for Data RecordingAnd Transmission
80
• ถงแม NRZI จะชวยลดการสญเสยbit 1 มากๆไดแตยงไมสามารถปองกนการสญเสย bit 0 มากๆได
• วธ Manchester coding (หรอเรยกวา phasemodulation) bit 1bit 0 เปนขาลง
2.7 Codes for Data RecordingAnd Transmission
81
• Manchester code ถกใชเปนวธการสงขอมลสาหรบใน local area networks.
•bit cell.
• วธ frequency modulation (FM) code. bit 1 ใหมกลางcell transit
2.7 Codes for Data RecordingAnd Transmission
82
• ดเหมอนวาวธของ FM จะดอยกวา Manchester codeเพราะตองมcell
•• วธของ Modified FM
cell 0• ใน MFM cell จะประกอยดวย bit 1 .
2.7 Codes for Data RecordingAnd Transmission
83
•เกดจากการ recording หรอ transmission ไดเลย
รบกวน•
สงดวย•
2.8 Error Detection and Correction
84
• วธ Check digits เปนการใส Check digitsปองกน data input errors.– เชน อกษรตวสดทายของ UPC barcodes และ ISBNs เปน check
digits.
•ขอผดพลาด
• วธ Cyclic redundancy checking (CRC) จะสนบสนนตรวจจบขอผดพลาด สาหรบ large blocks of data.
2.8 Error Detection and Correction
8/13/2014
15
85
• Checksums และCRCs (systematic error detection)
• control bits ตอนทายสดของ block of transmitteddata.– เรยกกลม bits syndrome.
• CRCs เปน polynomials ของ modulo 2 arithmetic field.
2.8 Error Detection and Correction
86
• Modulo 2
2.8 Error Detection and Correction
0 + 0 = 0 0 + 1 = 11 + 0 = 1 1 + 1 = 0
87
• Find the quotient andremainder when 1111101 isdivided by 1101 in modulo 2arithmetic.– As with traditional division,
we note that the dividend isdivisible once by the divisor.
– We place the divisor under thedividend and perform modulo2 subtraction.
2.8 Error Detection and Correction
88
• Find the quotient andremainder when 1111101 isdivided by 1101 in modulo 2arithmetic…– Now we bring down the next
bit of the dividend.
– We see that 00101 is notdivisible by 1101. So we placea zero in the quotient.
2.8 Error Detection and Correction
89
• Find the quotient andremainder when 1111101 isdivided by 1101 in modulo 2arithmetic…– 1010 is divisible by 1101 in
modulo 2.
– We perform the modulo 2subtraction.
2.8 Error Detection and Correction
90
• Find the quotient andremainder when 1111101 isdivided by 1101 in modulo 2arithmetic…
– We find the quotient is 1011,and the remainder is 0010.
• This procedure is very usefulto us in calculating CRCsyndromes.
2.8 Error Detection and Correction
Note: The divisor in this example correspondsto a modulo 2 polynomial: X 3 + X 2 + 1.
8/13/2014
16
91
• Suppose we want to transmit theinformation string: 1111101.
• The receiver and sender decide touse the (arbitrary) polynomialpattern, 1101.
• The information string is shiftedleft by one position less than thenumber of positions in the divisor.
• The remainder is found throughmodulo 2 division (at right) andadded to the information string:1111101000 + 111 = 1111101111.
2.8 Error Detection and Correction
92
• If no bits are lost or corrupted,dividing the receivedinformation string by theagreed upon pattern will give aremainder of zero.
• We see this is so in thecalculation at the right.
• Real applications use longerpolynomials to cover largerinformation strings.– Some of the standard poly-
nomials are listed in the text.
2.8 Error Detection and Correction