Transcript Lecture 2

Lecture 2
Number Representations
1
Number Representations
 Decimal
8547 = 8*103 + 5*102 + 4*101 + 7*100
 Common
form
V(D) = dn-1*10n-1 + dn-2*10n-2 + … + d0*100
 Binary
V(B) = bn-1*2n-1 + bn-2*2n-2 + … + b0*20
2
Number Representations

Binary to Decimal
(1101)2 =
1*23 + 1*22 + 0*21 + 1*20
=
8+4+0+1
=
13

Decimal to Binary
2
13
LSB
LSB
2
6
1
2
3
0
2
1
1
0
1
13 = (1101)2
MSB
MSB
3
Number Representations
 Octal



(เลขฐาน 8)
มีตวั เลขที่มีค่าตั้งแต่ 0 ถึง 7
กลุ่มของ Octal digit จะแสดงโดยใช้จานวนบิต 3 bits
การแปลง Binary number ให้เป็ น Octal number ทาโดย
การแบ่งกลุ่มของ Binary number ทีละ 3 บิต โดยให้เริ่ มที่ฝั่ง
LSB (least significant bit)
 Example
(101011010111)2 = 101 011 010 111
= (5327)8
4
Number Representations
 Hexadecimal (เลขฐาน





16)
ตัวเลขมีค่าตั้งแต่ 0 ถึง 9 และ ตัวอักษรตั้งแต่ A ถึง F
กลุ่มของ Hexadecimal จะแสดงโดยใช้จานวนบิต 4 bits
การแปลง Binary number ให้เป็ น Hexadecimal ทาโดย
การแบ่งกลุ่มของ Binary number ทีละ 4 บิต โดยให้เริ่ มที่ฝั่ง
LSB (least significant bit)
Example
(AF25)16 =
(1010 1111 0010 0101)2
ทาไมต้องใช้ Octal และ Hexadecimal เนื่องจากเป็ นทางลัดในการ
อ้างถึง Binary (shorthand notation)
5
Sign-and-magnitude
 Sign-and-magnitude






คือ Binary number ที่มีลกั ษณะในการบ่งบอกค่าบวกและลบ
เหมือนกับ Decimal number
ใช้ extra bit สาหรับการบ่งบอกว่าเป็ นค่าบวกหรื อลบ
Extra bit จะอยูท่ ี่ตาแหน่ง MSB เพื่อเป็ นตัวบ่งบอกว่าเป็ นบวกหรื อ
ลบ
MSB = 0 ; number is positive
MSB = 1 ; number is negative
Example
0101 = +5
1101 = -5
Extra bit
6
1’s complement
 1’s


complement
ค่าที่เป็ น negative หรื อค่าลบสามารถทาได้โดย complement แต่
ละบิตรวมทั้ง bit ที่เป็ น MSB ด้วย
Example
0101 = +5
1010 = -5
7
2’s complement
 2’s


complement
ค่าที่เป็ น negative หรื อค่าลบสามารถแสดงได้โดยการบวก 1 ให้กบั
1’s complement
Example
0101 = +5
1’s complement = 1010
2’s complement = 1010 +
1
1011 = -5
8
Sign-and-magnitude
 เป็ นการแสดงตัวเลขแบบง่าย แต่ไม่ได้ใช้ในระบบคอมพิวเตอร์ ปัจจุบน
ั
เนื่องจาก เช่นเมื่อเราต้องการบวกตัวเลข 2 ตัวที่มีค่าเป็ น opposite
sign เช่น +5 กับ -2 จะต้องทาการหาก่อนว่าตัวไหนเป็ นตัวที่
มากกว่า และตัวไหนมีค่าน้อยกว่า
 จาเป็ นต้องมี extra logic circuit เพื่อที่จะหาการเปรี ยบเทียบค่า
ระหว่างตัวเลข 2 ตัวก่อน
9
1’s complement
complement สามารถคานวณตัวเลขสองตัวได้โดยไม่ตอ้ ง
ผ่าน comparing circuit
 1’s
 Example
5+
-2
3
 ต้องนา
0101 +
1101
Carry bit 10010
1
0011
1’s complement
carry bit บวกเข้าไปกับผลลัพธ์อีกครั้งหนึ่ง
10
2’s complement
 ผลลัพธ์ที่ได้จากการทา 2’s
complement ไม่ตอ้ งมี
comparing circuit และไม่ตอ้ งมีการนา carry bit มาบวก
เข้าอีกทีหนึ่ง
 Example
5+
-2
3
0101 +
1110
10011
ignore
ดังนั้น 0011 = 3
11
Arithmetic Overflow
overflow จะเกิดขึ้นเมื่อมีการบวกหรื อลบตัวเลข
แล้วเกิดผลลัพธ์ข้ ึนมาแล้วทาให้มี bit เกินมาจากผลลัพธ์ เราเรี ยกว่า
overflow
 ภายใน Processor มีวงจรในการตรวจจับ Overflow โดย
Overflow = Cn-1  Cn
 Arithmetic
(C คือ carry bit ที่เกิดขึ้นจากการคานวณ)
12
Arithmetic Overflow
 Example
c4 c3 c2 c1
7+
2
9
0111 +
0010
1001
C3 = 1
C3  C4 = 1
C4 = 0
Overflow occurs
13
Arithmetic Overflow
 Example
c4 c3 c2 c1
-7 +
2
-5
1001 +
0010
1011
C3 = 0
C4 = 0
C3  C4 = 0
No Overflow
14
Memory locations & addresses
ประกอบด้วย set ของ storage cell ซึ่งแต่ละ
cell จะเก็บข้อมูลได้ 1 bit, กลุ่มของ bit เมื่อนามารวมกันเพื่อทา
การประมวลผลเราจะเรี ยกว่า word ซึ่ง word เป็ นหน่วยที่ใช้ใน
การเข้าถึงและเก็บข้อมูล
 การเข้าถึงข้อมูลที่เป็ น word จาเป็ นจะต้องอ้างถึง address(ที่
อยู)่ โดยปกติ Memory address จะมีค่าตั้งแต่ 0 ถึง 2k-1 ,
2k คือ Address size space
 Example
32 bit address = 232 = 4,294,967,296
ประมาณ 4 gig
 Memory
15
Memory locations & addresses
n-bits
32 bit is 4 bytes
First word
Second word
.
.
.
.
i-th word
.
.
.
.
Last word
Memory word
16
Memory locations & addresses
32 bits
b31
b30
…………………..
b1
b0
Sign bit : b31 = 0 for positive numbers
b31 = 1 for negative numbers
(a) A signed integer
8 bits
8 bits
8 bits
8 bits
ASCII
Character
ASCII
Character
ASCII
Character
ASCII
Character
(b) Four characters
Example of encoded information in a 32-bit word
17
Memory locations & addresses
1
byte = 8 bits
 1 word = 16 to 64 bits, โดยทัว่ ไปจะเป็ น 32 bits
 ส่ วนใหญ่จะใช้ byte-addressable เข้าถึง address ใน
Memory, ทุก ๆ byte ใน 1 word จะถูกเก็บต่อ ๆ กันใน
Memory address space
 ถ้าใน 1 word มี 4 byte การไล่ตาแหน่งของ word ลาดับ
ต่อไปก็จะเป็ น 0, 4, 8 ,16, ……
18
Big endian & Little endian
Word
address
Word
address
Byte address
Byte address
0
0
1
2
3
0
3
2
1
0
4
4
5
6
7
4
7
6
5
4
2k-3
2k-4
.
.
.
.
.
2k-4
2k-4
2k-3
.
.
.
.
.
2k-2
2k-1
(a) Big-endian assignment
2k-4
2k-1
2k-2
(b) Little-endian assignment
19
Big endian & Little endian
: lower byte address จะอยูท่ างซ้ายมือ
หรื อ most significant byte (left most of the
word)
่ างซ้าย
 Little-endian : higher byte address จะอยูท
มือ หรื อ most significant byte
 Big-endian
20
Memory Operations
 Load

Read or fetch data from a memory to a
processor
 Store

Write data from processor back to a memory
21