CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.

Slides:



Advertisements
Similar presentations
B261 Systems Architecture
Advertisements

Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Representing Numbers: Integers
HEXADECIMAL NUMBERS Code
COE 202: Digital Logic Design Signed Numbers
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
King Fahd University of Petroleum and Minerals
Data Representation Computer Organization &
Data Representation COE 205
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Assembly Language and Computer Architecture Using C++ and Java
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language and Computer Architecture Using C++ and Java
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
Data Representation ICS 233
1  1998 Morgan Kaufmann Publishers Chapter Four Arithmetic for Computers.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Chapter 3 Arithmetic for Computers. Arithmetic Where we've been: Abstractions: Instruction Set Architecture Assembly Language and Machine Language What's.
Lecture 12: Computer Arithmetic Today’s topic –Numerical representations –Addition / Subtraction –Multiplication / Division 1.
Arithmetic for Computers
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CENG 311 Machine Representation/Numbers
Lecture 5.
Computers Organization & Assembly Language
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Lecture Objectives: 1)Define the terms least significant bit and most significant bit. 2)Explain how unsigned integer numbers are represented in memory.
Lecture 2 Binary Values and Number Systems. The number 943 is an example of a number written in positional notation. The relative positions of the digits.
Operations on Bits Arithmetic Operations Logic Operations
Lecture 11: Chapter 2 Today’s topic –Numerical representations Reminders –Homework 3 posted, due 9/29/2014 –Homework 4 posted, due 10/6/2014 –Midterm 1.
THE BINARY SYSTEM.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Ch3a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Crunching Numbers Topics we need to explore Representing numbers on a computer.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
SAK Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
1 Lecture 7: MARS, Computer Arithmetic Today’s topics:  MARS intro  Numerical representations  Addition and subtraction.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.
Integer Operations Computer Organization and Assembly Language: Module 5.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
EE204 L03-ALUHina Anwar Khan EE204 Computer Architecture Lecture 03- ALU.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Representing Positive and Negative Numbers
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Lec 3: Data Representation
COMPUTER ARCHITECTURE & OPERATIONS I
Morgan Kaufmann Publishers
CS1010 Programming Methodology
BEE1244 Digital System and Electronics BEE1244 Digital System and Electronic Chapter 2 Number Systems.
Lecture 8: Addition, Multiplication & Division
Lecture 8: Addition, Multiplication & Division
MISP Assembly.
Computer Architecture & Operations I
Topic 3a Two’s Complement Representation
Computer Architecture
MIPS Assembly.
COMS 361 Computer Organization
Presentation transcript:

CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU

C. ErgunCMPE325 CH #3Slide #2 Positive Binary Numbers Computers use binary numbers (base 2) Example (6 bit number) = 0       2 0 = = 22 Each digit is d × Base i where i = 0 at the right and increases going to the left (same process as in base 10) Does not make sense to store binary numbers in ASCII since each character requires 1 byte

C. ErgunCMPE325 CH #3Slide #3 Converting to Binary To convert a number to base 2, continue to divide by 2, keeping the remainder at each step Consider the example 22 going the other direction 22 ÷ 2 = 11 remainder 0LSB 11 ÷ 2 = 5 remainder 1 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1MSB So the binary number is

C. ErgunCMPE325 CH #3Slide #4 Negative Numbers But, how can negative values be represented? The most obvious answer is to add one additional bit called the sign bit, used to indicate a positive or negative value Where does the sign bit go? Left or right? Arithmetic with signed numbers require extra step Both a positive and negative 0  While possible to overcome, there is a much better way

C. ErgunCMPE325 CH #3Slide #5 The second alternative is to replace all 0’s with 1’s and use a sign bit that is part of the magnitude (ones-complement) Arithmetic still requires extra hardware to do a subtract Both a positive and negative 0 Symmetric positive and negative numbers  Again, while additional hardware can solve the problem, a better method does not… Still two zeros 0x = +0 ten 0xFFFFFFFF = -0 ten Arithmetic not too hard Negative Numbers

C. ErgunCMPE325 CH #3Slide #6 Negative Numbers

C. ErgunCMPE325 CH #3Slide #7 Two’s Complement Can represent positive and negative numbers by first bit (MSB) as –2 31 position, then positive 2 n : d 31 x d 30 x d 2 x d 1 x d 0 x 2 0 Example two = 1x x x x2 2 +0x2 1 +0x2 0 = = -2,147,483,648 ten + 2,147,483,644 ten = -4 ten Note! Must specify width to find MSB => 32bits is used in MIPS, so d 31 is MSB

C. ErgunCMPE325 CH #3Slide #8 Two’s Complement Example Consider the same example as before with a 1 in the MSB = -1       2 0 = = -10 Notice that the result was not -22!

C. ErgunCMPE325 CH #3Slide #9 Two’s Complement Shortcut A simpler way is to convert between positive and negative values (goes both ways) Reverse every bit (01 and 10) Add 1 to the resulting number Consider our previous example again  = 1 0 ten The original value was -10 and the new value is 10. Explanation: x + x’ ≡ -1  x’ + 1 = -x x=-4 : two x’ : two x’ + 1: two invert: two add 1 : two

C. ErgunCMPE325 CH #3Slide #10 Two’s Complement

C. ErgunCMPE325 CH #3Slide #11 Two’s Complement in 8bits More common: use of 2's complement negatives have one additional number = = 1 … = = = = = -3 All negative numbers = -2 have a '1' in the = -1 highest position

C. ErgunCMPE325 CH #3Slide #12 Two’s Complement in MIPS two = 0 ten two = 1 ten two = 2 ten two = 2,147,483,645 ten two = 2,147,483,646 ten two = 2,147,483,647 ten two = –2,147,483,648 ten two = –2,147,483,647 ten two = –2,147,483,646 ten two =–3 ten two =–2 ten two =–1 ten

C. ErgunCMPE325 CH #3Slide #13 Two’s Complement in MIPS

C. ErgunCMPE325 CH #3Slide #14 Understanding Signed Ops Programmers can explicitly use unsigned data values (such as unsigned int ) Require unsigned operations such as sltu and sltiu Signed instructions help determine whether values are considered to have a sign bit For instance, lbu is for load byte unsigned One byte is copied from memory into a register The high order 24 bits are filled with the value 0 The lb instruction is signed The value is then sign extended, meaning that the sign bit from the first byte is repeated into the high order bits of the word Immediate values remain sign extended

C. ErgunCMPE325 CH #3Slide #15 Sign Extension Sign extended value No sign extension bits bits bits bits

C. ErgunCMPE325 CH #3Slide #16 Number formats Different compare operations required for both number types Signed integer slt Set an less than slti Set on less than immediate Unsigned integer sltu Set an less than sltiu Set on less than immediate

C. ErgunCMPE325 CH #3Slide #17 Number formats

C. ErgunCMPE325 CH #3Slide #18 Hexadecimal Hexadecimal is base 16, so digits are 0-9, A-F Since most values are multiples of 4 bits, hexadecimal is a popular way of representing numbers (commonly written 0xnnnnn) Easy to convert binary to hexadecimal by breaking to blocks of 4 bits (2 4 is 16 values)

C. ErgunCMPE325 CH #3Slide #19 Hexadecimal Table

C. ErgunCMPE325 CH #3Slide #20 Hexadecimal Example Consider the example F 2 6 B

C. ErgunCMPE325 CH #3Slide #21 Octal Octal is base 8 and appears occasionally, though not used as frequently Binary to octal is grouped into 3 bits Example

C. ErgunCMPE325 CH #3Slide #22 Shifting Bits The sll and srl instruction were mentioned before in passing, but its purpose should now make more sense sll $t0, $t1, 2# Shift bits left twice srl $t0, $t1, 2# Shift bits right twice Shifting a value to the left twice is the same as multiplying its value by 2 2 = = 6 ten = 24 ten

C. ErgunCMPE325 CH #3Slide #23 Addition & subtraction

C. ErgunCMPE325 CH #3Slide #24 Addition & subtraction

C. ErgunCMPE325 CH #3Slide #25 Overflow Overflow is when a number gets too large to fit The left most bit is not the same as the infinite number of bits to the left of it Can happen with both positive and negative values Handling overflow is the responsibility of the programmer

C. ErgunCMPE325 CH #3Slide #26 Overflow  The difference of two numbers can exceed any representation  2's complement: Numbers change sign and size

C. ErgunCMPE325 CH #3Slide #27 Detecting Overflow Overflow can not occur when adding numbers with different signs or subtracting numbers of the same sign (reverses of each other) Adding numbers of the same sign or subtracting numbers of different signs, however, can cause overflow Addition overflowOverflow conds Sign of operands the same, and Sign of result not the same Subtraction overflow Sign of operands different, and Sign of result different from sign of A A – B = C A Positive and B negative then C should be positive A Negative and B positive then C should be negative

C. ErgunCMPE325 CH #3Slide #28 How to Overcome:

C. ErgunCMPE325 CH #3Slide #29 How to Overcome:

C. ErgunCMPE325 CH #3Slide #30 The sll / srl Instructions When encoded, the sll is an arithmetic R- format instruction that uses the shamt field OP=00 rt rdshamtfunc Bits First Source Register Second Source Register Result Register Shift Amount Function Code

C. ErgunCMPE325 CH #3Slide #31 Shift Operations