Transcription

1 Introduction to MIPS Instruction Set Architecture The MIPS used by SPIM is a 32-bit reduced instruction set architecture with 32 integer and 32 floating point registers. Other characteristics are as follows: 1. Addressing modes: LIKE RISC, MIPS is a load-store architecture with only one addressing mode that is Displacement Mode. The following two instructions show how these modes are used. Load /Reading data from memory LW R2, 100(R3) The LW mnemonic means Load a Word (four bytes of data) from memory. R2 is destination to where the data will be loaded. Previous value of R2 will be over written. In displacement mode the physical address is calculated by adding R3 and 100 (Value of R ). So if R3 is 20, the final address will 120 and R2 will have a copy of 4 bytes of data stored at memory address 120. Note that value of R3 will remain unchanged. General Format is LW rt, Imm(rs), where Imm is a 16-bit value. Storing/Writing a word to memory SW R5, 100(R6) The SW mnemonic means Store a Word (four bytes of data) to memory R5 provides the source value to be written to memory at address R So if R6 = 45, this instruction will store the value of R5 at memory location 145. IMPORTANT: Note that value of R5 and R6 remain unchanged, only the memory location at address (100+R6) get a copy of data in R5. General Format is SW rt, Imm(rs), where Imm is a 16-bit value. In MIPS this is an immediate type (I-Type) instruction with following format: Note all MIPS instructions are 32-bit (4-bytes) with left most 6-bits as opcode. For LW 6-bit opcode is and for store word it Since there are 32 registers, so 5-bits are used to specify a register field. Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field Exercise 1: What will be 32-bit hex code for the above two examples. Field Field 2. ALU instructions. These are R-Type instructions that perform mathematical and logical operations. The format of these instruction is as follow: ADD R8, R6, R4 Here the value of R6 and R4 are added and result is written in R8. After the operation, R8 is updated with R6+R4 and previous value of R8 is destroyed. R6 and R4 remain unchanged. The opcode for all R-type instruction is The general format is OpCode rd, rs, rt, where opcode can be ADD (100000), SUB(100010), OR(100101), NOR(100111), etc. The ALU function is specified by 6 bits on the LSB side as shown below: Field rs field rt field rd field Shift Amount Function Field

2 Excercise2: Find 32-bit code for OR R7, R18, R12 and SUB R5, R4, R31 Field Field Branch instructions. There are only two branch instructions, BEQ and BNEQ. The format is as follows: BEQ R5, R7, 25 This instruction change the value of Program Counter if the values of R5 and R7 are equal. The new value will be PC+4+25*4. Note that each instruction is 4-bytes long, PC is incremented by 4 after ALL instructions. For branch instruction the immediate field represents branching distance in terms of number of instructions, hence multiplication of 4 in the above instruction. General format is BEQ rs, rt, branch offset. Field OPcode, for BEQ for BNEQ rs field rt field Branch offset in Number of Instructions Exercise 3: Find 32-bit code for BNEQ R7, R8, -5. If the PC value for branch instruction is 124 what will be new value of PC if the branch is taken. Field 4. I-Type ALU instructions. You can use 16-bit constant for immediate operations. The 16-bit constant is sign-extended in hardware for calculation on 32-bit registers. Example is as follows: ADDI R4, R5, 500 This instruction add 500 immediate value to R5 and result is update in R4. General format is OpCode Rt, Rs, Imm. The opcode for ADDI is , ORI is , etc. The format is as follows Field OPcode, rs field rt field 16-bit Immediate Data Exercise 5: What is the 32-code for ADDI R7, R8, 600?. There is no SUBI, why?. 5. Special Instructions. There are some more few special instructions. I will only discuss one over here. That is Set on Less than instruction used for branching. The format is as follows: SLT R5, R4, R11 SLTI R6, R7, 100 In the first instruction, the value of R5 = 1 if R4 <R11, 0 otherwise. In second instruction, the value of R6 is 1 if R7 <100. The format is same as R-type for first instruction and I-type for second. 6. Shift Instructions. These are used to logically shift a register. The format is as follows: SLL R5, R4, 4 SRL R7, R4, 21

3 The instruction format is as follows: Field rs field = rt field rd field Shift Amount Function Field = for SLL = for SRL 7. Unconditional Jump. These are J-Type This is used to jump to long distance. The format is simple: j This allows a jump to any PC value. There format is as follows Field bit Jump address

5 Introduction to SPIM SPIM is simulator commonly used for simulating MIPS assembly language program. The following points can be noted about SPIM. 1. SPIM simulates 32-bit architecture with 32 integer and 32 floating point register. Although these register are R0 to R31 the SPIM assigns them names that start with $ sign as explained below: $zero R0 $s0 R16 $at R1 $s1 R17 $v0 R2 $s2 R18 $v1 R3 $s3 R19 $a0 R4 $s4 R20 $a1 R5 $s5 R21 $a2 R6 $s6 R22 $a3 R7 $s7 R23 $t0 R8 $t8 R24 $t1 R9 $t9 R25 $t2 R10 $k0 R26 $t3 R11 $k1 R27 $t4 R12 $Gp R28 $t5 R13 $Sp R29 $t6 R14 $Fp R30 $t7 R15 $Ra R31 2. Of these $a0-$a3 are used for parameter passing in proc (we not cover that in this course), but can use this registers as temporaries. $t0 to $t9 are temporaries, and so are $s0 to $s7 for this course. 3. For printing (or reading) some values, a special command/procedure called SYSCALL is used that uses $v0 for parameter passing as follows: Service Value in Argument Results $V0 Print an integer 1 $a0 = Number to be printed Read an Integer 5 Number returned in $V0 Print a Float 2 $f12 is number to be printed Read a Float 6 Number returned in $f0 Print a String 4 $a0 contains address of null terminated string (see asciiz keyword in string declaration above) Read a String 8 $a0 = address of buffer $a1 = length of buffer asciiz keyword in string declaration above) Exit 10

6 Your First Program in Assembly Language On MIPS Let us start our first example of writing first program in assembly language to add two numbers. First of all, let us see what will be the code in MIPS and C. C code will be as follow: C Code int a=1234; b= 5678 int sum void main () { sum = a+b; cout <<sum; } Assembly Code.globl main.data value1:.word 1234 value2:.word 5678 #type globl not global.text #Code starts here main: la $a1, value1 # Load address, a1 has the address of value1 la $a2, value2 # Load address, a1 has the address of value2 lw $t1, 0($a1) #Value1 in t1 lw $t2, 0($a2) #value2 in t2 add $t0, $t1, $t2 # sum in t0 add $a0, $t0, $zero # pass parameter to a0 for printing add $v0, $zero, 1 # Syscall 1 syscall # Cout the number li $v0, 10 syscall # exit or STOP Type the above MIPS code in a file using notepad and save it as sum.s. Note use quotes to preserve extension. Now click on PCSPIM icon on your desktop. Now Load your program using file menu. Say yes if asked to initialize registers. Now the screen would look like as shown on the next page. Notice light green area where the registers are. Red area is in the text segment and it shows your program. Blue shows its equivalent hex code and dark green shows compiled assembly for MIPS in absolute register terms. Yellow is address of each instruction in instruction memory. Single step the program using F10 key and after running some SPIM kernel code, the program will jump to your main: Now notice how register change and as you single step your program.

8 Run this code and write the hex code for each instruction as you will need it in next lab. Single step this code to see register values change. Also notice how Program Counter (PC) changes. Homework: Write a program to sort an array using bubble sort. Home work should be submitted as rollno.s in the folder on Indus/common. It will be tested in the next lab.

SPIM Instruction Set This document gives an overview of the more common instructions used in the SPIM simulator. Overview The SPIM simulator implements the full MIPS instruction set, as well as a large

MIPS (SPIM) Assembler Syntax Comments begin with # Everything from # to the end of the line is ignored Identifiers are a sequence of alphanumeric characters, underbars (_), and dots () that do not begin

Programming with QtSpim: A User s Manual John McCranie November 15, 2013 1 1 Introduction to SPIM Spim is a self-contained simulator that runs MIPS32 programs. It utilizes the instruction set originally

ECE 312: Semester Project MIPS PROJECT INSTRUCTION SET FORMAT This is a description of the required MIPS instruction set, their meanings, syntax, semantics, bit encodings. The syntax given for each instruction

EE 352 Unit 4 Assembly and the MARS Simulator Control Flow (Branch Instructions) Directives Pseudo-instructions ASSEMBLERS Assembler Syntax In MARS and most assemblers each line of the assembly program

MIPS Branch and Jump Instructions Cptr280 Dr Curtis Nelson Review of the Machine Cycle When a program is executing, its instructions are located in main memory. The address of an instruction is the address

Midterm LAST NAME FIRST NAME PERM Number Instructions Please turn off all pagers, cell phones and beepers. Remove all hats & headphones. Place your backpacks, laptops and jackets at the front. Sit in every

COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

CSE 30321 Lecture 07-08 In Class Example Handout Part A: J-Type Example: If you look in your book at the syntax for j (an unconditional jump instruction), you see something like: e.g. j addr would seemingly

ISA: The Hardware Software Interface Instruction Set Architecture (ISA) is where software meets hardware In embedded systems, this boundary is often flexible Understanding of ISA design is therefore important

Assembly Language Programming Remember the programmer shown in our first lecture? Originally, computers were programmed manually. After a while, scientists began to consider ways to accelerate and facilitate

Instructions (354 Review) Forecast Instructions are the words of a computer Instruction set architecture (ISA) is its vocabulary With a few other things, this defines the interface of computers But implementations

Computer Architecture Lecture 2 : Instructions 1 Components of a Computer Hierarchical Layers of Program Code 3 Instruction Set The repertoire of instructions of a computer 2.1 Intr roduction Different

EE 352 Homework 1 Redekopp Name: Note: Attach all work to receive full credit Score: Pseudo-Instructions 1.) (11 pts.) Expand/convert each of the following pseudo-instructions to an actual MIPS instruction

CPS311 - COMPUTER ORGANIZATION A Brief Introduction to the MIPS Architecture A bit of history The MIPS architecture grows out of an early 1980's research project at Stanford University. In 1984, MIPS computer

COMP 303 MIPS Processor Design Project 3: Simple Execution Loop Due date: November 20, 23:59 Overview: In the first three projects for COMP 303, you will design and implement a subset of the MIPS32 architecture

Shift and Rotate Instructions Shift and rotate instructions facilitate manipulations of data (that is, modifying part of a 32-bit data word). Such operations might include: Re-arrangement of bytes in a

Problem 1 (3 parts, 30 points) Code Fragments Part A (5 points) Write a MIPS code fragment that branches to label Target when register $1 is less than or equal to register $2. You may use only two instructions.

MIPS Instruction ti Set Architecture I Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Architecture (1) the attributes of a system as seen by the

Chapter 4 The Processor Part I Introduction CPU performance factors Instruction count Determined by ISA and compiler CPI and Cycle time Determined by CPU hardware We will examine two MIPS implementations

Page 1 of 9 MIPS Instruction Reference This is a description of the MIPS instruction set, their meanings, syntax, semantics, and bit encodings. The syntax given for each instruction refers to the assembly

1 EE 109 Unit 8 MIPS Instruction Set 2 Architecting a vocabulary for the HW INSTRUCTION SET OVERVIEW 3 Instruction Set Architecture (ISA) Defines the software interface of the processor and memory system

EE 352 Unit 3 MIPS ISA Instruction Set Architecture (ISA) Defines the software interface of the processor and memory system Instruction set is the vocabulary the HW can understand and the SW is composed

CS/COE0447: Computer Organization and Assembly Language Datapath and Control Sangyeun Cho Dept. of Computer Science A simple MIPS We will design a simple MIPS processor that supports a small instruction

University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Sciences CS61c Summer 2001 Woojin Yu Midterm Exam This is a closed-book exam. No calculators

Loops in SPIM Iterative loops are easy to structure in a higher-level language, in which instruction sets may be repeated innumerable times via do, if, or for. In assembly language, there are no such commands;

Problem 1 (20 points) Compilation Perform at least five standard compiler optimizations on the following C code fragment by writing the optimized version (in C) to the right. Assume f is a pure function

CO20-320241 Computer Architecture and Programming Languages CAPL Lecture 13 & 14 Dr. Kinga Lipskoch Fall 2017 Frame Pointer (1) The stack is also used to store variables that are local to function, but

Supplement for MIPS (Section 44 of the textbook) Section 44 does a good job emphasizing that MARIE is a toy architecture that lacks key feature of real-world computer architectures Most noticable, MARIE

Instruction Set Architecture (ISA)... the attributes of a [computing] system as seen by the programmer, i.e. the conceptual structure and functional behavior, as distinct from the organization of the data

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface Chapter 4 The Processor: A Based on P&H Introduction We will examine two MIPS implementations A simplified version A more realistic pipelined

LECTURE 10 Pipelining: Advanced ILP EXCEPTIONS An exception, or interrupt, is an event other than regular transfers of control (branches, jumps, calls, returns) that changes the normal flow of instruction