Professional Documents
Culture Documents
01-02-2021
1
Summary of MIPS
Addressing Modes
2
Assembly for Memory Operand: Example 1
C code:
g = h + A[8];
g in $s1, h in $s2, base address of A in $s3
MIPS assembly code: … …
12
Index 8 requires offset of 32
8
4 bytes per word
4
lw $t0, 32($s3) # load word 0
add $s1, $s2, $t0
base
offset base register 3
Load and Store
•
All arithmetic instructions operate on registers
•
Memory is accessed through load and store instructions
•
Consider C code: A[12] = h + A[8];
Assume that $s3 contains the base address of A
•
$s2 contains h.
Write MIPS code:
lw $t0, 32($s3)
add $t0, $t0, $s2
sw $t0, 48($s3)
•
Note: arithmetic operands are registers, not memory!
invalid: add 48($s3), $s2, 32($s3)
4
Registers vs. Memory
Registers are faster to
access than memory
When data in memory:
Requires loads and stores for the data
Also, more instructions need to be executed
A compiler must use registers for storing
variables as much as possible:
Only spill to memory for less frequently used variables
Register optimization is important!
5
Immediate Operands
Constant data specified in an instruction:
addi $s3, $s3, 4
Can also use a negative constant
addi $s2, $s1, -1
Recollect Important Principle: Make the
common case fast:
Small constants are much more common
Immediate operand avoids a load instruction
6
Unsigned Binary Integers
Given an n-bit number
n 1 n2
x x n1 2 x n2 2 x1 2 x 0 2
1 0
Range: 0 to +2n – 1
Example:
0000 0000 0000 0000 0000 0000 0000 1011 2
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
9
Signed Negation
Complement each bit and add 1.
Complement means 1 → 0, 0 → 1
x x 1111...111 2 1
x 1 x
Example: negate +2
+2 = 0000 0000 … 00102
–2 = 1111 1111 … 11012 + 1
= 1111 1111 … 11102
10
Sign Extension
Representing a number using more bits:
Taking care to preserve the numeric value
In MIPS instruction set:
addi: extend immediate value
lb, lh: extend loaded byte/halfword
beq, bne: extend the displacement
Replicate the sign bit to the left.
For unsigned values: extend with 0s
Examples: 8-bit to 16-bit
+2: 0000 0010 => 0000 0000 0000 0010
–2: 1111 1110 => 1111 1111 1111 1110
11
Logical Operations
•
Instructions for bitwise manipulation
13
OR Operation
•
Useful to include bits in a word
– Set some bits to 1, leave others unchanged
14
NOT Operation
•
Useful to invert bits in a word
– Change 0 to 1, and 1 to 0
•
MIPS has NOR --- a 3-operand instruction instead…
– a NOR b == NOT ( a OR b )
slt rd, rs, rt
if (rs < rt) rd = 1; else rd = 0;
slti rt, rs, constant
if (rs < constant) rt = 1; else rt = 0;
16
Exercise
Assume a, b, i in $s1, $s2, $s3. Write code for
the C program segement:
if(a<b) i=i+1; else i=i-1; $s3 i
Hint: Use slt along with bne $s2 b
slt $t0, $s1, $s2 # if($s1<$s2) $s1 a
bne $t0, $zero, else
addi $s3,$s3,1
j exit
else: addi $s3,$s3,-1
exit:
17
Recap
Instruction Meaning
add $s1, $s2, $s3 $s1 = $s2 + $s3
sub $s1, $s2, $s3 $s1 = $s2 – $s3
lw $s1, 100($s2) $s1=Memory[$s2+100]
18
ALU Instructions: Format
R-type op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
I-type op rs rt address
op: basic operation, also called opcode
rs: the first register source operand
rt:
R-type: the second register source operand
I-type: destination register
rd: the register destination operand
shamt: shift amount in shift instructions
funct: selects the specific variant of the operation in op field, also
called function code
address: offset of memory address in data transfer instructions
19
MIPS Instruction Formats
R-format (Register-Register): Arithmetic operations
31 26 25 21 20 16 15 11 10 6 5 0
Op Rs Rt Rd Shamt Funct
Op Rs Rd immediate
I-format (Load/Store)
31 26 25 21 20 16 15 0
Op Rs Rt immediate
Op target 20
Examples of Instruction Format
op rs rt rd shamt funct
op rs rd address
op address
6-bits 26-bits
•
Example: j 10000
2 10000
6-bits 26-bits
22