Download Presentation
## Lecture 0

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Lecture 0**Number Representations & Operations**Objectives**• Understand that everything in digital word is stored and manipulated in binary format • Master the skill to convert representations between various formats (e.g., binary format and hexadecimal format) • Apply the conversion of unsigned and signed integer numbers between decimal format and binary format • Perform the addition and subtraction between two numbers in hexadecimal format • Detect the overflow of addition and subtraction between two numbers Chapter 2 — Instructions: Language of the Computer — 2**Coverage**• Textbook Chapter 2.4 • Online resources Chapter 2 — Instructions: Language of the Computer — 3**Computer: a binary world**• Everything inside a computer is saved and manipulated at binary format, i.e., in the sequence of ones and zeros Chapter 2 — Instructions: Language of the Computer — 4**Hexadecimal format**• For the convenience of demonstration, binary format is typically displayed in hexadecimal format • Each binary digit is called a bit • A bit can be one of two possible values, i.e., 0 or 1 • Eight consecutive bits are called a byte • Byte is the default unit of capacity in computer world • Each hexadecimal digit can be one of 16 possible values • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F • “0x” is the typical prefix to indicate hexadecimal format Chapter 2 — Instructions: Language of the Computer — 5**Conversion between binary and hexadecimal formats**• Convert 4 consecutive binary bits to 1 hexadecimal digit Chapter 2 — Instructions: Language of the Computer — 6**How to represent integers in a computer?**• Covered in Chapter 2.4 • Unsigned integer • No sign, i.e., 0 or positive • Signed integer • Can be negative, 0, or positive • No explicit ‘+’ or ‘-’ symbol to tell the sign of the number • Two’s complement • Incorporate the sign in the representation • Sign extension Chapter 2 — Instructions: Language of the Computer — 7**Generic numbering system**N=cn-1…c3c2c1c0 • N is an unsigned number represented by n digits • What is the value of N? N=Cn-1…+C3+C2+C1+C0 • The value of N is the sum of the values of all columns Ci=ci×qi • q: is the base value • i: is the column index Chapter 2 — Instructions: Language of the Computer — 8**Unsigned Binary Integers**• Given an n-bit number §2.4 Signed and Unsigned Numbers • Range: 0 to +2n – 1 • Example • 0000,0000,0000,0000,0000,0000,0000,10112= 0 + … + 1×23 + 0×22 +1×21 +1×20= 0 + … + 8 + 0 + 2 + 1 = 1110 • Using 32 bits • 0 to +4,294,967,295 Chapter 2 — Instructions: Language of the Computer — 9**Convert unsigned decimal integer to binary format**• Example: 87 • 87 = 64 + 16 + 4 + 2 + 1 = 26+24+22+21+20 • 8710 = 0101,01112 Chapter 2 — Instructions: Language of the Computer — 10**Two’s-Complement Signed Integers**• Given an n-bit number • Range: –2n – 1 to +2n – 1 – 1 • Example • 1111,1111,1111,1111,1111,1111,1111,11002= –1×231 + 1×230 + … + 1×22 +0×21 +0×20= –2,147,483,648 + 2,147,483,644 = –410 • Using 32 bits • –2,147,483,648 to +2,147,483,647 Chapter 2 — Instructions: Language of the Computer — 11**Two’s-Complement Signed Integers**• Most significant bit is the sign bit • 1 for negative numbers • 0 for non-negative numbers • 2n – 1 can’t be represented • Non-negative numbers have the same unsigned and two’s-complement representation • Some specific numbers • 0: 0000,0000,…,0000 • –1: 1111,1111,…,1111 • Most-negative: 1000,0000,…,0000 • Most-positive: 0111,1111,…,1111 Chapter 2 — Instructions: Language of the Computer — 12**Signed Negation**• Complement and add 1 • Complement means 1 → 0, 0 → 1 • Complement and add 1 → counterpart with opposite sign • Two uses • Given a negative decimal number, find its binary representation • Given a negative binary number, find its positive counterpart Chapter 2 — Instructions: Language of the Computer — 13**Examples**• Binary representation of -210 • +2 = 0000,00102 • –2 = 1111,11012 + 1 = 1111,11102 • Decimal value of 1111,10112 • -(1111,10112)=0000,01002+1=0000,01012 • 1111,10112=-510 Chapter 2 — Instructions: Language of the Computer — 14**Range of 2’s complement**• A demo • Start with 0 • Keep adding 1 to the n-bit number Chapter 2 — Instructions: Language of the Computer — 15**Sign Extension of Signed Numbers**• Representing a number using more bits • Preserve the numeric value • In MIPS instruction set • addi: extend immediate value • lb, lh: extend loaded byte/halfword • beq, bne: extend the displacement • Replicate the sign bit to the left • Examples: 8-bit to 16-bit • +2: 0000,0010 => 0000,0000,0000,0010 • –2: 1111,1110 => 1111,1111,1111,1110 Chapter 2 — Instructions: Language of the Computer — 16**Extension of Unsigned Numbers**• Simply zero extension • Add leading zeros • Examples: 8-bit to 16-bit • 210: 0000,0010 => 0000,0000,0000,0010 • 25410: 1111,1110 => 0000,0000,1111,1110 Chapter 2 — Instructions: Language of the Computer — 17**Addition/subtraction in base 16**• Typically both operands and result have the same number of digits S=A+B • si= • ai+bi+carryi-1 if ai+bi+carryi-1 < 16 • ai+bi+carryi-1-16 otherwise S=A-B • si= • ai-bi-borrowi-1 if ai-bi-borrowi-1 > 0 • 16+ai-bi-borrowi-1 otherwise Chapter 2 — Instructions: Language of the Computer — 18**Overflow**• The result of an arithmetic operation is beyond the representation range • An n-digit number has its range • How to decide if overflow occurs • Unsigned operations • Check if there is a carry (in addition) or borrow (in subtraction) • Signed operations • Check the signs of the operands and the results Chapter 2 — Instructions: Language of the Computer — 19**Overflow**• Addition with signed operands • S = A + B Chapter 2 — Instructions: Language of the Computer — 20**Overflow**• Subtraction with signed operands • S = A - B Chapter 2 — Instructions: Language of the Computer — 21