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

Similar documents
Chapter 2. Instructions:

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

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

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Instructions: Language of the Computer

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

CS3350B Computer Architecture MIPS Instruction Representation

MIPS Memory Access Instructions

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Reduced Instruction Set Computer (RISC)

Chapter 3. Instructions:

Topic Notes: MIPS Instruction Set Architecture

CS61C : Machine Structures

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

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

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

Reduced Instruction Set Computer (RISC)

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

Computer Architecture

CS31001 COMPUTER ORGANIZATION AND ARCHITECTURE. Debdeep Mukhopadhyay, CSE, IIT Kharagpur. Instructions and Addressing

Unsigned Binary Integers

Unsigned Binary Integers

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

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

CS61C : Machine Structures

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

Chapter 2. Instruction Set Architecture (ISA)

EEC 581 Computer Architecture Lecture 1 Review MIPS

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

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

Chapter 2A Instructions: Language of the Computer

CS61C Machine Structures. Lecture 10 - MIPS Branch Instructions II. 2/8/2006 John Wawrzynek. (

Quick Review. lw $t0, 4($a0) Registers x Memory. $a0 is simply another name for register 4 $t0 is another name for register (green sheet)

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Computer Architecture

Levels of Programming. Registers

CS222: MIPS Instruction Set

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

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

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

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

Review of the Machine Cycle

CS/COE0447: Computer Organization

UCB CS61C : Machine Structures

MIPS Coding Continued

Chapter 2: Instructions:

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

ECE232: Hardware Organization and Design

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

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

COMPUTER ORGANIZATION AND DESIGN

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

Control Instructions

COMP MIPS instructions 2 Feb. 8, f = g + h i;

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

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

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

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

Course Administration

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Systems Architecture I

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

CENG3420 Lecture 03 Review

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

CS61C Constants and Making Decisions in C/Assembly Language. Lecture 3. January 27, 1999 Dave Patterson (http.cs.berkeley.

Lab 3 (All Sections) Prelab: MIPS-Control Instructions

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

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

Chapter 2. Instructions: Language of the Computer

CMPE324 Computer Architecture Lecture 2

Computer Organization MIPS ISA

UCB CS61C : Machine Structures

Lecture 6 Decision + Shift + I/O

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

Lab 3 (Sections 300, 301 and 302) Prelab: MIPS-Control Instructions

CS 4200/5200 Computer Architecture I

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CS 61c: Great Ideas in Computer Architecture

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

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

Thomas Polzer Institut für Technische Informatik

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

MIPS function continued

Instructions: Assembly Language

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

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

CSE Lecture In Class Example Handout

Processor design - MIPS

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Programmable Machines

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

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

Programmable Machines

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

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

Transcription:

Control Instruction that potentially changes the flow of program execution MIPS conditional branch instructions bne $s4, $s3, LABEL goto LABEL if $s4!= $s3 beq $s4, $s3, LABEL goto LABEL if $s4 == $s3 Example if (i == h) h =i+j; h=i+j; YES (i == h)? NO bne $s5, $s6, LABEL add $s6, $s5, $s7 LABEL: LABEL: 40 Control MIPS unconditional branch instruction (i.e., jump) j LABEL Example i, f, g, and h are in registers $s4, $s5, $s6, $s7 if (i == h) f=g+h; else f=g h; f=g+h; YES (i == h)? NO f=g h bne $s4, $s7, ELSE add $s5, $s6, $s7 j EXIT ELSE: sub $s5, $s6, $s7 EXIT: EXIT 41 1

Control We have beq and bne; what about branch-if-less-than? We have slt if (s1<s2) t0=1; else t0=0; slt $t0, $s1, $s2 Can you make a psuedo instruction blt $s1, $s2, LABEL? Assembler needs a temporary register to do this $at is reserved for this purpose slt $at,$s1,$s2 # $at==1 when $s1<$s2 bne $at,$0,label # $at!=0 implies $at==1 42 In-class exercise with control Let s write an assembly language program: Print Big endian if machine is big endian, otherwise print Little endian We can do this by declaring some 4 bytes (word) in memory Then, load the word Check which which byte was put in the lowest position See inclass3.asm 43 2

Checking for endianness.data endian:.byte 0x1,0x2,0x3,0x4 m_lit:.asciiz Little endian!\n m_big:.asciiz Big endian!\n.text la $a0,m_lit # assume little end la $s0,endian lw $s1,0($s0) andi $s1,$s1,0x1 # select low bit bne $s1,$0,lit_end # LSB=1: little endian la $a0,m_big lit_end: li $v0,4 44 Addresses in Branches Branches use PC-relative addressing Target address: Location (address) that the branch goes to. beq $t0,$0,label Target address computed as: next PC = (current PC + 4) + offset 4 thus, LABEL is how many instructions to go forward (positive offset) or backward (negative offset) from current location. Branch uses I-format Two register source operands for comparison 16-bit immediate (offset) to specify target address 45 3

Addresses in Branches addi $t0,$t1,-100 LABEL: addi $t0,$t0,-1 beq $t0,$0,label add $a0,$t0,$0 addi $s0,$t0,1000 li $v0,1 addi $s1,$t1,1100 sub $t0,$s1,$s0 bne $t0,$0,label LABEL: li $v0,1 li $v0,10 add $a0,$t0,$0 LABEL= +3 instructions LABEL= -5 instructions I-format encoding: 0x110000003 I-format encoding: 0x1500fffb opcode: 000100 opcode: 000101 regs: 01000 ($t0), 00000 ($0) regs: 01000 ($t0), 00000 ($0) 46 Addresses in Branches addi $t0,$t1,-100 LABEL: addi $t0,$t0,-1 beq $t0,$0,label add $a0,$t0,$0 addi $s0,$t0,1000 li $v0,1 addi $s1,$t1,1100 sub $t0,$s1,$s0 bne $t0,$0,label LABEL: li $v0,1 li $v0,10 add $a0,$t0,$0 LABEL= +3 instructions LABEL= -5 instructions I-format encoding: 0x110000003 I-format encoding: 0x1500fffb opcode: 000100 opcode: 000101 regs: 01000 ($t0), 00000 ($0) regs: 01000 ($t0), 00000 ($0) 47 4

Addresses in Branches addi $t0,$t1,-100 LABEL: addi $t0,$t0,-1 beq $t0,$0,label add $a0,$t0,$0 addi $s0,$t0,1000 li $v0,1 addi $s1,$t1,1100 sub $t0,$s1,$s0 bne $t0,$0,label LABEL: li $v0,1 li $v0,10 add $a0,$t0,$0 LABEL= +3 instructions LABEL= -5 instructions I-format encoding: 0x110000003 I-format encoding: 0x1500fffb opcode: 000100 opcode: 000101 regs: 01000 ($t0), 00000 ($0) regs: 01000 ($t0), 00000 ($0) 48 Addresses in Branches addi $t0,$t1,-100 LABEL: addi $t0,$t0,-1 beq $t0,$0,label add $a0,$t0,$0 addi $s0,$t0,1000 li $v0,1 addi $s1,$t1,1100 sub $t0,$s1,$s0 bne $t0,$0,label LABEL: li $v0,1 li $v0,10 add $a0,$t0,$0 LABEL= +3 instructions LABEL= -5 instructions I-format encoding: 0x110000003 I-format encoding: 0x1500fffb opcode: 000100 opcode: 000101 regs: 01000 ($t0), 00000 ($0) regs: 01000 ($t0), 00000 ($0) 49 5

Addresses in Branches PC=PC+4+offset*4 Why the multiply by 4? How can the hardware do this easily? Offset can be negative Offset Represented as two s complement negative Complete calculation is: PC=PC+4+(sign-extend(offset)<<2) 50 J-format Jump op 26-bit immediate The address of next instruction is obtained from PC and the immediate value Next address = {PC[31:28],IMM[25:0],00} Address boundaries of 256MB Example j 10000 2 2500 55 6

Control What about comparisons against constant? Can we use beqi??? Why not? Comparison variants with an immediate slti $s0,$s1,16 addi $s0,$s1,-20 ($s0==0 when $s1==20) if (a > 20) { a = a + 2; } slti $at,$s0,21 # $at==1 when a<=20 bne $at,$0,lab # $at!=0 implies $at==1, skip addi $s0,$s0,2 LAB: # target when a<=20 56 Control Loops While loops " Example on page 74 For loops 57 7

While Loop while (condition == true) { some work; } use comparison, branch to exit, and jump to continue generally written as: loop: check condition branch if condition is false to exit some work j loop exit: 58 While Loop char *str = Hello World! ; char *s = str; while (*s!= \0 ) { printf( %c, *s); s = s + 1; } la $t0,str loop: lb $a0,0($t0) beq $a0,$0,exit li $v0,11 # print character addi $t0,$t0,1 j loop exit: See mips12.asm 59 8

Do While Loop do { some work; } while (condition == true); use comparison, branch to exit, and jump to continue generally written as: loop: some work check condition branch if condition is true to loop exit: 60 Do While Loop char *str = Hello World! ; char *s = str; /* note: this breaks with the empty string! */ do { printf( %c,*s); s = s + 1; } while (*s!= \0 ) la $t0,str lb $a0,0($t0) # start the loop loop: li $v0,11 # print character addi $t0,$t0,1 lb $a0,0($t0) bne $a0,$0,loop See mips11.asm 61 9

In-class Example Write code to reverse a null-terminated string 62 Examples of control flow Weather programs weather.c weather.asm - illustrates if-then-else and while loop weather2.asm - a slightly improved version (still if-then-else) weather3.asm - illustrates a computed goto (switch) weather4.asm - illustrates an algorithm change, using a table sam12.asm convert a 32-bit number into hex characters, which are displayed with the OS print string service We ll see many more examples! 63 10