SlideShare a Scribd company logo
1 of 64
Download to read offline
SPIM & MIPS 
Programming 
Department of Information and 
Management 
National Taiwan University 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
SPIM & MIPS 
Programming 
Roll Call for checking student list 
Take out your laptop now. 
Join FB Group 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
QtSPIM Installation 
14年10月7⽇日星期⼆二
QtSPIM Installation 
Windows 
Linux 32/64-bit 
Mac OS X 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
QtSPIM Screenshot 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
Outline 
Introduction to Assembly Language 
SPIM-Getting Started 
MIPS-Assembly Language 
Programming 
Homework 2-Programming 
Assignment 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
Chi-Chi Liao 
廖以圻 
Robin Lab, HCI Group 
chichi@cmlab.csie.ntu.edu.tw 
14年10月7⽇日星期⼆二
Chi-Chi Liao 
HCI 
A.I. 
Linear Algebra 
Cloud Computing, Hadoop 
System Programming 
Computer Graphics 
Web 
Machine Learning 
C, C++, Java, Python, Arduino, Processing, OpenGL, 
LibSVM, Node.js, JavaScript 
14年10月7⽇日星期⼆二
Chi-Chi Liao 
https://www.youtube.com/watch? 
v=Ko9aTztcfNE&feature=youtu.be 
14年10月7⽇日星期⼆二
TAs 
Chi-Chi 
Andi 
14年10月7⽇日星期⼆二
Assembly Language 
Introduction 
14年10月7⽇日星期⼆二
Assembly language 
Assembly language 
Symbolic representation of a computer’s binary 
encoding 
Assembler 
Translates assembly language into 
binary instructions 
Machine code 
Computer’s binary encoding 
14年10月7⽇日星期⼆二
Assembly Language 
14年10月7⽇日星期⼆二
Why Assembly 
A low level language 
the code and syntax is much closer to the 
computer's processor 
Direct hardware manipulation 
device drivers, low-level embedded systems, 
and real-time systems 
Speed optimization 
performance and efficiency 
14年10月7⽇日星期⼆二
To write in assembly is to understand 
exactly how the processor and memory 
work together to "make things happen". 
Sometimes to debug a higher-level 
language, you have to review the resulting 
assembly language. 
14年10月7⽇日星期⼆二
MIPS 
MIPS is a reduced instruction set 
computer (RISC) instruction set (ISA) 
developed by MIPS Technologies 
(formerly MIPS Computer Systems, 
Inc.). 
32 bits -> 64 bits 
14年10月7⽇日星期⼆二
SPIM 
A MIPS32 Simulator 
14年10月7⽇日星期⼆二
What is SPIM 
MIPS32 Simulator 
reads and executes assembly language program 
written for MIPS 32-bit architecture 
SPIM does not execute binary 
programs 
provides a simple debugger and minimal set of 
operating system services 
SPIM implements both a terminal 
and GUI 
14年10月7⽇日星期⼆二
QtSPIM Installation 
14年10月7⽇日星期⼆二
QtSPIM Installation 
Windows 
Linux 32/64-bit 
Mac OS X 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
QtSPIM Screenshot 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
Ref. of SPIM 
Official website of SPIM 
http://spimsimulator.sourceforge.net/ 
Assemblers, Linkers, and SPIM 
Simulator 
http://spimsimulator.sourceforge.net/ 
HP_AppA.pdf 
MIPS Instruction Reference 
http://www.mrc.uidaho.edu/mrc/people/jff/ 
digital/MIPSir.html 
14年10月7⽇日星期⼆二
MIPS 
Microprocessor without Interlocked 
Pipeline Stages 
14年10月7⽇日星期⼆二
MIPS memory layout 
MIPS 32-bit CPU (all registers are 32 
bits wide) 
accessible memory range: 0x00000000– 
0xFFFFFFFF 
Memory holds both instructions 
(text) and data 
If a program is loaded into SPIM, its .text 
segment is automatically placed at 0x00400000, 
its .data segment at 0x10000000 
14年10月7⽇日星期⼆二
MIPS Assembly 
Operation 
Code(Opcode) 
Arithmetic Instructions 
add, sub, addi, addu, addiu, subu 
Data Transfer Instructions 
lw, sw, lbu, sb, lui 
Logic Instructions 
beq, bne, slt, slti, sltu 
Branch and Jump- Related Instructions 
j , jr, jal 
14年10月7⽇日星期⼆二
add, sub 
a = b + c; 
add a, b, c 
a = b - c; 
sub a, b, c 
14年10月7⽇日星期⼆二
add, sub (your turn) 
f = ( a + b )- ( c + d ) 
14年10月7⽇日星期⼆二
add, sub 
add t0, a, b 
add t1, c, d 
sub f, t0, t1 
14年10月7⽇日星期⼆二
MIPS Assembly 
MIPS Registers and Usage Convention 
$zero constant 0 
$v0, $v1 expression of a function 
$a0 ~ $a3 argument 1~4 
$t0 ~ $t9 temporary registers 
$s0 ~ $s7 save registers 
$sp stack pointer 
$fp frame pointer 
$ra return address 
... ... 
http://en.wikibooks.org/wiki/MIPS_Assembly/Register_File 
14年10月7⽇日星期⼆二
add, sub (your turn) 
f = ( a + b )- ( c + d ) 
a, b, c, d, e are on $s0~$s4 
14年10月7⽇日星期⼆二
add, sub 
add $t0, $s1, $s2 
add $t1, $s3, $s4 
sub $s0, $t0, $t1 
14年10月7⽇日星期⼆二
MIPS Assembly 
Operation 
Code(Opcode) 
Arithmetic Instructions 
add, sub, addi, addu, addiu, subu 
Data Transfer Instructions 
lw, sw, lbu, sb, lui 
Logic Instructions 
beq, bne, slt, slti, sltu 
Branch and Jump- Related Instructions 
j , jr, jal 
14年10月7⽇日星期⼆二
lw, sw 
g = h + A[8] 
g on $s1 
h on $s2 
A[] start from $s3 (base register) 
14年10月7⽇日星期⼆二
lw, sw 
lw $t0, 32($s3) (8*4 is offset) 
add $s1, $s2, $t0 
14年10月7⽇日星期⼆二
lw, sw (your turn) 
A[10] = h + A[8] 
g on $s1 
h on $s2 
A[] start from $s3 (base register) 
14年10月7⽇日星期⼆二
lw, sw 
lw $t0, 32($s3) 
add $s1, $s2, $t0 
sw $t0, 40($s3) 
14年10月7⽇日星期⼆二
MIPS Assembly 
Operation 
Code(Opcode) 
Arithmetic Instructions 
add, sub, addi, addu, addiu, subu 
Data Transfer Instructions 
lw, sw, lbu, sb, lui 
Logic Instructions 
beq, bne, slt, slti, sltu 
Branch and Jump- Related Instructions 
j , jr, jal 
14年10月7⽇日星期⼆二
beq, bne, j 
beq register 1, register 2, L1 
if (content of register 1 == content of 
register 1) then go to L1. 
bne register 1, register 2, L1 
if (content of register 1 != content of 
register 1) then go to L1. 
14年10月7⽇日星期⼆二
beq, bne, j 
if (i == j) 
{f = g+ h;} 
else 
{f = g-h;} 
f ~ j on $s0 ~ $s4 
14年10月7⽇日星期⼆二
beq, bne, j 
bne $s3, $s4, Else # if i != j,goto Else 
add $s0, $s1, $s2 # if i==k , do this 
j Exit 
Else: sub $s0, $s1, $s2 
Exit: 
14年10月7⽇日星期⼆二
MIPS Assembly 
Some data types in MIPS 
.word, .half 32/16 bit integer 
.byte 8 bit integer 
.ascii, .asciiz string 
.double, .float floating point 
14年10月7⽇日星期⼆二
Assembler Syntax 
Comment : ( # ) 
Everything from the sharp sign to the end of the line is ignored 
Identifier : (A sequence of alphanumeric 
characters, _ , and .) 
Identifier are a sequence of alphanumeric characters, 
underscores (_), and dots (.) that do not begin with a number 
Instruction Opcode 
Instruction opcodes are reserved words that are not valid 
identifiers 
Label 
Labels are declared by putting them at the beginning of a line 
followed by a colon. 
14年10月7⽇日星期⼆二
MIPS-Hello World 
C 
int main() 
{ 
printf(“Hello Worldn”); 
return 0; 
} 
MIPS 
.data 
Mystr: .asciiz “Hello Worldn” 
.text 
main: 
li $v0, 4 
la $a0, Mystr 
syscall 
li $v0, 10 
syscall 
14年10月7⽇日星期⼆二
MIPS-Hello World 
MIPS 
.data 
Mystr: .asciiz “Hello Worldn” 
MyInteger: .word 100 
MyArray: .word 1, 2, 3 
.text 
main: 
li $v0, 4 
la $a0, Mystr 
syscall 
li $v0, 10 
syscall 
Put static data here 
Put your code here 
14年10月7⽇日星期⼆二
MIPS-Hello World 
MIPS 
.data 
Mystr: .asciiz “Hello Worldn” 
MyInteger: .word 100 
MyArray: .word 1, 2, 3 
.text 
main: 
# do anything you want 
... 
# end of the program 
li $v0, 10 
syscall 
Put static data here 
Put your code here 
14年10月7⽇日星期⼆二
MIPS System Calls 
SPIM provides a small set of operating-system-like 
services through the system call instruction 
A program loads the system call code into 
register $v0 and arguments into registers $a0- 
$a3 
(or $f12 for floating-point values) 
System calls that return values put their results 
in register $v0 (or $f0 for floating-point results) 
14年10月7⽇日星期⼆二
MIPS System Calls 
14年10月7⽇日星期⼆二
MIPS System Calls 
MIPS 
.data 
str: .asciiz “The answer = ” 
.text 
main: 
li $v0, 4 
la $a0, str 
syscall 
li $v0, 1 
li $a0, 5 
syscall 
li $v0, 10 
syscall 
14年10月7⽇日星期⼆二
Execute Program in 
1. Write your own assembly program, and save it as .s 
file 
2. Simulator - Reinitialize Simulator 
3. Open your .s file 
4. Simulator - Clear Registers 
5. Simulator - Run / Continue 
14年10月7⽇日星期⼆二
Homework 2 
Programming Assignment 
14年10月7⽇日星期⼆二
Homework 2 
This is an individual assignment 
Plagiarism will be heavily punished 
Write the following three programs in MIPS 
assembly language. (Must run correctly on SPIM) 
Area of a Triangle 
Tower of Hanoi 
Bubble Sort 
One bonus program : Variation of Fibonacci 
Computer Organization and Structure 
2013 
14年10月7⽇日星期⼆二
Documentation (20%) 
Detailed documentation for each program is 
required 
The following parts must be included: 
Your name, student ID, and email address 
Explanation of the design or the flow of each 
program 
What you’ve learned from writing the programs 
Problems or difficulties you’ve encountered 
during writing the programs are nice to be 
included in the document 
14年10月7⽇日星期⼆二
Area of a Triangle 
Introduction : 
A triangle is composed of three independent 
points. We will give you three points on a 2D 
surface, and you should calculate the area of 
this triangle. It’s ok to be zero, but not 
negative number. 
14年10月7⽇日星期⼆二
Area of A Triangle 
Your file should work like this: 
Please type 6 integers, x1, y1, x2, y2, x3, y3, and each with the enter 
key: 
x1 (input) 
y1 (input) 
x2 (input) 
y2 (input) 
x3 (input) 
y3 (input) 
The area is:(Your answer here.) 
Requirements: 
1. Print the correct answer 
2. The file name is Area.s 
14年10月7⽇日星期⼆二
Tower of Hanoi 
A hanoi tower with 3 rods A,B,C and n disks 
Move all the disks from A to C 
Input : 
positive integer n ( disks ), 1 ≤ n ≤ 5 
Output : 
Print all the steps 
Requirements: 
1. Print the correct steps 
2. The file name is Hanoi.s 
14年10月7⽇日星期⼆二
Bubble Sort 
Bubble sort, sometimes referred to as sinking sort, 
is a simple sorting algorithm that works by 
repeatedly stepping through the list to be sorted, 
comparing each pair of adjacent items and 
swapping them if they are in the wrong order. 
14年10月7⽇日星期⼆二
Bubble Sort 
Input : 
n positive integers, where n < 8 
Output : 
Sorting n1, n2, n3, n4, n5... in ascending order 
Requirements: 
1. Print the correct answer 
2. The file name is Sorting.s 
14年10月7⽇日星期⼆二
Bonus: 
Variation of Fibonacci 
Def. 
F(0) = 0 
F(1) = 1 
F(2) = 2 
F(n) = F(n-1) + F(n-3) , if n > 2 
So it would be: 0, 1, 2, 2, 3, 5, 7, 10, 15...... 
14年10月7⽇日星期⼆二
Bonus: 
Variation of Fibonacci 
Input : 
positive integer n 
Output : 
F(n), which it is a Fibonacci number 
Requirements: 
1. Print the correct answer 
2. The file name is Fibonacci.s 
14年10月7⽇日星期⼆二
Submission 
Deadline : 11:59 PM, Monday, Oct. 27, 2014 
You must submit at least the following files: 
Area.s 
Hanoi.s 
Sorting.s 
Fibonacci.s (optional) 
(Your student id)_hw2_document.pdf 
Please put all your files in a directory named by 
your student id in lowercase, and then compress 
it into one zipped file. 
The attach filename should be like 
b02xxxxxx.zip. 
Email your zipped file to TA 
chichi@cmlab.csie.ntu.edu.tw 
14年10月7⽇日星期⼆二
Grading Guidelines 
Description For Each Problem 
Program runs without error 
messages 10% 
Program executes correctly 60% 
Documentation and description 20% 
Implementation Detail 10% 
14年10月7⽇日星期⼆二
Deadline 
Late submission policy 
10% off from your total score each 
day 
14年10月7⽇日星期⼆二
Contact Information 
TA Hours @ 管院⼀一館五樓 503-C 
Chi-Chi Liao ( 廖以圻 ) Thur. 14:00 ~ 15:00 
Han-Chih Kuo ( 郭瀚智 ) Mon. 14:00 ~ 15:00 
Contact us if you have any problem 
Chi-Chi: chichi@cmlab.csie.ntu.edu.tw 
Andi: andikan@cmlab.csie.ntu.edu.tw 
Computer Organization and Structure 
2013 14年10月7⽇日星期⼆二
Thank You for 
Your Attention 
14年10月7⽇日星期⼆二

More Related Content

What's hot

Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
Rabia Khalid
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
 

What's hot (19)

Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
Mips
MipsMips
Mips
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPS
 
Test2 Sum05
Test2 Sum05Test2 Sum05
Test2 Sum05
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
C format string vulnerability
C format string vulnerabilityC format string vulnerability
C format string vulnerability
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 
Format string vunerability
Format string vunerabilityFormat string vunerability
Format string vunerability
 
Local Exploits
Local ExploitsLocal Exploits
Local Exploits
 
See through C
See through CSee through C
See through C
 
Introductionof c
Introductionof cIntroductionof c
Introductionof c
 
Instruction set-of-8086
Instruction set-of-8086Instruction set-of-8086
Instruction set-of-8086
 
Ch9b
Ch9bCh9b
Ch9b
 
Ch9c
Ch9cCh9c
Ch9c
 

Viewers also liked (6)

MARS MIPS (Assembly language)
MARS MIPS (Assembly language)MARS MIPS (Assembly language)
MARS MIPS (Assembly language)
 
Mips Assembly
Mips AssemblyMips Assembly
Mips Assembly
 
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
 
Chap 02[1]
Chap 02[1]Chap 02[1]
Chap 02[1]
 
03 mips assembly language
03 mips assembly language03 mips assembly language
03 mips assembly language
 
3. adressingmodes1
3. adressingmodes13. adressingmodes1
3. adressingmodes1
 

Similar to 2014 MIPS Progrmming for NTUIM

HW2.pdfCSEEEE 230 Computer Organization and Assembly La.docx
HW2.pdfCSEEEE 230 Computer Organization and Assembly La.docxHW2.pdfCSEEEE 230 Computer Organization and Assembly La.docx
HW2.pdfCSEEEE 230 Computer Organization and Assembly La.docx
adampcarr67227
 
Georgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software securityGeorgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software security
DefconRussia
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 

Similar to 2014 MIPS Progrmming for NTUIM (20)

MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
HW2.pdfCSEEEE 230 Computer Organization and Assembly La.docx
HW2.pdfCSEEEE 230 Computer Organization and Assembly La.docxHW2.pdfCSEEEE 230 Computer Organization and Assembly La.docx
HW2.pdfCSEEEE 230 Computer Organization and Assembly La.docx
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunpcDuino Presentation at SparkFun
pcDuino Presentation at SparkFun
 
ELAVARASAN.pdf
ELAVARASAN.pdfELAVARASAN.pdf
ELAVARASAN.pdf
 
f37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptf37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.ppt
 
f37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptf37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.ppt
 
Georgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software securityGeorgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software security
 
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Does Java Have a Future After Version 8? (Belfast JUG April 2014)Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. Programming
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Open Source Robotics as Booster to Creativity
Open Source Robotics as  Booster to CreativityOpen Source Robotics as  Booster to Creativity
Open Source Robotics as Booster to Creativity
 
What's new in Apache SystemML - Declarative Machine Learning
What's new in Apache SystemML  - Declarative Machine LearningWhat's new in Apache SystemML  - Declarative Machine Learning
What's new in Apache SystemML - Declarative Machine Learning
 
Introduction to Raspberry Pi and GPIO
Introduction to Raspberry Pi and GPIOIntroduction to Raspberry Pi and GPIO
Introduction to Raspberry Pi and GPIO
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
 

More from imetliao (6)

Tactile brush
Tactile brushTactile brush
Tactile brush
 
Hci intro open_hci
Hci intro open_hciHci intro open_hci
Hci intro open_hci
 
HCI Introduction
HCI IntroductionHCI Introduction
HCI Introduction
 
ThirdHand
ThirdHandThirdHand
ThirdHand
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to python and programming
Introduction to python and programmingIntroduction to python and programming
Introduction to python and programming
 

Recently uploaded

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Recently uploaded (20)

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 

2014 MIPS Progrmming for NTUIM

  • 1. SPIM & MIPS Programming Department of Information and Management National Taiwan University Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 2. SPIM & MIPS Programming Roll Call for checking student list Take out your laptop now. Join FB Group Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 4. QtSPIM Installation Windows Linux 32/64-bit Mac OS X Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 5. QtSPIM Screenshot Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 6. Outline Introduction to Assembly Language SPIM-Getting Started MIPS-Assembly Language Programming Homework 2-Programming Assignment Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 7. Chi-Chi Liao 廖以圻 Robin Lab, HCI Group chichi@cmlab.csie.ntu.edu.tw 14年10月7⽇日星期⼆二
  • 8. Chi-Chi Liao HCI A.I. Linear Algebra Cloud Computing, Hadoop System Programming Computer Graphics Web Machine Learning C, C++, Java, Python, Arduino, Processing, OpenGL, LibSVM, Node.js, JavaScript 14年10月7⽇日星期⼆二
  • 9. Chi-Chi Liao https://www.youtube.com/watch? v=Ko9aTztcfNE&feature=youtu.be 14年10月7⽇日星期⼆二
  • 10. TAs Chi-Chi Andi 14年10月7⽇日星期⼆二
  • 11. Assembly Language Introduction 14年10月7⽇日星期⼆二
  • 12. Assembly language Assembly language Symbolic representation of a computer’s binary encoding Assembler Translates assembly language into binary instructions Machine code Computer’s binary encoding 14年10月7⽇日星期⼆二
  • 14. Why Assembly A low level language the code and syntax is much closer to the computer's processor Direct hardware manipulation device drivers, low-level embedded systems, and real-time systems Speed optimization performance and efficiency 14年10月7⽇日星期⼆二
  • 15. To write in assembly is to understand exactly how the processor and memory work together to "make things happen". Sometimes to debug a higher-level language, you have to review the resulting assembly language. 14年10月7⽇日星期⼆二
  • 16. MIPS MIPS is a reduced instruction set computer (RISC) instruction set (ISA) developed by MIPS Technologies (formerly MIPS Computer Systems, Inc.). 32 bits -> 64 bits 14年10月7⽇日星期⼆二
  • 17. SPIM A MIPS32 Simulator 14年10月7⽇日星期⼆二
  • 18. What is SPIM MIPS32 Simulator reads and executes assembly language program written for MIPS 32-bit architecture SPIM does not execute binary programs provides a simple debugger and minimal set of operating system services SPIM implements both a terminal and GUI 14年10月7⽇日星期⼆二
  • 20. QtSPIM Installation Windows Linux 32/64-bit Mac OS X Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 21. QtSPIM Screenshot Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 22. Ref. of SPIM Official website of SPIM http://spimsimulator.sourceforge.net/ Assemblers, Linkers, and SPIM Simulator http://spimsimulator.sourceforge.net/ HP_AppA.pdf MIPS Instruction Reference http://www.mrc.uidaho.edu/mrc/people/jff/ digital/MIPSir.html 14年10月7⽇日星期⼆二
  • 23. MIPS Microprocessor without Interlocked Pipeline Stages 14年10月7⽇日星期⼆二
  • 24. MIPS memory layout MIPS 32-bit CPU (all registers are 32 bits wide) accessible memory range: 0x00000000– 0xFFFFFFFF Memory holds both instructions (text) and data If a program is loaded into SPIM, its .text segment is automatically placed at 0x00400000, its .data segment at 0x10000000 14年10月7⽇日星期⼆二
  • 25. MIPS Assembly Operation Code(Opcode) Arithmetic Instructions add, sub, addi, addu, addiu, subu Data Transfer Instructions lw, sw, lbu, sb, lui Logic Instructions beq, bne, slt, slti, sltu Branch and Jump- Related Instructions j , jr, jal 14年10月7⽇日星期⼆二
  • 26. add, sub a = b + c; add a, b, c a = b - c; sub a, b, c 14年10月7⽇日星期⼆二
  • 27. add, sub (your turn) f = ( a + b )- ( c + d ) 14年10月7⽇日星期⼆二
  • 28. add, sub add t0, a, b add t1, c, d sub f, t0, t1 14年10月7⽇日星期⼆二
  • 29. MIPS Assembly MIPS Registers and Usage Convention $zero constant 0 $v0, $v1 expression of a function $a0 ~ $a3 argument 1~4 $t0 ~ $t9 temporary registers $s0 ~ $s7 save registers $sp stack pointer $fp frame pointer $ra return address ... ... http://en.wikibooks.org/wiki/MIPS_Assembly/Register_File 14年10月7⽇日星期⼆二
  • 30. add, sub (your turn) f = ( a + b )- ( c + d ) a, b, c, d, e are on $s0~$s4 14年10月7⽇日星期⼆二
  • 31. add, sub add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1 14年10月7⽇日星期⼆二
  • 32. MIPS Assembly Operation Code(Opcode) Arithmetic Instructions add, sub, addi, addu, addiu, subu Data Transfer Instructions lw, sw, lbu, sb, lui Logic Instructions beq, bne, slt, slti, sltu Branch and Jump- Related Instructions j , jr, jal 14年10月7⽇日星期⼆二
  • 33. lw, sw g = h + A[8] g on $s1 h on $s2 A[] start from $s3 (base register) 14年10月7⽇日星期⼆二
  • 34. lw, sw lw $t0, 32($s3) (8*4 is offset) add $s1, $s2, $t0 14年10月7⽇日星期⼆二
  • 35. lw, sw (your turn) A[10] = h + A[8] g on $s1 h on $s2 A[] start from $s3 (base register) 14年10月7⽇日星期⼆二
  • 36. lw, sw lw $t0, 32($s3) add $s1, $s2, $t0 sw $t0, 40($s3) 14年10月7⽇日星期⼆二
  • 37. MIPS Assembly Operation Code(Opcode) Arithmetic Instructions add, sub, addi, addu, addiu, subu Data Transfer Instructions lw, sw, lbu, sb, lui Logic Instructions beq, bne, slt, slti, sltu Branch and Jump- Related Instructions j , jr, jal 14年10月7⽇日星期⼆二
  • 38. beq, bne, j beq register 1, register 2, L1 if (content of register 1 == content of register 1) then go to L1. bne register 1, register 2, L1 if (content of register 1 != content of register 1) then go to L1. 14年10月7⽇日星期⼆二
  • 39. beq, bne, j if (i == j) {f = g+ h;} else {f = g-h;} f ~ j on $s0 ~ $s4 14年10月7⽇日星期⼆二
  • 40. beq, bne, j bne $s3, $s4, Else # if i != j,goto Else add $s0, $s1, $s2 # if i==k , do this j Exit Else: sub $s0, $s1, $s2 Exit: 14年10月7⽇日星期⼆二
  • 41. MIPS Assembly Some data types in MIPS .word, .half 32/16 bit integer .byte 8 bit integer .ascii, .asciiz string .double, .float floating point 14年10月7⽇日星期⼆二
  • 42. Assembler Syntax Comment : ( # ) Everything from the sharp sign to the end of the line is ignored Identifier : (A sequence of alphanumeric characters, _ , and .) Identifier are a sequence of alphanumeric characters, underscores (_), and dots (.) that do not begin with a number Instruction Opcode Instruction opcodes are reserved words that are not valid identifiers Label Labels are declared by putting them at the beginning of a line followed by a colon. 14年10月7⽇日星期⼆二
  • 43. MIPS-Hello World C int main() { printf(“Hello Worldn”); return 0; } MIPS .data Mystr: .asciiz “Hello Worldn” .text main: li $v0, 4 la $a0, Mystr syscall li $v0, 10 syscall 14年10月7⽇日星期⼆二
  • 44. MIPS-Hello World MIPS .data Mystr: .asciiz “Hello Worldn” MyInteger: .word 100 MyArray: .word 1, 2, 3 .text main: li $v0, 4 la $a0, Mystr syscall li $v0, 10 syscall Put static data here Put your code here 14年10月7⽇日星期⼆二
  • 45. MIPS-Hello World MIPS .data Mystr: .asciiz “Hello Worldn” MyInteger: .word 100 MyArray: .word 1, 2, 3 .text main: # do anything you want ... # end of the program li $v0, 10 syscall Put static data here Put your code here 14年10月7⽇日星期⼆二
  • 46. MIPS System Calls SPIM provides a small set of operating-system-like services through the system call instruction A program loads the system call code into register $v0 and arguments into registers $a0- $a3 (or $f12 for floating-point values) System calls that return values put their results in register $v0 (or $f0 for floating-point results) 14年10月7⽇日星期⼆二
  • 47. MIPS System Calls 14年10月7⽇日星期⼆二
  • 48. MIPS System Calls MIPS .data str: .asciiz “The answer = ” .text main: li $v0, 4 la $a0, str syscall li $v0, 1 li $a0, 5 syscall li $v0, 10 syscall 14年10月7⽇日星期⼆二
  • 49. Execute Program in 1. Write your own assembly program, and save it as .s file 2. Simulator - Reinitialize Simulator 3. Open your .s file 4. Simulator - Clear Registers 5. Simulator - Run / Continue 14年10月7⽇日星期⼆二
  • 50. Homework 2 Programming Assignment 14年10月7⽇日星期⼆二
  • 51. Homework 2 This is an individual assignment Plagiarism will be heavily punished Write the following three programs in MIPS assembly language. (Must run correctly on SPIM) Area of a Triangle Tower of Hanoi Bubble Sort One bonus program : Variation of Fibonacci Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 52. Documentation (20%) Detailed documentation for each program is required The following parts must be included: Your name, student ID, and email address Explanation of the design or the flow of each program What you’ve learned from writing the programs Problems or difficulties you’ve encountered during writing the programs are nice to be included in the document 14年10月7⽇日星期⼆二
  • 53. Area of a Triangle Introduction : A triangle is composed of three independent points. We will give you three points on a 2D surface, and you should calculate the area of this triangle. It’s ok to be zero, but not negative number. 14年10月7⽇日星期⼆二
  • 54. Area of A Triangle Your file should work like this: Please type 6 integers, x1, y1, x2, y2, x3, y3, and each with the enter key: x1 (input) y1 (input) x2 (input) y2 (input) x3 (input) y3 (input) The area is:(Your answer here.) Requirements: 1. Print the correct answer 2. The file name is Area.s 14年10月7⽇日星期⼆二
  • 55. Tower of Hanoi A hanoi tower with 3 rods A,B,C and n disks Move all the disks from A to C Input : positive integer n ( disks ), 1 ≤ n ≤ 5 Output : Print all the steps Requirements: 1. Print the correct steps 2. The file name is Hanoi.s 14年10月7⽇日星期⼆二
  • 56. Bubble Sort Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. 14年10月7⽇日星期⼆二
  • 57. Bubble Sort Input : n positive integers, where n < 8 Output : Sorting n1, n2, n3, n4, n5... in ascending order Requirements: 1. Print the correct answer 2. The file name is Sorting.s 14年10月7⽇日星期⼆二
  • 58. Bonus: Variation of Fibonacci Def. F(0) = 0 F(1) = 1 F(2) = 2 F(n) = F(n-1) + F(n-3) , if n > 2 So it would be: 0, 1, 2, 2, 3, 5, 7, 10, 15...... 14年10月7⽇日星期⼆二
  • 59. Bonus: Variation of Fibonacci Input : positive integer n Output : F(n), which it is a Fibonacci number Requirements: 1. Print the correct answer 2. The file name is Fibonacci.s 14年10月7⽇日星期⼆二
  • 60. Submission Deadline : 11:59 PM, Monday, Oct. 27, 2014 You must submit at least the following files: Area.s Hanoi.s Sorting.s Fibonacci.s (optional) (Your student id)_hw2_document.pdf Please put all your files in a directory named by your student id in lowercase, and then compress it into one zipped file. The attach filename should be like b02xxxxxx.zip. Email your zipped file to TA chichi@cmlab.csie.ntu.edu.tw 14年10月7⽇日星期⼆二
  • 61. Grading Guidelines Description For Each Problem Program runs without error messages 10% Program executes correctly 60% Documentation and description 20% Implementation Detail 10% 14年10月7⽇日星期⼆二
  • 62. Deadline Late submission policy 10% off from your total score each day 14年10月7⽇日星期⼆二
  • 63. Contact Information TA Hours @ 管院⼀一館五樓 503-C Chi-Chi Liao ( 廖以圻 ) Thur. 14:00 ~ 15:00 Han-Chih Kuo ( 郭瀚智 ) Mon. 14:00 ~ 15:00 Contact us if you have any problem Chi-Chi: chichi@cmlab.csie.ntu.edu.tw Andi: andikan@cmlab.csie.ntu.edu.tw Computer Organization and Structure 2013 14年10月7⽇日星期⼆二
  • 64. Thank You for Your Attention 14年10月7⽇日星期⼆二