MIPS!Assembly! - University of the...
Transcript of MIPS!Assembly! - University of the...
![Page 1: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/1.jpg)
ì Computer Systems and Networks ECPE 170 – Jeff Shafer – University of the Pacific
MIPS Assembly
![Page 2: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/2.jpg)
Lab Schedule
AcAviAes ì Today / Next Tuesday
ì MIPS discussion ì PracAce problems
(whiteboard) ì Using the QtSPIM
simulator ì MIPS funcAons
Assignments Due ì Lab 10
ì Due by Nov 17th 5:00am
ì Lab 11 ì Due by Dec 1st 5:00am
ì Lab 12 ì Due by Dec 8th 5:00am
ì Final Exam ì Tue Dec 9th 8-‐10am
Fall 2014 Computer Systems and Networks
2
![Page 3: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/3.jpg)
Person of the Day – John Cocke
ì Computer architecture pioneer ì “Father of RISC Architecture” ì Developed IBM 801 processor,
1975-‐1980
ì Winner, ACM Turing Award, 1987
Fall 2014 Computer Systems and Networks
3
RISC = Reduced InstrucNon Set CompuNng
Achieve higher performance with simple instrucAons that execute faster
![Page 4: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/4.jpg)
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
Fall 2014 Computer Systems and Networks
4
![Page 5: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/5.jpg)
Class to Date
Fall 2014 Computer Systems and Networks
5
Human (C Code)
Compiler (Assembly code)
Compiler (Object file / binary code)
Linker (Executable program)
![Page 6: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/6.jpg)
Class Now
Fall 2014 Computer Systems and Networks
6
Human (Assembly code)
Assembler (Object file / binary code)
Linker (Executable Program)
![Page 7: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/7.jpg)
ì MIPS
Fall 2014 Computer Systems and Networks
7
![Page 8: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/8.jpg)
MIPS Overview
ì Family of computer processors first introduced in 1981
ì Microprocessor without Interlocked Pipeline Stages ì Original acronym ì Now MIPS stands for nothing at all…
Fall 2014 Computer Systems and Networks
8
![Page 9: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/9.jpg)
MIPS Products
ì Embedded devices ì Cisco/Linksys routers ì Cable boxes ì MIPS processor is buried inside System-‐on-‐a-‐Chip (SOC)
ì Gaming / entertainment ì Nintendo 64 ì PlaystaAon 2, PSP
ì Computers? ì Not so much anymore… ì SGI / DEC / NEC workstaAons back in 1990’s
Fall 2014 Computer Systems and Networks
9
![Page 10: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/10.jpg)
MIPS Design
ì RISC – What does this mean? ì Reduced InstrucAon Set CompuAng ì Simplified design for instrucAons ì Use more instrucAons to accomplish same task
ì But each instrucAon runs much faster!
ì 32 bits (originally) – What does this mean? ì 1 “word”= 32 bits ì Size of data processed by an integer add instrucAon ì New(er) MIPS64 design is 64 bits, but we won’t
focus on that
Fall 2014 Computer Systems and Networks
10
![Page 11: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/11.jpg)
ì MIPS Assembly Programming
Fall 2014 Computer Systems and Networks
11
![Page 12: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/12.jpg)
Quotes – Donald Knuth
Fall 2014 Computer Systems and Networks
12
“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 moAvaAon in the MIPS labs!
![Page 13: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/13.jpg)
Why Learn Assembly Programming?
ì Computer Science track ì Understand capabiliAes (and limitaAons) of physical
machine ì Ability to opAmize program performance (or
funcAonality) 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 instrucAon? ì How does the processor know what data to process?
Fall 2014 Computer Systems and Networks
13
![Page 14: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/14.jpg)
Instruction Set Architecture
ì InstrucNon Set Architecture (ISA) is the interface between hardware and sojware ì Specifies the format of processor instrucAons ì Specifies the format of memory addresses
(and addressing modes) ì Specifies the primiAve operaAons the processor can
perform
Fall 2014 Computer Systems and Networks
14
![Page 15: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/15.jpg)
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 Sojware
Developers Manual ì hkp://www.intel.com/content/www/us/en/processors/
architectures-‐sojware-‐developer-‐manuals.html ì No joke – the manual PDF from August 2012
is 3020 pages long!
Fall 2014 Computer Systems and Networks
15
![Page 16: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/16.jpg)
Instruction Set Architecture
ì Processor families share the same ISA
ì Example ISAs: ì Intel x86 ì Intel / AMD x86-‐64 ì Intel Itanium ì ARM ì IBM PowerPC ì MIPS
Fall 2014 Computer Systems and Networks
16
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
![Page 17: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/17.jpg)
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
Fall 2014 Computer Systems and Networks
17
![Page 18: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/18.jpg)
Arithmetic Instructions
ì AddiAon
ì SubtracAon
Fall 2014 Computer Systems and Networks
18
add <result>, <input1>, <input2>
sub <result>, <input1>, <input2>
OperaAon / “Op code” Operands
![Page 19: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/19.jpg)
Task : Write Code
ì Write MIPS assembly for
Fall 2014 Computer Systems and Networks
19
f = (g+h) – (i+j)
add temp0, g, h add temp1, i, j sub f, temp0, temp1
![Page 20: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/20.jpg)
Fall 2014 Computer Systems and Networks
20
CongratulaNons!
You’re now an assembly programming expert!
![Page 21: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/21.jpg)
Data Sources
ì Previous example was (just a liHle 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)
Fall 2014 Computer Systems and Networks
21
Problem: You can’t make up variables in assembly! (as least, not in this fashion)
![Page 22: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/22.jpg)
Data Sources
Fall 2014 Computer Systems and Networks
22
Where can we explicitly place data in assembly programming?
CPU
ALU
1. Registers ì On the CPU itself ì Very close to ALU ì Tiny ì Access Ame: 1 cycle
2. Memory ì Off-‐chip ì Large ì Access Ame: 100+ cycles
Cache Memory
![Page 23: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/23.jpg)
Aside – Cache
ì Review: Does the programmer explicitly manage the cache?
ì Answer: No! ì The assembly programmer just reads/writes
memory addresses ì Cache is managed automaAcally in hardware ì Result: Memory appears to be faster than it really is
Fall 2014 Computer Systems and Networks
23
![Page 24: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/24.jpg)
ECPE 71
ì From your knowledge of ECPE 71 (Digital Design), how would you construct a register?
Fall 2014 Computer Systems and Networks
24
Flip Flops! (D Flip Flop shown)
![Page 25: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/25.jpg)
ECPE 71 – Group of Registers
Fall 2014 Computer Systems and Networks
25
![Page 26: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/26.jpg)
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 convenLon for other uses ì We’ll learn a few more later…
Fall 2014 Computer Systems and Networks
26
Name Use
$zero Constant value: ZERO
$s0-$s7 Local variables
$t0-$t9 Temporary results
![Page 27: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/27.jpg)
Task : Write Code
ì Write MIPS assembly using registers for:
Fall 2014 Computer Systems and Networks
27
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
![Page 28: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/28.jpg)
More Arithmetic Instructions
ì Add Immediate
Fall 2014 Computer Systems and Networks
28
addi <result>, <input1>, <constant>
Can be a posiAve or negaAve number!
Register Register
![Page 29: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/29.jpg)
Task : Write Code
ì Write MIPS assembly using registers for:
Fall 2014 Computer Systems and Networks
29
f = g+20
Code:
addi $s0, $s1, 20 Map:
$s0 = f $s1 = g
![Page 30: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/30.jpg)
Memory
ì Challenge: Limited supply of registers ì Physical limitaAon: We can’t put more on the
processor chip, and maintain their current speed ì Many elements compete for space in the CPU…
ì SoluAon: Store data in memory
ì MIPS provides instrucAons that transfer data between memory and registers
Fall 2014 Computer Systems and Networks
30
![Page 31: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/31.jpg)
Memory Fundamentals
Fall 2014 Computer Systems and Networks
31
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
![Page 32: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/32.jpg)
Memory Fundamentals
Fall 2014 Computer Systems and Networks
32
Yes, it’s a pain to keep moving data between registers and memory.
But consider it your moLvaLon to reduce the number of memory accesses. That will improve program performance!
![Page 33: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/33.jpg)
Memory Fundamentals
ì Four quesAons to ask when accessing memory: 1. What direcNon 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?
Fall 2014 Computer Systems and Networks
33
![Page 34: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/34.jpg)
CPU
Memory – Fundamental Operations
Load ì Copy data from
memory to register
Store ì Copy data from
register to memory
Fall 2014 Computer Systems and Networks
34
CPU Memory Memory
![Page 35: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/35.jpg)
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
instrucAon as an “immediate”
Fall 2014 Computer Systems and Networks
35
Memory
0 1 2 3 4
Base
Offset
MIPS notaNon: offset(base)
![Page 36: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/36.jpg)
Memory – Register Name
ì What is the name of the register to use as either the data desAnaAon (for a load) or a data source (for a store)?
ì Use the same register names previously learned
Fall 2014 Computer Systems and Networks
36
![Page 37: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/37.jpg)
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 instrucNon for each quanNty of data
ì No opAon to load an enAre array! ì Will need a loop that loads 1 element at a Ame…
Fall 2014 Computer Systems and Networks
37
![Page 38: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/38.jpg)
Memory – Data Transfer Instructions
ì Load (copy from memory to register)
ì Store (copy from register to memory)
Fall 2014 Computer Systems and Networks
38
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 LocaAon
![Page 39: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/39.jpg)
Example
ì What will this instrucNon do?
ì Load word copies from memory to register: ì Base address: stored in register $s2 ì Offset: 20 bytes ì DesAnaAon register: $s1 ì Amount of data transferred: 1 word (32 bits)
Fall 2014 Computer Systems and Networks
39
lw $s1, 20($s2)
![Page 40: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/40.jpg)
Task : Write Code
ì Write MIPS assembly for:
Fall 2014 Computer Systems and Networks
40
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
![Page 41: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/41.jpg)
Memory Address
ì Slight flaw in previous soluAon ì The programmer intended to load the 16th array
element ì Each element is 4 bytes (1 word) ì The offset is in bytes ì 16 * 4 = 64
Fall 2014 Computer Systems and Networks
41
Correct Code: # Assume $s3 is already set
lw $t0, 64($s3) add $s1, $s2, $t0
![Page 42: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/42.jpg)
Task : Write Code
ì Write MIPS assembly for:
Fall 2014 Computer Systems and Networks
42
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
![Page 43: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/43.jpg)
Task : Write Code
ì Write MIPS assembly for:
Fall 2014 Computer Systems and Networks
43
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
![Page 44: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/44.jpg)
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
Fall 2014 Computer Systems and Networks
44
![Page 45: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/45.jpg)
ì MIPS Branches / Loops
Fall 2014 Computer Systems and Networks
45
![Page 46: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/46.jpg)
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)
Fall 2014 Computer Systems and Networks
46
beq <reg1>, <reg2>, <destination>
slt <reg1>, <reg2>, <reg3>
j <destination>
![Page 47: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/47.jpg)
Task : Write Code
ì Write MIPS assembly for:
Fall 2014 Computer Systems and Networks
47
if (A == B) { <equal-‐code> } else {
<not-‐equal-‐code> } <aker-‐if-‐code>
A==B?
… …
True False
![Page 48: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/48.jpg)
Task : Write Code
ì Write MIPS assembly:
Fall 2014 Computer Systems and Networks
48
Code:
beq $s0,$s1,equal <not-equal-code> j done
equal: <equal-code> j done
done: <aker-‐if-‐code>
Map: $s0 = A $s1 = B
![Page 49: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/49.jpg)
Task : Write Code
ì Write MIPS assembly for:
Fall 2014 Computer Systems and Networks
49
while (A != B) { <loop-‐body> } <post-‐loop-‐code>
A!=B?
…
…
True
False
![Page 50: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/50.jpg)
Task : Write Code
ì Write MIPS assembly:
Fall 2014 Computer Systems and Networks
50
Code:
start: beq $s0,$s1,done <loop-body> j start
done: <post-loop-code>
Map: $s0 = A $s1 = B
![Page 51: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/51.jpg)
Fall 2014 Computer Systems and Networks
51
There are many, many, variaAons of branch or test instrucAons intended to simplify programming
1. Show: Appendix A Reference
2. Discuss: InstrucAon versus Pseudo-‐InstrucLon
![Page 52: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/52.jpg)
Resources
ì Resources on Website – view “Resources” page ì MIPS InstrucNon Set (parAal guide)
ì Resources available in Sakai site (under ECPE 170) ì HP_AppA.pdf
ì Appendix A from famous Hennessy & Pakerson Computer OrganizaLon textbook
ì Assemblers, Linkers, and the SPIM simulator ì StarAng on page 51 is an overview of the MIPS assembly
commands! ì MIPS_Green_Sheet.pdf
ì “Cheat sheet” for expert programmers ì MIPS commands, registers, memory convenAons, …
Fall 2014 Computer Systems and Networks
52
![Page 53: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/53.jpg)
ì MIPS Simulator Walkthrough
Fall 2014 Computer Systems and Networks
53
![Page 54: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/54.jpg)
Fall 2014 Computer Systems and Networks
54
Single Step Bunon!
(Advance by 1 instrucNon)
![Page 55: MIPS!Assembly! - University of the Pacificecs-network.serv.pacific.edu/...ecpe-170/slides/12mipsassembly.pdf · ComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader033.fdocuments.us/reader033/viewer/2022051507/5a78e77d7f8b9a77088cd4f0/html5/thumbnails/55.jpg)
Fall 2014 Computer Systems and Networks
55