Professional Documents
Culture Documents
MIPS Programming
2011 Sem 1
MIPS Programming
Instruction
Fetch
Instruction
Decode
Operand
Fetch
Execute
Result
Store
Next
Instruction
2011 Sem 1
MIPS Programming
2011 Sem 1
MIPS Programming
MIPS Programming
ISA is determined by
2011 Sem 1
MIPS Programming
Instruction
Decode
Operand
Fetch
Execute
Result
Store
Next
Instruction
2011 Sem 1
how is it decoded?
Successor instruction
jumps, conditions, branches
2011 Sem 1
MIPS Programming
ASSEMBLY LANGUAGE
Machine code
Instructions are represented in binary
1000110010100000 is an instruction that tells one computer to add two
numbers
Hard and tedious for programmer
Assembly language
MIPS Programming
2011 Sem 1
MIPS Programming
10
CONCEPT #1:
DATA STORAGE
Memory Organization
Registers
MIPS Programming
11
2011 Sem 1
MIPS Programming
Address
0
1
2
3
4
5
6
7
8
9
10
11
Memory
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
:
12
2n 1.
2n-2 words with byte addresses 0, 4, 8,
, 2n 4.
2011 Sem 1
MIPS Programming
Address
0
Memory
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
8 bits
:
13
REGISTERS (1/3)
2011 Sem 1
MIPS Programming
14
REGISTERS (2/3)
Register
number
Usage
Name
Register
number
Usage
$zero
Constant value 0
$t8-$t9
24-25
$v0-$v1
2-3
More
temporaries
$gp
28
Global pointer
$sp
29
Stack pointer
$a0-$a3
4-7
Arguments
$fp
30
Frame pointer
$t0-$t7
8-15
Temporaries
$ra
31
Return address
$s0-$s7
16-23
Program variables
2011 Sem 1
MIPS Programming
15
REGISTERS (3/3)
Classifying general-purpose register computers:
ALU (Arithmetic-Logic Unit) instruction has two or three
operands?
Add R2, R1 (R2=R2+R1) or Add R3, R2, R1 (R3=R2+R1)
2011 Sem 1
MIPS Programming
16
CONCEPT #2:
MEMORY ADDRESSING MODE
Memory Locations and Addresses
Memory Operations
Addressing Modes
MIPS Programming
17
2011 Sem 1
MIPS Programming
18
2011 Sem 1
MIPS Programming
19
Byte 0
Byte 1
Byte 2
Byte 3
Byte 4
Byte 5
Byte 6
Byte 7
Aligned word
Mis-aligned word
MIPS Programming
20
2011 Sem 1
MIPS Programming
21
Up to 2k
addressable
locations.
Address
MAR
Memory
0
1
2
3
4
5
MDR
Control lines
(R/W, etc.)
2011 Sem 1
MIPS Programming
22
ADDRESSING MODES
Addressing mode
Example
Meaning
Register
Add R4,R3
R4 R4+R3
Immediate
Add R4,#3
R4 R4+3
Displacement
Add R4,100(R1)
R4 R4+Mem[100+R1]
Register indirect
Add R4,(R1)
R4 R4+Mem[R1]
Indexed / Base
Add R3,(R1+R2)
R3 R3+Mem[R1+R2]
Direct or absolute
Add R1,(1001)
R1 R1+Mem[1001]
Memory indirect
Add R1,@(R3)
R1 R1+Mem[Mem[R3]]
Auto-increment
Add R1,(R2)+
R1 R1+Mem[R2]; R2 R2+d
Auto-decrement
Add R1,(R2)
R2 R2-d; R1 R1+Mem[R2]
Scaled
Add R1,100(R2)[R3]
R1 R1+Mem[100+R2+R3*d]
2011 Sem 1
MIPS Programming
23
rs
rt
op: opcode
rs: first source register
rd
op
r1
r2
r3
100
2011 Sem 1
MIPS Programming
24
Immediate mode
op
rs
rt
op: opcode
immed
2011 Sem 1
r1
r3
immed: constant
12
MIPS Programming
25
rs
rt
Memory
immed
register
Memory
r2
r1
100
Address = 250
150
2011 Sem 1
MIPS Programming
250
88
26
rs
rt
immed
PC
Target address
r1
r2
PC
2011 Sem 1
MIPS Programming
Address = (PC +
4) + (6 4)
27
MIPS Programming
28
STANDARD OPERATIONS
2011 Sem 1
Data Movement
Arithmetic
Shift
Logical
Control flow
Subroutine Linkage
call, return
Interrupt
trap, return
Synchronization
String
Graphics
29
load
22%
conditional branch
20%
compare
16%
store
12%
add
8%
and
6%
sub
5%
move register-register
4%
call
1%
10
return
1%
Total
96%
MIPS Programming
30
2011 Sem 1
MIPS Programming
31
CONCEPT #4:
INSTRUCTION FORMATS
Instruction Length
Instruction Fields
Type and Size of Operands
MIPS Programming
32
INSTRUCTION LENGTH
Variable-length instructions.
Intel 80x86: Instructions vary from 1 to 17
bytes long.
Digital VAX: Instructions vary from 1 to 54
bytes long.
Require multi-step fetch and decode.
Allow for a more flexible (but complex) and
compact instruction set.
Fixed-length instructions.
MIPS Programming
33
INSTRUCTION FIELDS
An instruction consists of opcode and a certain number
(possible zero) of operands.
MIPS Programming
34
2011 Sem 1
MIPS Programming
35
MIPS Programming
36
Issues:
Code size, speed/performance, design complexity.
Things to be decided:
Number of registers
Number of addressing modes
Number of operands in an instruction
MIPS Programming
37
2011 Sem 1
MIPS Programming
38
2011 Sem 1
MIPS Programming
39
Type-B instructions
opcode
operand
operand
6 bits
5 bits
5 bits
opcode
operand
unused
6 bits
5 bits
5 bits
We see that there are wasted bits, and the maximum total
number of instructions is 26 or 64.
2011 Sem 1
MIPS Programming
40
Type-B instructions
opcode
operand
operand
6 bits
5 bits
5 bits
opcode
operand
11 bits
5 bits
Encoding concern: The first 6 bits of the opcode for any typeB instruction must not be identical to the opcode of any of the
type-A instructions. (Why?)
2011 Sem 1
MIPS Programming
41
Type-A instructions
opcode
operand
operand
6 bits
5 bits
5 bits
opcode
operand
11 bits
5 bits
Type-B instructions
2011 Sem 1
MIPS Programming
42
EXAMPLE
Design an expanding opcode for the following to be encoded in a
36-bit instruction format. An address takes up 15 bits and a register
number 3 bits.
7 instructions with two addresses and one register number.
500 instructions with one address and one register number.
50 instructions with no address or register.
One
possible
answer:
3 bits
15 bits
15 bits
3 bits
000 110
opcode
address
address
register
111
000000 + 9 bits
opcode
address
register
unused
unused
111
2011 Sem 1
000001
:
+ 9 0s
110010
opcode
MIPS Programming
43
MIPS Programming
44
MIPS Programming
45
# tmp = b + c
# a = tmp - d
MIPS Programming
47
ARITHMETIC: ADDITION
Addition in assembly
C:
a = b + c;
MIPS: add $s0, $s1, $s2
$s0 variable a
$s1 variable b
$s2 variable c
Why?
Keep the hardware simple
Design principle: Simplicity favors regularity
2011 Sem 1
MIPS Programming
48
INSTRUCTION SYNTAX
add
Operation
Source 1
Destination
(gets the result)
MIPS Programming
49
Register
number
Usage
Name
Register
number
Usage
$zero
Constant value 0
$t8-$t9
24-25
$v0-$v1
2-3
More
temporaries
$gp
28
Global pointer
$sp
29
Stack pointer
$a0-$a3
4-7
Arguments
$fp
30
Frame pointer
$t0-$t7
8-15
Temporaries
$ra
31
Return address
$s0-$s7
16-23
Program variables
2011 Sem 1
MIPS Programming
50
ARITHMETIC: SUBTRACTION
Subtraction in assembly
C:
MIPS:
a = b c;
sub $s0, $s1, $s2
$s0 variable a
$s1 variable b
$s2 variable c
2011 Sem 1
MIPS Programming
51
C statement
a = b + c - d;
A single instruction can handle at most two source operands.
Break it up into multiple instructions and use temporary
register $t0 $t7
add $t0, $s1, $s2 # tmp = b + c
sub $s0, $t0, $s3 # a = tmp - d
A single line of C statement may break up into several lines of
MIPS assembly
Who is doing this break up when you write high-level
language code?
2011 Sem 1
MIPS Programming
52
C statement
f = (g + h) (i + j);
2011 Sem 1
MIPS Programming
53
TRY IT YOURSELF
z = a + b + c + d;
a:$s0
b:$s1
c:$s2
z:$s4
# tmp0 = a + b
# tmp1 = c + d
# z = tmp0 + tmp1
z = (a b) + c;
a:$s0
b:$s1
c:$s2
d:$s3
2011 Sem 1
z:$s4
# tmp0 = a - b
# z = tmp0 + c
MIPS Programming
54
CONSTANT/IMMEDIATE OPERANDS
Immediates are numerical constants
They appear often in operations; so there is special instruction
for them
Add immediate (addi)
C:
a = a + 4;
MIPS: addi $s0, $s0, 4
# $s0 = $s0 + 4
2011 Sem 1
MIPS Programming
55
REGISTER ZERO
One particular immediate, the number zero (0), appears very
often in code
Define register zero ($0 or $zero) to always have the value 0
C:
f = g;
MIPS: add $s0, $s1, $zero
2011 Sem 1
MIPS Programming
56
LOGICAL OPERATIONS
<<
<<
MIPS instruction
sll
Shift right
>>
>>, >>>
srl
Bitwise AND
&
&
and, andi
Bitwise OR
or, ori
Bitwise NOT
nor
2011 Sem 1
MIPS Programming
57
2011 Sem 1
MIPS Programming
58
2011 Sem 1
a = a * 8;
sll $s0, $s0, 3
MIPS Programming
59
2011 Sem 1
MIPS Programming
60
2011 Sem 1
MIPS Programming
61
BITWISE OR INSTRUCTION
OR: bitwise operation that places a 1 in the result if
either operand bit is 1
Can be used to force certain bits to 1s
Example: ori $t0, $t1, 0xFFF
$t1:
0000 1001 1100 0011 0101 1101 1001 1100
OxFFF: 0000 0000 0000 0000 0000 1111 1111 1111
$t0:
0000 1001 1100 0011 0101 1111 1111 1111
2011 Sem 1
MIPS Programming
62
2011 Sem 1
MIPS Programming
63
READING ASSIGNMENT
2011 Sem 1
MIPS Programming
64
Programming in MIPS
Assembly Code
2011 Sem 1
MIPS Programming
65
Data
Operations
Load: a word (4 bytes) is transferred from memory to register
Store: a word (4 bytes) is transferred from memory to a register
Arithmetic-logic operations
Between registers
Between registers and immediate operands
2011 Sem 1
MIPS Programming
66
REGISTERS
General
Floating
2011 Sem 1
point registers
MIPS Programming
67
2011 Sem 1
MIPS Programming
68
2011 Sem 1
MIPS Programming
69
MEMORY LAYOUT
2011 Sem 1
MIPS Programming
70
MEMORY LAYOUT
2011 Sem 1
MIPS Programming
71
lw
$t0, 40($s3)
add
$t0, $s2, $t0
sw
$t0, 28($s3)
Each array element occupies a word (4 bytes).
$s3 contains the base address (address of first element) of
array A.
MIPS Programming
72
$s0 + 4
$s0 = 8000
$t0, 4($s0)
8007
8006
8005
8004
8003
8002
8001
8000
Word at address
$s0 + 4
Register $t0
MIPS Programming
73
$s0 + 12
$s0 = 8000
$t0, 12($s0)
8015
8014
8013
8012
:
8003
8002
8001
8000
Word at address
$s0 + 12
:
Register $t0
MIPS Programming
74
Similar in format:
lb
sb
$t1, 12($s3)
$t2, 13($s3)
2011 Sem 1
MIPS Programming
75
2011 Sem 1
MIPS Programming
76
ARRAYS (1/2)
$t0, 4($s3)
# offset = 14 = 4
2011 Sem 1
77
ARRAYS (2/2)
A[3]
12($s3)
A[3] = h + A[1];
A[2]
lw $t0, 4($s3)
add $t0, $s2, $t0
sw $t0, 12($s3)
A[1]
4($s3)
A[0]
$s3
2011 Sem 1
MIPS Programming
78
2011 Sem 1
MIPS Programming
79
2011 Sem 1
MIPS Programming
80
EXAMPLE
swap(int v[], int k)
$5 contains k.
{ int temp;
$2 contains 4k
temp = v[k]
(because each word
Base address
v[k] = v[k+1];
of array v in $4
contains 4 bytes)
v[k+1] = temp;
}
swap:
muli $2, $5, 4
add $2, $4, $2
lw $15, 0($2)
lw $16, 4($2)
sw $16, 0($2)
sw $15, 4($2)
jr $31
2011 Sem 1
MIPS Programming
81
CONSTANTS
Small constants are used quite frequently (50% of operands).
Examples:
A = A + 5;
B = B + 1;
C = C 18;
Solution:
2011 Sem 1
$29, $29, 4
$8, $18, 10
$29, $29, 6
$29, $29, 4
MIPS Programming
82
1001
and
1011
1001
1000
MIPS Programming
83
0000000000000000
Lower-order bits
filled with zeros.
ori
1010101010101010
0000000000000000
0000000000000000
1111000011110000
1010101010101010
1111000011110000
MIPS Programming
84
Example:
lui
$t0, 43690
# 4369010 = 10101010101010102
lui
$t0, 0xAAAA
# AAAA16 = 10101010101010102
or
2011 Sem 1
$at, 0xABAB
$at, $at, 0xCDCD
$t0, $t0, $at
MIPS Programming
85
2011 Sem 1
MIPS Programming
86
LA Example
2011 Sem 1
MIPS Programming
87
Symbolic Address
2011 Sem 1
MIPS Programming
88
2011 Sem 1
MIPS Programming
89
Decision-making instructions
Alter the control flow of the program
Change the next instruction to be executed
2011 Sem 1
MIPS Programming
90
CONDITIONAL BRANCH
beq $r1,$r2, L1
go to statement labeled L1 if the value in register $r1 equals the
value in register $r2
beq is branch if equal
C code: if (a == b) goto L1
2011 Sem 1
MIPS Programming
91
UNCONDITIONAL JUMP
j L1
Jump to label L1 unconditionally
C code: goto L1
2011 Sem 1
MIPS Programming
92
IF STATEMENT (1/2)
C statement: if (i == j) f = g + h;
MIPS statements:
bne $s3, $s4, Exit
add $s0, $s1, $s2
Exit:
# if (i!=j) Exit
# f = g + h
2011 Sem 1
# if (i==j) goto L1
# goto Exit
# f = g + h
MIPS Programming
93
IF STATEMENT (2/2)
C statement:
if (i == j) f = g + h;
else f = g - h;
MIPS statements:
bne
add
j
Else: sub
Exit:
true
false
Else:
f = g+h
i==j?
f = g-h
Exit:
MIPS Programming
94
LOOPS (1/2)
2011 Sem 1
MIPS Programming
95
LOOPS (2/2)
Code:
Loop: if (j != k) Exit;
i = i+1;
goto Loop;
Exit:
2011 Sem 1
# if (j!= k) Exit
# repeat loop
MIPS Programming
96
INEQUALITIES (1/2)
We have beq and bne, what about branch-if-lessthan? We do not have a blt instruction.
Use slt (set on less than) or slti.
2011 Sem 1
MIPS Programming
97
INEQUALITIES (2/2)
To build
2011 Sem 1
MIPS Programming
98
Label:
Work by:
1. Calculating address
2. Load data
3. Perform task
2011 Sem 1
MIPS Programming
99
$t8, $zero, 0
$t1, $zero, 0
$t2, $t0, $t1
$t3, 0($t2)
$zero, $t3, L2
$t8, $t8, 1
$t1, $t1, 4
100
$t8, $zero, 0
$t1, $zero, 156
$t2, $t0, $t1
$t3, 0($t2)
$zero, $t3, L2
$t8, $t8, 1
$t1, $t1, 4
$t1, $zero, L1
2011 Sem 1
MIPS Programming
101
$t8, $zero, 0
$t1, $zero, 160
$t2, $t0, $t1
$t3, 4($t2)
$zero, $t3, L2
$t8, $t8, 1
$t1, $t1, 4
$t1, $zero, L1
2011 Sem 1
MIPS Programming
102
Calling procedures
PROCEDURE CALL
jal ProcedureAddress
jal = jump and link
First, PC + 4 is stored in $ra
Then, an unconditional jump is performed to
ProcedureAddress
At the end of the procedure, return with
jr $ra
2011 Sem 1
MIPS Programming
104
REGISTER USE
Registers
MIPS Programming
105
Stack
2011 Sem 1
MIPS Programming
106
2011 Sem 1
MIPS Programming
107
Push
2011 Sem 1
MIPS Programming
108
Pop
2011 Sem 1
MIPS Programming
109
2011 Sem 1
MIPS Programming
110
2011 Sem 1
MIPS Programming
111
2011 Sem 1
MIPS Programming
112
2011 Sem 1
MIPS Programming
113
2011 Sem 1
MIPS Programming
114
2011 Sem 1
MIPS Programming
115
2011 Sem 1
MIPS Programming
116
The jr Instruction
2011 Sem 1
MIPS Programming
117
Calling Convention
2011 Sem 1
MIPS Programming
118
2011 Sem 1
MIPS Programming
119
2011 Sem 1
MIPS Programming
120
Chain of Calls
2011 Sem 1
MIPS Programming
121
Register Problem
Registers $s0$s7 must not be altered by a
subroutine.
This restriction creates a problem when
subroutines call other subroutines.
The solution is to allow a subroutine to use
$s0$s7. However, before using one of these
registers, the subroutine must save its value on
the stack.
Later, when the subroutine returns to its caller, it
must restore the register to its initial state.
2011 Sem 1
MIPS Programming
122
Stack Linkage
2011 Sem 1
MIPS Programming
123
Stack Linkage
2011 Sem 1
MIPS Programming
124
2011 Sem 1
MIPS Programming
125
Frame Pointer
2011 Sem 1
MIPS Programming
126
2011 Sem 1
MIPS Programming
127
2011 Sem 1
MIPS Programming
128
2011 Sem 1
MIPS Programming
129
NON-NESTED PROCEDURE
int leaf_example
(int g, int h, int i, int j) {
int f ;
f = (g+h)-(i+j) ;
return f ;
}
2011 Sem 1
MIPS Programming
130
MIPS TRANSLATION
Leaf_example:
addi $sp,$sp,-12 # room for 3 vars
sw $t1,8($sp)
sw $t0,4($sp)
sw $s0,0($sp)
add $t0,$a0,$a1 # $t0=g+h
add $t1,$a2,$a3 # $t1=i+j
sub $s0,$t0,$t1 # f = $t0-$t1
add $v0,$s0,$zero # returns the result in $v0
lw $s0,0($sp) # restore $s0
lw $t0,4($sp) # restore $t0
lw $t1,8($sp) # restore $t1
add $sp,$sp,12 # restore stack
jr $ra # jump back to calling routine
2011 Sem 1
MIPS Programming
131
NESTED PROCEDURE
2011 Sem 1
MIPS Programming
132
MIPS TRANSLATION
Fact: addi $sp,$sp,8 # make room for 2 items
sw $ra,4($sp) # save the return address
sw $a0,0($sp) # save the argument n
slti $t0,$a0,1 # test for n<1
beq $t0,$zero,L1 # if n>=1, to to L1
addi $v0,$zero,1 # return 1
addi $sp,$sp,8 # pop 2 items off stack
jr $ra # return to after jal
L1:
addi $a0,$a0,-1 # n>=1: argument gets (n-1)
jal fact
lw $a0,0($sp) # return from jal: restore arguments
lw $ra,4($sp)
addi $sp,$sp,8
mul $v0,$a0,$v0 # return n* fact(n-1)
jr $ra # return to caller
2011 Sem 1
MIPS Programming
133
STACK ALLOCATION
High address
$fp
$fp
$sp
Saved argument
registers (if any)
Saved return
address
Saved
registers (if any)
$sp
Activation record /
Stack frame
Low address
2011 Sem 1
MIPS Programming
134
SYSCALL
2011 Sem 1
#
#
#
#
#
MIPS Programming
135
IMPORTANT SYSCALL
2011 Sem 1
MIPS Programming
136
li
$v0, 4
la
$a0, string
syscall
.data
string:
.asciiz
2011 Sem 1
MIPS Programming
137
AN EXAMPLE
Read integer
li
$v0, 5
syscall
#
#
#
#
Print integer
li
$v0, 1
Lw
$a0, int
syscall
2011 Sem 1
#
#
#
#
MIPS Programming
138
MIPS SIMULATOR
Try out PCSpim (MIPS simulator)
ftp://ftp.cs.wisc.edu/pub/spim/pcspim.exe or
http://www.cs.wisc.edu/~larus/spim.html
2011 Sem 1
MIPS Programming
139
MIPS INSTRUCTIONS
CLASSIFICATION
Instructions are classified according to their operands;
instructions with same operand types have same
representation
R-format (Register format)
2011 Sem 1
MIPS Programming
141
R-FORMAT (1/2)
opcode
rs
rt
rd
shamt funct
2011 Sem 1
MIPS Programming
142
R-FORMAT (2/2)
opcode
funct
rs (Source Register)
rt (Target Register)
rd (Destination Register)
shamt
amount a shift instruction will shift by. Shifting a 32-bit word by more than 31 is
useless, so this field is only 5 bits (so it can represent the numbers 0-31)
set to 0 in all but the shift instructions
2011 Sem 1
MIPS Programming
143
MIPS instruction:
add
opcode = 0
funct
= 32
rd
= 8 (destination)
rs
= 9 (first operand)
rt
= 10 (second operand)
shamt
= 0 (not a shift)
2011 Sem 1
MIPS Programming
144
add
instruction:
$8, $9, $10
rs
rt
rd
10
shamt
funct
32
012A 402016
2011 Sem 1
MIPS Programming
145
sll
instruction:
$8, $9, 4
rs
rt
rd
shamt
funct
0009 410016
2011 Sem 1
MIPS Programming
146
I-FORMAT (1/4)
2011 Sem 1
MIPS Programming
147
I-FORMAT (2/4)
Define
opcode
rs
rt
immediate
2011 Sem 1
MIPS Programming
148
I-FORMAT (3/4)
opcode
same as before except that, since there is no funct field,
opcode uniquely specifies an instruction
rs
specifies the source register operand (if any)
rt
specifies register to receive result
note the difference from R-format instructions
2011 Sem 1
MIPS Programming
149
I-FORMAT (4/4)
2011 Sem 1
MIPS Programming
150
MIPS instruction:
addi
opcode
=8
rs
rt
= 21 (target register)
2011 Sem 1
MIPS Programming
151
MIPS instruction:
addi
22
21
-50
22D5 FFCE16
2011 Sem 1
MIPS Programming
152
INSTRUCTION ADDRESS
2011 Sem 1
MIPS Programming
153
BRANCHES: PC-RELATIVE
ADDRESSING (1/5)
Use I-Format
opcode
rs
rt
immediate
2011 Sem 1
MIPS Programming
154
BRANCHES: PC-RELATIVE
ADDRESSING (2/5)
beq $9,$8,End
PC
End
2011 Sem 1
MIPS Programming
155
BRANCHES: PC-RELATIVE
ADDRESSING (3/5)
Solution: specify target address relative to the PC
Let the 16-bit immediate field be a signed twos complement
integer to be added to the PC if we take the branch.
Now we can branch 215 bytes from the PC, which should be
enough to cover almost any loop
End
beq $9,$8,End
PC
End
2011 Sem 1
MIPS Programming
156
BRANCHES: PC-RELATIVE
ADDRESSING (4/5)
2011 Sem 1
MIPS Programming
157
BRANCHES: PC-RELATIVE
ADDRESSING (5/5)
Branch Calculation:
If we dont take the branch:
PC = PC + 4
PC+4 = address of next instruction
Observations
immediate field specifies the number of words to jump,
which is simply the number of instructions to jump
immediate field can be positive or negative.
Due to hardware, add immediate to (PC+4), not to PC; will
be clearer why later in the course
2011 Sem 1
MIPS Programming
158
MIPS Code:
Loop: beq
add
addi
j
End:
2011 Sem 1
= 4 (look up in table)
= 9 (first operand)
= 0 (second operand)
= ???
MIPS Programming
159
MIPS Code:
Loop: beq
add
addi
j
End:
#
#
#
#
#
rel
rel
rel
rel
rel
addr:
addr:
addr:
addr:
addr:
0
4
8
12
16
immediate field:
Number of instructions to add to (or subtract from) the PC,
starting at the instruction following the branch
In beq case, immediate = 3
End = (PC + 4) + (immediate 4)
2011 Sem 1
MIPS Programming
160
MIPS Code:
Loop: beq
add
addi
j
End:
#
#
#
#
#
rel
rel
rel
rel
rel
addr:
addr:
addr:
addr:
addr:
0
4
8
12
16
rs
rt
immediate
00000
0000000000000011
000100
2011 Sem 1
01001
MIPS Programming
161
J-FORMAT (1/5)
2011 Sem 1
MIPS Programming
162
J-FORMAT (2/5)
26 bits
target address
2011 Sem 1
MIPS Programming
163
J-FORMAT (3/5)
Optimization:
Just like with branches, jumps will only jump to wordaligned addresses, so last two bits are always 00 (in binary)
So lets just take this for granted and not even specify them
2011 Sem 1
MIPS Programming
164
J-FORMAT (4/5)
2011 Sem 1
MIPS Programming
165
J-FORMAT (5/5)
Summary:
New PC = { PC[31..28], target address, 00 }
2011 Sem 1
MIPS Programming
166
2011 Sem 1
beq
add
addi
beq
:
PC
Loop: beq $9,$0,End
add $8,$8,$10
addi $9,$9,-1
beq $0, $0, Loop
End:
#Addr: 8
#Addr:12
#Addr:16
#Addr:20
#Addr:24
PC = 8
PC + 4 = 12
Distance between End and (PC+4)
= (24 12) bytes
= 12 bytes
= 3 words
immediate = 3
new PC = (PC+4) + (immediatex4)
= 12 + (3x4) = 24
MIPS Programming
167
PC
2011 Sem 1
beq
add
addi
beq
#Addr: 8
#Addr:12
#Addr:16
#Addr:20
#Addr:24
PC = ?
PC + 4 = ?
Distance between Loop and (PC+4)
=
=
=
immediate =
:
MIPS Programming
168
beq
add
addi
j
Loop: beq
add
addi
j
End:
Loop
#Addr: 8
#Addr:12
#Addr:16
#Addr:20
#Addr:24
0000..01000
immediate =2 (26 bits)
PC
new PC
$9,$0,End
$8,$8,$10
$9,$9,-1
Loop
0000..10100
PC[31..28], immed, 00
MIPS Programming
0000
169
..01000
beq
add
addi
j
:
Loop: beq
add
addi
j
End:
$9,$0,End
$8,$8,$10
$9,$9,-1
Far
#Addr: 8
#Addr:12
#Addr:16
#Addr:20
#Addr:24
0000..10100
Far
0001..01000
MIPS Programming
170
2011 Sem 1
MIPS Programming
171
rs
rd
funct
Register
Immediate
addressing: operand is a
constant within the instruction itself (addi,
andi, ori, slti)
op
2011 Sem 1
rs
rt
immediate
MIPS Programming
172
rs
rt
Register
2011 Sem 1
Memory
Address
+
MIPS Programming
word
173
rs
rt
PC
Memory
Address
+
word
PC
2011 Sem 1
Memory
Address
:
MIPS Programming
word
174
END
2011 Sem 1
MIPS Programming
175