Professional Documents
Culture Documents
RISC Goals
EECC550 - Shaaban
#1 Lec # 2 Winter 2012 11-29-2012
Registers
R0 - R31
$0 - $31
32 bits each
31
GPRs
R0 = 0
PC
HI
LO
OP
rs
rt
I-Type: ALU
OP
rs
rt
Load/Store, Branch
J-Type: Jumps
OP
rd
sa
funct
immediate
jump target
EECC550 - Shaaban
#2 Lec # 2 Winter 2012 11-29-2012
MIPS uses Big Endian operand storage in memory where the most
significant byte (msb) is in low memory (this is similar to IBM
360/370, Motorola 68k, SPARC, HP PA-RISC).
Least Significant Byte
lsb
msb
In low memory
High Memory
0
Aligned
EECC550 - Shaaban
#3 Lec # 2 Winter 2012 11-29-2012
$zero
$at
$v0-$v1
4-7
8-15
16-23
24-25
26-27
28
29
30
31
$a0-$a3
$t0-$t7
$s0-$s7
$t8-$t9
$k0-$k1
$gp
$sp
$fp
$ra
$0 - $31
Usage
Preserved on call?
Constant value 0
Reserved for assembler
Values for result and
expression evaluation
Arguments
Temporaries
Saved
More temporaries
Reserved for operating system
Global pointer
Stack pointer
Frame pointer
Return address (ra)
n.a.
no
no
yes
no
yes
no
yes
yes
yes
yes
yes
EECC550 - Shaaban
#4 Lec # 2 Winter 2012 11-29-2012
2 Immediate Addressing:
4 PC-Relative Addressing:
Where the address is the sum of the PC and the 16-address field in
the instruction shifted left 2 bits. (I-Type, branches)
5 Pseudodirect Addressing:
e.g. j 10000
Where the jump address is the 26-bit jump target from the
instruction shifted left 2 bits concatenated with the 4 upper bits of
the PC (J-Type)
EECC550 - Shaaban
#5 Lec # 2 Winter 2012 11-29-2012
OP
6 bits
[31:26]
rs
2nd operand
rt
Destination
rd
shamt
funct
5 bits
5 bits
5 bits
5 bits
6 bits
[25:21]
[20:16]
[15:11]
[10:6]
[5:0]
Function
Field
Secondary
Opcode?
Operand register in rs
Destination register in rd
Examples:
add $1,$2,$3
sub $1,$2,$3
Operand register in rt
and $1,$2,$3
or $1,$2,$3
EECC550 - Shaaban
#6 Lec # 2 Winter 2012 11-29-2012
Destination
2nd operand
OP
rs
rt
6 bits
5 bits
5 bits
16 bits
[31:26]
[25:21]
[20:16]
[15:0]
imm16
immediate
Examples:
OP = 12
Result register in rt
add immediate:
addi $1,$2,100
and immediate
andi $1,$2,10
Constant operand
in immediate
EECC550 - Shaaban
#7 Lec # 2 Winter 2012 11-29-2012
Src./Dest.
imm16
OP
rs
rt
6 bits
5 bits
5 bits
16 bits
[31:26]
[25:21]
[20:16]
[15:0]
Examples:
source register in rt
Offset
Store word:
sw $3, 500($4)
Load word:
lw $1, 32($2)
Destination register in rt
base register in rs
Mem[R[rs] + address] R[rt]
PC PC + 4
R[rt] Mem[R[rs] + address]
PC PC + 4
base register in rs
Offset
EECC550 - Shaaban
#8 Lec # 2 Winter 2012 11-29-2012
OP
rs
rt
6 bits
5 bits
5 bits
16 bits
[31:26]
[25:21]
[20:16]
[15:0]
OP = 4
Examples:
OP = 5
Branch on equal
beq $1,$2,100
Added
to PC+4 to form
branch target
R[rs] = R[rt] :
R[rs] R[rt] :
PC PC + 4 + address x 4
PC PC + 4
EECC550 - Shaaban
#9 Lec # 2 Winter 2012 11-29-2012
OP
jump target
6 bits
26 bits
[31:26]
[25:0]
Examples:
Word = 4 bytes
Jump
j 10000
jal 10000
Jump target
in words
PC(31-28),jump_target,00
0 0
2 bits
PC PC + 4
PC PC(31-28),jump_target,00
EECC550 - Shaaban
#10 Lec # 2 Winter 2012 11-29-2012
1 Register (direct) op
R-Type
Second Operand
rs
rt
Destination
rd
register
I-Type
First Operand
2 Immediate
op
rs
Base
3 Displacement:
Base+index
(load/store)
op
Second Operand
Destination
rs
rt
immed
src/dest
rt
immed
register
4 PC-relative
(branches)
op
rs
rt
Memory
+
immed
Memory
PC
EECC550 - Shaaban
#11 Lec # 2 Winter 2012 11-29-2012
MIPS Arithmetic
Instructions
Examples
(Integer)
Instruction
add
subtract
add immediate
add unsigned
subtract unsigned
add imm. unsign.
multiply
multiply unsigned
divide
Example
add $1,$2,$3
sub $1,$2,$3
addi $1,$2,100
addu $1,$2,$3
subu $1,$2,$3
addiu $1,$2,100
mult $2,$3
multu$2,$3
div $2,$3
divide unsigned
divu $2,$3
Move from Hi
Move from Lo
mfhi $1
mflo $1
Meaning
$1 = $2 + $3
$1 = $2 $3
$1 = $2 + 100
$1 = $2 + $3
$1 = $2 $3
$1 = $2 + 100
Hi, Lo = $2 x $3
Hi, Lo = $2 x $3
Lo = $2 $3,
Hi = $2 mod $3
Lo = $2 $3,
Hi = $2 mod $3
$1 = Hi
$1 = Lo
Comments
3 operands; exception possible
3 operands; exception possible
+ constant; exception possible
3 operands; no exceptions
3 operands; no exceptions
+ constant; no exceptions
64-bit signed product
64-bit unsigned product
Lo = quotient, Hi = remainder
Unsigned quotient & remainder
Used to get copy of Hi
Used to get copy of Lo
EECC550 - Shaaban
#12 Lec # 2 Winter 2012 11-29-2012
Example
and $1,$2,$3
or $1,$2,$3
xor $1,$2,$3
nor $1,$2,$3
andi $1,$2,10
ori $1,$2,10
xori $1, $2,10
sll $1,$2,10
srl $1,$2,10
sra $1,$2,10
sllv $1,$2,$3
srlv $1,$2, $3
srav $1,$2, $3
Meaning
$1 = $2 & $3
$1 = $2 | $3
$1 = $2 $3
$1 = ~($2 |$3)
$1 = $2 & 10
$1 = $2 | 10
$1 = ~$2 &~10
$1 = $2 << 10
$1 = $2 >> 10
$1 = $2 >> 10
$1 = $2 << $3
$1 = $2 >> $3
$1 = $2 >> $3
Comment
3 reg. operands; Logical AND
3 reg. operands; Logical OR
3 reg. operands; Logical XOR
3 reg. operands; Logical NOR
Logical AND reg, constant
Logical OR reg, constant
Logical XOR reg, constant
Shift left by constant
Shift right by constant
Shift right (sign extend)
Shift left by variable
Shift right by variable
Shift right arith. by variable
EECC550 - Shaaban
#13 Lec # 2 Winter 2012 11-29-2012
Comment
Store word
Store half word
Store byte
lw $1, 30($2)
lh $1, 40($3)
lhu $1, 40($3)
lb $1, 40($3)
lbu $1, 40($3)
Load word
Load half word
Load half word unsigned
Load byte
Load byte unsigned
lui $1, 40
Word = 4 bytes
LUI
R5
R5
0000 0000
EECC550 - Shaaban
#14 Lec # 2 Winter 2012 11-29-2012
Example
beq $1,$2,100
bne $1,$2,100
slt $1,$2,$3
slti $1,$2,100
sltu $1,$2,$3
sltiu $1,$2,100
jump
j 10000
jump register
jr $31
jal 10000
Conditional
Branches
Compares
Jumps
Meaning
if ($1 == $2) go to PC+4+100
Equal test; PC relative branch
if ($1!= $2) go to PC+4+100
Not equal test; PC relative branch
if ($2 < $3) $1=1; else $1=0
Compare less than; 2s comp.
if ($2 < 100) $1=1; else $1=0
Compare < constant; 2s comp.
if ($2 < $3) $1=1; else $1=0
Compare less than; natural numbers
if ($2 < 100) $1=1; else $1=0
Compare < constant; natural numbers
go to 10000
Jump to target address
go to $31
For switch, procedure return
$31 = PC + 4; go to 10000
For procedure call
EECC550 - Shaaban
#15 Lec # 2 Winter 2012 11-29-2012
Register zero always has the value zero (even if you try to write it).
Branch/jump and link put the return addr. PC+4 into the link register
(R31).
All instructions change all 32 bits of the destination register (including
lui, lb, lh) and all read all 32 bits of sources (add, sub, and, or, )
Immediate arithmetic and logical instructions are extended as follows:
logical immediates ops are zero extended to 32 bits.
arithmetic immediates ops are sign extended to 32 bits (including
addu).
The data loaded by the instructions lb and lh are extended as follows:
lbu, lhu are zero extended.
lb, lh are sign extended.
Overflow can occur in these arithmetic and logical instructions:
add, sub, addi
it cannot occur in addu, subu, addiu, and, or, xor, nor, shifts, mult,
multu, div, divu
EECC550 - Shaaban
#16 Lec # 2 Winter 2012 11-29-2012
EECC550 - Shaaban
#17 Lec # 2 Winter 2012 11-29-2012
A[8]
32 bytes
Steps:
4x8
MIPS Instructions:
lw $t0,32($s3)
add $s1,$s2,$t0
Word = 4 bytes
MIPS Instructions:
add $t1,$s4,$s4
add $t1,$t1,$t1
add $t1,$t1,$s3
lw $t0,0($t1)
add $s1,$s2,$t0
A[] array of words in memory
4i bytes
# $t1 = 2*i
$s3
# $t1 = 4*i
#$t1 = address of A[i]
# $t0 = A[i]
# g = h + A[i]
EECC550 - Shaaban
#19 Lec # 2 Winter 2012 11-29-2012
Mips Instructions:
beq $s3,$s4, True
sub $s0,$s1,$s2
j Exit
True: add $s0,$s1,$s2
Exit:
Else
#
#
#
#
branch if i==j
f = g-h (false)
go to Exit
f = g+h (true)
EECC550 - Shaaban
#20 Lec # 2 Winter 2012 11-29-2012
Index
update
add
add
add
lw
add
add
bne
Word = 4 bytes
$t1,$s3,$s3
$t1,$t1,$t1
$t1,$t1,$s5
$t1,0($t1)
$s1,$s1,$t1
$s3,$s3,$s4
$s3,$s2,Loop
#
#
#
#
#
#
#
slt $t0,$s0,$s1
#
#
bne $t0,$zero, Less #
#
Value = 0
. . .
#
$t0 = 1 if
$s0<$s1 (g < h)
goto Less
if $t0 != 0
(if (g < h)
Less:
EECC550 - Shaaban
#22 Lec # 2 Winter 2012 11-29-2012
#
#
#
#
#
#
#
#
Exit:
save[] array of words in memory
EECC550 - Shaaban
#23 Lec # 2 Winter 2012 11-29-2012
(k) {
0: f=i+j;
1: f=g+h;
2: f=gh;
3: f=ij;
break;
break;
break;
break;
/*
/*
/*
/*
k=0*/
k=1*/
k=2*/
k=3*/
Method: Use k to index a jump address table in memory, and then jump via
the value loaded.
Steps:
1st test that k matches one of the cases (0<=k<=3); if not, the code exits.
Multiply k by 4 to index table of words.
Assume 4 sequential words in memory, base address in $t2, have
addresses corresponding to labels L0, L1, L2, L3. i.e Jump address table
Load a register $t1 with jump table entry address.
Jump to address in register $t1 using jump register jr $t1.
EECC550 - Shaaban
#24 Lec # 2 Winter 2012 11-29-2012
Jump table
k=0
L0:
k=1
L1:
k=2
L2:
k=3
L3:
Exit:
slti $t3,$s5,0
bne $t3,$zero,Exit
slti $t3,$s5,4
beq $t3,$zero,Exit
add $t1,$s5,$s5
add $t1,$t1,$t1
add $t1,$t1,$t2
lw $t1,0($t1)
jr $t1
add $s0,$s3,$s4
j
Exit
add $s0,$s1,$s2
j
Exit
sub $s0,$s1,$s2
j
Exit
sub $s0,$s3,$s4
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Test if k < 0
if k < 0,goto Exit
Test if k < 4
if k >= 4,goto Exit
Temp reg $t1 = 2*k
Temp reg $t1 = 4*k
$t1 = addr JumpTable[k]
$t1 = JumpTable[k]
jump based on $t1
k=0 so f = i + j
end case, goto Exit
k=1 so f = g + h
end case, goto Exit
k=2 so f = g h
end case, goto Exit
k=3 so f = i j
end of switch statement
EECC550 - Shaaban
#25 Lec # 2 Winter 2012 11-29-2012
(level)
MIPS Instructions:
address
1000
add $a0,$s0,$zero # x = a
1004
add $a1,$s1,$zero # y = b
1008
jal sum
# $ra=1012,go to sum
Jump And Link = Procedure call
1012
...
Return address 1012 in $ra
2000 sum: add $v0,$a0,$a1
R[$31] 1012
2004
jr $ra
Jump Register = Return
$ra = $31
ra = return address
EECC550 - Shaaban
#26 Lec # 2 Winter 2012 11-29-2012
Stack
$29
$sp
stack
pointer
global
pointer
$28 $gp
Low memory
Heap
Static
Variables declared
once per program
Code
Program
EECC550 - Shaaban
#27 Lec # 2 Winter 2012 11-29-2012
MIPS Code:
sumSquare:
subi
sw
sw
sw
addi
jal
lw
lw
lw
add
addi
jr
Result in $vo
$sp,$sp,12
$ra,$ 8($sp)
$a0,$ 0($sp)
$a1,$ 4($sp)
$a1,$a0,$zero
mult
$ra,$ 8($sp)
$a0,$ 0($sp)
$a1,$ 4($sp)
$vo,$v0,$a1
$sp,$sp,12
$ra
Return
#
#
#
#
#
#
#
#
#
#
#
X = $a0
Y = $a1
space on stack
save return address
save x On
Save
save y Stack
mult(x,x)
call mult
get return address
restore x From
Restore
restore y Stack
mult()+ y
=> stack space
EECC550 - Shaaban
#28 Lec # 2 Winter 2012 11-29-2012