Lect2 PDF
Lect2 PDF
Lect2 PDF
Instructions: Overview
MIPS Arithmetic
Example:
C code:
A = B + C
MIPS code:
MIPS Arithmetic
A = B + C + D;
E = F - A;
MIPS code
(arithmetic):
MIPS Arithmetic
Why?
Control
Input
Memory
Datapath
Processor
Output
I/O
Memory Organization
8 bits of data
8 bits of data
3
4
5
6
8 bits of data
...
8 bits of data
8 bits of data
8 bits of data
8 bits of data
Memory Organization
Bytes are load/store units, but most data items use larger words
For MIPS, a word is 32 bits or 4 bytes.
32 bits of data
4
8
12
32 bits of data
32 bits of data
32 bits of data
...
Load/Store Instructions
A[8] = h + A[8];
MIPS code
(load):
(arithmetic):
(store):
value
offset
address
lw $t0, 32($s3)
add $t0, $s2, $t0
sw $t0, 32($s3)
A MIPS Example
$5=k
$4=Base
swap:
muli
add
lw
lw
sw
sw
jr
address of v[]
$2,
$2,
$15,
$16,
$16,
$15,
$31
$5,
4
$4,
$2
0($2)
4($2)
0($2)
4($2)
MIPS
Instruction
Meaning
Machine Language
Instructions, like registers and words of data, are also 32 bits long
opcode
operation
6 bits
rs
first
register
source
operand
5 bits
rt
rd
shamt
100000
funct
second
register
source
operand
register shift
destin- amount
ation
operand
5 bits
5 bits
6 bits
5 bits
26 25
opcode
21 20
rs
16 15
rt
11 10
rd
shamt
funct
rd
rt
26 25
21 20
16 15
11 10
0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
opcode
rs
rt
rd
shamt
funct
0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
Encoding = 0x00622020
13
Machine Language
100011 10010
6 bits
5 bits
op
rs
01000
5 bits
rt
0000001111101010
16 bits
16 bit offset
26 25
opcode
21 20
rs
16 15
rt
Immediate Value
rt
Immediate
lw $5, 3000($2)
rs
31
26 25
21 20
16 15
1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
opcode
rs
rt
Immediate Value
1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
Encoding = 0x8C450BB8
15
26 25
opcode
21 20
rs
16 15
rt
Immediate Value
rt
Immediate
sw $5, 3000($2)
rs
31
26 25
21 20
16 15
1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
opcode
rs
rt
Immediate Value
1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
Encoding = 0xAC450BB8
The immediate value is signed
16
Processor
Memory
memory for data, programs,
compilers, editors, etc.
Memory Organization:
Big/Little Endian Byte Order
Little-endian
Memory
Example:
I-type instructions
0000000000011001
if (i==j) h = i + j;
Addresses in Branch
Instructions:
bne $t4,$t5,Label
beq $t4,$t5,Label
Format:
I
op
rs
rt
16 bit offset
Addresses in Branch
26 25
opcode
21 20
rs
16 15
rt
rs
Offset
Encoded by
40/4 = 10
26 25
21 20
16 15
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
opcode
rs
rt
Immediate Value
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
Encoding = 0x1009000A
23
Example:
if (i!=j)
h=i+j;
else
h=i-j;
000010
00000000000000000000011001
6 bits
26 bits
op
26 bit number
word-relative
addressing:
Addresses in Jump
op
26 bit address
Constants
MIPS Instructions:
addi $29, $29, 4
slti $8, $18, 10
andi $29, $29, 6
ori $29, $29, 4
Immediate Operands
op
11101
11101
5 bits
5 bits
rs
rt
0000000000000100
16 bits
16 bit number
ori
0000000000000000
0000000000000000
0000000000000000
1010101010101010
1010101010101010
1010101010101010
So far
Instruction
Format
add $s1,$s2,$s3
sub $s1,$s2,$s3
lw $s1,100($s2)
sw $s1,100($s2)
bne $s4,$s5,Lab1
beq $s4,$s5,Lab2
j Lab3
R
R
I
I
I
I
J
Meaning
$s1 = $s2 + $s3
$s1 = $s2 $s3
$s1 = Memory[$s2+100]
Memory[$s2+100] = $s1
Next instr. is at Lab1 if $s4 != $s5
Next instr. is at Lab2 if $s4 = $s5
Next instr. is at Lab3
Formats:
R
op
rs
rt
rd
op
rs
rt
16 bit address
op
shamt
26 bit address
funct
Logical Operations
Shift Logical Left (SLL $S1,$S2,10)
Shift Logical Right (SRL $S1,$S2,10)
AND (AND $S1,$S2,$S3)
OR (OR $S1,$S2,$S3)
NOR (NOR $S1,$S2,$S3)
Shift Operations
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
op
AND Operations
OR Operations
NOT Operations
Change 0 to 1, and 1 to 0
a NOR b == NOT ( a OR b )
Register 0:
always read as
zero
Control Flow
New instruction:
slt $t0, $s1, $s2
if
1 < +1 $t0 = 1
# unsigned
Usage
the constant value 0
values for results and expression evaluation
arguments
temporaries
saved
more temporaries
global pointer
stack pointer
frame pointer
return address
Procedure Calling
Steps required
1.
2.
3.
4.
5.
6.
$fp
$sp
$sp
$fp
Saved argument
registers (if any)
Saved return address
Saved saved
registers (if any)
Low address
a.
b.
c.
Variables that are local to a procedure but do not fit into registers (e.g., local arrays, structures, etc.) are also stored in the stack. This area of the stack is the frame. The frame pointer
$fp points to the top of the frame and the stack pointer to the bottom. The frame pointer does
not change during procedure execution, unlike the stack pointer, so it is a stable base
register from which to compute offsets to local variables.
Use of the frame pointer is optional. If there are no local variables to store in the stack it is
not efficient to use a frame pointer.
Byte/Halfword Operations
lb rt, offset(rs)
sb rt, offset(rs)
lh rt, offset(rs)
sh rt, offset(rs)
rs
rt
Immediate
2. Register addressing
op
rs
rt
rd
...
funct
Registers
Register
3. Base addressing
op
rs
rt
Memory
Address
Register
Byte
Halfword
4. PC-relative addressing
op
rs
rt
Memory
Address
PC
Word
5. Pseudodirect addressing
op
Address
PC
Memory
Word
Word
Overview of MIPS
op
rs
rt
rd
shamt
op
rs
rt
16 bit address
op
26 bit address
funct
Summarize MIPS:
MIPS operands
Name
Example
Comments
$s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform
arithmetic. MIPS register $zero always equals 0. Register $at is
32 registers $a0-$a3, $v0-$v1, $gp,
$fp, $sp, $ra, $at
reserved for the assembler to handle large constants.
Memory[0],
30
words
Memory[4294967292]
add
subtract
Category
Arithmetic
Instruction
Data transfer
Conditional
branch
Unconditional jump
$s1 = 100 * 2
16
Comments
branch on equal
beq
$s1, $s2, 25
if ($s1 == $s2) go to
PC + 4 + 100
bne
$s1, $s2, 25
if ($s1 != $s2) go to
PC + 4 + 100
slt
slti
jump
j
jr
jal
jump register
jump and link
else $s1 = 0
2500
$ra
2500
Alternative Architectures
Design alternative:
A dominant architecture:
80x86
Complexity
Saving grace:
the most frequently used instructions are not too difficult to build
compilers avoid the portions of the architecture that are slow
Summary
Design Principles: