Professional Documents
Culture Documents
Chapter 2
This material is for educational uses only. Slides adapted from D. Patterson and M. Irwin, and some contents are based on the material provided by other
paper/book authors and may be copyrighted by them.
Head’s Up
Last week’s material
Introduction to MIPS assembler, adds/loads/stores
This week’s material
MIPS control flow and logic operations
- Reading assignment - PH 2.5-2.6
Next week’s material
Supporting procedure calls and returns; addressing
modes
- Reading assignment - PH: 2.7-2.9, A.6, D.2
Review: Signed Binary 2’sc binary decimal
Representation -23 = 1000 -8
-(23 - 1) = 1001 -7
1010 -6
1011 -5
complement all the bits 1100 -4
1101 -3
0101
1110 -2
and add a 1 1011
1111 -1
0110 and add a 1 0000 0
0001 1
1010
0010 2
complement all the bits 0011 3
0100 4
23 - 1 = 0101 5
0110 6
0111 7
Review: MIPS Number Representations
32-bit signed numbers (2’s complement):
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten maxint
0000 0000 0000 0000 0000 0000 0000 0001two = + 1ten
...
0111 1111 1111 1111 1111 1111 1111 1110two = + 2,147,483,646ten
0111 1111 1111 1111 1111 1111 1111 1111two = + 2,147,483,647ten
1000 0000 0000 0000 0000 0000 0000 0000two = – 2,147,483,648ten
1000 0000 0000 0000 0000 0000 0000 0001two = – 2,147,483,647ten
...
MSB 1111 1111 1111 1111 1111 1111 1111 1110two = – 2ten minint
1111 1111 1111 1111 1111 1111 1111 1111two = – 1ten
LSB
Converting <32 bit values into 32 bit values
copy the most significant bit (the sign bit) into the
“empty” bits
0010 -> 0000 0010
1010 -> 1111 1010
sign extend versus zero extend
Review: MIPS Organization
Arithmetic instructions – to/from the register file
hex
Instructions for Making Decisions
Decision making instructions
alter the control flow
i.e., change the "next" instruction to be executed
Example:if (i==j) h = i + j;
Machine Formats:
op rs rt 16 bit number I format
bne 5 16 17 ????
beq 4 16 17 ????
00
branch dst
32 32 Add
address
PC 32 Add 32
32
32 4 32 ?
Offset Tradeoffs
Why not just store the word offset in the low
order 16 bits?
Then the two low order zeros wouldn’t have to be
concatenated, it would be less confusing, …
5 16 17
Remember
z After the bne instruction is fetched, the PC is updated so
that it is addressing the add instruction (PC = PC + 4).
z The offset (plus 2 low-order zeros) is sign-extended and
added to the (updated) PC
Another Instruction for Changing Flow
MIPS also has an unconditional branch
instruction or jump instruction:
j Lbl #go to Lbl
Example: if (i!=j)
h=i+j;
else
h=i-j;
Machine Format:
op 26-bit address J format
2 ????
Exec Decode
00
32
4
PC 32
Example: Jump Practice
For simplicity, all instructions other than the jump instruction are
no-ops. The jump instruction jumps to the first instruction of the
program.
becomes
bne $s0, $s1, L2
j L1
L2:
Compiling While Loops
0? 16
?
17
? 8
?
0? 42 =?0x2a
Yet More Instructions for Making Decisions
Since constant operands are popular in
comparisons, also have slti
New instruction:
slti $t0, $s0, 10 # if $s0 < 10
# then
# $t0 = 1
# else
# $t0 = 0
Machine format:
op rs rt 16 bit number I format
10(0xa)
? 16
? 8
? 10(0x000a)
?
Other Branch Instructions
Can use slt, beq, bne, and the fixed value of
0 in $zero to create all relative conditions
less than blt $s1, $s2, Lbl
Machine format:
op rs funct R format
0? 9? 0? 0
?
0? 8 = 0x08
?
Compiling a Case (Switch) Statement
switch (k) { Memory
L0 case 0: h=i+j; break; /*k=0*/
L1 case 1: h=i+h; break; /*k=1*/
L2 case 2: h=i-j; break; /*k=2*/
L2
Assuming three sequential words in L1
$t4o
memory starting at the address in $t4 L0
have the addresses of the labels L0,
L1, and L2. k is in $s2
add $t1, $s2, $s2 #$t1 = 2*k
add $t1, $t1, $t1 #$t1 = 4*k
add $t1, $t1, $t4 #$t1 = addr of JumpT[k]
lw $t0, 0($t1) #$t0 = JumpT[k]
jr $t0 #jump based on $t0
L0: add $s3, $s0, $s1 #k=0 so h=i+j
j Exit
L1: add $s3, $s0, $s3 #k=1 so h=i+h
j Exit
L2: sub $s3, $s0, $s1 #k=2 so h=i-j
Exit: . . .
Review: MIPS Instructions, so far
Category Instr OpCd Example Meaning
Arithmetic add 0 & 20 add $s1, $s2, $s3 $s1 = $s2 + $s3
(R & I subtract 0 & 22 sub $s1, $s2, $s3 $s1 = $s2 - $s3
format) add immediate 8 addi $s1, $s2, 4 $s1 = $s2 + 4
Data load word 23 lw $s1, 100($s2) $s1 = Memory($s2+100)
transfer store word 2b sw $s1, 100($s2) Memory($s2+100) = $s1
(I format)
Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L
branch br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L
(I format) set on less a slti $s1, $s2, 100 if ($s2<100) $s1=1;
than immediate else $s1=0
(R format) set on less 0 & 2a slt $s1, $s2, $s3 if ($s2<$s3) $s1=1;
than else $s1=0
Uncond. jump (J format) 2 j 2500 go to 10000
jump jump register 0 & 08 jr $t1 go to $t1
(R format)
MIPS Organization
Processor
Memory
Register File
1…1100
src1 addr src1
5 32 data
src2 addr 32
5 registers
dst addr ($zero - $ra) read/write
5 src2
write data addr
32 32 data 230
32 words
32 bits
br offset
32 Add
read data
32 PC 32 Add 32 32
32
4 write data 0…1100
Fetch 32
PC = PC+4 32 0…1000
4 5 6 7 0…0100
32 ALU 0 1 2 3
Exec Decode 0…0000
32
32 bits word address
32
(binary)
byte address
(big Endian)
MIPS Data Types
Bit: 0, 1
Bit String: sequence of bits of a particular length
4 bits is a nibble
8 bits is a byte
16 bits is a half-word
32 bits is a word
64 bits is a double-word
Character:
ASCII 7 bit code
Decimal:
digits 0-9 encoded as 00002 thru 10012
two decimal digits packed per 8 bit (byte)
msb lsb
big endian
0 1 2 3
Aligned
Alignment restriction: requires
that objects fall on address that
is multiple of their size Not
Aligned
Example: Little endian/Big endian
Highest mem add 00000000 00000000 00000001 11000010 Lowest mem add
Highest mem add 11000010 00000001 00000000 00000000 Lowest mem add
Loading and Storing Bytes
op rs rt 16 bit number
0 16 10 8 0x00
0 16 10 8 0x03
Example
Compiling Another While Loop
Logical Operations
There are a number of bit-wise logical operations
in the MIPS ISA
and $t0, $t1, $t2 #$t0 = $t1 & $t2
or $t0, $t1, $t2 #$t0 = $t1 | $t2
nor $t0, $t1, $t2 #$t0 = not($t1 | $t2)
0 9 10 8 0x24
1010101010101010 0000000000000000
0000000000000000 1010101010101010
Review: MIPS Instructions, so far
Category Instr OpC Example Meaning
Arithmetic add 0 & 20 add $s1, $s2, $s3 $s1 = $s2 + $s3
(R & I subtract 0 & 22 sub $s1, $s2, $s3 $s1 = $s2 - $s3
format) add immediate 8 addi $s1, $s2, 4 $s1 = $s2 + 4
shift left logical 0 & 00 sll $s1, $s2, 4 $s1 = $s2 << 4
shift right 0 & 02 srl $s1, $s2, 4 $s1 = $s2 >> 4 (fill with
logical zeros)
shift right 0 & 03 sra $s1, $s2, 4 $s1 = $s2 >> 4 (fill with
arithmetic sign bit)
and 0 & 24 and $s1, $s2, $s3 $s1 = $s2 & $s3
or 0 & 25 or $s1, $s2, $s3 $s1 = $s2 | $s3
nor 0 & 27 nor $s1, $s2, $s3 $s1 = not ($s2 | $s3)
and immediate c and $s1, $s2, ff00 $s1 = $s2 & 0xff00
or immediate d or $s1, $s2, ff00 $s1 = $s2 | 0xff00
load upper f lui $s1, 0xffff $s1 = 0xffff0000
immediate
Review: MIPS Instructions, so far
Category Instr OpC Example Meaning
Data load word 23 lw $s1, 100($s2) $s1 = Memory($s2+100)
transfer store word 2b sw $s1, 100($s2) Memory($s2+100) = $s1
(I format) load byte 20 lb $s1, 101($s2) $s1 = Memory($s2+101)
store byte 28 sb $s1, 101($s2) Memory($s2+101) = $s1
load half 21 lh $s1, 101($s2) $s1 = Memory($s2+102)
store half 29 sh $s1, 101($s2) Memory($s2+102) = $s1
Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L
branch br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L
(I & R set on less a slti $s1, $s2, 100 if ($s2<100) $s1=1;
format) than immediate else $s1=0
set on less 0 & 2a slt $s1, $s2, $s3 if ($s2<$s3) $s1=1;
than else $s1=0
Uncond. jump 2 j 2500 go to 10000
jump jump register 0 & 08 jr $t1 go to $t1
Review: MIPS R3000 ISA
Instruction Categories Registers
Load/Store
Computational R0 - R31
Jump and Branch
Floating Point
- coprocessor PC
HI
Memory Management
LO
Special
3 Instruction Formats: all 32 bits wide
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
OP rs rt rd shamt funct R format
OP rs rt 16 bit number I format
OP 26 bit jump target J format