MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

Similar documents
MIPS Processor Overview

QtSPIM and MARS : MIPS Simulators

MIPS ProgramTemplate

CENG3420 Lab 1-1: MIPS assembly language programing

CPSC 330 Computer Organization

COMP2421 COMPUTER ORGANZATION. Lab 3

EECS 322 The SPIM simulator

Instructions: Language of the Computer

SPIM Instruction Set

Assignment 1: Pipelining Implementation at SPIM Simulator

Course Administration

MIPS Architecture and Assembly Language Overview

USING A SIMULATOR. QUICK INTRODUCTION From various sources For cs470

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Graduate Institute of Electronics Engineering, NTU. SPIM and MIPS Asm. Presenter: 沈文中 (TA) Date: 2004/10/13

Computer Systems and Architecture

SPIM & MIPS Programming

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Five classic components

Instructions: Language of the Computer

Course Administration

Programming in MIPS. Tutorial

MIPS (SPIM) Assembler Syntax

MIPS%Assembly% E155%

Lecture 4: MIPS Instruction Set

Five classic components

Chapter 2: Instructions:

Lecture 5: Procedure Calls

CS3350B Computer Architecture

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

We will study the MIPS assembly language as an exemplar of the concept.

SPIM S20: A MIPS R2000 Simulator

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

CS3350B Computer Architecture MIPS Introduction

Assembler. Lecture 8 CS301

Chapter 2A Instructions: Language of the Computer

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

ECE 250 / CS 250 Computer Architecture. Procedures

CS222: MIPS Instruction Set

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS Assembly Language. Today s Lecture

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

Reduced Instruction Set Computer (RISC)

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

Instruction Set Architectures Part I: From C to MIPS. Readings:

Reduced Instruction Set Computer (RISC)

Computer Organization MIPS ISA

MIPS Assembly Programming

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

MIPS Procedure Calls. Lecture 6 CS301

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

MIPS function continued

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

MIPS Assembly (Functions)

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Topic Notes: MIPS Instruction Set Architecture

MIPS Assembly Language Guide

SPIM & MIPS Programming

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

MIPS Coding Continued

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

EN164: Design of Computing Systems Lecture 09: Processor / ISA 2

Computer Architecture

Levels of Programming. Registers

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

MIPS Assembly Language Programming

ECE369. Chapter 2 ECE369

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Instruction encoding. MIPS Instruction encoding

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

MIPS Reference Guide

CS222: Dr. A. Sahu. Indian Institute of Technology Guwahati

MIPS Hello World. MIPS Assembly 1. # PROGRAM: Hello, World! # Data declaration section. out_string:.asciiz "\nhello, World!\n"

Lectures 3-4: MIPS instructions

MIPS Assembly Language

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Programming with QtSpim: A User s Manual

Lecture # 1. SPIM & MIPS Programming

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

EEC 581 Computer Architecture Lecture 1 Review MIPS

ECE 30 Introduction to Computer Engineering

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Transcription:

MIPS and QTSPIM Recap Cptr280 Dr Curtis Nelson MIPS Architecture The MIPS architecture is considered to be a typical RISC architecture Simplified instruction set Programmable storage 32 x 32-bit General Purpose Registers (register 0 is a fixed value of 0) 2^32 bytes of addressable main memory 1

MIPS-32 Instruction Set Architecture Registers R0 - R31 PC HI LO 3 Instruction Formats: all 32 bits wide op rs rt rd sa funct op rs rt immediate op jump target R format I format J format Register Names in MIPS Assembly Language With MIPS, there is a convention for mapping register names into general purpose register numbers. name register number usage $zero 0 constant 0 $v0-$v1 2-3 results $a0-$a3 4-7 arguments $t0-$t7 8-15 temporaries $s0-$s7 16-23 saved $t8-$t9 24-25 more temps $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address 2

MIPS Arithmetic Instructions MIPS assembly language arithmetic instructions: add $t0, $s1, $s2 sub $t0, $s1, $s2 Each arithmetic instruction performs one operation. Each specifies exactly three operands that are all contained in the datapath's register file ($t0,$s1,$s2) destination source1 op source2 Instruction Format (R format) 0 17 18 8 0 0x22 MIPS Instruction Fields MIPS fields are given names to make them easier to refer to: op rs rt rd shamt funct op 6-bits opcode that specifies the operation rs 5-bits register file address of the first source operand rt 5-bits register file address of the second source operand rd 5-bits register file address of the result s destination shamt 5-bits shift amount (for shift instructions) funct 6-bits function code augmenting the opcode 3

MIPS Memory Model Address space 32 address bits meaning 2 32 unique memory locations MIPS Assembly Language Program Structure Just plain text file with data declarations, program code (name of file should end in suffix.s to be used with SPIM simulator) Data declaration section followed by program code section 4

Data Declarations Placed in section of program identified with assembler directive.data Declares variable names used in program; storage allocated in main memory (RAM) Code Placed in section of text identified with assembler directive.text Contains program code (instructions) Starting point for code execution given label main: Ending point of main code should use exit system call (covered later under System Calls) 5

Comments Anything following # on a line # This stuff would be considered a comment Template for a MIPS assembly language program # Comment giving name of program and description of function # Template.s # Bare-bones outline of MIPS assembly language program.data # variable declarations here #....text main: # indicates start of code (first instruction to execute) # remainder of program code here #... #... 6

Example Program: add2numbersprog1.asm ## Program adds 10 and 11.text.globl main # text section # call main by SPIM main: ori $8,$0,0xA # load 10" into register 8 ori $9,$0,0xB # load 11" into register 9 add $10,$8,$9 # add registers 8 and 9, put result # in register 10 Code Translation Example Assumes Variables Stored in Register File if-then-else structure written in C: if (a < 0) then { b = 0 a; d++; } else { b = a; e++;} a is stored in: $t8 b $s0 d $t1 e $t2 if-then-else structure written in MIPS assembly language: bgez $t8, else sub $s0, $r0, $t8 addi $t1, $t1, 1 b next else: mov $s0, $t8 addi $t2, $t2, 1 next: 7

Code Translation Example Assumes Variables Stored in Main Memory QTSPIM Introduction What is SPIM? a simulator that runs assembly programs for MIPS R2000/R3000 RISC computers What does SPIM do? reads MIPS assembly language files and translates to machine language executes the machine language instructions shows contents of registers and memory works as a debugger (supports break-points and single-stepping) provides basic Operating System (OS)-like services, like simple I/O 8

Learning MIPS & SPIM MIPS assembly is a low-level programming language. The best way to learn any programming language is from live code. We will start by going through a few example programs and explaining the key concepts. Further, an examples directory has been created in /wwu/class/cptr280 of several simple, well-documented assembly programs for you to experiment with. Line-by-line syntax can best be learned by picking up what you need from the textbook and on-line tutorials. Start by copying existing programs and modifying them incrementally making sure you understand the behavior at each step. Tip: The best way to understand and remember a construct or keyword is to experiment with it in code, not by reading about it. Using SPIM Loading source file Use File -> Open menu Simulation Simulator -> Settings : In the Display section check only the first two items: Save window positions and General registers in hexadecimal In the Execution section check only Allow pseudo instructions Simulator -> Set Value : to load PC with address of first instruction enter Address or Register Name as PC and enter Value as 0x00400000 reason: the text area of memory, where programs are stored, starts here Simulator -> Go : run loaded program Click the OK button in the Run Parameters pop-up window if the StartingAddress: value is 0x00400000 Simulator -> Break : stop execution Simulator -> Clear Registers and Reinitialize : clean-up before new run 9

Using SPIM Simulator -> Reload : load file again after editing Simulator -> Single Step or Multiple Step : stepping to debug Simulator -> Breakpoints : set breakpoints Notes: Text segment window of SPIM shows assembly and corresponding machine code pseudo-instructions each expand to more than one machine instruction If Load trap file is checked in Simulator -> Settings then text segment shows additional trap-handling code If Delayed Branches is checked in Simulator -> Settings then statementx will execute before control jumps to L1 in following code to avoid this, insert nop before statementx: jal L1 statementx L1: nop SPIM Example Program: add2numbersprog1.asm ## Program adds 10 and 11.text.globl main # text section # call main by SPIM main: ori $8,$0,0xA # load 10" into register 8 ori $9,$0,0xB # load 11" into register 9 add $10,$8,$9 # add registers 8 and 9, put result # in register 10 10

MIPS Assembly Code Layout Typical Program Layout.data #data section # user program data.text.globl main #code section #starting point: must be global main: # user program code MIPS Assembler Directives Top-level Directives:.text indicates that following items are stored in the user text segment, typically instructions.data indicates that following data items are stored in the data segment.globl sym declare that symbol sym is global and can be referenced from other files 11

More MIPS Assembler Directives Common Data Definitions:.word w1,, wn store n 32-bit quantities in successive memory words.half h1,, hn store n 16-bit quantities in successive memory halfwords.byte b1,, bn store n 8-bit quantities in successive memory bytes.ascii str store the string in memory but do not null-terminate it characters are represented in single quotes a strings are represented in double-quotes str special characters, e.g.. \n, \t, follow C convention.asciiz str store the string in memory and null-terminate it SPIM System Calls System Calls (syscall) OS-like services Method load system call code into register $v0 (see following table for codes) load arguments into registers $a0,, $a3 call system with SPIM instruction syscall after call, return value is in register $v0, or $f0 for floating point results 12

SPIM System Call Codes Service Code (put in $v0) Arguments Result print_int 1 $a0=integer print_float 2 $f12=float print_double 3 $f12=double print_string 4 $a0=address of string read_int 5 int in $v0 read_float 6 float in $f0 read_double 7 double in $f0 read_string 8 $a0=buffer, $a1=length sbrk 9 $a0=amount addr in $v0 exit 10 SPIM Example Program: systemcalls.asm ## Enter two integers in ## console window ## Sum is displayed.text.globl main main: la $t0, value lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0) li $v0, 4 la $a0, msg1 syscall system call code for print_string li $v0, 5 syscall sw $v0, 0($t0) li $v0, 5 syscall sw $v0, 4($t0) system call code for read_int result returned by call li $v0, 1 move $a0, $t3 syscall li $v0, 10 syscall.data value:.word 0, 0, 0 msg1:.asciiz Sum = " argument to print_string call system call code for print_int argument to print_int call system call code for exit 13

More SPIM Example Programs In the class examples directory you will find 18 simple well-documented MIPS assembly programs. Run the code in the order below of increasing complexity. 1. add2numbersprog1 2. add2numbersprog2 3. storewords 4. swap2memorywords 5. branchjump 6. systemcalls 7. overflow 8. averageofbytes 9. printloop 10. sumofsquaresprog1 11. sumofsquaresprog2 12. sumofsquaresprog3 13. proccallsprog1 14. proccallsprog1modified 15. proccallsprog2 16. addfirst100 17. factorialnonrecursive 18. factorialrecursive Conclusions The code presented so far should get you started in writing your own MIPS assembly. Remember the only way to master the MIPS assembly language in fact, any computer language is to write lots and lots of code. For anyone aspiring to understand modern computer architecture it is extremely important to master MIPS assembly as all modern computers (since the mid-80 s) have been inspired by, if not based fully or partly on the MIPS instruction set architecture. 14