Flow of Control -- Conditional branch instructions

Similar documents
Examples of branch instructions

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

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

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

MIPS Instruction Format

MIPS Instruction Reference

F. Appendix 6 MIPS Instruction Reference

Mips Code Examples Peter Rounce

The MIPS Instruction Set Architecture

SPIM Instruction Set

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

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

MIPS Instruction Set

Assembly Programming

Computer Architecture. The Language of the Machine

M2 Instruction Set Architecture

MIPS Reference Guide

CSc 256 Midterm 2 Fall 2011

Week 10: Assembly Programming

Computer Architecture. MIPS Instruction Set Architecture

CSc 256 Midterm (green) Fall 2018

Assembly Language. Prof. Dr. Antônio Augusto Fröhlich. Sep 2006

CSc 256 Midterm 2 Spring 2012

CS3350B Computer Architecture MIPS Instruction Representation

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

MIPS%Assembly% E155%

CPS311 - COMPUTER ORGANIZATION. A bit of history

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

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

Computer Architecture

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC)

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

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

TSK3000A - Generic Instructions

MIPS Assembly Language. Today s Lecture

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

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

Q1: /30 Q2: /25 Q3: /45. Total: /100

Final Project: MIPS-like Microprocessor

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

Question 0. Do not turn this page until you have received the signal to start. (Please fill out the identification section above) Good Luck!

Programming the processor

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

LAB B Translating Code to Assembly

Unsigned Binary Integers

Unsigned Binary Integers

Introduction to MIPS Processor

ECE260: Fundamentals of Computer Engineering

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

EE108B Lecture 3. MIPS Assembly Language II

Review: MIPS Organization

ECE260: Fundamentals of Computer Engineering

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

Course Administration

ECE Exam I February 19 th, :00 pm 4:25pm

Midterm. CS64 Spring Midterm Exam

MACHINE LANGUAGE. To work with the machine, we need a translator.

EE 109 Unit 8 MIPS Instruction Set

Chapter 2A Instructions: Language of the Computer

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

MIPS Assembly Language Programming

HW2 solutions You did this for Lab sbn temp, temp,.+1 # temp = 0; sbn temp, b,.+1 # temp = -b; sbn a, temp,.+1 # a = a (-b) = a + b;

CENG3420 Lecture 03 Review

Arithmetic for Computers

RISC-V Assembly and Binary Notation

A Processor. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter , 4.1-3

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

Anne Bracy CS 3410 Computer Science Cornell University. See P&H Chapter: , , Appendix B

Mark Redekopp, All rights reserved. EE 352 Unit 3 MIPS ISA

EE 109 Unit 13 MIPS Instruction Set. Instruction Set Architecture (ISA) Components of an ISA INSTRUCTION SET OVERVIEW

Processor. Han Wang CS3410, Spring 2012 Computer Science Cornell University. See P&H Chapter , 4.1 4

CS 61c: Great Ideas in Computer Architecture

Anne Bracy CS 3410 Computer Science Cornell University. [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

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

MIPS Assembly Language

Kernel Registers 0 1. Global Data Pointer. Stack Pointer. Frame Pointer. Return Address.

Instruction Set Architecture of. MIPS Processor. MIPS Processor. MIPS Registers (continued) MIPS Registers

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

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

Part II Instruction-Set Architecture. Jan Computer Architecture, Instruction-Set Architecture Slide 1

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

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

Computer Architecture. Chapter 3: Arithmetic for Computers

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

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

CS153: Compilers Lecture 2: Assembly

Exam in Computer Engineering

CS222: MIPS Instruction Set

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

CS 4200/5200 Computer Architecture I

Programmable Machines

CSc 256 Final Fall 2016

5DV118 Computer Organization and Architecture Umeå University Department of Computing Science Stephen J. Hegner. Topic 3: Arithmetic

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

Programmable Machines

ece4750-tinyrv-isa.txt

Transcription:

Flow of Control -- Conditional branch instructions You can compare directly Equality or inequality of two registers One register with 0 (>, <,, ) and branch to a target specified as a signed displacement expressed in number of instructions (not number of bytes) from the instruction following the branch in assembly language, it is highly recommended to use labels and branch to labeled target addresses because: the computation above is too complicated some pseudo-instructions are translated into two real instructions 1/15/99 CSE378 Instr. encoding. (ct d) 1 Examples of branch instructions Beq rs,rt,target #go to target if rs = rt Beqz rs, target #go to target if rs = 0 Bne rs,rt,target #go to target if rs!= rt Bltz rs, target #go to target if rs < 0 etc. but note that you cannot compare directly 2 registers for <, > 1/15/99 CSE378 Instr. encoding. (ct d) 2 1

Comparisons between two registers Use an instruction to set a third register slt rd,rs,rt #rd = 1 if rs < rt else rd = 0 sltu rd,rs,rt #same but rs and rt are considered unsigned Example: Branch to Lab1 if $5 < $6 slt $10,$5,$6 #$10 = 1 if $5 < $6 otherwise $10 = 0 bnez $10,Lab1 # branch if $10 =1, I.e., $5<$6 There exist pseudo instructions to help you! blt $5,$6,Lab1 # pseudo instruction translated into # slt $1,$5,$6 # bne $1,$0,Lab1 Note the use of register 1 by the assembler 1/15/99 CSE378 Instr. encoding. (ct d) 3 Unconditional transfer of control Can use beqz $0, target but limited range (±32K instr.) Use of Jump instructions jump target #special format for target byte address (26 bits) jr $rs #jump to address stored in rs (good for switch #statements and transfer tables) To call/return functions and procedures jal target #jump to target address; save PC of #following instruction in $31 (aka $ra) jr $31 # jump to address stored in $31 (or $ra) Also possible to use jalr rs,rd # jump to address stored in rs; rd = PC of # following instruction in rd 1/15/99 CSE378 Instr. encoding. (ct d) 4 2

Branch addressing format Need Opcode, one or two registers, and an offset No base register since offset added to PC When using one register, can use the second register field to expand the opcode similar to function field for arith instructions beq $4,$5,1000 Opc rs rt/func target offset bgtz $4,1000 1/15/99 CSE378 Instr. encoding. (ct d) 5 How to address operands The ISA specifies addressing modes MIPS, as a RISC machine has very few addressing modes register mode. Operand is in a register base or displacement or indexed mode Operand is at address register + 16-bit signed offset immediate mode. Operand is a constant encoded in the instruction PC-relative mode. As base but the register is the PC 1/15/99 CSE378 Instr. encoding. (ct d) 6 3

Some interesting instructions. Multiply Multiplying 2 32-bit numbers yields a 64-bit result Use of HI and LO registers Mult rs,rt #HI/LO = rs*rt Multu rs,rt Then need to move the HI or LO or both to regular registers mflo rd #rd = LO mfhi rd #rd = HI Once more the assembler can come to the rescue with a pseudo inst mul rd,rs,rt #generates mult and mflo #and mfhi if necessary 1/15/99 CSE378 Instr. encoding. (ct d) 7 Some interesting instructions. Divide Similarly, divide needs two registers LO gets the quotient HI gets the remainder If an operand is negative, the remainder is not specified by the MIPS ISA. 1/15/99 CSE378 Instr. encoding. (ct d) 8 4

Logic instructions Used to manipulate bits within words, set-up masks etc. A sample of instructions and rd,rs,rt #rd=and(rs,rt) andi rd,rs,immed or rd,rs,rt xor rd,rs,rt Immediate constant limited to 16 bits. If more use Lui. There is a pseudo-instruction NOT not rt,rs #does 1 s complement (bit by bit #complement of rs in rt) 1/15/99 CSE378 Instr. encoding. (ct d) 9 Example of use of logic instructions Create a mask of all 1 s for the low-order byte of $6. Don t care about the other bits. ori $6,$6,0x00ff #$6[7:0] set to 1 s Clear high-order byte of register 7 but leave the 3 other bytes unchanged lui $5,0x00ff #$5 = 0x00ff0000 ori $5,$5,0xffff #$5 = 0x00ffffff and $7,$7,$5 #$7 =0x00 ( whatever was #there before) 1/15/99 CSE378 Instr. encoding. (ct d) 10 5

Shift instructions Logical shifts -- Zeroes are inserted sll rd,rt,shm #left shift of shm bits; inserting 0 s on #the right srl rd,rt,shm #right shift of shm bits; inserting 0 s #on the left Arithmetic shifts (useful only on the right) sra rd,rt,shm # Sign bit is inserted on the left Example let $5 = ff00 0000 sll $6,$5,3 #$6 = 0xf800 0000 srl $6,$5,3 #$6 = 0x1fe0 0000 sra $6,$5,3 #$6 = 0xffe0 0000 1/15/99 CSE378 Instr. encoding. (ct d) 11 Example -- High-level language int a[100]; int i; for (i=0;i<100;i++){ a[i] = 5; } 1/15/99 CSE378 Instr. encoding. (ct d) 12 6

Assembly language version Assume: start address of array a in r15. We use r8 to store the value of i and r9 for the value 5 add $8,$0,$0 #initialize i li $9,5 #r9 has the constant 5 Loop: mul $10,$8,4 #r10 has i in bytes #could use a shift left by 2 addu $14,$10,$15 #address of a[i] sw $9,0($14) #store 5 in a[i] addiu $8,$8,1 #increment i blt $8,100,Loop #branch if loop not finished #taking lots of liberty here! 1/15/99 CSE378 Instr. encoding. (ct d) 13 Machine language version (generated by SPIM) [0x00400020] 0x00004020 add $8, $0, $0 ; 1: add $8,$0,$0 [0x00400024] 0x34090005 ori $9, $0, 5 ; 2: li $9,5 [0x00400028] 0x34010004 ori $1, $0, 4 ; 3: mul $10,$8,4 [0x0040002c] 0x01010018 mult $8, $1 [0x00400030] 0x00005012 mflo $10 [0x00400034] 0x014f7021 addu $14, $10, $15 ; 4: addu $14,$10,$15 [0x00400038] 0xadc90000 sw $9, 0($14) ; 5: sw $9,0($14) [0x0040003c] 0x25080001 addiu $8, $8, 1 ; 6: addiu $8,$8,1 [0x00400040] 0x29010064 slti $1, $8, 100 ; 7: blt $8,100,Loop [0x00400044] 0x1420fff9 bne $1, $0, -28 [Loop-0x00400044] 1/15/99 CSE378 Instr. encoding. (ct d) 14 7