mips-spim taiwan

33
SPIM & MIPS PROGRAMMING Created and Presented by Shu-Yang Lin, Hsun-Pei Wang, Liang-Tsen Shen Computer Organization and Structure 2011 Department of Information Management National Taiwan University

Upload: eestest-iguy

Post on 21-Aug-2015

1.427 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: MIPS-SPIM Taiwan

SPIM & MIPS PROGRAMMINGCreated and Presented by

Shu-Yang Lin, Hsun-Pei Wang, Liang-Tsen Shen

Computer Organization and Structure 2011

Department of Information ManagementNational Taiwan University

Page 2: MIPS-SPIM Taiwan

OUTLINE

Introduction to Assembly Language

SPIM –Getting Started

MIPS –Assembly Language Programming

Homework 2 (Programming Assignment)

Page 3: MIPS-SPIM Taiwan

ASSEMBLY LANGUAGE

Machine languageMachine language• Computer’s binary encoding

Assembly languageAssembly language• Symbolic representation of a computer’s binary

encoding

AssemblerAssembler• Translates assembly language into binary instructions

Page 4: MIPS-SPIM Taiwan

WHAT IS SPIM?

MIPS32 simulator that reads and executes assembly language program written for SPIM.

MIPS32 simulator that reads and executes assembly language program written for SPIM.

SPIM does NOT execute binary program.SPIM does NOT execute binary program.

Page 5: MIPS-SPIM Taiwan

REFERENCES OF SPIM

Official website of SPIM:http://spimsimulator.sourceforge.net/

Assemblers, Linkers, and the SPIM Simulator:http://pages.cs.wisc.edu/~larus/HP_AppA.pdf

MIPS Instruction Reference:http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

Page 6: MIPS-SPIM Taiwan

QTSPIM - INSTALLATION

/http://spimsimulator.sourceforge.net

/

Page 7: MIPS-SPIM Taiwan

QTSPIM - INSTALLATION

Windows

Mac

Linux

Page 8: MIPS-SPIM Taiwan

QTSPIM - SCREENSHOT

Register Window Data Window

Text Window

Console Window

Page 9: MIPS-SPIM Taiwan

MIPS ASSEMBLY LANGUAGEOperation Code (Opcode)

Page 10: MIPS-SPIM Taiwan

MIPS ASSEMBLY LANGUAGE

$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

nMIPS Registers and Usage Convention

Page 11: MIPS-SPIM Taiwan

MIPS ASSEMBLY LANGUAGE

• 32/16 bit integer.word, .half

• 8 bit integer.byte

• string.ascii, .asciiz

• floating point .double, .float

SSome Data Types in MIPS

Page 12: MIPS-SPIM Taiwan

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).

Page 13: MIPS-SPIM Taiwan

MIPS SYSTEM CALLS

FIGURE A.9.1 System services.

$v0

Page 14: MIPS-SPIM Taiwan

EXAMPLE – HELLO WORLDC vs MIPS

int main()

{

printf(“Hello World¥”);

return 0;

}

.data

Mystr: .asciiz “Hello World¥n”

.text

main:

li $v0, 4

la $a0, Mystr

syscall

li $v0, 10

syscall

Page 15: MIPS-SPIM Taiwan

EXAMPLE – HELLO WORLDeMIPS Architecture

.dataMystr: .asciiz “Hello World¥n”

Yourint: .word 75

Hisarray: .word 100, 100, 100

.word 20 , 40 , 60

.word 1 , 2 , 3

.text……

Page 16: MIPS-SPIM Taiwan

EXAMPLE – HELLO WORLDeMIPS Architecture

.data

.textmain:

# do anything you want

……

# end of the program

li $v0, 10

syscall

Page 17: MIPS-SPIM Taiwan

MIPS SYSTEM CALLS - EXAMPLE

.datastr:.asciiz "the answer = "

.textli $v0, 4 # system call code for print_strla $a0, str # address of string to printsyscall # print the stringli $v0, 1 # system call code for print_intli $a0, 5 # integer to printsyscall # print it

Output: “the answer = 5’’

FIGURE A.9.1 System services.

Page 18: MIPS-SPIM Taiwan

Everything from the sharp sign to the end of the line is ignored.

Identifier are a sequence of alphanumeric characters, underbars (_), and dots (.) that do not begin with a number.

Instruction opcodes are reserved words that are not valid identifiers.

Labels are declared by putting them at the beginning of a line followed by a colon.

PAGE 18

ASSEMBLER SYNTAX

Comment (#)

Identifier (A sequence of alphanumeric characters, _ , and .)

Instruction Opcode

Label

Page 19: MIPS-SPIM Taiwan

HOMEWORK

Page 20: MIPS-SPIM Taiwan

HOMEWORK

This is an individual assignment

Plagiarism will be heavily punished

Write the following three programs in MIPS assembly language. (Must run correctly on SPIM)

Simple Calculator

Star Pyramid

Pascal Triangle

Page 21: MIPS-SPIM Taiwan

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 learnt from writing the programs

Problems or difficulties you’ve encountered during writing the programs are nice to be included in the document

Page 22: MIPS-SPIM Taiwan

PROBLEM 1. SIMPLE CALCULATORSample input / output Input:

• Two integer x, y( x, y may be positive, negative or zero)

• An operator (+, ‐, *, /) Requirements:

• Print  the correct answer.• Can do many calculations 

iteratively• If the answer of division includes a 

remainder please print it out• The file name is Calculator.s

Page 23: MIPS-SPIM Taiwan

PROBLEM 2: STAR PYRAMID

Build a pyramid using stars(*) and white spaces given number of rows

*

Example:

** *

** ** *** ** ** *** ** ** ** *

44

33

22

11

Space # of rows = 5

Page 24: MIPS-SPIM Taiwan

PROBLEM 2: STAR PYRAMID

Input: Number of rows of the pyramid: N

(N>0)

Requirements: Print the correct answer

(Number of white spaces and stars should be correct)

File Name: StarPyramid.s

Pyramid may seemed to be a little inclined caused by the default display settings of the console window in QtSPIM will NOT affect your score

Sample Input

Sample Output

Page 25: MIPS-SPIM Taiwan

PROBLEM 3: DEFORMATION OF PASCAL TRIANGLE

F(m ,n) = F(m-1 ,n-1)+ 2*F(m-1 ,n) (F(x, 0)=1 for all x and F(0, y)=0 for y>0)

1 0 0 0 0 0 …

1 1 0 0 0 0 …

1 3 1 0 0 0 …

1 7 5 1 0 0 …

1 15 17 7 1 0 …

0 1 2 3 4 5 …

0

1

2

3

4

*2

*2 *2

*2 *2 *2

*2 *2 *2 *2

Page 26: MIPS-SPIM Taiwan

PROBLEM 3: DEFORMATION OF PASCAL TRIANGLE

Input: Two integers m, n (m=1~20, n=1~99)

Requirements:

Output F(m ,n) = F(m-1 ,n-1)+ 2*F(m-1 ,n) (F(x, 0)=1 for all x and F(0, y)=0 for y>0)

Print the correct answer Can do many calculations iteratively

File Name: PascalDeform.s

Page 27: MIPS-SPIM Taiwan

HOW TO EXECUTE MY PROGRAM USING QTSPIM?

Write your own assembly program, and save it as .s fileWrite your own assembly program, and save it as .s file

Simulator Clear RegistersSimulator Clear Registers

Simulator Reinitialize SimulatorSimulator Reinitialize Simulator

Open your .s fileOpen your .s file

Simulator  Run / ContinueSimulator  Run / Continue

Page 28: MIPS-SPIM Taiwan

SUBMISSION Deadline: 11:59pm, 1 Nov. (Tue.)

You must submit at least the following files: Calculator.s

StarPyramid.s

PascalDeform.s

(Your student id)_hwX_document.doc/pdf/docx

Please put all your files in a directory named by your student id in uppercase, and then compress it into one zipped file. (e.g. zip/tgz …)

You should upload your zipped file HW#2 to https://filestork.net/suv6oro7 before the deadline.

The file name should be like B99xxxxxx.zip, eg. B99705099.zip

Rename your file for multiple uploads, eg. B99705099_v2.zip, B99705099_v3.zip …

Page 29: MIPS-SPIM Taiwan

SUBMISSION

7https://filestork.net/suv6oro7

Remember to rename your file for multiple uploads!!!

Page 30: MIPS-SPIM Taiwan

GRADING GUIDELINES

Description For Each ProblemProblem

Program runs without error messages 10%

Program executes correctly 60%

Documentation and description ofthe program

20%

Implementation Detail 10%

Page 31: MIPS-SPIM Taiwan

CONTACT INFORMATION

TA Hours @ 管五 503-CShu-Yang Lin (林書漾) Fri. 10:00~12:00

Hsun-Pei Wang (王珣沛) Fri. 10:00~12:00

Liang-Tsen Shen (沈亮岑) Mon. 10:00~12:00

Contact us if you have any problem 林書漾 [email protected]

王珣沛 [email protected]

沈亮岑 [email protected]

Page 32: MIPS-SPIM Taiwan

DEADLINE

2011/11/1 (Tue.)

Late submission policy: 10% off from your total score each day after the deadline

Page 33: MIPS-SPIM Taiwan

THANK YOU FOR YOUR ATTENTIONAny Questions?