MIPS Assembly 2 Lab Schedule Activities Assignments Due This - - PowerPoint PPT Presentation

mips assembly
SMART_READER_LITE
LIVE PREVIEW

MIPS Assembly 2 Lab Schedule Activities Assignments Due This - - PowerPoint PPT Presentation

Computer Systems and Networks ECPE 170 Jeff Shafer University of the Pacific MIPS Assembly 2 Lab Schedule Activities Assignments Due This Week Lab 10 Due by Apr 12 th 5:00am MIPS discussion Practice problems


slide-1
SLIDE 1

ì

Computer Systems and Networks

ECPE 170 – Jeff Shafer – University of the Pacific

MIPS Assembly

slide-2
SLIDE 2

Lab Schedule

Activities

ì

This Week

ì

MIPS discussion

ì

Practice problems (whiteboard)

ì

Using the QtSPIM simulator

ì

Discuss available resources

Assignments Due

ì

Lab 10

ì

Due by Apr 12th 5:00am ì

Lab 11

ì

Due by Apr 19th 5:00am ì

Lab 12

ì

Due by May 3rd 5:00am

Spring 2017 Computer Systems and Networks

2

slide-3
SLIDE 3

Person of the Day – John Cocke

ì

Computer architecture pioneer

ì

“Father of RISC Architecture”

ì

Developed IBM 801 processor, 1975-1980 ì

Winner, ACM Turing Award, 1987

Spring 2017 Computer Systems and Networks

3

RISC = Reduced Instruction Set Computing Achieve higher performance with simple instructions that execute faster

slide-4
SLIDE 4

Person of the Day – John Hennessy

ì

Computer architecture pioneer

ì

Popularized RISC architecture in early 1980’s

ì

Founder of MIPS Computer Systems in 1984

ì

Currently president of an obscure school: Stanford University

Spring 2017 Computer Systems and Networks

4

slide-5
SLIDE 5

Class to Date

Spring 2017 Computer Systems and Networks

5

Human (C Code) Compiler (Assembly code) Compiler (Object file / binary code) Linker (Executable program)

slide-6
SLIDE 6

Class Now

Spring 2017 Computer Systems and Networks

6

Human (Assembly code) Assembler (Object file / binary code) Linker (Executable Program)

slide-7
SLIDE 7

ì

MIPS

Spring 2017 Computer Systems and Networks

7

slide-8
SLIDE 8

MIPS Overview

ì Family of computer processors first introduced in

1981

ì Microprocessor without Interlocked Pipeline Stages

ì

Original acronym

ì

Now MIPS stands for nothing at all…

Spring 2017 Computer Systems and Networks

8

slide-9
SLIDE 9

MIPS Products

ì

Embedded devices

ì

Cisco/Linksys routers

ì

Cable boxes

ì

MIPS processor is buried inside System-on-a-Chip (SOC) ì

Gaming / entertainment

ì

Nintendo 64

ì

Playstation, Playstation 2, PSP ì

Computers?

ì

Not so much anymore…

ì

SGI / DEC / NEC workstations back in 1990’s

Spring 2017 Computer Systems and Networks

9

slide-10
SLIDE 10

MIPS Products

ì

NASA New Horizons probe

ì

Launched January 2006 ì

MIPS “Mongoose-V” chip

ì

12 MhZ

(2006, remember?)

ì

Radiation Hardened

ì

Based on R3000 (PlayStation CPU)

Spring 2017 Computer Systems and Networks

10 http://blog.imgtec.com/mips-processors/mips-goes-to-pluto http://synova.com/proc/MongooseV.pdf

slide-11
SLIDE 11

MIPS Design

ì RISC – What does this mean?

ì

Reduced Instruction Set Computing

ì

Simplified design for instructions

ì

Use more instructions to accomplish same task

ì But each instruction runs much faster!

ì 32 bits (originally) – What does this mean?

ì

1 “word”= 32 bits

ì

Size of data processed by an integer add instruction

ì

New(er) MIPS64 design is 64 bits, but we won’t focus on that

Spring 2017 Computer Systems and Networks

11

slide-12
SLIDE 12

ì

MIPS Assembly Programming

Spring 2017 Computer Systems and Networks

12

slide-13
SLIDE 13

Quotes – Donald Knuth

Spring 2017 Computer Systems and Networks

13

“People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird.” – Donald Knuth

This is your motivation in the assembly labs!

slide-14
SLIDE 14

Why Learn Assembly Programming?

ì

Computer Science track

ì

Understand capabilities (and limitations) of physical machine

ì

Ability to optimize program performance (or functionality) at the assembly level if necessary ì

Computer Engineer track

ì

Future courses (e.g. ECPE 173) will focus on processor design

ì

Start at the assembly programming level and move into hardware

ì How does the processor implement the add instruction? ì How does the processor know what data to process?

Spring 2017 Computer Systems and Networks

14

slide-15
SLIDE 15

Instruction Set Architecture

ì Instruction Set Architecture (ISA) is the interface

between hardware and software

ì

Specifies the format of processor instructions

ì

Specifies the format of memory addresses (and addressing modes)

ì

Specifies the primitive operations the processor can perform

Spring 2017 Computer Systems and Networks

15

slide-16
SLIDE 16

Instruction Set Architecture

ì ISA is the “contract” between the hardware

designer and the assembly-level programmer

ì Documented in a manual that can be hundreds or

thousands of pages long

ì

Example: Intel 64 and IA-32 Architectures Software Developers Manual

ì

http://www.intel.com/content/www/us/en/process

  • rs/architectures-software-developer-manuals.html

ì

No joke – the manual PDF from December 2015 is 3883 pages long!

Spring 2017 Computer Systems and Networks

16

slide-17
SLIDE 17

Instruction Set Architecture

ì Processor families share the same ISA ì Example ISAs:

ì

Intel x86

ì

Intel / AMD x86-64

ì

Intel Itanium

ì

ARM

ì

IBM PowerPC

ì

MIPS

Spring 2017 Computer Systems and Networks

17

All completely different, in the way that C++, Java, Perl, and PHP are all different… … and yet learning one language makes learning the next one much easier

slide-18
SLIDE 18

Why MIPS?

ì Why choose MIPS?

ì

The MIPS ISA manual (volume 1, at least) is a svelte 108 pages!

ì

Extremely common ISA in textbooks

ì

Freely available simulator

ì

Common embedded processor

ì

Good building-block for other RISC-style processors

ì

Aligns with ECPE 173 course

Spring 2017 Computer Systems and Networks

18

slide-19
SLIDE 19

Arithmetic Instructions

ì Addition ì Subtraction

Spring 2017 Computer Systems and Networks

19

add <result>, <input1>, <input2> sub <result>, <input1>, <input2> Operation / “Op code” Operands

slide-20
SLIDE 20

Task : Write Code

ì Write MIPS assembly for

Spring 2017 Computer Systems and Networks

20

f = (g+h) – (i+j)

add temp0, g, h add temp1, i, j sub f, temp0, temp1

slide-21
SLIDE 21

Spring 2017 Computer Systems and Networks

21

Congratulations! You’re now an assembly programming expert!

slide-22
SLIDE 22

Data Sources

ì Previous example was (just a little bit) fake…

ì

We made up some variables: temp0, temp1, f, g, h, i, and j

ì

This is what you do when programming in C++ (or any high level language)

Spring 2017 Computer Systems and Networks

22

Problem: You can’t make up variables in assembly!

(as least, not in this fashion)

slide-23
SLIDE 23

Data Sources

Spring 2017 Computer Systems and Networks

23

Where can we explicitly place data in assembly programming?

CPU ALU

1.

Registers

ì

On the CPU itself

ì

Very close to ALU

ì

Tiny

ì

Access time: 1 cycle

2.

Memory

ì

Off-chip

ì

Large

ì

Access time: 100+ cycles Cache Memory

slide-24
SLIDE 24

Aside – Cache

ì Review: Does the programmer explicitly manage

the cache?

ì Answer: No!

ì

The assembly programmer just reads/writes memory addresses

ì

Cache is managed automatically in hardware

ì

Result: Memory appears to be faster than it really is

Spring 2017 Computer Systems and Networks

24

slide-25
SLIDE 25

ECPE 71

ì From your knowledge of ECPE 71

(Digital Design), how would you construct a register?

Spring 2017 Computer Systems and Networks

25

Flip Flops! (D Flip Flop shown)

slide-26
SLIDE 26

ECPE 71 – Group of Registers

Spring 2017 Computer Systems and Networks

26

slide-27
SLIDE 27

Registers

ì

MIPS design: 32 integer registers, each holding 32 bits

ì

“Word size” = 32 bits ì

This is only 19 – where are the rest of the 32?

ì

Reserved by convention for other uses

ì

We’ll learn a few more later…

Spring 2017 Computer Systems and Networks

27

Name Use $zero Constant value: ZERO $s0-$s7 Local variables $t0-$t9 Temporary results

slide-28
SLIDE 28

Task : Write Code

ì Write MIPS assembly using registers for:

Spring 2017 Computer Systems and Networks

28

f = (g+h) – (i+j)

Code:

add $t0, $s0, $s1 add $t1, $s2, $s3 sub $s4, $t0, $t1

Map: $s0 = g $s1 = h $s2 = i $s3 = j $s4 = f

slide-29
SLIDE 29

More Arithmetic Instructions

ì Add Immediate

Spring 2017 Computer Systems and Networks

29

addi <result>, <input1>, <constant> Can be a positive or negative number! Register Register

slide-30
SLIDE 30

Task : Write Code

ì Write MIPS assembly using registers for:

Spring 2017 Computer Systems and Networks

30

f = g+20

Code:

addi $s0, $s1, 20

Map: $s0 = f $s1 = g

slide-31
SLIDE 31

Memory

ì Challenge: Limited supply of registers

ì

Physical limitation: We can’t put more on the processor chip, and maintain their current speed

ì

Many elements compete for space in the CPU… ì Solution: Store data in memory ì MIPS provides instructions that transfer data

between memory and registers

Spring 2017 Computer Systems and Networks

31

slide-32
SLIDE 32

Memory Fundamentals

Spring 2017 Computer Systems and Networks

32

MIPS cannot directly manipulate data in memory! Data must be moved to a register first! (And results must be saved to a register when finished)

This is a common design in RISC-style machines: a load-store architecture

slide-33
SLIDE 33

Memory Fundamentals

Spring 2017 Computer Systems and Networks

33

Yes, it’s a pain to keep moving data between registers and memory. But consider it your motivation to reduce the number of memory

  • accesses. That will improve

program performance!

slide-34
SLIDE 34

Memory Fundamentals

ì Four questions to ask when accessing memory:

1.

What direction do I want to copy data? (i.e. to memory, or from memory?)

2.

What is the specific memory address?

3.

What is the specific register name? (or number)

4.

How much data do I want to move?

Spring 2017 Computer Systems and Networks

34

slide-35
SLIDE 35

CPU

Memory – Fundamental Operations

Load

ì

Copy data from memory to register

Store

ì

Copy data from register to memory

Spring 2017 Computer Systems and Networks

35

CPU Memory Memory

slide-36
SLIDE 36

Memory – Determining Address

ì

There are many ways to calculate the desired memory address

ì

These are called addressing modes

ì

We’ll just learn one mode now: base + offset

ì

The base address could be HUGE! (32 bits)

ì

We’ll place it in a register

ì

The offset is typically small

ì

We’ll directly include it in the instruction as an “immediate”

Spring 2017 Computer Systems and Networks

36

Memory 1 2 3 4 Base Offset

MIPS notation: offset(base)

slide-37
SLIDE 37

Memory – Register Name

ì What is the name of the register to use as either

the data destination (for a load) or a data source (for a store)?

ì Use the same register names previously learned

Spring 2017 Computer Systems and Networks

37

slide-38
SLIDE 38

Memory - Data Transfer Size

ì How much data do I want to load or store?

ì

A full word? (32 bits)

ì

A “half word”? (16 bits)

ì

A byte? (8 bits) ì We’ll have a different instruction for each quantity

  • f data

ì No option to load an entire array!

ì

Will need a loop that loads 1 element at a time…

Spring 2017 Computer Systems and Networks

38

slide-39
SLIDE 39

Memory – Data Transfer Instructions

ì Load (copy from memory to register) ì Store (copy from register to memory)

Spring 2017 Computer Systems and Networks

39

lw <reg>, <offset>(<base addr reg>) lb <reg>, <offset>(<base addr reg>) sw <reg>, <offset>(<base addr reg>) sb <reg>, <offset>(<base addr reg>) Word: Byte: Word: Byte: Register Memory Location

slide-40
SLIDE 40

Example

ì What will this instruction do? ì Load word copies from memory to register:

ì

Base address: stored in register $s2

ì

Offset: 20 bytes

ì

Destination register: $s1

ì

Amount of data transferred: 1 word (32 bits)

Spring 2017 Computer Systems and Networks

40

lw $s1, 20($s2)

slide-41
SLIDE 41

Task : Write Code

ì Write MIPS assembly for:

Spring 2017 Computer Systems and Networks

41

g = h + array[16]

(Array of words. Can leave g and h in registers)

Code:

# Assume $s3 is already set

lw $t0, 16($s3) add $s1, $s2, $t0

Map: $s1 = g $s2 = h $s3 = base address of array

slide-42
SLIDE 42

Memory Address

ì Slight flaw in previous solution

ì

The programmer intended to load the 16th array element

ì

Each element is 4 bytes (1 word)

ì

The offset is in bytes

ì

16 * 4 = 64

Spring 2017 Computer Systems and Networks

42

Correct Code:

# Assume $s3 is already set

lw $t0, 64($s3) add $s1, $s2, $t0

slide-43
SLIDE 43

Task : Write Code

ì Write MIPS assembly for:

Spring 2017 Computer Systems and Networks

43

array[12] = h + array[8]

(Array of words. Assume h is in register)

Code:

# Assume $s3 is already set

lw $t0, 32($s3) add $t1, $s2, $t0 sw $t1, 48($s3)

Map: $s2 = h $s3 = base address of array $t1 = temp

slide-44
SLIDE 44

Task : Write Code

ì Write MIPS assembly for:

Spring 2017 Computer Systems and Networks

44

g = h + array[i]

(Array of words. Assume g, h, and i are in registers)

Code:

# "Multiply" i by 4 add $t1, $s4, $s4 # x2 add $t1, $t1, $t1 # x2 again # Get addr of array[i] add $t1, $t1, $s3 # Load array[i] lw $t0, 0($t1) # Compute add add $s1, $s2, $t0

Map: $s1 = g $s2 = h $s3 = base address of array $s4 = i

slide-45
SLIDE 45

Aside – Compiler

ì When programming in C / C++, are your variables

(int, float, char, …) stored in memory or in registers?

ì Answer: It depends ì Compiler will choose where to place variables

ì

Registers: Loop counters, frequently accessed scalar values, variables local to a procedure

ì

Memory: Arrays, infrequently accessed data values

Spring 2017 Computer Systems and Networks

45

slide-46
SLIDE 46

ì

MIPS Branches / Loops

Spring 2017 Computer Systems and Networks

46

slide-47
SLIDE 47

Branches, Tests, Jump

ì Branch on Equal (if $1 == $2, goto dest) ì Set on Less Than (if $2 < $3, set $1 = 1, otherwise 0) ì Jump (goto dest)

Spring 2017 Computer Systems and Networks

47

beq <reg1>, <reg2>, <destination> slt <reg1>, <reg2>, <reg3> j <destination>

slide-48
SLIDE 48

Task : Write Code

ì Write MIPS assembly for:

Spring 2017 Computer Systems and Networks

48

if (A == B) { <equal-code> } else { <not-equal-code> } <after-if-code>

A==B ?

… …

True False

slide-49
SLIDE 49

Task : Write Code

ì Write MIPS assembly:

Spring 2017 Computer Systems and Networks

49

Code:

beq $s0,$s1,equal <not-equal-code> j done equal: <equal-code> j done done: <after-if-code>

Map: $s0 = A $s1 = B

slide-50
SLIDE 50

Task : Write Code

ì Write MIPS assembly for:

Spring 2017 Computer Systems and Networks

50

while (A != B) { <loop-body> } <post-loop-code>

A!=B?

… …

True False

slide-51
SLIDE 51

Task : Write Code

ì Write MIPS assembly:

Spring 2017 Computer Systems and Networks

51

Code:

start: beq $s0,$s1,done <loop-body> j start done: <post-loop-code>

Map: $s0 = A $s1 = B

slide-52
SLIDE 52

Spring 2017 Computer Systems and Networks

52

There are many, many, variations of branch or test instructions intended to simplify programming

  • 1. Show: Appendix A Reference
  • 2. Discuss: Instruction versus Pseudo-Instruction
slide-53
SLIDE 53

Resources

ì

Resources on Website – view “Resources” page

ì

MIPS Instruction Set (partial guide) ì

Resources available in Sakai site (under ECPE 170)

ì

HP_AppA.pdf

ì Appendix A from famous Hennessy & Patterson

Computer Organization textbook

ì Assemblers, Linkers, and the SPIM simulator ì Starting on page 51 is an overview of the MIPS assembly

commands!

ì

MIPS_Green_Sheet.pdf

ì “Cheat sheet” for expert programmers ì MIPS commands, registers, memory conventions, …

Spring 2017 Computer Systems and Networks

53

slide-54
SLIDE 54

ì

MIPS Simulator Walkthrough

Spring 2017 Computer Systems and Networks

54

slide-55
SLIDE 55

Spring 2017 Computer Systems and Networks

55

Single Step Button!

(Advance by 1 instruction)

slide-56
SLIDE 56

Spring 2017 Computer Systems and Networks

56