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

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Lecture 4: MIPS Instruction Set

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

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

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

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

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

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

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

MIPS Reference Guide

Course Administration

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

ECE 30 Introduction to Computer Engineering

Chapter 2. Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2A Instructions: Language of the Computer

SPIM Instruction Set

Lecture 5: Procedure Calls

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

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

The Program Counter. QtSPIM Register Display (Fixed-Point Registers) Erik Jonsson School of Engineering and Computer Science

Topic Notes: MIPS Instruction Set Architecture

MIPS%Assembly% E155%

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

MIPS Coding Continued

Computer Organization MIPS ISA

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

Reduced Instruction Set Computer (RISC)

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS Assembly Language Programming

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Reduced Instruction Set Computer (RISC)

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

MIPS Assembly Language Programming

Chapter 2: Instructions:

CS3350B Computer Architecture MIPS Instruction Representation

Mips Code Examples Peter Rounce

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

Instructions: Language of the Computer

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

Computer Architecture

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Course Administration

ECE232: Hardware Organization and Design

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

CS222: MIPS Instruction Set

Course Administration

Assembler. Lecture 8 CS301

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Computer Architecture

Unsigned Binary Integers

Unsigned Binary Integers

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CENG3420 Lecture 03 Review


Chapter 2. Instruction Set. The MIPS Instruction Set. Arithmetic Operations. Instructions: Language of the Computer

CS3350B Computer Architecture MIPS Introduction

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Grading: 3 pts each part. If answer is correct but uses more instructions, 1 pt off. Wrong answer 3pts off.

CS/COE1541: Introduction to Computer Architecture

MIPS (SPIM) Assembler Syntax

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Compiling Techniques

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

CS3350B Computer Architecture

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Five classic components

COMPUTER ORGANIZATION AND DESIGN

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

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

MIPS function continued

Five classic components

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Control MIPS unconditional branch instruction (i.e., jump)

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

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

CSc 256 Midterm (green) Fall 2018

Chapter 4. The Processor. Computer Architecture and IC Design Lab

EEC 581 Computer Architecture Lecture 1 Review MIPS

QtSPIM and MARS : MIPS Simulators

Introduction to MIPS Processor

Computer Architecture. MIPS Instruction Set Architecture

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Levels of Programming. Registers

MIPS Functions and the Runtime Stack

MIPS Instruction Set Architecture (2)

Systems Architecture I

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Examples of branch instructions

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

Lectures 3-4: MIPS instructions

Programmable Machines

Programmable Machines

Transcription:

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 R3 + 100). 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 R6 +100. 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 100011 2 and for store word it 101011 2. 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 000000. 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 0 0 0 0 0 0 0 rs field rt field rd field Shift Amount Function Field

Excercise2: Find 32-bit code for OR R7, R18, R12 and SUB R5, R4, R31 Field 0 0 0 0 0 0 0 Field 0 0 0 0 0 0 0 3. 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, 000100 for BEQ 000101 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 001000, ORI is 001101, 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

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

A Guide to Assembly Language Programming in MIPS on SPIM. Jahangir Ikram Beginners write assembly language programs by first writing a code in C and then converting it to assembly language. A reverse process is also often employed by recognizing typical assembly language constructs and converting them to high-level directly. C Code Integer Declaration int count = 10; I, j, k; count.word 10 i.word j.word MIPS Equivalent Byte size vairable char sum = 1, total= 5; char uni[10] = university ; sum.byte 1 total.byte 5 uni.byte u, n, i etc; Strings string str ( hello world ); str.asciiz hello world Arrays int age[100]; age.word 0:100 ; 100 words initialized to zero Assignment count = count +1; sum = age[20] +age[0] + count; The following shows a more complex conversion from C to assembly. CONTROL while (count < b) { age[count] = age[count+1]; count = count+1; } la R1, count # (load address works in spim) #R1 has address of count lw R2, 0(R1) # R2 has the value of count addi R2, R2, 1 # Increment sw R2, 0(R1) # Save count, if need be la R3, age #R3 has base address of age; lw R4, 80[R3] #20 * 4 = 80, R4 = age[20] lw R5, 0(R3) #R5 = age[0] add R4, R4, R5 # add R4, R4, R2 #R2 had value of count above # R4 has final sum.data age.word 12 23 34 54 23 45.code la R4, age ; Load base address of age Addi R2, R0, 0 ; clear R2 to 0. lable1: sll R6, R2, 2 ; R6 = R2 * 4 add R3, R6, R4 :R2= count, R4 = age ;R3 = address of age[count] lw R7, 4(R3) ;R7 = age[count+1] sw R7, 0(R6) ; age[count] = R7 addi R2, R2, 1 ; Count = count +1; slt R8, R2, R10 ; Assume R10 = b ;R8 = 1 if count<b bnez R8, R0, label1

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

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.

Exercise 2: Finding a maximum in an array. void main () { int age[] ={15, 23, 14, 25, 60, 10, 9, 11, 60, 40}; int max = 0; for (int i=0; i<10; i++) { if ( max < age[i] ) max = age[i]; } cout <<age<<endln; return; }.globl main.data age:.word 9,5,12,5,20,11,66,44,22,12.text main: la $t1,age #load base address of age array addi $t5,$zero,10 #store size of array addi $t4,$zero,0 #intialize counter to zero addi $t7,$zero,0 #intialize amx to zero loop: lw $t0,0($t1) # load one element from array addi $t1,$t1,4 #increment base address by 4! addi $t4,$t4,1 #increment counter bgt $t7,$t0,skip #compare max with present element add $t7,$zero,$t0 #update max with greater skip: bne $t4,$t5,loop #loop 10 times add $a0,$zero,$t7 li $v0,1 syscall li $v0,10 syscall # cout max #return

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.