Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 410, Spring 2008 Computer Systems

Similar presentations


Presentation on theme: "CSE 410, Spring 2008 Computer Systems"— Presentation transcript:

1 CSE 410, Spring 2008 Computer Systems
Testing and Branching CSE 410, Spring 2008 Computer Systems 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

2 Reading and References
Computer Organization and Design Section 2.5, Logical Operations Section 2.6, Instructions for Making Decisions (CD) Section A.9, SPIM (CD) Section A.10 through page A-50, MIPS R2000 We’ll see a version of this chip as a guest. 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

3 Goto Considered Harmful
“Oh what a tangled web we weave, When first we practice to deceive!” Sir Walter Scott “We’re not lost. We’re locationally challenged” Ford “That code looks like spaghetti!” anon Branching in assembly language can turn your program into a rat’s nest that cannot be debugged Keep control flow simple and logical SESE blocks Use comments describing the overall logic (if, while, for, … pseudo-code is great!) 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

4 Conditional Branch ... A change in the program’s flow of control that depends on some condition ... ... ? yes no ... 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

5 Unconditional Branch (or, jump)
... j, jal, jr ... 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

6 Determinism (1 path) V.S. Nondeterminism (n-paths)
T/F: a program’s execution can be exactly determined ahead of time if no branches are used T/F: a program’s execution can be exactly determined ahead of time if only unconditional branches are used T/F: a program’s execution can be exactly determined ahead of time if conditional branches are used This is what differentiates us from a calc watch! What if we had an if that said: if ( timeOfDay % 2 == 0 ) do path 1; else do path 2; 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

7 Blocks A chunk of code, an atomic block
SESE – single entry, single exit At most one label (entry point) in the beginning At most one label (exit point) at the end Analagous to a function call with only one return as the last instruction. 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

8 Branch instructions Branch instructions are I-format instructions
op code field two register fields 16-bit offset field Simplest branches check for equality beq $t0, $t1, address bne $t0, $t1, address 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

9 if (i==j) then a=b; Assume all values are in registers
Note that the test is inverted compared to if! Why? # $t0=i, $t1=j, $s0=a, $s1=b bne $t0, $t1, skip move $s0, $s1 skip: 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

10 while (s[i]==k) i = i+j;
# $s0=addr(s), $v1=i, $a0=k, $a1=j loop: sll $v0,$v1,2 # v0 = 4*i addu $v0,$s0,$v0 # v0 = addr(s[i]) lw $v0,0($v0) # v0 = s[i] addu $v1,$v1,$a1 # i = i+j beq $v0,$a0,loop # loop if equal subu $v1,$v1,$a1 # i = i-j 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

11 for (i=0; i<10; i++) s[i] = i;
# $s0=addr(s), $t1=i move $t1,$zero # i = 0 loop: sll $t0,$t1,2 # t0 = i*4 addu $t0,$s0,$t0 # t0 = addr(s[i]) sw $t1,0($t0) # s[i] = i addu $t1,$t1,1 # i++ slt $t0,$t1,10 # if (i<10) $t0=1 bnez $t0,loop # loop if (i<10) 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

12 How do we encode the destination?
op code base reg src/dest offset or immediate value 6 bits 5 bits 5 bits 16 bits Calculating the destination address 4*(the 16-bit offset value) is added to the Program Counter (PC, our current loc) The offset is a word offset in this case Why? The base register is always the PC, so we don’t need to specify it in the instruction Covers a range of 216 words (64 KW) Is this enough? Governing branching and looping is the principle of locality (both spatial and temporal) 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

13 Comparison instructions
For comparisons other than equality slt : set less than sltu : set less than unsigned slti : set less than constant value sltiu : set less than unsigned constant set t0 to 1 if t1<t2 slt $t0, $t1, $t2 Usually paired with a branch (below not optimized) #if( a < b ) then go somewhere, else go somewhere different Slt $t0, $t1, $t2 #a = $t1, b = $t2 Beq $t0, $zero, Else-Label #if == 0, ! ( a < b) #logic for if a < b J End-Label Else-Label: #logic for if ! (a < b) End-Label: 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

14 Pseudo-instructions The assembler is your friend Original code:
It will build instruction sequences for you It deals with address/label conversions for you Original code: bge $a0,$t1,end # if a0>=t1 skip Actual instructions: slt $at,$a0,$t1 # if a0<t1 at=true beq $at,$0,end # skip if at==false 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

15 Jump Instructions Jump instructions provide longer range than branch instructions 26-bit word offset in J-Type instructions j : jump jal : jump and link (store return address) 32-bit address in register jumps jr : jump through register jalr : jump through register and link 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

16 J-Type fields op code word offset 6 bits 26 bits The word offset value is multiplied by 4 to create a byte offset (quick sll by ?) the result is 28 bits wide Then concatenated with top 4 bits of PC to make a 32 bit destination address Why? Offset implies this, but is the 26 bits absolute or relative? What if we need to form an absolute 32-bit address to span the whole memory space (in this example, 4GB) Would this J-Type instruction suffice? 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington

17 Important Jumps Jump and link (jal) Jump through register (jr)
call procedure and store return address in $ra Jump through register (jr) return to caller using the address in $ra We will talk about procedure calls in excruciating detail shortly Crucio! (Latin: crux, “I torture”) 5/20/2019 cse decisions © Perkins, DWJohnson & University of Washington


Download ppt "CSE 410, Spring 2008 Computer Systems"

Similar presentations


Ads by Google