Conditional Branching (beq)

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

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
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Solution 2nd Exam.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
Assembly Code Example Selection Sort.
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.
The University of Adelaide, School of Computer Science
CPSC CPSC 3615 Computer Architecture Chapter 3 Instructions and Addressing (Text Book: Chapter 5)
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
MIPS Instruction Set Advantages
Lecture 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
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 Instructions Language of The Computer (MIPS) 2.
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
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.
MIPS Programming.
MIPS Assembly.
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
MIPS instructions.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
The University of Adelaide, School of Computer Science
MIPS coding.
Pick up the handout on your way in!!
Instructions - Type and Format
ARM Control Structures
Logical and Decision Operations
MIPS Instruction Encoding
ECE232: Hardware Organization and Design
MIPS coding.
MIPS Instruction Encoding
Instruction encoding The ISA defines Format = Encoding
MIPS coding.
MIPS Coding.
The University of Adelaide, School of Computer Science
Part II Instruction-Set Architecture
Flow of Control -- Conditional branch instructions
3.
MIPS Coding.
COMS 361 Computer Organization
COMS 361 Computer Organization
ARM Control Structures
MIPS Assembly.
Flow of Control -- Conditional branch instructions
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
Chapter 6 Computer Architecture (part II)
9/27: Lecture Topics Memory Data transfer instructions
7/6/
9/13/
MIPS instructions.
Presentation transcript:

Conditional Branching (beq) # MIPS assembly addi $s0, $0, 4 # $s0 = 0 + 4 = 4 addi $s1, $0, 1 # $s1 = 0 + 1 = 1 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4 beq $s0, $s1, target # branch is taken addi $s1, $s1, 1 # not executed sub $s1, $s1, $s0 # not executed target: # label add $s1, $s1, $s0 # $s1 = 4 + 4 = 8 Labels indicate instruction locations in a program. They cannot use reserved words and must be followed by a colon (:).

The Branch Not Taken (bne) # MIPS assembly addi $s0, $0, 4 # $s0 = 0 + 4 = 4 addi $s1, $0, 1 # $s1 = 0 + 1 = 1 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4 bne $s0, $s1, target # branch not taken addi $s1, $s1, 1 # $s1 = 4 + 1 = 5 sub $s1, $s1, $s0 # $s1 = 5 – 4 = 1 target: add $s1, $s1, $s0 # $s1 = 1 + 4 = 5

Unconditional Branching / Jumping (j) # MIPS assembly addi $s0, $0, 4 # $s0 = 4 addi $s1, $0, 1 # $s1 = 1 j target # jump to target sra $s1, $s1, 2 # not executed addi $s1, $s1, 1 # not executed sub $s1, $s1, $s0 # not executed target: add $s1, $s1, $s0 # $s1 = 1 + 4 = 5

Unconditional Branching (jr) # MIPS assembly 0x00002000 addi $s0, $0, 0x2010 0x00002004 jr $s0 #jump to address that is stored in $s0 0x00002008 addi $s1, $0, 1 0x0000200C sra $s1, $s1, 2 0x00002010 lw $s3, 44($s1)

High-Level Code Constructs if statements if/else statements while loops for loops

If Statement High-level code MIPS assembly code if (i == j) f = g + h; f = f – i; MIPS assembly code # $s0 = f, $s1 = g, $s2 = h # $s3 = i, $s4 = j

If Statement High-level code MIPS assembly code if (i == j) f = g + h; f = f – i; MIPS assembly code # $s0 = f, $s1 = g, $s2 = h # $s3 = i, $s4 = j bne $s3, $s4, L1 add $s0, $s1, $s2 L1: sub $s0, $s0, $s3 Notice that the assembly tests for the opposite case (i != j) than the test in the high-level code (i == j).

If / Else Statement High-level code MIPS assembly code if (i == j) f = g + h; else f = f – i; MIPS assembly code # $s0 = f, $s1 = g, $s2 = h # $s3 = i, $s4 = j

If / Else Statement High-level code MIPS assembly code if (i == j) f = g + h; else f = f – i; MIPS assembly code # $s0 = f, $s1 = g, $s2 = h # $s3 = i, $s4 = j bne $s3, $s4, L1 add $s0, $s1, $s2 j done L1: sub $s0, $s0, $s3 done:

While Loops High-level code MIPS assembly code // determines the power // of x such that 2x = 128 int pow = 1; int x = 0; while (pow != 128) { pow = pow * 2; x = x + 1; } MIPS assembly code # $s0 = pow, $s1 = x addi $s0, $0, 1 add $s1, $0, $0 addi $t0, $0, 128 while: beq $s0, $t0, done sll $s0, $s0, 1 addi $s1, $s1, 1 j while done: Notice that the assembly tests for the opposite case (pow == 128) than the test in the high-level code (pow != 128).

For Loops The general form of a for loop is: for (initialization; condition; loop operation) loop body initialization: executes before the loop begins condition: is tested at the beginning of each iteration loop operation: executes at the end of each iteration loop body: executes each time the condition is met

For Loops High-level code MIPS assembly code // add the numbers from 0 to 9 int sum = 0; int i; for (i=0; i!=10; i = i+1) { sum = sum + i; } MIPS assembly code # $s0 = i, $s1 = sum addi $s1, $0, 0 add $s0, $0, $0 addi $t0, $0, 10 for: beq $s0, $t0, done add $s1, $s1, $s0 addi $s0, $s0, 1 j for done: Notice that the assembly tests for the opposite case (i == 128) than the test in the high-level code (i != 10).

Less Than Comparisons High-level code MIPS assembly code // add the powers of 2 from 1 // to 100 int sum = 0; int i; for (i=1; i < 101; i = i*2) { sum = sum + i; } MIPS assembly code # $s0 = i, $s1 = sum addi $s1, $0, 0 addi $s0, $0, 1 addi $t0, $0, 101 loop: slt $t1, $s0, $t0 beq $t1, $0, done add $s1, $s1, $s0 sll $s0, $s0, 1 j loop done: #$t1 = 1 if i < 101.

Arrays Useful for accessing large amounts of similar data Array element: accessed by index Array size: number of elements in the array

Arrays 5-element array Base address = 0x12348000 (address of the first array element, array[0]) First step in accessing an array: load base address into a register

Arrays // high-level code int array[5]; array[0] = array[0] * 2; array[1] = array[1] * 2; # MIPS assembly code # array base address = $s0 lui $s0, 0x1234 # put 0x1234 in upper half of $S0 ori $s0, $s0, 0x8000 # put 0x8000 in lower half of $s0 lw $t1, 0($s0) # $t1 = array[0] sll $t1, $t1, 1 # $t1 = $t1 * 2 sw $t1, 0($s0) # array[0] = $t1 lw $t1, 4($s0) # $t1 = array[1] sw $t1, 4($s0) # array[1] = $t1

Arrays Using For Loops // high-level code int array[1000]; int i; for (i=0; i < 1000; i = i + 1) array[i] = array[i] * 8; # MIPS assembly code # $s0 = array base address, $s1 = i

Arrays Using For Loops # MIPS assembly code # $s0 = array base address, $s1 = i # initialization code lui $s0, 0x23B8 # $s0 = 0x23B80000 ori $s0, $s0, 0xF000 # $s0 = 0x23B8F000 addi $s1, $0, 0 # i = 0 addi $t2, $0, 1000 # $t2 = 1000 loop: slt $t0, $s1, $t2 # i < 1000? beq $t0, $0, done # if not then done sll $t0, $s1, 2 # $t0 = i * 4 (byte offset) add $t0, $t0, $s0 # address of array[i] lw $t1, 0($t0) # $t1 = array[i] sll $t1, $t1, 3 # $t1 = array[i] * 8 sw $t1, 0($t0) # array[i] = array[i] * 8 addi $s1, $s1, 1 # i = i + 1 j loop # repeat done:

ASCII Codes American Standard Code for Information Interchange assigns each text character a unique byte value For example, S = 0x53, a = 0x61, A = 0x41 Lower-case and upper-case letters differ by 0x20 (32).

Cast of Characters