Professional Documents
Culture Documents
02 Isa
02 Isa
Jason Mars
• The agreed-upon interface between all the software that runs on the machine
and the hardware that executes it.
Application
Operating
Compiler System
Instruction Set
Architecture
Instr. Set Proc. I/O system
Digital Design
Circuit Design
C program
C program
compiler
Assembly
C program
compiler
Assembly
assembler
Object
C program
compiler
Assembly
assembler
Object Library
linker
Executable
C program
compiler
Assembly
assembler
Object Library
linker
Executable
loader
Memory 3
C program
compiler
Assembly
assembler
Object Library
linker
Executable
Memory 3
instruction memory
120007a30: 0f00bb27 ldah gp,15(t12)
120007a34: 509cbd23 lda gp,-25520(gp)
120007a38: 00005d24 ldah t1,0(gp)
120007a3c: 0000bd24 ldah t4,0(gp)
120007a40: 2ca422a0 ldl t0,-23508(t1)
120007a44: 130020e4 beq t0,120007a94
120007a48: 00003d24 ldah t0,0(gp)
120007a4c: 2ca4e2b3 stl zero,-23508(t1)
120007a50: 0004ff47 clr v0
120007a54: 28a4e5b3 stl zero,-23512(t4)
120007a58: 20a421a4 ldq t0,-23520(t0)
120007a5c: 0e0020e4 beq t0,120007a98
120007a60: 0204e147 mov t0,t1
120007a64: 0304ff47 clr t2
120007a68: 0500e0c3 br 120007a80
Processor
PC
instruction memory
120007a30: 0f00bb27 ldah gp,15(t12)
120007a34: 509cbd23 lda gp,-25520(gp)
120007a38: 00005d24 ldah t1,0(gp)
120007a3c: 0000bd24 ldah t4,0(gp)
120007a40: 2ca422a0 ldl t0,-23508(t1)
120007a44: 130020e4 beq t0,120007a94
120007a48: 00003d24 ldah t0,0(gp)
120007a4c: 2ca4e2b3 stl zero,-23508(t1)
120007a50: 0004ff47 clr v0
120007a54: 28a4e5b3 stl zero,-23512(t4)
120007a58: 20a421a4 ldq t0,-23520(t0)
120007a5c: 0e0020e4 beq t0,120007a98
120007a60: 0204e147 mov t0,t1
120007a64: 0304ff47 clr t2
120007a68: 0500e0c3 br 120007a80
Processor
PC
instruction memory
120007a30: 0f00bb27 ldah gp,15(t12)
120007a34: 509cbd23 lda gp,-25520(gp)
120007a38: 00005d24 ldah t1,0(gp)
120007a3c: 0000bd24 ldah t4,0(gp)
120007a40: 2ca422a0 ldl t0,-23508(t1)
120007a44: 130020e4 beq t0,120007a94
120007a48: 00003d24 ldah t0,0(gp)
120007a4c: 2ca4e2b3 stl zero,-23508(t1)
120007a50: 0004ff47 clr v0
120007a54: 28a4e5b3 stl zero,-23512(t4)
120007a58: 20a421a4 ldq t0,-23520(t0)
120007a5c: 0e0020e4 beq t0,120007a98
120007a60: 0204e147 mov t0,t1
120007a64: 0304ff47 clr t2
120007a68: 0500e0c3 br 120007a80
Processor
PC
instruction memory
120007a30: 0f00bb27 ldah gp,15(t12)
120007a34: 509cbd23 lda gp,-25520(gp)
120007a38: 00005d24 ldah t1,0(gp)
120007a3c: 0000bd24 ldah t4,0(gp)
120007a40: 2ca422a0 ldl t0,-23508(t1)
120007a44: 130020e4 beq t0,120007a94
120007a48: 00003d24 ldah t0,0(gp)
120007a4c: 2ca4e2b3 stl zero,-23508(t1)
120007a50: 0004ff47 clr v0
120007a54: 28a4e5b3 stl zero,-23512(t4)
120007a58: 20a421a4 ldq t0,-23520(t0)
120007a5c: 0e0020e4 beq t0,120007a98
120007a60: 0204e147 mov t0,t1
120007a64: 0304ff47 clr t2
120007a68: 0500e0c3 br 120007a80
y=x+b
source operands
• Operations
destination operand operation
• How many?
y=x+b
• Which ones
source operands
• Operations
destination operand operation
• How many?
y=x+b
• Which ones
• Operands source operands
• How many?
• Location (add r1, r2, r5)
• Types
• How to specify?
• Operations
destination operand operation
• How many?
y=x+b
• Which ones
• Operands source operands
• How many?
• Location (add r1, r2, r5)
• Types
• How to specify?
• Instruction format how does the computer know what
0001 0100 1101 1111
• Size means?
• How many formats?
• We’ll look at some of the decisions facing an instruction set architect, and
• how those decisions were made in the design of the MIPS instruction set.
Fixed Length
32 bits
shift
opcode rs rt rd amount
funct
32 bits
32 bits
opcode target
32 bits
shift
opcode rs rt rd amount
funct
32 bits
opcode target
32 bits
32 bits
32 bits
opcode target
32 bits
shift
opcode rs rt rd amount
funct
32 bits
opcode target
32 bits
Operand Locate a
Fetch
Execute Compute
Result Deposit
Store
Next Determin
Instruction
Execute Compute
• All MIPS instructions are 32 bits long.
Result Deposit
Store
• this decision impacts every other ISA decision
we make because it makes instruction bits Next Determin
Instruction
scarce.
6 bits 26 bits
opcode target
• and
• - More instructions
• Forces heavy dependence on
registers, which is exactly what you • + Fast implementation (e.g., easy
want in today’s CPUs pipelining)
• Elegant
• and
• - More instructions
• Forces heavy dependence on
registers, which is exactly what you • + Fast implementation (e.g., easy
want in today’s CPUs pipelining)
• Elegant
Why else?
Accumulator:
1 address add A acc ← acc + mem[A]
Stack:
0 address add tos ← tos + next
Load/Store:
3 address add Ra Rb Rc Ra ← Rb + Rc
load Ra Rb Ra ← mem[Rb]
store Ra Rb mem[Rb] ← Ra
Accumulator:
Accumulator:
11 address
address add A
add A acc ←
acc acc ++ mem[A]
← acc mem[A]
Stack:
Stack:
00 address
address add
add tos ←
tos tos ++ next
← tos next
General Purpose
General Purpose Register:
Register:
22 address
address add A
add ABB EA(A) ←
EA(A) EA(A) ++ EA(B)
← EA(A) EA(B)
33 address
address add A
add ABBCC EA(A) ←
EA(A) EA(B) ++ EA(C)
← EA(B) EA(C)
Load/Store:
Load/Store:
33 address
address add Ra
add Ra Rb
Rb Rc
Rc Ra ←
Ra Rb ++ Rc
← Rb Rc
load Ra
load Ra Rb
Rb Ra ←
Ra mem[Rb]
← mem[Rb]
store Ra
store Ra Rb
Rb mem[Rb] ←
mem[Rb] Ra
← Ra
Accumulator:
Accumulator:
Accumulator:
11 address
address add A
add A acc ←
acc acc ++ mem[A]
← acc mem[A]
1 address add A acc ← acc + mem[A]
Stack:
Stack:
Stack:
00 address
address add
add tos ←
tos tos ++ next
← tos next
0 address add tos ← tos + next
General Purpose
General Purpose Register:
Register:
General Purpose Register:
22 address
address add A
add ABB EA(A) ←
EA(A) EA(A) ++ EA(B)
← EA(A) EA(B)
2 address add A B EA(A) ← EA(A) + EA(B)
33 address
address add A
add ABBCC EA(A) ←
EA(A) EA(B) ++ EA(C)
← EA(B) EA(C)
3 address add A B C EA(A) ← EA(B) + EA(C)
Load/Store:
Load/Store:
Load/Store:
33 address
address add Ra
add Ra Rb
Rb Rc
Rc Ra ←
Ra Rb ++ Rc
← Rb Rc
3 address add Ra Rb Rc Ra ← Rb + Rc
load Ra
load Ra Rb
Rb Ra ←
Ra mem[Rb]
← mem[Rb]
load Ra Rb Ra ← mem[Rb]
store Ra
store Ra Rb
Rb mem[Rb] ←
mem[Rb] Ra
← Ra
store Ra Rb mem[Rb] ← Ra
Accumulator:
Accumulator:
Accumulator:
11 address
address
Accumulator: add A
add A acc ←
acc acc ++ mem[A]
← acc mem[A]
1 address add A acc ← acc + mem[A]
1 address add A acc ← acc + mem[A]
Stack:
Stack:
Stack:
00 address
Stack: address add
add tos ←
tos tos ++ next
← tos next
0 address add tos ← tos + next
0 address add tos ← tos + next
General Purpose
General Purpose Register:
Register:
General Purpose Register:
22 address
Generaladdress add A
add
Purpose Register: ABB EA(A) ←
EA(A) EA(A) ++ EA(B)
← EA(A) EA(B)
2 address add A B EA(A) ← EA(A) + EA(B)
332address
address
address
add A
add
add
AB
A
BC
B
C EA(A) ←
EA(A)
EA(A)
EA(B) ++ EA(C)
← EA(B)
← EA(A) +
EA(C)
EA(B)
3 address add A B C EA(A) ← EA(B) + EA(C)
3 address add A B C EA(A) ← EA(B) + EA(C)
Load/Store:
Load/Store:
Load/Store:
33 address
address
Load/Store: add Ra
add Ra Rb
Rb Rc
Rc Ra ←
Ra Rb ++ Rc
← Rb Rc
3 address add Ra Rb Rc Ra ← Rb + Rc
3 address load
load Ra Rb
add Ra
Ra Rb Rc
Rb Ra
Ra
Ra← mem[Rb]
←mem[Rb]
← Rb + Rc
load Ra Rb Ra ← mem[Rb]
store
store
load Ra Rb
Ra
Ra Rb
Rb mem[Rb]
mem[Rb]
Ra ← ← Ra
← Ra
mem[Rb]
store Ra Rb mem[Rb] ← Ra
store Ra Rb mem[Rb] ← Ra
Code sequence
sequence for
for C
C ==A
A++BBfor
forfour
fourclasses
classesofofinstruction
instructionsets:
sets:
Stack Accumulator
Accumulator GPRegister
GP Register GPRegister
GP Register
(register-memory)
(register-memory) (load-store)
(load-store)
Push A Load A
Load A ADD
ADDC,
C,A,
A,BB LoadR1,A
Load R1,A
Push B Add
Add B
B Load
LoadR2,B
R2,B
Add Store
Store C
C Add
Add R3,R1,R2
R3,R1,R2
Pop C Store
StoreC,R3
C,R3
Code sequence
Codesequence for
sequencefor CC===A
forC AA+++BBBfor
for four
forfour classes
classesofof
fourclasses instruction
ofinstruction sets:
instructionsets:
sets:
Stack
Stack Accumulator
Accumulator
Accumulator GP
GP Register
GPRegister
Register GPRegister
GP
GP Register
Register
(register-memory)
(register-memory)
(register-memory) (load-store)
(load-store)
(load-store)
Push
PushAA Load
Load AA
LoadA ADD
ADDC,
ADD C,A,
C, A,BB
A, B LoadR1,A
Load
Load R1,A
R1,A
Push
PushBB Add
Add B
Add BB Load
LoadR2,B
Load R2,B
R2,B
Add
Add Store
StoreC
Store CC Add
Add R3,R1,R2
Add R3,R1,R2
R3,R1,R2
Pop
PopCC Store
StoreC,R3
Store C,R3
C,R3
Code sequence
Codesequence
sequence for
sequencefor C
CC==
forC
for A
==A ++BB
AA++ BBfor
for four
forfour
for classes
fourclasses
four ofof
classesof
classes instruction
instruction
ofinstruction sets:
sets:
instructionsets:
sets:
Stack
Stack Accumulator
Accumulator
Accumulator
Accumulator GP
GP
GP Register
GPRegister
Register
Register GP
GP GP
GP Register
Register
Register
Register
(register-memory)
(register-memory)
(register-memory)
(register-memory) (load-store)
(load-store)
(load-store)
(load-store)
Push
PushAA Load
Load
LoadA
Load A
AA ADD
ADDC,
ADD
ADD C,A,
C, A,BB
A, B LoadR1,A
Load
Load
Load R1,A
R1,A
R1,A
Push
PushBB Add
Add B
Add
Add BBB Load R2,B
LoadR2,B
Load
Load R2,B
R2,B
Add
Add Store
StoreC
Store
Store C
CC Add
Add
Add R3,R1,R2
AddR3,R1,R2
R3,R1,R2
R3,R1,R2
PopCC
Pop Store
Store
StoreC,R3
C,R3
Store C,R3
C,R3
Code
Codesequence
sequence
sequence
sequence for
for C
for
for C
CC==
=A
=A
AA ++BB
++ BBfor
for
for
for four
four
four
four classes
classes
classes
classes ofof
ofof instruction
instruction
instruction
instruction sets:
sets:
sets:
sets:
Code sequence for C = A + B for four classes of instruction sets:
Stack
Stack
Stack
Accumulator
Accumulator
Accumulator
Accumulator
Accumulator
GP
GP
GP
GP
Register
GPRegister
Register
Register
Register GP
GP
GP
GP
GP Register
Register
Register
Register
Register
(register-memory)
(register-memory)
(register-memory)
(register-memory)
(register-memory) (load-store)
(load-store)
(load-store)
(load-store)
(load-store)
Push
PushAA A
Push Load
Load
Load
Load A
LoadA
AAA ADD
ADDC,
ADD
ADD
ADD C,A,
C,
C, A,B
A,
A, BB
B Load
Load
Load
Load
Load R1,A
R1,A
R1,A
R1,A
R1,A
Push
PushBB B
Push Add
AddB
Add
Add
Add BBBB Load
Load
Load
Load
Load R2,B
R2,B
R2,B
R2,B
R2,B
Add
AddAdd Store
Store
StoreC
Store
Store C
CCC Add
Add
Add
Add R3,R1,R2
R3,R1,R2
R3,R1,R2
AddR3,R1,R2
R3,R1,R2
PopPop
PopCC C Store
Store
StoreC,R3
C,R3
Store
StoreC,R3
C,R3
C,R3
• Register direct R3
• Register direct R3
• Immediate (literal) #25
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
• Register direct R3
• Immediate (literal) #25
• Direct (absolute)
M[10000]
register direct
OP rs rt rd sa funct
lw $1, disp($2)
immediate
rt
(R1 = M[R2 + disp])
register direct
OP rs rt rd sa funct
lw $1, disp($2)
immediate
rt
(R1 = M[R2 + disp])
register direct
OP rs rt rd sa funct
• Instruction Length
• Instruction Formats
• Addressing Modes
32 bits
32 bits
opcode target
32 bits
shift
opcode rs rt rd amount
funct
32 bits
opcode target
32 bits
6 bits 26 bits
register direct
OP rs rt rd sa funct
0 8 bits of data
1 8 bits of data
2 8 bits of data
3 8 bits of data
4 8 bits of data
5 8 bits of data
6 8 bits of data
...
Sunday, January 13, 13
Memory Organization
• Bytes are nice, but most data items use larger "words"
0 32 bits of data
4 32 bits of data
Registers hold 32 bits of data
8 32 bits of data
12 32 bits of data
• 3 instruction formats
• R0 always equals 0.
• Which instructions?
• Arithmetic
• Arithmetic
• add, subtract, multiply, divide
• Arithmetic
• add, subtract, multiply, divide
• Logical
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• load word, store word
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• load word, store word
• Conditional branch
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• load word, store word
• Conditional branch
• branch equal, branch less than, etc
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• load word, store word
• Conditional branch
• branch equal, branch less than, etc
• Unconditional branc (jump)
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• load word, store word
• Conditional branch
• branch equal, branch less than, etc
• Unconditional branc (jump)
• jump, jump and link, return
• Arithmetic
• add, subtract, multiply, divide
• Logical
• and, or, shift left, shift right
• Data transfer
• load word, store word
• Conditional branch
• branch equal, branch less than, etc
• Unconditional branc (jump) Control
• jump, jump and link, return
• Conditional Branches
• Conditional Branches
• Conditional Branches
• Conditional Branches
• Goto-like Jumps/Branches
• Conditional Branches
• Goto-like Jumps/Branches
• Function/Procedure calls
• Function/Procedure calls
• Studies show that almost all conditional branches go short distances from the
current program counter (loops, if-then-else). Is this a good thing? Why/Why
not?
• Studies show that almost all conditional branches go short distances from the
current program counter (loops, if-then-else). Is this a good thing? Why/Why
not?
• Studies show that almost all conditional branches go short distances from the
current program counter (loops, if-then-else). Is this a good thing? Why/Why
not?
• e.g., beq $1, $2, 100 => if ($1 == $2) PC = (PC+4) + 100 * 4
• Studies show that almost all conditional branches go short distances from the
current program counter (loops, if-then-else). Is this a good thing? Why/Why
not?
• e.g., beq $1, $2, 100 => if ($1 == $2) PC = (PC+4) + 100 * 4
condition
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
if (i<j)
w = w+1;
else
w = 5;
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=5, j=10
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=5, j=10
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=5, j=10
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=5, j=10
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=5, j=10
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=5, j=10
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=6, j=4
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=6, j=4
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=6, j=4
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
• beq, bne beq r1, r2, addr => if (r1 == r2) goto addr
• slt $1, $2, $3 => if ($2 < $3) $1 = 1; else $1 = 0
• these, combined with $0, can implement all fundamental branch conditions
• Always, never, !=, = =, >, <=, >=, <, >(unsigned), <= (unsigned), ...
i=6, j=4
slt $temp, $i, $j
if (i<j) beq $temp, $0, L1
w = w+1; add $w, $w, #1
else
beq $0, $0, L2
w = 5;
L1: add $w, $0, #5
L2:
6 bits 26 bits
• Branch (e.g., beq) uses PC-relative addressing mode (uses few bits if address
typically close). That is, it uses base+displacement mode, with the PC being
the base. If opcode is 6 bits, how many bits are available for displacement?
Determines how far can you jump.
• Branch (e.g., beq) uses PC-relative addressing mode (uses few bits if address
typically close). That is, it uses base+displacement mode, with the PC being
the base. If opcode is 6 bits, how many bits are available for displacement?
Determines how far can you jump.
• Branch (e.g., beq) uses PC-relative addressing mode (uses few bits if address
typically close). That is, it uses base+displacement mode, with the PC being
the base. If opcode is 6 bits, how many bits are available for displacement?
Determines how far can you jump.
00
jump destination address
Summary of MIPS
MIPS operands
Name Example Comments
$s0–$s7, $t0–$t9, Fast locations for data. In MIPS, data must be in registers to perform arithmetic. MIPS
32 registers $zero,$a0–$a3, $v0–$v1, register $zero always equals 0. $gp (28) is the global pointer, $sp (29) is the stack
$gp, $fp, $sp, $ra pointer, $fp (30) is the frame pointer, and $ra (31) is the return address.
Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so sequential
230 memory words Memory[4], . . . , word addresses differ by 4. Memory holds data structures, arrays, and spilled registers,
Memory[4294967292] such as those saved on procedure calls.
FIGURE 2.19 MIPS architecture revealed through Section 2.7. Highlighted portions show MIPS assembly language structures intro-
duced in Section 2.7. The J-format, used for jump and jump-and-link instructions, is explained in Section 2.9.
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
4. increase by 1
5. check if i still < 100
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
label
for(i = 0; i < 100; i++) and $t0, $t0, $zero #let i = 0
{ addi $t1, $zero, 100 #temp = 100
sum+=A[i]; LOOP: lw $t3, 0($s0) #temp1 = A[i]
} add $v0, $v0, $t3 #sum += temp1
addi $s0, $s0, 4 #addr of A[i+1]
addi $t0, $t0, 1 #i = i+1
bne $t1, $t0, LOOP #if i < 100
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13
Practice
label
for(i = 0; i < 100; i++) and $t0, $t0, $zero #let i = 0
{ addi $t1, $zero, 100 #temp = 100
sum+=A[i]; LOOP: lw $t3, 0($s0) #temp1 = A[i]
} add $v0, $v0, $t3 #sum += temp1
addi $s0, $s0, 4 #addr of A[i+1]
addi $t0, $t0, 1 #i = i+1
bne $t1, $t0, LOOP #if i < 100
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
Assume
4. increase by 1
int is 32 bits
5. check if i still < 100
$s0 = &A[0]
$v0 = sum;
$t0 = i;
Sunday, January 13, 13