+ All Categories
Home > Documents > Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and...

Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and...

Date post: 17-Jan-2016
Category:
Upload: daniela-davidson
View: 325 times
Download: 11 times
Share this document with a friend
Popular Tags:
138
Chapter 6 <1> COMPUTER ARCHITECTURE Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris
Transcript
Page 1: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <1> CO

MPU

TER

ARCH

ITEC

TURE

Digital Design and Computer Architecture, 2nd Edition

Chapter 6

David Money Harris and Sarah L. Harris

Page 2: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <2> CO

MPU

TER

ARCH

ITEC

TURE Chapter 6 :: Topics

• Introduction• Assembly Language• Machine Language• Programming• Addressing Modes• Lights, Camera, Action:

Compiling, Assembling, & Loading• Odds and Ends

Page 3: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <3> CO

MPU

TER

ARCH

ITEC

TURE

• Jumping up a few levels of abstraction

• Architecture: (Chapter 6) programmer’s view of computer– Defined by instructions &

operand locations• Microarchitecture: how

to implement an architecture in hardware (covered in Chapter 7) Physics

Devices

AnalogCircuits

DigitalCircuits

Logic

Micro-architecture

Architecture

OperatingSystems

ApplicationSoftware

electrons

transistorsdiodes

amplifiersfilters

AND gatesNOT gates

addersmemories

datapathscontrollers

instructionsregisters

device drivers

programs

Introduction

Page 4: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <4> CO

MPU

TER

ARCH

ITEC

TURE

• Instructions: commands in a computer’s language– Assembly language: human-readable format of

instructions– Machine language: computer-readable format (1’s

and 0’s)• MIPS architecture:

– Developed by John Hennessy and his colleagues at Stanford and in the 1980’s.

– Used in many commercial systems, including Silicon Graphics, Nintendo, and Cisco

Once you’ve learned one architecture, it’s easy to learn others

Assembly Language

Page 5: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <5> CO

MPU

TER

ARCH

ITEC

TURE

• President of Stanford University• Professor of Electrical Engineering

and Computer Science at Stanford since 1977

• Co-invented the Reduced Instruction Set Computer (RISC) with David Patterson

• Developed the MIPS architecture at Stanford in 1984 and cofounded MIPS Computer Systems

• As of 2004, over 300 million MIPS microprocessors had been sold

John Hennessy

Page 6: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <6> CO

MPU

TER

ARCH

ITEC

TURE

Underlying design principles, as articulated by Hennessy and Patterson:

1.Simplicity favors regularity2.Make the common case fast3.Smaller is faster4.Good design demands good

compromises

Architecture Design Principles

Page 7: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <7> CO

MPU

TER

ARCH

ITEC

TURE

• add: mnemonic indicates operation to perform• b, c: source operands (on which the operation is

performed)• a: destination operand (to which the result is written)

C Codea = b + c;

MIPS assembly codeadd a, b, c

Instructions: Addition

Page 8: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <8> CO

MPU

TER

ARCH

ITEC

TURE

• Similar to addition - only mnemonic changes

• sub: mnemonic• b, c: source operands• a: destination operand

C Codea = b - c;

MIPS assembly codesub a, b, c

Instructions: Subtraction

Page 9: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <9> CO

MPU

TER

ARCH

ITEC

TURE

Simplicity favors regularity• Consistent instruction format• Same number of operands (two sources and

one destination)• easier to encode and handle in hardware

Design Principle 1

Page 10: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <10> CO

MPU

TER

ARCH

ITEC

TURE

• More complex code is handled by multiple MIPS instructions.

C Codea = b + c - d;

MIPS assembly codeadd t, b, c # t = b + csub a, t, d # a = t - d

Multiple Instructions

Page 11: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <11> CO

MPU

TER

ARCH

ITEC

TURE

Make the common case fast• MIPS includes only simple, commonly used instructions• Hardware to decode and execute instructions can be

simple, small, and fast• More complex instructions (that are less common)

performed using multiple simple instructions• MIPS is a reduced instruction set computer (RISC), with

a small number of simple instructions• Other architectures, such as Intel’s x86, are complex

instruction set computers (CISC)

Design Principle 2

Page 12: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <12> CO

MPU

TER

ARCH

ITEC

TURE

• Operand location: physical location in computer– Register operands– Memory operands– Immediate operands (located in the

instruction itself (used for constants)

Operands

Page 13: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <13> CO

MPU

TER

ARCH

ITEC

TURE

• MIPS has 32 32-bit registers• Registers are faster than memory• MIPS called “32-bit architecture” because

it operates on 32-bit data

Operands: Registers

Page 14: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <14> CO

MPU

TER

ARCH

ITEC

TURE

Smaller is Faster• MIPS includes only a small number of

registers

Design Principle 3

Page 15: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <15> CO

MPU

TER

ARCH

ITEC

TURE

Name Register Number Usage

$0 0 the constant value 0

$at 1 assembler temporary

$v0-$v1 2-3 Function return values

$a0-$a3 4-7 Function arguments

$t0-$t7 8-15 temporaries

$s0-$s7 16-23 saved variables

$t8-$t9 24-25 more temporaries

$k0-$k1 26-27 OS temporaries

$gp 28 global pointer

$sp 29 stack pointer

$fp 30 frame pointer

$ra 31 Function return address

MIPS Register Set

Page 16: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <16> CO

MPU

TER

ARCH

ITEC

TURE

• Registers:– $ before name – Example: $0, “register zero”, “dollar zero”

• Registers used for specific purposes: • $0 always holds the constant value 0.• the saved registers, $s0-$s7, used to hold

variables• the temporary registers, $t0 - $t9, used to

hold intermediate values during a larger computation

• Discuss others later

Operands: Registers

Page 17: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <17> CO

MPU

TER

ARCH

ITEC

TURE

• Revisit add instruction

C Code

a = b + c

MIPS assembly code# $s0 = a, $s1 = b, $s2 = cadd $s0, $s1, $s2

Instructions with Registers

Page 18: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <18> CO

MPU

TER

ARCH

ITEC

TURE

• Too much data to fit in only 32 registers• Store more data in memory• Memory is large, but slow• Commonly used variables kept in registers

(Example of principle #2: Make the common case fast)

Operands: Memory

Page 19: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <19> CO

MPU

TER

ARCH

ITEC

TURE

Data

00000003 4 0 F 3 0 7 8 8

0 1 E E 2 8 4 2

F 2 F 1 A C 0 7

A B C D E F 7 8

00000002

00000001

00000000

Word Address

Word 3

Word 2

Word 1

Word 0

• Each 32-bit data word has a unique address

Word-Addressable Memory

Page 20: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <20> CO

MPU

TER

ARCH

ITEC

TURE

• Memory read called load• Mnemonic: load word (lw)• Format:

lw $s0, 5($t1)• Address calculation:

– add base address ($t1) to the offset (5)– address = ($t1 + 5)

• Result:– $s0 holds the value at address ($t1 + 5)

Any register may be used as base address

Reading Word-Addressable Memory

Page 21: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <21> CO

MPU

TER

ARCH

ITEC

TURE

Data

00000003 4 0 F 3 0 7 8 8

0 1 E E 2 8 4 2

F 2 F 1 A C 0 7

A B C D E F 7 8

00000002

00000001

00000000

Word Address

Word 3

Word 2

Word 1

Word 0

• Example: read a word of data at memory address 1 into $s3– address = ($0 + 1) = 1– $s3 = 0xF2F1AC07 after load

Assembly codelw $s3, 1($0) # read memory word 1 into $s3

Reading Word-Addressable Memory

Page 22: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <22> CO

MPU

TER

ARCH

ITEC

TURE

• Memory write are called store• Mnemonic: store word (sw)

Writing Word-Addressable Memory

Page 23: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <23> CO

MPU

TER

ARCH

ITEC

TURE

Data

00000003 4 0 F 3 0 7 8 8

0 1 E E 2 8 4 2

F 2 F 1 A C 0 7

A B C D E F 7 8

00000002

00000001

00000000

Word Address

Word 3

Word 2

Word 1

Word 0

• Example: Write (store) the value in $t4 into memory address 7 – add the base address ($0) to the offset (0x7) – address: ($0 + 0x7) = 7

Offset can be written in decimal (default) or hexadecimal

Assembly codesw $t4, 0x7($0) # write the value in $t4 # to memory word 7

Writing Word-Addressable Memory

Page 24: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <24> CO

MPU

TER

ARCH

ITEC

TURE

Word Address Data

0000000C

00000008

00000004

00000000

width = 4 bytes

4 0 F 3 0 7 8 8

0 1 E E 2 8 4 2

F 2 F 1 A C 0 7

A B C D E F 7 8

Word 3

Word 2

Word 1

Word 0

• Each data byte has unique address• Load/store words or single bytes: load byte (lb) and

store byte (sb) • 32-bit word = 4 bytes, so word address increments by 4

Byte-Addressable Memory

Page 25: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <25> CO

MPU

TER

ARCH

ITEC

TURE

• The address of a memory word must now be multiplied by 4. For example,– the address of memory word 2 is 2 × 4 = 8– the address of memory word 10 is 10 × 4 = 40

(0x28)• MIPS is byte-addressed, not word-

addressed

Reading Byte-Addressable Memory

Page 26: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <26> CO

MPU

TER

ARCH

ITEC

TURE

Word Address Data

0000000C

00000008

00000004

00000000

width = 4 bytes

4 0 F 3 0 7 8 8

0 1 E E 2 8 4 2

F 2 F 1 A C 0 7

A B C D E F 7 8

Word 3

Word 2

Word 1

Word 0

• Example: Load a word of data at memory address 4 into $s3.

• $s3 holds the value 0xF2F1AC07 after load

MIPS assembly codelw $s3, 4($0) # read word at address 4 into $s3

Reading Byte-Addressable Memory

Page 27: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <27> CO

MPU

TER

ARCH

ITEC

TURE

Word Address Data

0000000C

00000008

00000004

00000000

width = 4 bytes

4 0 F 3 0 7 8 8

0 1 E E 2 8 4 2

F 2 F 1 A C 0 7

A B C D E F 7 8

Word 3

Word 2

Word 1

Word 0

• Example: stores the value held in $t7 into memory address 0x2C (44)

MIPS assembly codesw $t7, 44($0) # write $t7 into address 44

Writing Byte-Addressable Memory

Page 28: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <28> CO

MPU

TER

ARCH

ITEC

TURE

0 1 2 3

MSB LSB

4 5 6 7

8 9 A B

C D E F

ByteAddress

3 2 1 00

7 6 5 44

B A 9 88

F E D CC

ByteAddress

WordAddress

Big-Endian Little-Endian

MSB LSB

• How to number bytes within a word?• Little-endian: byte numbers start at the little (least

significant) end• Big-endian: byte numbers start at the big (most

significant) end• Word address is the same for big- or little-endian

Big-Endian & Little-Endian Memory

Page 29: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <29> CO

MPU

TER

ARCH

ITEC

TURE

0 1 2 3

MSB LSB

4 5 6 7

8 9 A B

C D E F

ByteAddress

3 2 1 00

7 6 5 44

B A 9 88

F E D CC

ByteAddress

WordAddress

Big-Endian Little-Endian

MSB LSB

• Jonathan Swift’s Gulliver’s Travels: the Little-Endians broke their eggs on the little end of the egg and the Big-Endians broke their eggs on the big end

• It doesn’t really matter which addressing type used – except when the two systems need to share data!

Big-Endian & Little-Endian Memory

Page 30: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <30> CO

MPU

TER

ARCH

ITEC

TURE

• Suppose $t0 initially contains 0x23456789• After following code runs on big-endian system, what

value is $s0?

sw $t0, 0($0)lb $s0, 1($0)

• In a little-endian system?

Big-Endian & Little-Endian Example

Page 31: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <31> CO

MPU

TER

ARCH

ITEC

TURE

• Suppose $t0 initially contains 0x23456789• After following code runs on big-endian system, what

value is $s0?

sw $t0, 0($0)lb $s0, 1($0)

• In a little-endian system?

• Big-endian: 0x00000045• Little-endian: 0x00000067

Big-Endian & Little-Endian Example

23 45 67 89

0 1 2 3

23 45 67 890

3 2 1 0Word

Address

Big-Endian Little-Endian

Byte Address

Data Value

Byte Address

Data Value

MSB LSB MSB LSB

Page 32: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <32> CO

MPU

TER

ARCH

ITEC

TURE

• lw and sw use constants or immediates e.g. sw $t1, 48($0)• Value is immediately available from instruction• Constant is a 16-bit 2’s complement number• addi: add immediate• Is subtract immediate (subi) necessary?

C Code

a = a + 4;b = a – 12;

MIPS assembly code# $s0 = a, $s1 = baddi $s0, $s0, 4addi $s1, $s0, -12

Operands: Constants/Immediates

Page 33: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <33> CO

MPU

TER

ARCH

ITEC

TURE

Good design demands good compromises• Number of instruction formats should be

minimum- to adhere to Design Principles 1 and 3 (simplicity

favors regularity, and smaller is faster).• Multiple instruction formats allow flexibility

- Constants are very frequent in code (so “make the common case fast”, Design Principle #2)

- add, sub: use 3 register operands- lw, sw,addi: use 2 register operands and a constant

• The compromise is to have 3 instruction formats

Design Principle 4

Page 34: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <34> CO

MPU

TER

ARCH

ITEC

TURE

• Binary representation of instructions• Computers only understand 1’s and 0’s• 32-bit instructions

– Simplicity favors regularity: 32-bit data & instructions

• 3 instruction formats:– R-Type: register operands– I-Type:immediate operand– J-Type: for jumping (discuss later)

Machine Language

Page 35: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <35> CO

MPU

TER

ARCH

ITEC

TURE

op rs rt rd shamt funct6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

R-Type

• Register-type• Uses 3 register operands:

– rs, rt: source registers– rd: destination register

• Other fields:– op: the operation code or opcode (0 for R-type instructions)– funct: the function code. When op= 0, funct tells the

processor which R-type operation to perform– shamt: the shift amount for shift instructions, otherwise it’s 0

R-Type

Page 36: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <36> CO

MPU

TER

ARCH

ITEC

TURE

add $s0, $s1, $s2

sub $t0, $t3, $t5

Assembly Code

0 17 18 16 0 32

Field Values

0 11 13 8 0 34

op rs rt rd shamt funct

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

000000 10001 10010 10000 00000 100000

op rs rt rd shamt funct

000000 01011 01101 01000 00000 100010

Machine Code

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

(0x02328020)

(0x016D4022)

Note the order of registers in the assembly code:

add rd, rs, rt

R-Type Examples

Page 37: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <37> CO

MPU

TER

ARCH

ITEC

TURE

op rs rt imm6 bits 5 bits 5 bits 16 bits

I-Type

• Immediate-type• 3 operands:

– rs, rt: register operands– imm: 16-bit 2’s complement immediate

• Other fields:– op: the opcode– Simplicity favors regularity: all instructions have opcode– Operation is completely determined by opcode

I-Type

Page 38: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <38> CO

MPU

TER

ARCH

ITEC

TURE

Assembly Code

8 17 16 5

Field Valuesop rs rt imm

6 bits 5 bits 5 bits 16 bits

addi $s0, $s1, 5

addi $t0, $s3, -12

lw $t2, 32($0)

sw $s1, 4($t1)

8 19 8 -12

35 0 10 32

43 9 17 4

(0x22300005)

(0x2268FFF4)

(0x8C0A0020)

(0xAD310004)

001000 10001 10000 0000 0000 0000 0101

op rs rt imm

Machine Code

6 bits 5 bits 5 bits 16 bits

001000 10011 01000 1111 1111 1111 0100

100011 00000 01010 0000 0000 0010 0000

101011 01001 10001 0000 0000 0000 0100

Note the differing order of registers in assembly and machine codes:

addi rt, rs, imm

lw rt, imm(rs)

sw rt, imm(rs)

I-Type Examples

Page 39: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <39> CO

MPU

TER

ARCH

ITEC

TURE

op addr6 bits 26 bits

J-Type

• Jump-type• 26-bit address operand (addr)• Used for jump instructions (j: op=2,

jal: op=3)

Machine Language: J-Type

Page 40: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <40> CO

MPU

TER

ARCH

ITEC

TURE

op rs rt rd shamt funct6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

R-Type

op rs rt imm6 bits 5 bits 5 bits 16 bits

I-Type

op addr6 bits 26 bits

J-Type

Review: Instruction Formats

Page 41: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <41> CO

MPU

TER

ARCH

ITEC

TURE

• 32-bit instructions & data stored in memory• Sequence of instructions: only difference

between two applications• To run a new program:

– No rewiring required– Simply store new program in memory

• Program Execution:– Processor fetches (reads) instructions from memory

in sequence (Von Neumann architecture)– Processor performs the specified operation

Power of the Stored Program

Page 42: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <42> CO

MPU

TER

ARCH

ITEC

TURE

addi $t0, $s3, -12

Machine CodeAssembly Code

lw $t2, 32($0)

add $s0, $s1, $s2

sub $t0, $t3, $t5

0x8C0A0020

0x02328020

0x2268FFF4

0x016D4022

Address Instructions

0040000C 0 1 6 D 4 0 2 2

2 2 6 8 F F F 4

0 2 3 2 8 0 2 0

8 C 0 A 0 0 2 0

00400008

00400004

00400000

Stored Program

Main Memory

PC

The Stored Program

Program Counter (PC): keeps track of current instruction

Page 43: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <43> CO

MPU

TER

ARCH

ITEC

TURE

001000 10001 10111 1111 1111 1111 0001

op rs rt imm

addi $s7, $s1, -15

Machine Code Assembly Code

8 17 23 -15

Field Values

(0x2237FFF1)

op rs rt imm

2 2 3 7 F F F 1

000000 10111 10011 01000 00000 100010

op rs rt rd shamt funct

sub $t0, $s7, $s3 0 23 19 8 0 34(0x02F34022)

op rs rt rd shamt funct

0 2 F 3 4 0 2 2

• Start with opcode: tells how to parse rest• If opcode all 0’s

– R-type instruction– Function bits tell operation

• Otherwise– opcode tells operation

Interpreting Machine Code

Page 44: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <44> CO

MPU

TER

ARCH

ITEC

TURE

• High-level languages: – e.g., C, Java, Python– Written at higher level of abstraction

• Common high-level software constructs:– if/else statements– for loops– while loops– arrays– function calls

Programming

Page 45: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <45> CO

MPU

TER

ARCH

ITEC

TURE

• Wrote the first computer program

• Her program calculated the Bernoulli numbers on Charles Babbage’s Analytical Engine

• She was the daughter of the poet Lord Byron

Ada Lovelace, 1815-1852

Page 46: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <46> CO

MPU

TER

ARCH

ITEC

TURE

• and, or, xor, nor– and: useful for masking bits

• Masking all but the least significant byte of a value: 0xF234012F AND 0x000000FF = 0x0000002F

– or: useful for combining bit fields• Combine 0xF2340000 with 0x000012BC:

0xF2340000 OR 0x000012BC = 0xF23412BC– nor: useful for inverting bits:

• A NOR $0 = NOT A

• andi, ori, xori– 16-bit immediate is zero-extended (not sign-extended)– nori not needed

Logical Instructions

Page 47: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <47> CO

MPU

TER

ARCH

ITEC

TURE

1111 1111 1111 1111 0000 0000 0000 0000$s1

0100 0110 1010 0001 1111 0000 1011 0111$s2

$s3

$s4

$s5

$s6

Source Registers

ResultAssembly Code

and $s3, $s1, $s2

or $s4, $s1, $s2

xor $s5, $s1, $s2

nor $s6, $s1, $s2

Logical Instructions Example 1

Page 48: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <48> CO

MPU

TER

ARCH

ITEC

TURE

1111 1111 1111 1111 0000 0000 0000 0000$s1

0100 0110 1010 0001 1111 0000 1011 0111$s2

0100 0110 1010 0001 0000 0000 0000 0000$s3

1111 1111 1111 1111 1111 0000 1011 0111$s4

1011 1001 0101 1110 1111 0000 1011 0111$s5

0000 0000 0000 0000 0000 1111 0100 1000$s6

Source Registers

ResultAssembly Code

and $s3, $s1, $s2

or $s4, $s1, $s2

xor $s5, $s1, $s2

nor $s6, $s1, $s2

Logical Instructions Example 1

Page 49: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <49> CO

MPU

TER

ARCH

ITEC

TURE

0000 0000 0000 0000 0000 0000 1111 1111$s1

Assembly Code

0000 0000 0000 0000 1111 1010 0011 0100imm

$s2

$s3

$s4

andi $s2, $s1, 0xFA34

Source Values

Result

ori $s3, $s1, 0xFA34

xori $s4, $s1, 0xFA34

zero-extended

Logical Instructions Example 2

Page 50: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <50> CO

MPU

TER

ARCH

ITEC

TURE

0000 0000 0000 0000 0000 0000 1111 1111$s1

Assembly Code

0000 0000 0000 0000 1111 1010 0011 0100imm

0000 0000 0000 0000 0000 0000 0011 0100$s2

0000 0000 0000 0000 1111 1010 1111 1111$s3

0000 0000 0000 0000 1111 1010 1100 1011$s4

andi $s2, $s1, 0xFA34

Source Values

Result

ori $s3, $s1, 0xFA34

xori $s4, $s1, 0xFA34

zero-extended

Logical Instructions Example 2

Page 51: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <51> CO

MPU

TER

ARCH

ITEC

TURE

• sll: shift left logical– Example: sll $t0, $t1, 5 # $t0 <= $t1 << 5

• srl: shift right logical– Example: srl $t0, $t1, 5 # $t0 <= $t1 >> 5

• sra: shift right arithmetic– Example: sra $t0, $t1, 5 # $t0 <= $t1 >>> 5

Shift Instructions

Page 52: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <52> CO

MPU

TER

ARCH

ITEC

TURE

• sllv: shift left logical variable– Example: sllv $t0, $t1, $t2 # $t0 <= $t1 << $t2

• srlv: shift right logical variable– Example: srlv $t0, $t1, $t2 # $t0 <= $t1 >> $t2

• srav: shift right arithmetic variable– Example: srav $t0, $t1, $t2 # $t0 <= $t1 >>> $t2

Variable Shift Instructions

Page 53: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <53> CO

MPU

TER

ARCH

ITEC

TURE

sll $t0, $s1, 2

srl $s2, $s1, 2

sra $s3, $s1, 2

Assembly Code

0 0 17 8 2 0

Field Valuesop rs rt rd shamt funct

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

0 0 17 18 2 2

0 0 17 19 2 3

000000 00000 10001 01000 00010 000000

op rs rt rd shamt funct

Machine Code

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

000000 00000 10001 10010 00010 000010

000000 00000 10001 10011 00010 000011

(0x00114080)

(0x00119082)

(0x00119883)

Shift Instructions

Page 54: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <54> CO

MPU

TER

ARCH

ITEC

TURE

• Make 16-bit constants using addi:

• Make 32-bit constants using load upper immediate (lui) and ori:

C Code

int a = 0xFEDC8765;

MIPS assembly code# $s0 = alui $s0, 0xFEDCori $s0, $s0, 0x8765

C Code// int is a 32-bit signed wordint a = 0x4f3c;

MIPS assembly code# $s0 = aaddi $s0, $0, 0x4f3c

Generating Constants

Page 55: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <55> CO

MPU

TER

ARCH

ITEC

TURE

• Special registers: lo, hi• 32 × 32 multiplication, 64 bit result

– mult $s0, $s1 – Result in {hi, lo}

• 32-bit division, 32-bit quotient, remainder– div $s0, $s1 – Quotient in lo– Remainder in hi

• Moves from lo/hi special registers– mflo $s2– mfhi $s3

Multiplication, Division

Page 56: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <56> CO

MPU

TER

ARCH

ITEC

TURE

• Execute instructions out of sequence• Types of branches:

– Conditional• branch if equal (beq)• branch if not equal (bne)

– Unconditional• jump (j)• jump register (jr)• jump and link (jal)

Branching

Page 57: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <57> CO

MPU

TER

ARCH

ITEC

TURE

addi $t0, $s3, -12

Machine CodeAssembly Code

lw $t2, 32($0)

add $s0, $s1, $s2

sub $t0, $t3, $t5

0x8C0A0020

0x02328020

0x2268FFF4

0x016D4022

Address Instructions

0040000C 0 1 6 D 4 0 2 2

2 2 6 8 F F F 4

0 2 3 2 8 0 2 0

8 C 0 A 0 0 2 0

00400008

00400004

00400000

Stored Program

Main Memory

PC

Review: The Stored Program

Page 58: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <58> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly addi $s0, $0, 4 # $s0 = 0 + 4 = 4 addi $s1, $0, 1 # $s1 = 0 + 1 = 1 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4 beq $s0, $s1, target # branch is taken addi $s1, $s1, 1 # not executed sub $s1, $s1, $s0 # not executed

target: # label add $s1, $s1, $s0 # $s1 = 4 + 4 = 8

Labels indicate instruction location. They can’t be reserved words and must be followed by colon (:)

Conditional Branching (beq)

Page 59: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <59> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly addi $s0, $0, 4 # $s0 = 0 + 4 = 4 addi $s1, $0, 1 # $s1 = 0 + 1 = 1 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4 bne $s0, $s1, target # branch not taken addi $s1, $s1, 1 # $s1 = 4 + 1 = 5 sub $s1, $s1, $s0 # $s1 = 5 – 4 = 1

target: add $s1, $s1, $s0 # $s1 = 1 + 4 = 5

The Branch Not Taken (bne)

Page 60: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <60> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly addi $s0, $0, 4 # $s0 = 4 addi $s1, $0, 1 # $s1 = 1 j target # jump to target sra $s1, $s1, 2 # not executed addi $s1, $s1, 1 # not executed sub $s1, $s1, $s0 # not executed

target: add $s1, $s1, $s0 # $s1 = 1 + 4 = 5

Unconditional Branching (j)

Page 61: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <61> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly0x00002000 addi $s0, $0, 0x20100x00002004 jr $s0 0x00002008 addi $s1, $0, 10x0000200C sra $s1, $s1, 20x00002010 lw $s3, 44($s1)

jr is an R-type instruction.

Unconditional Branching (jr)

Page 62: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <62> CO

MPU

TER

ARCH

ITEC

TURE

• if statements• if/else statements• while loops• for loops

High-Level Code Constructs

Page 63: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <63> CO

MPU

TER

ARCH

ITEC

TURE

C Code

if (i == j) f = g + h;

f = f – i;

MIPS assembly code# $s0 = f, $s1 = g, $s2 = h# $s3 = i, $s4 = j

If Statement

Page 64: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <64> CO

MPU

TER

ARCH

ITEC

TURE

C Code

if (i == j) f = g + h;

f = f – i;

MIPS assembly code# $s0 = f, $s1 = g, $s2 = h# $s3 = i, $s4 = j bne $s3, $s4, L1 add $s0, $s1, $s2

L1: sub $s0, $s0, $s3

Assembly tests opposite case (i != j) of high-level code (i == j)

If Statement

Page 65: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <65> CO

MPU

TER

ARCH

ITEC

TURE

C Code

if (i == j) f = g + h;else f = f – i;

MIPS assembly code

If/Else Statement

Page 66: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <66> CO

MPU

TER

ARCH

ITEC

TURE

C Code

if (i == j) f = g + h;else f = f – i;

MIPS assembly code# $s0 = f, $s1 = g, $s2 = h# $s3 = i, $s4 = j bne $s3, $s4, L1 add $s0, $s1, $s2 j doneL1: sub $s0, $s0, $s3done:

If/Else Statement

Page 67: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <67> CO

MPU

TER

ARCH

ITEC

TURE

C Code// determines the power// of x such that 2x = 128int pow = 1;int x = 0;

while (pow != 128) { pow = pow * 2; x = x + 1;}

MIPS assembly code

Assembly tests for the opposite case (pow == 128) of the C code (pow != 128).

While Loops

Page 68: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <68> CO

MPU

TER

ARCH

ITEC

TURE

C Code// determines the power// of x such that 2x = 128int pow = 1;int x = 0;

while (pow != 128) { pow = pow * 2; x = x + 1;}

MIPS assembly code# $s0 = pow, $s1 = x

addi $s0, $0, 1 add $s1, $0, $0 addi $t0, $0, 128while: beq $s0, $t0, done sll $s0, $s0, 1 addi $s1, $s1, 1 j whiledone:

Assembly tests for the opposite case (pow == 128) of the C code (pow != 128).

While Loops

Page 69: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <69> CO

MPU

TER

ARCH

ITEC

TURE

for (initialization; condition; loop operation)

statement

• initialization: executes before the loop begins• condition: is tested at the beginning of each iteration• loop operation: executes at the end of each iteration• statement: executes each time the condition is met

For Loops

Page 70: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <70> CO

MPU

TER

ARCH

ITEC

TURE

High-level code// add the numbers from 0 to 9int sum = 0;int i;

for (i=0; i!=10; i = i+1) { sum = sum + i;}

MIPS assembly code# $s0 = i, $s1 = sum

For Loops

Page 71: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <71> CO

MPU

TER

ARCH

ITEC

TURE

C Code// add the numbers from 0 to 9int sum = 0;int i;

for (i=0; i!=10; i = i+1) { sum = sum + i;}

MIPS assembly code

For Loops

Page 72: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <72> CO

MPU

TER

ARCH

ITEC

TURE

C Code// add the numbers from 0 to 9int sum = 0;int i;

for (i=0; i!=10; i = i+1) { sum = sum + i;}

MIPS assembly code# $s0 = i, $s1 = sum addi $s1, $0, 0 add $s0, $0, $0 addi $t0, $0, 10for: beq $s0, $t0, done add $s1, $s1, $s0 addi $s0, $s0, 1 j fordone:

For Loops

Page 73: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <73> CO

MPU

TER

ARCH

ITEC

TURE

C Code// add the powers of 2 from 1 // to 100int sum = 0;int i;

for (i=1; i < 101; i = i*2) { sum = sum + i;}

MIPS assembly code

Less Than Comparison

Page 74: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <74> CO

MPU

TER

ARCH

ITEC

TURE

C Code// add the powers of 2 from 1 // to 100int sum = 0;int i;

for (i=1; i < 101; i = i*2) { sum = sum + i;}

MIPS assembly code# $s0 = i, $s1 = sum addi $s1, $0, 0 addi $s0, $0, 1 addi $t0, $0, 101loop: slt $t1, $s0, $t0 beq $t1, $0, done add $s1, $s1, $s0 sll $s0, $s0, 1 j loopdone:

$t1 = 1 if i < 101

Less Than Comparison

Page 75: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <75> CO

MPU

TER

ARCH

ITEC

TURE

• Access large amounts of similar data• Index: access each element• Size: number of elements

Arrays

Page 76: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <76> CO

MPU

TER

ARCH

ITEC

TURE

array[4]array[3]

array[2]array[1]array[0]0x12348000

0x12348004

0x123480080x1234800C

0x12340010

• 5-element array• Base address = 0x12348000 (address of first element, array[0])

• First step in accessing an array: load base address into a register

Arrays

Page 77: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <77> CO

MPU

TER

ARCH

ITEC

TURE

// C Codeint array[5];array[0] = array[0] * 2;array[1] = array[1] * 2;

Accessing Arrays

Page 78: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <78> CO

MPU

TER

ARCH

ITEC

TURE

// C Codeint array[5];array[0] = array[0] * 2;array[1] = array[1] * 2;

# MIPS assembly code# array base address = $s0 lui $s0, 0x1234 # 0x1234 in upper half of $S0 ori $s0, $s0, 0x8000 # 0x8000 in lower half of $s0

lw $t1, 0($s0) # $t1 = array[0] sll $t1, $t1, 1 # $t1 = $t1 * 2 sw $t1, 0($s0) # array[0] = $t1

lw $t1, 4($s0) # $t1 = array[1] sll $t1, $t1, 1 # $t1 = $t1 * 2 sw $t1, 4($s0) # array[1] = $t1

Accessing Arrays

Page 79: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <79> CO

MPU

TER

ARCH

ITEC

TURE

// C Codeint array[1000];int i;

for (i=0; i < 1000; i = i + 1) array[i] = array[i] * 8;

# MIPS assembly code# $s0 = array base address, $s1 = i

Arrays using For Loops

Page 80: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <80> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly code# $s0 = array base address, $s1 = i# initialization code lui $s0, 0x23B8 # $s0 = 0x23B80000 ori $s0, $s0, 0xF000 # $s0 = 0x23B8F000 addi $s1, $0, 0 # i = 0 addi $t2, $0, 1000 # $t2 = 1000

loop: slt $t0, $s1, $t2 # i < 1000? beq $t0, $0, done # if not then done sll $t0, $s1, 2 # $t0 = i * 4 (byte offset) add $t0, $t0, $s0 # address of array[i] lw $t1, 0($t0) # $t1 = array[i] sll $t1, $t1, 3 # $t1 = array[i] * 8 sw $t1, 0($t0) # array[i] = array[i] * 8 addi $s1, $s1, 1 # i = i + 1 j loop # repeatdone:

Arrays Using For Loops

Page 81: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <81> CO

MPU

TER

ARCH

ITEC

TURE

• American Standard Code for Information Interchange

• Each text character has unique byte value– For example, S = 0x53, a = 0x61, A = 0x41– Lower-case and upper-case differ by 0x20 (32)

ASCII Code

Page 82: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <82> CO

MPU

TER

ARCH

ITEC

TURE ASCII Characters

Page 83: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <83> CO

MPU

TER

ARCH

ITEC

TURE

• ASCII chars are 8-bits, so MIPS offers lb, lbu and sb

• lbu $s1, 3($0) # $s1 Mem[3] (zero-extended byte)• lb $s2, 1($0) # $s2 Mem[1] (sign-extended byte)• sb $s3, 5 ($0) # Mem[5] LSByte of $s3

• For 16-bit UNICODE chars (used in Java): lh, lhu, sh• See www.unicode.org for much more!

Moving Characters

Page 84: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <84> CO

MPU

TER

ARCH

ITEC

TURE

•ASCII strings are arrays of chars, stored in consecutive bytes of memory

• ASCII strings have variable (i.e. unknown) length • Must be terminated w/ NULL character (NULL’s code = 0x00)

• E.g. “Hello!” is 0x48 65 6C 6C 6F 21 00 (7 bytes, not 6!)

ASCII Strings

Page 85: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <85> CO

MPU

TER

ARCH

ITEC

TURE

// C Codechar chararray[10];int i;

for (i=0; chararray[i] != 0; i = i + 1) chararray[i] = chararray[i] - 32;

# MIPS assembly code# uses direct addressing (i.e. pointer to the string)# $s0 = base address of chararray# $s1 is not used: no need for index variable i !

Manipulating ASCII strings

Page 86: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <86> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly code# uses direct addressing (i.e. pointer to the string)# $s0 = base address of chararray

lbu $t2, 0($s0) # get 1st char: $t2= Mem[&chararray] # (= chararray[0])loop: beq $t2, $0, done # if chararray[i]==NULL, exit loop addi $t2, $t2, -32 # convert to upper case: t2=t2-32 sb $t2, 0($s0) # store new value in array: # Mem[chararray[i] = $t2 addi $s0, $s0, 1 # address = address + 1 lbu $t2, 0($s0) # get next char: $t2=Mem[&chararray] # (= chararray[i]) j loop # repeatdone:

Manipulating ASCII strings

Page 87: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <87> CO

MPU

TER

ARCH

ITEC

TURE

• Caller: calling function (in this case, main)• Callee: called function (in this case, sum)

C Codevoid main(){ int y; y = sum(42, 7); ...}

int sum(int a, int b){ return (a + b);}

Function Calls

Page 88: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <88> CO

MPU

TER

ARCH

ITEC

TURE

• Caller:– passes arguments to callee– jumps to callee

• Callee: – performs the function– returns result to caller– returns to point of call– must not overwrite registers or memory needed by

caller

Function Conventions

Page 89: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <89> CO

MPU

TER

ARCH

ITEC

TURE

• Call Function: jump and link (jal) • Return from function: jump register (jr)• Arguments: $a0 - $a3• Return values: $v0, $v1

MIPS Function Conventions

Page 90: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <90> CO

MPU

TER

ARCH

ITEC

TURE

C Codeint main() { simple(); a = b + c;}

void simple() { return;}

MIPS assembly code

0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2...

0x00401020 simple: jr $ra

void means that simple doesn’t return a value

Function Calls

Page 91: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <91> CO

MPU

TER

ARCH

ITEC

TURE

C Codeint main() { simple(); a = b + c;}

void simple() { return;}

MIPS assembly code

0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2...

0x00401020 simple: jr $ra

jal: jumps to simple (PC 0x00401020) $ra = PC + 4 = 0x00400204

jr $ra: jumps to address in $ra (PC 0x00400204)

Function Calls

Page 92: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <92> CO

MPU

TER

ARCH

ITEC

TURE

MIPS conventions:• Argument values: $a0 - $a3• Return values: $v0-$v1

Input Arguments & Return Value

Page 93: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <93> CO

MPU

TER

ARCH

ITEC

TURE

C Codeint main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments ...}

int diffofsums(int f, int g, int h, int i){ int result; result = (f + g) - (h + i); return result; // return value}

Input Arguments & Return Value

Page 94: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <94> CO

MPU

TER

ARCH

ITEC

TURE

MIPS assembly code# $s0 = y

main: ... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call Function add $s0, $v0, $0 # y = returned value ...

# $s0 = resultdiffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

Input Arguments & Return Value

Page 95: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <95> CO

MPU

TER

ARCH

ITEC

TURE

MIPS assembly code# $s0 = resultdiffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

• diffofsums overwrote 3 registers: $t0, $t1, $s0•diffofsums can use stack to temporarily store registers

Input Arguments & Return Value

Page 96: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <96> CO

MPU

TER

ARCH

ITEC

TURE

• Memory used to temporarily save variables

• Like stack of dishes, last-in-first-out (LIFO) queue

• Expands: uses more memory when more space needed (push)

• Contracts: uses less memory when the space is no longer needed (pop)

The Stack

Page 97: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <97> CO

MPU

TER

ARCH

ITEC

TURE

Data

7FFFFFFC 12345678

7FFFFFF8

7FFFFFF4

7FFFFFF0

Address

$sp 7FFFFFFC

7FFFFFF8

7FFFFFF4

7FFFFFF0

Address Data

12345678

$sp

AABBCCDD

11223344

• MIPS stack grows down (from higher to lower memory addresses)

• Stack pointer: $sp points to “top” of the stack, in MIPS the last full location (vs. first empty)

The Stack

Page 98: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <98> CO

MPU

TER

ARCH

ITEC

TURE

# MIPS assembly# $s0 = resultdiffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

• Called functions must have no unintended side effects

• But diffofsums overwrites 3 registers: $t0, $t1, $s0

How Functions use the Stack

Page 99: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <99> CO

MPU

TER

ARCH

ITEC

TURE

# $s0 = resultdiffofsums: addi $sp, $sp, -12 # make space on stack # to store 3 registers sw $s0, 8($sp) # save $s0 on stack sw $t0, 4($sp) # save $t0 on stack sw $t1, 0($sp) # save $t1 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $t1, 0($sp) # restore $t1 from stack lw $t0, 4($sp) # restore $t0 from stack lw $s0, 8($sp) # restore $s0 from stack addi $sp, $sp, 12 # deallocate stack space jr $ra # return to caller

Storing Register Values on the Stack

Page 100: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <100> CO

MPU

TER

ARCH

ITEC

TURE

Data

FC

F8

F4

F0

Address

$sp

(a)

Data

FC

F8

F4

F0

Address

$sp

(b)

$s0

Data

$sp

(c)

$t0

FC

F8

F4

F0

Address

? ??

sta

ck fr

am

e$t1

The stack during diffofsums Call

Page 101: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <101> CO

MPU

TER

ARCH

ITEC

TURE

PreservedCallee-Saved

Non-preservedCaller-Saved

$s0-$s7 $t0-$t9

$ra $a0-$a3

$sp, $fp, $gp $v0-$v1

stack above $sp stack below $sp

Register saving

To share the burden of saving on the stack, the registers are divided into two groups:

Page 102: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <102> CO

MPU

TER

ARCH

ITEC

TURE

proc1: addi $sp, $sp, -4 # make space on stack sw $ra, 0($sp) # save $ra on stack jal proc2 ... lw $ra, 0($sp) # restore $ra from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

Multiple Function Calls

Page 103: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <103> CO

MPU

TER

ARCH

ITEC

TURE

# $s0 = resultdiffofsums: addi $sp, $sp, -4 # make space on stack to

# store one register sw $s0, 0($sp) # save $s0 on stack # no need to save $t0 or $t1 add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $s0, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

Storing Saved Registers on the Stack

Page 104: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <104> CO

MPU

TER

ARCH

ITEC

TURE

High-level codeint factorial(int n) { if (n <= 1) return 1; else return (n * factorial(n-1));}

Recursive Function Call

Page 105: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <105> CO

MPU

TER

ARCH

ITEC

TURE

MIPS assembly code

Recursive Function Call

Page 106: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <106> CO

MPU

TER

ARCH

ITEC

TURE

MIPS assembly code

0x90 factorial: addi $sp, $sp, -8 # make room for 2 items0x94 sw $a0, 4($sp) # push $a00x98 sw $ra, 0($sp) # push $ra0x9C addi $t0, $0, 2 0xA0 slt $t0, $a0, $t0 # a <= 1 ?0xA4 beq $t0, $0, else # no: go to else 0xA8 addi $v0, $0, 1 # yes: return 10xAC addi $sp, $sp, 8 # restore $sp0xB0 jr $ra # return0xB4 else: addi $a0, $a0, -1 # n = n - 10xB8 jal factorial # recursive call: # factorial(n-1)0xBC lw $a0, 4($sp) # pop $a0 (= saved n)0xC0 mul $v0, $a0, $v0 # n * factorial(n-1)0xC4 lw $ra, 0($sp) # pop $ra0xC8 addi $sp, $sp, 8 # restore $sp0xCC jr $ra # return

Recursive Function Call

Page 107: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <107> CO

MPU

TER

ARCH

ITEC

TURE

$sp FC

F8

F4

F0

$ra

EC

E8

E4

E0

DC

FC

F8

F4

F0

EC

E8

E4

E0

DC

FC

F8

F4

F0

EC

E8

E4

E0

DC

$sp

$sp

$sp

$sp

$a0 = 1$v0 = 1 x 1

$a0 = 2$v0 = 2 x 1

$a0 = 3$v0 = 3 x 2

$v0 = 6

$sp

$sp

$sp

$sp

DataAddress DataAddress DataAddress

$a0 (0x3)

$ra (0xBC)

$a0 (0x2)

$ra (0xBC)

$a0 (0x1)

$ra

$a0 (0x3)

$ra (0xBC)

$a0 (0x2)

$ra (0xBC)

$a0 (0x1)

Stack During Recursive Call

Page 108: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <108> CO

MPU

TER

ARCH

ITEC

TURE

• Caller– Put arguments in $a0-$a3– Save any needed registers ($ra, maybe $t0-t9)– jal callee– Restore registers– Look for result in $v0

• Callee– Save registers that would be changed ($s0-$s7,etc)– Perform function (using arguments in $a0-$a3)– Put result in $v0– Restore registers that were saved– jr $ra

Function Call Summary

Page 109: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <109> CO

MPU

TER

ARCH

ITEC

TURE

How do we address the operands?• Register Only• Immediate• Base Addressing

How do we address the next instruction?• (Default: PC <= PC + 4 )• PC-Relative• Pseudo Direct

Addressing Modes

Page 110: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <110> CO

MPU

TER

ARCH

ITEC

TURE

Register Only• Operands found in registers

– Example: add $s0, $t2, $t3– Example: sub $t8, $s1, $0

Immediate• 16-bit immediate (extended to 32-bits) used as an

operand– Example: addi $s4, $t5, -73 # sign-extend– Example: ori $t3, $t7, 0xFF # zero-extend

Addressing Modes

Page 111: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <111> CO

MPU

TER

ARCH

ITEC

TURE

Base Addressing• Address of operand is:

base address + sign-extended immediate

– Example: lw $s4, 72($0)• address = $0 + 72

– Example: sw $t2, -25($t1)• address = $t1 - 25

Addressing Modes

Page 112: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <112> CO

MPU

TER

ARCH

ITEC

TURE

PC-Relative Addressing (used in beq and bne)

0x10 beq $t0, $0, else0x14 addi $v0, $0, 1 0x18 addi $sp, $sp, i 0x1C jr $ra0x20 else: addi $a0, $a0, -10x24 jal factorial

beq $t0, $0, else

Assembly Code Field Values

4 8 0 3

op rs rt imm

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits(beq $t0, $0, 3)

Addressing Modes

Page 113: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <113> CO

MPU

TER

ARCH

ITEC

TURE

Pseudo-direct Addressing (used in J-types)

0x0040005C jal sum...0x004000A0 sum: add $v0, $a0, $a1

0000 0000 0100 0000 0000 0000 1010 0000JTA

26-bit addr (0x0100028)

(0x004000A0)

0000 0000 0100 0000 0000 0000 1010 0000

0 1 0 0 0 2 8

000011 00 0001 0000 0000 0000 0010 1000

op addr

Machine CodeField Values

3 0x0100028

6 bits 26 bits

(0x0C100028)

op imm

6 bits 26 bits

Addressing Modes

Page 114: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <114> CO

MPU

TER

ARCH

ITEC

TURE

Assembly Code

High Level Code

Compiler

Object File

Assembler

Executable

Linker

Memory

Loader

Object FilesLibrary Files

How to Compile & Run a Program

Page 115: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <115> CO

MPU

TER

ARCH

ITEC

TURE

• Graduated from Yale University with a Ph.D. in mathematics

• Developed first compiler• Helped develop the COBOL

programming language• Highly awarded naval officer• Received World War II

Victory Medal and National Defense Service Medal, among others

Grace Hopper, 1906-1992

Page 116: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <116> CO

MPU

TER

ARCH

ITEC

TURE

• Instructions (in the .text segment)• Data

– Global/static: allocated before program begins (in the .data segment)

– Dynamic: allocated within program (heap and stack)

• How big is memory?– At most 232 = 4 gigabytes (4 GB)– From address 0x00000000 to 0xFFFFFFFF

What is Stored in Memory?

Page 117: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <117> CO

MPU

TER

ARCH

ITEC

TURE

SegmentAddress

0xFFFFFFFC

0x80000000

0x7FFFFFFC

0x10010000

0x1000FFFC

0x10000000

0x0FFFFFFC

0x00400000

0x003FFFFC

0x00000000

Reserved

Stack

Heap

Static Data

Text

Reserved

Dynamic Data

MIPS Memory Map

Page 118: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <118> CO

MPU

TER

ARCH

ITEC

TURE

int f, g, y; // global variables

int main(void) { f = 2; g = 3; y = sum(f, g);

return y;}

int sum(int a, int b) { return (a + b);}

Example Program: C Code

Page 119: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <119> CO

MPU

TER

ARCH

ITEC

TURE

int f, g, y; // global

int main(void) {

f = 2; g = 3;

y = sum(f, g); return y;}

int sum(int a, int b) { return (a + b);}

.dataf:g:y:

.textmain: addi $sp, $sp, -4 # push 1

item sw $ra, 0($sp) # store $ra addi $a0, $0, 2 # $a0 = 2 sw $a0, f # f = 2 addi $a1, $0, 3 # $a1 = 3 sw $a1, g # g = 3 jal sum # call sum sw $v0, y # y = sum() lw $ra, 0($sp) # restore

$ra addi $sp, $sp, 4 # pop 1 item jr $ra # return to

OSsum: add $v0, $a0, $a1 # $v0 = a +

b jr $ra # return

Example Program: MIPS Assembly

Page 120: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <120> CO

MPU

TER

ARCH

ITEC

TURE

Symbol Address

Example Program: Symbol Table

Page 121: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <121> CO

MPU

TER

ARCH

ITEC

TURE

Symbol Address

f 0x10000000

g 0x10000004

y 0x10000008

main 0x00400000

sum 0x0040002C

Example Program: Symbol Table

Page 122: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <122> CO

MPU

TER

ARCH

ITEC

TURE

Executable file header Text Size Data Size

Text segment

Data segment

Address Instruction

Address Data

0x00400000

0x00400004

0x00400008

0x0040000C

0x00400010

0x00400014

0x00400018

0x0040001C

0x00400020

0x00400024

0x00400028

0x0040002C

0x00400030

addi $sp, $sp, -4

sw $ra, 0 ($sp)

addi $a0, $0, 2

sw $a0, 0x8000 ($gp)

addi $a1, $0, 3

sw $a1, 0x8004 ($gp)

jal 0x0040002C

sw $v0, 0x8008 ($gp)

lw $ra, 0 ($sp)

addi $sp, $sp, -4

jr $ra

add $v0, $a0, $a1

jr $ra

0x10000000

0x10000004

0x10000008

f

g

y

0xC (12 bytes)0x34 (52 bytes)

0x23BDFFFC

0xAFBF0000

0x20040002

0xAF848000

0x20050003

0xAF858004

0x0C10000B

0xAF828008

0x8FBF0000

0x23BD0004

0x03E00008

0x00851020

0x03E00008

Example Program: Executable

Page 123: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <123> CO

MPU

TER

ARCH

ITEC

TURE

y

g

f

0x03E00008

0x00851020

0x03E00008

0x23BD0004

0x8FBF0000

0xAF828008

0x0C10000B

0xAF858004

0x20050003

0xAF848000

0x20040002

0xAFBF0000

0x23BDFFFC

MemoryAddress

$sp = 0x7FFFFFFC0x7FFFFFFC

0x10010000

0x00400000

Stack

Heap

$gp = 0x10008000

PC = 0x00400000

0x10000000

Reserved

Reserved

Example Program: In Memory

Page 124: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <124> CO

MPU

TER

ARCH

ITEC

TURE

• Pseudo-instructions• Exceptions• Signed and unsigned instructions• Floating-point instructions

Odds & Ends

Page 125: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <125> CO

MPU

TER

ARCH

ITEC

TURE

Pseudo-instruction MIPS Instructionsli $s0, 0x1234AA77 lui $s0, 0x1234

ori $s0, 0xAA77

clear $t0 add $t0, $0, $0

move $s1, $s2 add $s2, $s1, $0

nop sll $0, $0, 0

Pseudo-instructions

The assembler implements a virtual machine, by adding pseudo-instruction capability to the actual MIPS architecture. There are many possible pseudo-instructions (e.g. beq $s1, 22, Branch_target )

BUT: the pseudo-instructions vary between assemblers, so using them could mean your code is no longer “portable”, it is assembler specific!

Page 126: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <126> CO

MPU

TER

ARCH

ITEC

TURE

• Unscheduled function call to exception handler (part of the OS)

• Caused by:– Hardware, also called an interrupt, e.g., keyboard– Software, also called traps, e.g., undefined instruction

• When exception occurs, the processor:– Records the cause of the exception– Jumps to exception handler (at instruction address

0x80000180 in MIPS)– Deals with the problem causing the exception– Returns to program

Exceptions

Page 127: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <127> CO

MPU

TER

ARCH

ITEC

TURE

• Not part of processor’s register file– Cause: Records cause of exception– EPC (Exception PC): Records PC where exception

occurred• EPC and Cause: part of Coprocessor 0• Move from Coprocessor 0

– mfc0 $t0, EPC– Moves contents of EPC into $t0

Exception Registers

Page 128: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <128> CO

MPU

TER

ARCH

ITEC

TURE

Exception CauseHardware Interrupt 0x00000000

System Call 0x00000020

Breakpoint / Divide by 0 0x00000024

Undefined Instruction 0x00000028

Arithmetic Overflow 0x00000030

. . . (lots more—see Green Card) . . .

Exception Causes

Page 129: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <129> CO

MPU

TER

ARCH

ITEC

TURE

• Processor saves: Cause code for exception type

and EPCPC value that caused the exception • Processor jumps to exception handler (0x80000180)• Exception handler:

– Saves registers on stack– Reads Cause register

mfc0 $t0, Cause– Handles exception– Restores registers– Returns to program

mfc0 $k0, EPC jr $k0

Exception Flow

Page 130: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <130> CO

MPU

TER

ARCH

ITEC

TURE

• Addition and subtraction• Multiplication and division• Set less than

Signed & Unsigned Instructions

Page 131: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <131> CO

MPU

TER

ARCH

ITEC

TURE

• Signed: add, addi, sub– Same operation as unsigned versions– But processor causes exception on overflow

• Unsigned: addu, addiu, subu– Doesn’t cause exception on overflow

Note: C language ignores exceptions; FORTRAN and others require the program be notified.

Beware: addiu sign-extends the immediate, before treating it as an unsigned number !

Addition & Subtraction

Page 132: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <132> CO

MPU

TER

ARCH

ITEC

TURE

• Signed: mult, div• Unsigned: multu, divu

Multiplication & Division

0xFFFFFFFF = -1 (signed) or = 2^32 -1 (unsigned)

So0xFFFFFFFF x 0xFFFFFFFF = ?

= 0x0000000000000001 w/ mult (signed)= 0xFFFFFFFE00000001 w/ multu (unsigned)

Page 133: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <133> CO

MPU

TER

ARCH

ITEC

TURE

• Signed: slt, sltitreats operands as signed

• Unsigned: sltu, sltiutreats operands as unsigned

Beware: sltiu sign-extends the immediate, before comparing it to the register, treating both as unsigned

Set Less Than

Page 134: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <134> CO

MPU

TER

ARCH

ITEC

TURE

• Signed:– Sign-extends to create 32-bit value to load into

register– Load halfword: lh– Load byte: lb

• Unsigned:– Zero-extends to create 32-bit value– Load halfword unsigned: lhu– Load byte: lbu

Loads

Page 135: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <135> CO

MPU

TER

ARCH

ITEC

TURE

• Floating-point coprocessor (Coprocessor 1)• 32 32-bit floating-point registers ($f0-$f31)• Double-precision values held in two floating

point registers– e.g., $f0 and $f1, $f2 and $f3, etc.– Double-precision floating point registers: $f0, $f2, $f4, etc.

Floating-Point Instructions

Page 136: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <136> CO

MPU

TER

ARCH

ITEC

TURE

Name Register Number Usage

$fv0 - $fv1 0, 2 return values

$ft0 - $ft3 4, 6, 8, 10 temporary variables

$fa0 - $fa1 12, 14 Function arguments

$ft4 - $ft8 16, 18 temporary variables

$fs0 - $fs5 20, 22, 24, 26, 28, 30 saved variables

Floating-Point Registers

Page 137: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <137> CO

MPU

TER

ARCH

ITEC

TURE

op cop ft fs fd funct6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

F-Type

• Opcode = 17 (0100012)

• Single-precision: – cop = 16 (0100002)

– add.s, sub.s, div.s, neg.s, abs.s, etc.

• Double-precision: – cop = 17 (0100012)

– add.d, sub.d, div.d, neg.d, abs.d, etc.

• 3 register operands:– fs, ft: source operands– fd: destination operands

F-Type Instruction Format

Page 138: Chapter 6 Digital Design and Computer Architecture, 2 nd Edition Chapter 6 David Money Harris and Sarah L. Harris.

Chapter 6 <138> CO

MPU

TER

ARCH

ITEC

TURE

• Set/clear condition flag: fpcond– Equality: c.seq.s, c.seq.d– Less than: c.lt.s, c.lt.d– L.T. or equal: c.le.s, c.le.d

• Conditional branch– bclf: branches if fpcond is FALSE– bclt: branches if fpcond is TRUE

• Loads and stores – lwc1: lwc1 $ft1, 42($s1)– swc1: swc1 $fs2, 17($sp)

Floating-Point Branches


Recommended