The MIPS 32 Our objective is to get an appreciation with: working with a “typical” computer machine language. programming in assembly language. debuging.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
CS3350B Computer Architecture Winter 2015 Lecture 4
Chapter 2 Instructions: Language of the Computer
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CSE431 L02 MIPS ISA Review.1Irwin, PSU, 2005 CSE 431 Computer Architecture Fall 2005 Lecture 02: MIPS ISA Review Mary Jane Irwin (
331 Week 3. 1Spring 2005 Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000.
14:332:331 Computer Architecture and Assembly Language Spring 06 Week 4: Addressing Mode, Assembler, Linker [Adapted from Dave Patterson’s UCB CS152 slides.
MIPS Architecture CPSC 321 Computer Architecture Andreas Klappenecker.
331 W02.1Spring 05 Announcements  HW1 is due on this Friday  Appendix A (on CD) is very helpful to HW1.
OK, we are now ready to begin Chapter 2 of our text
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
331 W02.1Fall 2003 Announcements  HW1 is due on this Friday (Sept 12 th )  Appendix A is very helpful to HW1. Check out system calls on Page A-48. 
331 Week 3. 1Fall 2003 Head’s Up  This week’s material l MIPS control flow operations (for, while, if-the-else, …) -Reading assignment - PH 3.5  Reminders.
ECE 4436ECE 5367 ISA I. ECE 4436ECE 5367 CPU = Seconds= Instructions x Cycles x Seconds Time Program Program Instruction Cycle CPU = Seconds= Instructions.
331 Practice Exam.1Fall 2003 Naming Conventions for Registers 0$zero constant 0 (Hdware) 1$atreserved for assembler 2$v0expression evaluation & 3$v1function.
Lecture 4 section 2.4-?? As opposed to CISC – Complicated Instruction Set Architecture (ala the x86)
CS.305 Computer Architecture Computer Abstractions and Technology Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005, and from.
CMPT 334 Computer Organization
CSE331 W03.1Irwin&Li Fall 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 3 Section 1: Mary Jane Irwin (
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
CS224 Fall 2011 Chap 2a Computer Organization CS224 Fall 2011 Chapter 2 a With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture slide.
CSE331 W02.1Irwin Fall 2001 PSU Computer Architecture Discussion Lecture # 2 MIPS Programming.
CS35101 Computer Architecture Spring 2006 Week 5 Paul Durand ( Course url:
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
1 Computer Architecture COSC 3430 Lecture 3: Instructions.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CS Computer Architecture Spring 2006 Week 4 Paul Durand ( Course url:
1  1998 Morgan Kaufmann Publishers Machine Instructions: Language of the Machine Lowest level of programming, control directly the hardware Assembly instructions.
CSIE30300 Computer Architecture Unit 02: MIPS ISA Review Hsin-Chou Chi [Adapted from material by and
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
CSE 340 Computer Architecture Spring 2014 MIPS ISA Review.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
Digital Systems Design L02 MIPS ISA Review.1 Digital Systems Design Lecture 02: MIPS ISA Review Adapted from Mary Jane Irwin ( )
C OMPUTER A RCHITECTURE & O RGANIZATION MIPS Instruction Set Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
EI209 Chapter 2.1Haojin Zhu, SJTU, 2015 EI 209 Computer Organization Fall 2015 Chapter 2: Instructions: Language of the Computer Haojin Zhu (
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
CPE 232 MIPS ISA1 CPE 232 Computer Organization MIPS ISA Dr. Gheith Abandah [Adapted from the slides of Professor Mary Irwin ( which.
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
CSE 340 Computer Architecture Summer 2016 MIPS ISA Review.
CS35101 Computer Architecture Spring 2006 Week 2.
MIPS Assembly.
Computer Architecture Instruction Set Architecture
Instructions: Language of the Computer
ELEN 468 Advanced Logic Design
ECE3055 Computer Architecture and Operating Systems MIPS ISA
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
Chapter 2 Instructions: Language of the Computer
Instruction encoding The ISA defines Format = Encoding
Computer Architecture
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
COMS 361 Computer Organization
COMS 361 Computer Organization
Computer Architecture
CS352H Computer Systems Architecture
Presentation transcript:

The MIPS 32 Our objective is to get an appreciation with: working with a “typical” computer machine language. programming in assembly language. debuging programs at the machine level. designing machine language “subroutines” and “service routines” that can be used in predominately high level language programs. We are doing that by investigating and working in a MIPS 32 environment. It is our objective to develop the capability to feel confident that we can be “comfortable” working in any machine/assembly level environment. It is not our objective to become competent MISP 32 programmers.

An Aside: Breadboards in the Lab  We have breadboards (one per group) and lockers in the lab for you to keep projects that need to remain set up for a “reasonable” period of time. Breadboards are not to leave the lab.  You must return chips to the proper bins expeditiously so that there are enough chips for all to use. At the end of the quarter all boards must be cleaned up before grades can be issued.  When you want to “keep” a breadboard with chips on it, put the breadboard in a locker and put your names on a “sticky” on the outside of the locker. I prefer you not put a lock on the locker unless you have some of your own equipment/supplies in it. Breadboards without names on the locker may be confiscated.

MIPS (RISC) Design Principles  Simplicity favors regularity l fixed size instructions – 32-bits l small number of instruction formats l opcode always the first 6 bits  Good design demands good compromises l three instruction formats  Smaller is faster l limited instruction set l compromise on number of registers in register file l limited number of addressing modes  Make the common case fast l arithmetic operands from the register file (load-store machine) l allow instructions to contain immediate operands

MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100 Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 registers ($zero - $ra) PC ALU byte address (big Endian) Fetch PC = PC+4 DecodeExec Add 32 4 Add 32 branch offset

MIPS R3000 Instruction Set Architecture (ISA)  Instruction Categories l Computational l Load/Store l Jump and Branch l Floating Point -coprocessor l Memory Management l Special R0 - R31 PC HI LO Registers OP rs rt rdsafunct rs rt immediate jump target 3 Instruction Formats: all 32 bits wide R format I format J format

MIPS Addressing Modes 1. Register addressing op rs rt rd funct Register word operand op rs rt offset 2. Base addressing base register Memory word or byte operand 3. Immediate addressing op rs rt operand 4. PC-relative addressing op rs rt offset Program Counter (PC) Memory branch destination instruction 5. Pseudo-direct addressing op jump address Program Counter (PC) Memory jump destination instruction||

MIPS Register Convention NameRegister Number UsagePreserve on call? $zero0constant 0 (hardware)n.a. $at1reserved for assemblern.a. $v0 - $v12-3returned valuesno $a0 - $a34-7argumentsyes $t0 - $t78-15temporariesno $s0 - $s716-23saved valuesyes $t8 - $t924-25temporariesno $gp28global pointeryes $sp29stack pointeryes $fp30frame pointeryes $ra31return addr (hardware)yes

Why use Assembly Language Programing ?  When speed is critical. Maybe use Assembly Language for critical components.  To exploit specialized machine capabilities.  When no High Lever Language compiler is available for a machine.  When one wants to debug particularly complex structures. Why not:  Inherently machine dependent.  Assembly Language programs are much longer.  Assembly Language programs tend to be harder to debug.  Abstraction level is much lower that with a High Level Language.

SPIM Simulator (

A MIPS Sample Program C program MIPS Assy Program Machine code Memory Dump Reverse Engineered Code

Homework 2  Analyze MIPS program  Write MIPS program  Use SPIM simulator  Look at MIPS Context Frames

Supporting Procedures Process:  Place parameters where procedure can access them  Transfer control to the procedure  Acquire storage resources for the procedure  Perform the task  Place result where calling program can access it  Return control to calling program Support structure:  $a0-$a3 argument passing registers  $v0-$v1 return value registers  $ra return address register

Procedure Call Convention First the caller must:

MIPS 32 Context Frames

Calling Procedure: Spilling Registers  What if the callee needs more registers? What if the procedure is recursive? l uses a stack – a last-in-first-out queue – in memory for passing additional values or saving (recursive) return address(es)  $sp, is used to address the stack (which “grows” from high address to low address) l add data onto the stack – push $sp = $sp – 4 data on stack at new $sp l remove data from the stack – pop data from stack at $sp $sp = $sp + 4 low addr high addr $sptop of stack

MIPS Arithmetic Instructions  MIPS assembly language arithmetic statement add$t0, $s1, $s2 sub$t0, $s1, $s2  Each arithmetic instruction performs only one operation  Each arithmetic instruction fits in 32 bits and specifies exactly three operands destination  source1 op source2  Those operands are all contained in the datapath’s register file ( $t0,$s1,$s2 ) – indicated by $  Operand order is fixed (destination first)

MIPS Arithmetic Instructions  MIPS assembly language arithmetic statement add$t0, $s1, $s2 sub$t0, $s1, $s2  Each arithmetic instruction performs only one operation  Each arithmetic instruction fits in 32 bits and specifies exactly three operands destination  source1 op source2  Each arithmetic instruction performs only one operation  Each arithmetic instruction fits in 32 bits and specifies exactly three operands destination  source1 op source2  Operand order is fixed (destination first)  Those operands are all contained in the datapath’s register file ( $t0,$s1,$s2 ) – indicated by $

 Instructions, like registers and words of data, are 32 bits long  Arithmetic Instruction Format (R format): add $t0, $s1, $s2 Machine Language - Add Instruction op rs rt rd shamt funct op6-bitsopcode that specifies the operation rs5-bitsregister file address of the first source operand rt5-bitsregister file address of the second source operand rd5-bitsregister file address of the result’s destination shamt5-bitsshift amount (for shift instructions) funct6-bitsfunction code augmenting the opcode

addi$sp, $sp, 4#$sp = $sp + 4 slti $t0, $s2, 15#$t0 = 1 if $s2<15  Machine format (I format): MIPS Immediate Instructions op rs rt 16 bit immediate I format  Small constants are used often in typical code  Possible approaches? l put “typical constants” in memory and load them l create hard-wired registers (like $zero) for constants like 1 l have special instructions that contain constants !  The constant is kept inside the instruction itself! l Immediate format limits values to the range –1 to -2 15

 We'd also like to be able to load a 32 bit constant into a register, for this we must use two instructions  a new "load upper immediate" instruction lui $t0,  Then must get the lower order bits right, use ori $t0, $t0, How About Larger Constants?

MIPS Memory Access Instructions  MIPS has two basic data transfer instructions for accessing memory lw$t0, 4($s3) #load word from memory sw$t0, 8($s3) #store word to memory  The data is loaded into (lw) or stored from (sw) a register in the register file – a 5 bit address  The memory address – a 32 bit address – is formed by adding the contents of the base address register to the offset value l A 16-bit field meaning access is limited to memory locations within a region of  2 13 or 8,192 words (  2 15 or 32,768 bytes) of the address in the base register l Note that the offset can be positive or negative

 Load/Store Instruction Format (I format): lw $t0, 24($s2) Machine Language - Load Instruction op rs rt 16 bit offset Memory dataword address (hex) 0x x x x c 0xf f f f f f f f $s2 0x $s2 = 0x x x120040ac $t0

Byte Addresses  Since 8-bit bytes are so useful, most architectures address individual bytes in memory l The memory address of a word must be a multiple of 4 (alignment restriction)  Big Endian: leftmost byte is word address IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA  Little Endian:rightmost byte is word address Intel 80x86, DEC Vax, DEC Alpha (Windows NT) msblsb little endian byte big endian byte 0

Loading and Storing Bytes  MIPS provides special instructions to move bytes lb$t0, 1($s3) #load byte from memory sb$t0, 6($s3) #store byte to memory op rs rt 16 bit offset  What 8 bits get loaded and stored? l load byte places the byte from memory in the rightmost 8 bits of the destination register -what happens to the other bits in the register? l store byte takes the byte from the rightmost 8 bits of a register and writes it to a byte in memory -what happens to the other bits in the memory word?

 MIPS conditional branch instructions: bne $s0, $s1, Lbl#go to Lbl if $s0  $s1 beq $s0, $s1, Lbl#go to Lbl if $s0=$s1 Ex: if (i==j) h = i + j; bne $s0, $s1, Lbl1 add $s3, $s0, $s1 Lbl1:... MIPS Control Flow Instructions  Instruction Format (I format): op rs rt 16 bit offset  How is the branch destination address specified?

Specifying Branch Destinations  Use a register (like in lw and sw) added to the 16-bit offset l which register? Instruction Address Register (the PC) -its use is automatically implied by instruction -PC gets updated (PC+4) during the fetch cycle so that it holds the address of the next instruction l limits the branch distance to to instructions from the (instruction after the) branch instruction, but most branches are local anyway PC Add 32 offset sign-extend from the low order 16 bits of the branch instruction branch dst address ? Add 4 32

 MIPS also has an unconditional branch instruction or jump instruction: j label#go to label Other Control Flow Instructions  Instruction Format (J Format): op 26-bit address PC from the low order 26 bits of the jump instruction

Branching Far Away  What if the branch destination is further away than can be captured in 16 bits?  The assembler comes to the rescue – it inserts an unconditional jump to the branch target and inverts the condition beq$s0, $s1, L1 becomes bne$s0, $s1, L2 jL1 L2:

 MIPS procedure call instruction: jalProcedureAddress#jump and link  Saves PC+4 in register $ra to have a link to the next instruction for the procedure return  Machine format (J format):  Then can do procedure return with a jr$ra#return  Instruction format (R format): Instructions for Accessing Procedures op 26 bit address op rs funct

MIPS ISA – First look CategoryInstrOp CodeExampleMeaning Arithmetic (R & I format) add0 and 32add $s1, $s2, $s3$s1 = $s2 + $s3 subtract0 and 34sub $s1, $s2, $s3$s1 = $s2 - $s3 add immediate8addi $s1, $s2, 6$s1 = $s2 + 6 or immediate13ori $s1, $s2, 6$s1 = $s2 v 6 Data Transfer (I format) load word35lw $s1, 24($s2)$s1 = Memory($s2+24) store word43sw $s1, 24($s2)Memory($s2+24) = $s1 load byte32lb $s1, 25($s2)$s1 = Memory($s2+25) store byte40sb $s1, 25($s2)Memory($s2+25) = $s1 load upper imm15lui $s1, 6$s1 = 6 * 2 16 Cond. Branch (I & R format) br on equal4beq $s1, $s2, Lif ($s1==$s2) go to L br on not equal5bne $s1, $s2, Lif ($s1 !=$s2) go to L set on less than0 and 42slt $s1, $s2, $s3if ($s2<$s3) $s1=1 else $s1=0 set on less than immediate 10slti $s1, $s2, 6if ($s2<6) $s1=1 else $s1=0 Uncond. Jump (J & R format) jump2j 2500go to jump register0 and 8jr $t1go to $t1 jump and link3jal 2500go to 10000; $ra=PC+4