Branches Two branch instructions:

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
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.
CS/COE0447 Computer Organization & Assembly Language
The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
Deeper Assembly: Addressing, Conditions, Branching, and Loops
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.
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.
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
MIPS Instruction Set Advantages
1 Today  Finish-up procedures/stack  strlen example  Machine language, the binary representation for instructions. —We’ll see how it is designed for.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
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)
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization Instructions Language of The Computer (MIPS) 2.
EE 3755 Datapath Presented by Dr. Alexander Skavantzos.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
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.
June 14, 2016Machine Language and Pointers1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }
What does this code do? label: sub $a0, $a0, 1 bne $a0, $zero, label
CSCI206 - Computer Organization & Programming
Lecture 6: Assembly Programs
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Instructions for Making Decisions
Pick up the handout on your way in!!
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MIPS Instructions.
MIPS Instruction Encoding
MIPS coding.
The University of Adelaide, School of Computer Science
MIPS Instruction Encoding
Instruction encoding The ISA defines Format = Encoding
MIPS coding.
The University of Adelaide, School of Computer Science
Flow of Control -- Conditional branch instructions
MIPS Assembly.
MIPS Coding Continued.
9/27: Lecture Topics Memory Data transfer instructions
7/6/
Conditional Branching (beq)
Presentation transcript:

Branches Two branch instructions: beq $t0, $t1, label # if t0 == t1, jump to “label” bne $t0, $t1, label # if t0 != t1, jump to “label” For branch instructions, the constant field is not an address, but an offset from the current program counter (PC) to the target address. beq $t0, $t1, EQ add $t0, $t0, $t1 addi $t1, $t0, $0 EQ: add $v1, $v0, $v0 Since the branch target EQ is three instructions past the beq, the address field contains 3 000100 10001 10010 0000 0000 0000 0011 op rs rt address (offset)

J-type format In most programs, branch targets are less than 32,767 instructions away branches are mostly used in loops and conditionals programmers are taught to make loop bodies short For “far” jumps, use j and jal instructions (J-type instruction format) address is always a multiple of 4 (32 bits per instruction) only the top 26 bits actually stored (last two are always 0) For even longer jumps, the jump register (jr) instruction can be used. jr $ra # Jump to 32-bit address in register $ra opcode address (exact) 6 bits 26 bits

Pseudo-branches 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

Implementing 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 Immediate versions: slti used instead of slt slti $at, $a0, 5 // $at = 1if $a0 < 5 bne $at, $0, Label // Branch if $a0 < 5 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 if you use $at, the assembler will warn you

Translating an if-statement The easiest way is often to invert the original condition: if(t0 < t1){ bge $t0, $t1, other ... ... } // other stuff other: # other stuff How about an if-else statement? if(t0 < t1) // thing 1 else // thing 2 // other stuff bge $t0, $t1, else # thing 1 j other else: # thing 2 other: # other stuff

Translating a while-loop A while-loop is essentially an iterated if-statement: while(t0 < t1){ loop: bge $t0, $t1, other ... ... } j loop // other stuff other: # other stuff Translate into MIPS: if(a0 % 4 == 0) // MIPS instruction: rem if(a0 % 100 != 0) v0 = 1; // leap year else if(a0 % 400 == 0) v0 = 1; else v0 = 0; li $v0, 0 andi $t0, $a0, 3 bne $t0, $0, done rem $t0, $a0, 100 beq $t0, $0, else li $v0, 1 j done else: rem $t0, $a0, 400 done:

Memory: Variables and Arrays For now, we have been translating C++ variables as MIPS registers C++ programs can have many variables, complex data structures Let’s consider a simple C++ code snippet: char c = 0; // 1 byte of memory, initialized to 0 c++; More specifically, a value is being loaded from address &c into a register, modified, and then stored back into address &c la $t0, c # load address &c into t0 lb $t1, 0($t0) # (load byte) t1 = Mem[t0 + 0] addi $t1, $t1, 1 # t1++ sb $t1, 0($t0) # (store byte) Mem[t0 + 0] = t1 Load c from memory into a register Add 1 to that register Store the result back into c Pseudo-instructions like add, sub, div, etc. “overload” the standard built-in instructions.

Declaring variables Variables (labeled chunks of memory) are declared in the .data section .data: c: .byte 0 # char c = 0; a: .byte 0 1 2 # char a[3] = {0, 1, 2}; b: .space 50 # char b[50]; .text: main: ... Both lb and sb are I-type instructions: can specify a 16-bit immediate la $t0, a # t0 = &a[0] lb $t1, 0($t0) # t1 = a[0] lb $t2, 1($t0) # t2 = a[1] sb $t2, 2($t0) # a[2] = t2 This technique cannot be used to loop through an array: $t1($t0)

Looping through an array Consider this code: for(i = 0; i < 10; i++) a[i] = 0; What’s going on here? Answer: Pointer arithmetic! for(char *p = &a[0]; p < (a + 10); p++) *p = 0; li $t0, 0 # t0 = i la $t1, a # t1 = &a[i] loop: bge $t0, 10, done sb $0, 0($t1) # a[i] = 0 addi $t0, $t0, 1 addi $t1, $t1, 1 j loop done: la $t0, a # t0 = &a[i] addi $t1, $t0, 10 loop: bge $t0, $t1, done sb $0, 0($t0) # a[i] = 0 addi $t0, $t0, 1 j loop done:

Accessing random array elements /* a = array of char */ lo = 1, hi = 31; while(lo <= hi) { mid = (lo+hi)/2 if(a[mid]==17) break; if(a[mid]<17) lo = mid+1; else hi = mid-1; } A complication: array of (32-bit) ints &a[i] is actually &a[0] + 4*i Add 4 to pointer when looping through an array of ints Use lw/sw instead of lb/sb la $t0, a li $t1, 1 li $t2, 31 loop: bgt $t1, $t2, done add $t3, $t1, $t2 sra $t3, $t3, 1 add $t4, $t3, $t0 lb $t4, 0($t4) beq $t4, 17, done bge $t4, 17, else addi $t1, $t3, 1 j loop else: addi $t2, $t3, -1 done:

Translate into MIPS /* a = array of int */ for(i = 0; i < 10; i++) { if(a[i]==a[i+1]) break; } Try this on your own: for(i = 9; i >= 0; i--) { a[i+1]=a[i]; a[0] = 0; la $t0, a addi $t1, $t0, 40 loop: beq $t0, $t1, done lw $t2, 0($t0) lw $t3, 4($t0) beq $t2, $t3, done addi $t0, $t0, 4 j loop done: la $t0, a addi $t1, $t0, 40 loop: beq $t1, $t0, done lw $t2, -4($t1) sw $t2, 0($t1) addi $t1, $t1, -4 j loop done: sw $0, 0($t1)