Title: MIPS%20instructions
1MIPS instructions
2Outline
- MIPS Instructions continued
- Logical operations
- Instructions for making decisions
3MIPS Instructions So Far
- Arithmetic instructions
- Each MIPS arithmetic instruction performs only
one operation and has three operands - All operands from registers
- Or one operand is an immediate (constant)
- Data transfer instructions
- Load from memory or store a register value to
memory - How to implement Ai using MIPS instructions?
- Instructions are encoded using 0s and 1s
- They are stored in memory along with data
- Stored-program concept
4Logical Operations
- Often we need to operate on bit fields within a
word - For example, how to access a byte of word
- How to extract op code from an instruction?
- We need logical operations
- Which allow us to pack and unpack bits into words
and perform logical operations such as logical
and, logical or, and logical negation
5Shifts
- Shift instructions move all the bits in a word to
the left or to the right - Shift left logical (sll) move all the bits to the
left by the specified number of bits - Shift right logical (srl) move all the bits to
the right - Filling the emptied bits with 0s
6Example
- Suppose register s0 (16) is 9ten
- What do we have in t2 (10) after
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
7Example
- Suppose register s0 (16) is 9ten
- We have in t2 (10) after
- The value is 144ten 9ten ? 24
- In general, shifting left by i bits gives the
same result as multiplying by 2i
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0
8Example
- Suppose register s0 (16) is 9ten
- We have in t2 (10) after
- The value is NOT 9ten ? 228
- Note that overflow happens this time
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9How to Extract Opcode and Other Fields
- We want to use the minimum number of instructions
- Extract the opcode?
- Shift the instruction to right by 26 bits
- How about rs?
10How to Extract Opcode and Other Fields
- We want to use the minimum number of instructions
- Extract the opcode?
- Shift the instruction to the right by 26 bits
- How about rs?
- Shift the instruction to the left by 6 bits
- Then shift the result to the right by 27 bits
11Shift Left Logical Instruction Encoding
31
26
25
21
20
16
15
11
10
6
5
0
opcode
rs
rt
rd
shamt
funct
rd
shamt
sll 10, 16, 4
rt
31
26
25
21
20
16
15
11
10
6
5
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
opcode
rs
rt
rd
shamt
funct
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
Encoding 0x00105100
12Bit-wise AND
- Apply AND bit by bit
- The resulting bit is 1 if both of the input bits
are 1 and zero otherwise - There is also a version of AND with an immediate
- Note that the immediate is treated as an unsigned
16-bit number - In other words, the immediate is zero-extended to
a 32-bit number
13Bit-wise OR
- Apply OR bit by bit
- The resulting bit is 1 if at least one of the
input bits is 1 and zero otherwise - There are also two versions of OR
- As in andi, the immediate is treated as an
unsigned 16-bit number
14NOT
- Since NOT takes one operand and results in one
operand, it is not included in MIPS as an
instruction - Because in MIPS each arithmetic operation takes
exactly three operands - Instead, NOR is included
- The resulting bit is 0 if at least one of the
input bits is 1 - How to implement NOT using NOR?
- Using zero as one of the input operands
- It is included in MIPS as a pseudoinstruction
15Instructions for Making Decisions
- A distinctive feature of programs is that they
can make different decisions based on the input
data
16Instruction beq (branch if equal)
- To support decision making, MIPS has two
conditional branch instructions, similar to an
if statement with a goto - In C, it is equivalent to
- Note that L1 is a label and we are comparing
values in register1 and register2
17Instruction bne
- Similarly, bne (branch not equal) means go to the
statement labeled with L1 if the value in
register1 does not equal to the value in regster2 - Equivalent to
18Instruction j (jump)
- MIPS has also an unconditional branch, equivalent
to goto in C - Jump to the instruction labeled with L1
19Compiling if-then-else
- Suppose variables f, g, h, i, and j are in
registers s0 through s4, how to implement the
following in MIPS?
20Compiling if-then-else
- Suppose variables f, g, h, i, and j are in
registers s0 through s4, how to implement the
following in MIPS?
21Compiling if-then-else
- Suppose variables f, g, h, i, and j are in
registers s0 through s4, how to implement the
following in MIPS?
22MIPS Assembly for if-then-else
- Now it is straightforward to translate the C
program into MIPS assembly