Docsity

Lecture Slides on MIPS Arithmetic Instructions | 332 331, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Lecture Slides on MIPS Arithmetic Instructions | 332 331 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Computer Architecture and Assembly Language

Spring 2005Week 7

[Adapted from Dave Patterson’s UCB CS152 slides and

Mary Jane Irwin’s PSU CSE331 slides]

Week 7.^

MIPS arithmetic instructions Instruction^

Example

Meaning

Comments

add^

add $1,$2,$

3 operands; exception possible

subtract^

sub $1,$2,$

3 operands; exception possible

add immediate

addi $1,$2,

+ constant; exception possible

add unsigned

addu $1,$2,$

3 operands; no exceptions

subtract unsigned

subu $1,$2,$

3 operands; no exceptions

add imm. unsign.

addiu $1,$2,100 $1 = $2 + 100

+ constant; no exceptions

multiply^

mult $2,$

Hi, Lo = $2 x $

64-bit signed product

multiply unsigned

multu$2,$

Hi, Lo = $2 x $

64-bit unsigned product

divide^

div $2,$

Lo = $2 ÷ $3,

Lo = quotient, Hi = remainder Hi = $2 mod $

divide unsigned

divu $2,$

Lo = $2 ÷ $3,

Unsigned quotient & remainder Hi = $2 mod $

Move from Hi

mfhi $^

$1 = Hi^

Used to get copy of Hi

Move from Lo

mflo $^

$1 = Lo^

Used to get copy of Lo

Number Representation ‰^ Bits are just bits (have no inherent meaning)^ z^ conventions define the relationships between bits andnumbers ‰^ Binary numbers (base 2) - integers^0000

→^^0001

→^^0010

→^^0011

→^^0100

→^^0101

→^^0110

→^^0111

→^^1000

→^^1001

→^... z^ in decimal from 0 to 2

n^ -1 for n bits

‰^ Of course, it gets more complicated^ z^ storage locations (e.g., register file words) are finite, sohave to worry about overflow (i.e., when the number is toobig to fit into 32 bits)^ z^ have to be able to represent negative numbers, e.g., howdo we specify -8 in

addi^

$sp,^

$sp,^

-^

#$sp^

=^ $sp

-^8

z^ in real systems have to provide for more that just integers,e.g., fractions and real numbers (and floating point)

Spring 2005

MIPS Representations ‰^ 32-bit signed numbers (2’s complement):^0000

0000 two

= 0^ ten

=^ +^ two^

1 ten

=^ +^ two^

2 ten

=^ +^ two^

ten

= +^ two^

ten

= –^ two^

ten

= –^ two^

ten

= –^ two^

ten

= –^ two^

3 ten

= –^ two^

2 ten

= –^ two^

1 ten

‰^ What if the bit string represented addresses?^ z^

need operations that also deal with only positive (unsigned)integers

maxint minint

Two's Complement Operations ‰ Negating a two's complement number: complement all thebits and add a 1^ z^ remember: “negate” and “invert” are quite different! ‰ Converting n-bit numbers into numbers with more than nbits:^ z^ MIPS 16-bit immediate gets converted to 32 bits for arithmetic^ z^ copy the most significant bit (the sign bit) into the other bits

z^ sign extension versus zero extend

(lb^ vs.

lbu)

Goal: Design an ALU for the MIPS ISA ‰^ Must support the Arithmetic/Logic operations ofthe ISA ‰^ Tradeoffs of cost and speed based on frequency ofoccurrence, hardware budget

‰^ Just like in grade school (carry/borrow 1s)

**- 0110

  • 0101**

‰^ Two's complement operations easy^ z^

subtraction using addition of negative numbers^0111

→^

- 0110

→^

‰^ Overflow (result too large for finite computer word):^ z^

Addition & Subtraction e.g., adding two n-bit numbers does not yield an n-bit number^0111 + 0001^1000

‰^ Just like in grade school (carry/borrow 1s)

**- 0110

  • 0101**

‰^ Two's complement operations easy^ z^

subtraction using addition of negative numbers^0111

→^

- 0110

→^

‰^ Overflow (result too large for finite computer word):^ z^

Addition & Subtraction^1101 e.g., adding two n-bit numbers does not yield an n-bit number^0111 + 0001

Building 32-bit Adder

1-bitFA

A^0 B^0

c=carry_in^0 S^0 c^1

1-bitFA

A^1 B^1

S^1 c 2

1-bitFA

A^2 B^2

S^2 c 3 c=carry_out 32

1-bitFA

A^31 B^31

...^ c^31 S^31

‰^ Just connect the carry-out ofthe least significant bit FA tothe carry-in of the next leastsignificant bit and connect...

Spring 2005

Building 32-bit Adder/Subtractor ‰^ Remember 2’scomplement is just^ z^ complement all the bits^ z^ add a 1 in the leastsignificant bit^ A^0111

→^

0111

B^ -^0110

→^ +

1010

1-bitFA^

c=carry_in^0 S^0 c^1

1-bitFA^

S^1 c 2

1-bitFA^

S^2 c 3 c=carry_out 32

1-bitFA^

... c^31 S^31

A^0 A^1 A^2 A^31

B^0 B^1 B^2 B^31

add/subt

control^ B^0 (0=add,1=subt)

Bif control = 0,^0 !Bif control = 1^0

Spring 2005

Overflow Detection ‰ Overflow: the result is too large to represent in thenumber of bits allocated ‰ Overflow occurs when^ z^ adding two positives yields a negative^ z^ or, adding two negatives gives a positive^ z^ or, subtract a negative from a positive gives a negative^ z^ or, subtract a positive from a negative gives a positive ‰ On your own: Prove you can detect overflow by:^ z^ Carry into MSB xor Carry out of MSB

– 6^

–4 – 5^7

Week 7.^

A Simple ALU Cell

carry_in 1-bitFA carry_out

A B

add/subt add/subt

result

op

AND^ OR^ XOR^ NOR

B or!B

Mux

A and B A and !B^ A or B^ A or !B A xor B A xor !B^ A nor B^ A nor !B

A + B A - B

Tailoring the ALU to the MIPS ISA ‰^ Need to support the set-on-less-than instruction (

slt)

z^ remember:

slt^ is an arithmetic instruction

z^ produces a 1 if rs < rt and 0 otherwise z^ use subtraction: (a - b) < 0 implies a < b ‰ Need to support test for equality (

beq)

z^ use subtraction: (a - b) = 0 implies a = b ‰ Need to add the overflow detection hardware

Modifying the ALU Cell for slt

carry_in 1-bitFA

add/subt

op

A

result

B less

carry_out

add/subt