Download presentation
Presentation is loading. Please wait.
Published byAvis Rodgers Modified over 6 years ago
1
The University of Adelaide, School of Computer Science
19 November 2018 Lecture 0 Number Representations & Operations Chapter 2 — Instructions: Language of the Computer
2
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
3
Coverage Textbook Chapter 2.4 Online sources
Chapter 2 — Instructions: Language of the Computer — 3
4
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
5
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
6
Conversion between binary and hexadecimal formats
Convert 4 consecutive binary bits to 1 hexadecimal digit H B 8 1 9 2 A 3 4 C 5 D 6 E 7 F Chapter 2 — Instructions: Language of the Computer — 6
7
How to represent integers in a computer?
The University of Adelaide, School of Computer Science 19 November 2018 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 Before we discuss the details of instruction set, we need to talk about two things. How are numbers are represented in a computer? How to store them and address them in the memory? Chapter 2 — Instructions: Language of the Computer — 7 Chapter 2 — Instructions: Language of the Computer
8
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
9
Unsigned Binary Integers
The University of Adelaide, School of Computer Science 19 November 2018 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 + … = 1110 Using 32 bits 0 to +4,294,967,295 The value of ith column is the value of the digit times its weight. The value of the n-bit number is the sum of the values of all columns. Chapter 2 — Instructions: Language of the Computer — 9 Chapter 2 — Instructions: Language of the Computer
10
Convert unsigned decimal integer to binary format
Example: 87 87 = = 8710 = 0101,01112 Chapter 2 — Instructions: Language of the Computer — 10
11
Two’s-Complement Signed Integers
The University of Adelaide, School of Computer Science 19 November 2018 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× ×230 + … + 1×22 +0×21 +0×20 = –2,147,483, ,147,483,644 = –410 Using 32 bits –2,147,483,648 to +2,147,483,647 Chapter 2 — Instructions: Language of the Computer — 11 Chapter 2 — Instructions: Language of the Computer
12
Two’s-Complement Signed Integers
The University of Adelaide, School of Computer Science 19 November 2018 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 Chapter 2 — Instructions: Language of the Computer
13
The University of Adelaide, School of Computer Science
19 November 2018 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 Chapter 2 — Instructions: Language of the Computer
14
The University of Adelaide, School of Computer Science
19 November 2018 Examples Binary representation of -210 +2 = 0000,00102 –2 = 1111, = 1111,11102 Decimal value of 1111,10112 -(1111,10112)=0000, =0000,01012 1111,10112=-510 Chapter 2 — Instructions: Language of the Computer — 14 Chapter 2 — Instructions: Language of the Computer
15
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
16
Sign Extension of Signed Numbers
The University of Adelaide, School of Computer Science 19 November 2018 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 Chapter 2 — Instructions: Language of the Computer
17
Extension of Unsigned Numbers
Simply zero extension Add leading zeros Examples: 8-bit to 16-bit 210: ,0010 => 0000,0000,0000,0010 25410: 1111,1110 => 0000,0000,1111,1110 Chapter 2 — Instructions: Language of the Computer — 17
18
Addition/subtraction in base 16
Typically both operands and result have the same number of digits C=A+B ci= ai+bi+carryi-1 if ai+bi+carryi-1 < 16 ai+bi+carryi otherwise C=A-B ai-bi-borrowi-1 if ai-bi-borrowi-1 > 0 16+ai-bi-borrowi-1 otherwise Chapter 2 — Instructions: Language of the Computer — 18
19
The University of Adelaide, School of Computer Science
19 November 2018 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 Use examples Chapter 2 — Instructions: Language of the Computer — 19 Chapter 2 — Instructions: Language of the Computer
20
Overflow Addition with signed operands C = A + B A B C + No overflow -
Don’t care Chapter 2 — Instructions: Language of the Computer — 20
21
Overflow Subtraction with signed operands C = A - B A B C + Don’t care
No overflow - Overflow Chapter 2 — Instructions: Language of the Computer — 21
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.