coe 308

12
COE 308 King Fahd University of Petroleum and Minerals Computer Engineering Department College of Computer Science And Engineering Dr. A. Bouhraoua Arithmetic for Computers 1 COE 308 Lectures 3: Arithmetic for Computers – Application to the MIPS Architecture

Upload: vasilis-julius

Post on 02-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

COE 308. Lectures 3: Arithmetic for Computers – Application to the MIPS Architecture. Why Arithmetics. Microprocessors perform: Data Movement Additions Subtractions Logical Operations Comparisons. Arithmetic Operations. Understand Arithmetic for the Computer Algorithms - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 1

COE 308

Lectures 3:Arithmetic for Computers – Application

to the MIPS Architecture

Page 2: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 2

Why Arithmetics• Microprocessors perform:

– Data Movement– Additions– Subtractions– Logical Operations– Comparisons

Arithmetic Operations

Understand Arithmetic for the Computer• Algorithms• Architectures• Circuits

Goal

Page 3: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 3

Binary Numbers

N = dw-1Bw-1 +…+ diBi + d1B1 +d0B0

• Base B is Ten Decimal Number• Base B is Two Binary Number

In MIPS, 32 bits words0000 0000 0000 0000 0000 0000 0000 0011two = 3ten

0000 0000 0000 0000 0000 0000 0001 0101two = 21ten

.......1111 1111 1111 1111 1111 1111 1111 1110two = 4,294,967,294ten

1111 1111 1111 1111 1111 1111 1111 1111two = 4,294,967,295ten

Page 4: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 4

ACSII or Decimal vs Binary Numbers

• Attractive because: Human- Friendly representation• Non attractive to computers:

– Understand only binary

– Binary operations easy to perform

– Does not know how to perform operations on ASCII or Decimal number.

– Need to convert ASCII/Decimal to binary before and after operation

• Used only for I/O (User Interface)

Use Binary Representation

For Internal Computations

Convert to/from ASCII for I/O

Only

Page 5: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 5

Number Representation• Numbers in computers

– Just a representation of real numbers – Real numbers have an infinite number of digits

153ten = …..00000……………………0000010011001two

= 0000 0000 0000 0000 0000 0000 1001 1001two in a 32 bits word

Add, Subtract, Multiply, Divide

Result bigger than number of

available slots (bits)

Overflow

Page 6: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 6

Signed Numbers• Subtraction of a big number from small number

is a negative number

Need for Signed Numbers Representation

Intuitive Representation: Sign + Magnitude

Sign Magnitude• Many Shortcomings

– Where to put the Sign bit? Left? Right?– Extra step to set the sign for addition and subtraction– Positive and negative 0 leads to programming problems

Page 7: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 7

Two’s Complement

What is the result of A – B when B >> A?Will Try to borrow from a string of leading 0s. Result will have a string of leading 1s.

Leading 0s: Positive numberLeading 1s: Negative number

• Imbalance between the number of negative numbers and the number of positive numbers– Not a worry anymore.

• Used by ALL Computers today

• Easy recognition of positive/negative numbers

• Easy arithmetics

Page 8: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 8

Negation and Sign Extension

• Two’s complement negation– Invert all bits of number one by one– Add the value 1

• Coming from: x + x = -1 (0 + (-1) = -1)– Means – x = x + 1

• Two’s Complement Sign Extension:– Copy the Most Significant Bit to the left

• Coming from the fact that – Positive numbers have an infinite number of 0s to the

left– Negative numbers have an infinite number of 1s to

the left

Page 9: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 9

Addition• Straight Forward Operation

– Adding two number 6 and 7:0000 0000 0000 0000 0000 0000 0000 0110two= 6ten

0000 0000 0000 0000 0000 0000 0000 0111two= 7ten

0000 0000 0000 0000 0000 0000 0000 1101two= 13ten

+

=

(0) 0 0

(0) 0 0

(1) 0 0

(1) 1 1

(0) 1 1

0 1

(0)0 (0)0 (0)0 (1)1 (1)1 (0)1

• Overflow can occur

• MIPS instructions: add, addi, addu, addui

Page 10: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 10

Subtraction• Subtract A from B

• Add A to Two’s Complement of B

0000 0000 0000 0000 0000 0000 0000 0111two= 7ten

1111 1111 1111 1111 1111 1111 1111 1010two= -6ten

0000 0000 0000 0000 0000 0000 0000 0001two= 1ten

+

=

• Result either >0, 0 or <0• Overflow can occur• MIPS instructions: sub, subi, subu, subiu

Page 11: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 11

Overflow Condition• Addition is operation between A and B (any sign)• Subtraction is addition of two’s complement• Overflow in addition/subtraction

– 33rd bit set with the value of result instead of the proper sign

Operation Operand A Operand B Result

A+B > 0 > 0 < 0

A+B < 0 < 0 > 0

A-B > 0 < 0 < 0

A-B < 0 > 0 > 0

• Add (add), add immediate (addi) and subtract (sub) cause exceptions on overflow

• Add unsigned(addu) , add immediate unsigned(addiu) and subtract unsigned (subu) do NOT cause exceptions on overflow

Page 12: COE 308

COE 308COE 308

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Dr. A. Bouhraoua Arithmetic for Computers 12

Logical Operations• And: Bit to bit logical AND

– Used to Clear specified bits• Or: Bit to Bit logical OR:

– Used to Set specified bits• Shift Operations:

– Many uses: Many algorithms, available in some high level languages like C/C++

– SLL, SLR

Logical Operations C Operators MIPS Instruction

Shift Left << sll

Shift Right >> slr

Bit-by-bit AND & and, andi

Bit-by-bit OR | or, ori