March 21, 2016©2003-2009 Craig ZIlles (adapted from slides by Howard Huang) 1 What does this code do? label:sub$a0, $a0, 1 bne$a0, $zero, label Arf.

Slides:



Advertisements
Similar presentations
Henk Corporaal TUEindhoven 2011
Advertisements

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.
CENG 311 Decisions in C/Assembly Language
Goal: Write Programs in Assembly
4.
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
Branches Two branch instructions:
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Comp Sci Control structures 1 Ch. 5 Control Structures.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Fall 2003SYCS-401 Operating Systems Instruction Set Architecture An overview of MIPS R3000 assembly language.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
1 CS232: Computer Architecture II Fall 2011 Intel i7 Quad-core.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Lecture 4: MIPS Instruction Set
17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization and Design Addressing Modes Montek Singh Sep 30, 2015 Lecture 7.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CDA 3101 Spring 2016 Introduction to Computer Organization
Computer Organization Instructions Language of The Computer (MIPS) 2.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
1 CS232 Exam 1 Solutions February 20, 2004 Name:  This exam has 5 pages, including this cover.  There are three questions, worth a total of 100 points.
Ch2a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
What does this code do? label: sub $a0, $a0, 1 bne $a0, $zero, label
CSCI206 - Computer Organization & Programming
CS2100 Computer Organisation
Computer Architecture & Operations I
Decision Making.
Instructions for Making Decisions
The University of Adelaide, School of Computer Science
Pick up the handout on your way in!!
Instructions - Type and Format
Week 6 The darkness, the loop of negative thoughts on repeat, clamours and interferes with the music I hear in my head. Lady Gaga.
CSCI206 - Computer Organization & Programming
MIPS coding.
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
Instruction encoding The ISA defines Format = Encoding
The University of Adelaide, School of Computer Science
Data manipulation instructions
Flow of Control -- Conditional branch instructions
Flow of Control -- Conditional branch instructions
9/27: Lecture Topics Memory Data transfer instructions
Conditional Branching (beq)
Presentation transcript:

March 21, 2016© Craig ZIlles (adapted from slides by Howard Huang) 1 What does this code do? label:sub$a0, $a0, 1 bne$a0, $zero, label Arf

March 21, 2016More MIPS instructions2 Today’s Lecture  We’ll go into more detail about the ISA. —Pseudo-instructions —Using branches for conditionals

March 21, 2016More MIPS instructions3 Pseudo-instructions  MIPS assemblers support pseudo-instructions that give the illusion of a more expressive instruction set, but are actually translated into one or more simpler, “real” instructions.  In addition to the la (load address) we saw on last lecture, you can use the li and move pseudo-instructions: li $a0, 2000# Load immediate 2000 into $a0 move$a1, $t0# Copy $t0 into $a1  They are probably clearer than their corresponding MIPS instructions: addi$a0, $0, 2000# Initialize $a0 to 2000 add$a1, $t0, $0# Copy $t0 into $a1  We’ll see lots more pseudo-instructions this semester. —A complete list of instructions is given in Appendix A of the text.Appendix A —Unless otherwise stated, you can always use pseudo-instructions in your assignments and on exams.

March 21, 2016More MIPS instructions4  The instructions in a program usually execute one after another, but it’s often necessary to alter the normal control flow.  Conditional statements execute only if some test expression is true. // Find the absolute value of *a0 v0 = *a0; if (v0 < 0) v0 = -v0;// This might not be executed v1 = v0 + v0;  Loops cause some statements to be executed many times. // Sum the elements of a five-element array a0 v0 = 0; t0 = 0; while (t0 < 5) { v0 = v0 + a0[t0];// These statements will t0++;// be executed five times } Control flow in high-level languages

March 21, 2016More MIPS instructions5  It can be useful to draw control-flow graphs when writing loops and conditionals in assembly: // Find the absolute value of *a0 v0 = *a0; if (v0 < 0) v0 = -v0; v1 = v0 + v0; // Sum the elements of a0 v0 = 0; t0 = 0; while (t0 < 5) { v0 = v0 + a0[t0]; t0++; } Control-flow graphs

March 21, 2016More MIPS instructions6  In section, we introduced some of MIPS’s control-flow instructions j // for unconditional jumps bne and beq// for conditional branches slt and slti// set if less than (w/ and w/o an immediate)  And how to implement loops  Today, we’ll talk about —MIPS’s pseudo branches —if/else —case/switch (bonus material) MIPS control instructions

March 21, 2016More MIPS instructions7  The MIPS processor only supports two branch instructions, beq and bne, but to simplify your life the assembler provides the following other branches: blt$t0, $t1, L1// Branch if $t0 < $t1 ble$t0, $t1, L2// Branch if $t0 <= $t1 bgt$t0, $t1, L3// Branch if $t0 > $t1 bge$t0, $t1, L4// Branch if $t0 >= $t1  There are also immediate versions of these branches, where the second source is a constant instead of a register.  Later this semester we’ll see how supporting just beq and bne simplifies the processor design. Pseudo-branches

 Most pseudo-branches are implemented using slt. For example, a branch- if-less-than instruction blt $a0, $a1, Label is translated into the following. slt$at, $a0, $a1// $at = 1 if $a0 < $a1 bne$at, $0, Label// Branch if $at != 0  This supports immediate branches, which are also pseudo-instructions. For example, blti $a0, 5, Label is translated into two instructions. slti$at, $a0, 5// $at = 1if $a0 < 5 bne$at, $0, Label// Branch if $a0 < 5  All of the pseudo-branches need a register to save the result of slt, even though it’s not needed afterwards. —MIPS assemblers use register $1, or $at, for temporary storage. —You should be careful in using $at in your own programs, as it may be overwritten by assembler-generated code. March 21, 2016More MIPS instructions8 Implementing pseudo-branches

March 21, 2016More MIPS instructions9 Translating an if-then statement  We can use branch instructions to translate if-then statements into MIPS assembly code. v0 = *a0;lw $v0, 0($a0) if (v0 < 0)bgt$v0, 0, skip v0 = -v0;sub$v0, $zero, $v0 v1 = v0 + v0;skip:add$v1, $v0, $v0  Sometimes it’s easier to invert the original condition. —In this case, we changed “continue if v0 = 0 ”. —This saves a few instructions in the resulting assembly code.

March 21, 2016More MIPS instructions10  Let’s write a program to see if a number is a power of 3. See supplementary material. Control-flow Example

March 21, 2016More MIPS instructions11 Translating an if-then-else statements  If there is an else clause, it is the target of the conditional branch —And the then clause needs a jump over the else clause // increase the magnitude of v0 by one if (v0 < 0)bge$v0, $0, E v0 --;sub$v0, $v0, 1 jL else v0 ++;E:add$v0, $v0, 1 v1 = v0;L:move$v1, $v0  Dealing with else-if code is similar, but the target of the first branch will be another if statement. —Drawing the control-flow graph can help you out.

March 21, 2016More MIPS instructions12 Bonus Material

March 21, 2016More MIPS instructions13 Case/Switch Statement  Many high-level languages support multi-way branches, e.g. switch (two_bits) { case 0: break; case 1: /* fall through */ case 2:count ++; break; case 3: count += 2; break; }  We could just translate the code to if, thens, and elses: if ((two_bits == 1) || (two_bits == 2)) { count ++; } else if (two_bits == 3) { count += 2; }  This isn’t very efficient if there are many, many cases.

March 21, 2016More MIPS instructions14 Case/Switch Statement switch (two_bits) { case 0: break; case 1: /* fall through */ case 2:count ++; break; case 3: count += 2; break; }  Alternatively, we can: 1.Create an array of jump targets 2.Load the entry indexed by the variable two_bits 3.Jump to that address using the jump register, or jr, instruction  This is much easier to show than to tell. —(see the example with the lecture notes online)