Download Lecture Notes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS2504, Spring'2007
©Dimitris Nikolopoulos
MIPS Instruction Fields
op


rs
rt
rd
shamt
funct
OK, what if the instructions does not use all
these fields?
What if I have a load/store instruction?
17
CS2504, Spring'2007
©Dimitris Nikolopoulos
MIPS Instruction Fields

A compromise:



op
offset needs to be larger than 32 bytes
Instructions need to be of same length
MIPS uses 16-bit offsets
rs
rt
rd
offset
shamt
funct
18
CS2504, Spring'2007
©Dimitris Nikolopoulos
Some discussion



What if the architect would like to use more
than 32 registers?
How does the hardware know what is the
correct encoding of the instruction?
What is the maximum offset?



Representation of positive/negative sign needs one
bit
Can we encode any offset in a program?
Is there an implication for program performance
here?
19
CS2504, Spring'2007
©Dimitris Nikolopoulos
Logical Operations
Logical operations C operators Java operatorsMIPS instructions
sll
Shift left
<<
<<
slr
Shift right
>>
>>
and, andi
Bit-by-bit and
&
&
or, ori
Bit-by-bit or
|
|
nor
Bit-by-bit not
~
~
20
CS2504, Spring'2007
©Dimitris Nikolopoulos
Shifting
sll $t2, $s0, 4

$s0=0000 0000 0000 0000 0000 0000 0000 1001

$t2=0000 0000 0000 0000 0000 0000 1001 0000

shamt field in instruction format explained

Coincides with multiplication with 24

Fast way to do multiplications
srl $t2, $s0, 4

$s0=0000 0000 0000 0000 0000 0000 0000 1001

$t2=0000 0000 0000 0000 0000 0000 0000 0000
21
CS2504, Spring'2007
©Dimitris Nikolopoulos
Bit masks

And operation:
If both bits in the same location on the two
registers are 1 then 1
 Otherwise 0
 Can be used to “conceal” bits in a register
and $t0,$t1,$t2
$t1= 0000 0000 0000 0000 0000 1101 0000 0000
$t2= 0000 0000 0000 0000 0011 1100 0000 0000
$t0= 0000 0000 0000 0000 0000 1100 0000 0000
Quiz: sketch an algorithm to find how many ones
there are in a register

22
CS2504, Spring'2007
©Dimitris Nikolopoulos
Bit masks

Or operation:
If either bit in the same location on either of the
two registers is 1 then 1
 Otherwise 0
or $t0,$t1,$t2
$t1= 0000 0000 0000 0000 0000 1101 0000 0000
$t2= 0000 0000 0000 0000 0011 1100 0000 0000
$t0= 0000 0000 0000 0000 0011 1101 0000 0000
Quiz: sketch an algorithm to find how many zeros
there are in a register

23
CS2504, Spring'2007
©Dimitris Nikolopoulos
Bit masks

Not operation:
Invert bit by bit
 One register operand, trouble for MIPS ISA...
 Architects decided to go with a NOR instead
nor $t0, $t1, $t2
$t1= 0000 0000 0000 0000 0000 1101 0000 0000
$t2= 0000 0000 0000 0000 0011 1100 0000 0000
scratch= 0000 0000 0000 0000 0011 1101 0000 0000
$t0= 1111 1111 1111 1111 1100 0010 1111 1111
Quiz:how do you implement a NOT with a NOR?

24
CS2504, Spring'2007
©Dimitris Nikolopoulos
Branching Example
25
CS2504, Spring'2007
©Dimitris Nikolopoulos
Branching in MIPS
beq register1, register2, L1

Goto label L1 if registers equal
bne register1, register2, L1

Goto label L1, if registers unequal
26
CS2504, Spring'2007
©Dimitris Nikolopoulos
Branching Example
27
CS2504, Spring'2007
©Dimitris Nikolopoulos
Branching Example

If (i==j)
bne $s3, $s4, Else

Then part: f = g + h
add $s0, $s1, $s2
j Exit #go to Exit. Unconditional branch.

Else part: f = g ­ h
Else: sub $s0, $s1, $s2
Exit: #target of the then part
28
CS2504, Spring'2007
©Dimitris Nikolopoulos
Branching example

Discussion:




Branches can be conditional or unconditional
Programmer has a simplified way to write branches
by using labels
Labels simplify assembly language but they are not
typically necessary in high-level languages
Similar labelling simplifies data declarations and
load/store instructions
29
CS2504, Spring'2007
©Dimitris Nikolopoulos
Loops
While (save[i] == k) i ++;
// i and k are in $s3, $s5, base of save is in
// $s6
Loop: sll $t1, $s3, 2 #multiply i by 4
add $t1,$t1,$s6 # point to save[i]
lw $t0,0($t1) # get save[i]
bne $t0, $s5, Exit # leave
addi $s3, $s3, 1 #i++
j Loop
30
CS2504, Spring'2007
©Dimitris Nikolopoulos
Memory access




Need to find save[i]
Need to move i integers ahead of save[0]
Each integer is 4 bytes
Need to move ahead 4*i bytes



Use shift for the multiplication
Add 4*i bytes to base
Then load
31
CS2504, Spring'2007
©Dimitris Nikolopoulos
Understanding loops





Evaluate condition
Execute body (condition == true)
Branch back or exit
Can we implement for loops this way?
Loop defines a basic block


Single entry
Single exit
32
CS2504, Spring'2007
©Dimitris Nikolopoulos
Other branching tests
slt $t0, $s3, $s4

Set $t0 to 1 if $s3 is less than $s4
slti $t0, $s3, 10

Set $t0 to 1 if $s3 is less than 10
Simplicity:


Branch on less than equal too complicated to
implement in hardware, so use two instructions
Slt, slti, beq, bne and a register set always to zero
($zero, or $0) are enough to implement all useful
branches!
33