MIPS instructions Outline MIPS Instructions continued Logical operations

  • Slides: 22
Download presentation
MIPS instructions

MIPS instructions

Outline • MIPS Instructions – continued – Logical operations – Instructions for making decisions

Outline • MIPS Instructions – continued – Logical operations – Instructions for making decisions 1/30/2022 CDA 3100 2

MIPS Instructions So Far • Arithmetic instructions – Each MIPS arithmetic instruction performs only

MIPS 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 A[i] using MIPS instructions? • Instructions are encoded using 0’s and 1’s – They are stored in memory along with data – Stored-program concept 1/30/2022 CDA 3100 3

Logical Operations • Often we need to operate on bit fields within a word

Logical 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 1/30/2022 CDA 3100 4

Shifts • Shift instructions move all the bits in a word to the left

Shifts • 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 0’s 1/30/2022 CDA 3100 5

Example • Suppose register $s 0 ($16) is 9 ten 31 30 29 28

Example • Suppose register $s 0 ($16) is 9 ten 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 1 0 0 1 – What do we have in $t 2 ($10) after 1/30/2022 CDA 3100 6

Example • Suppose register $s 0 ($16) is 9 ten 31 30 29 28

Example • Suppose register $s 0 ($16) is 9 ten 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 1 0 0 1 – We have in $t 2 ($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 1 0 0 0 0 • The value is 144 ten = 9 ten 24 – In general, shifting left by i bits gives the same result as multiplying by 2 i 1/30/2022 CDA 3100 7

Example • Suppose register $s 0 ($16) is 9 ten 31 30 29 28

Example • Suppose register $s 0 ($16) is 9 ten 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 1 0 0 1 – We have in $t 2 ($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 • The value is NOT 9 ten 228 – Note that overflow happens this time 1/30/2022 CDA 3100 8

How to Extract Opcode and Other Fields • We want to use the minimum

How 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? 31 26 25 opcode 1/30/2022 21 20 rs 16 15 rt 11 10 rd CDA 3100 6 shamt 5 0 funct 9

How to Extract Opcode and Other Fields • We want to use the minimum

How 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 31 26 25 opcode 1/30/2022 21 20 rs 16 15 rt 11 10 rd CDA 3100 6 shamt 5 0 funct 10

Shift Left Logical Instruction Encoding 31 26 25 opcode 21 20 rs 16 15

Shift Left Logical Instruction Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10 rd 6 5 shamt 0 funct rd shamt sll $10, $16, 4 rt 31 26 25 21 20 16 15 11 10 6 5 0 0 0 1 0 0 0 1 0 0 0 0 opcode rs rt rd shamt funct 0 0 0 1 0 0 0 1 0 0 0 0 Encoding = 0 x 00105100 1/30/2022 CDA 3100 11

Bit-wise AND • Apply AND bit by bit – The resulting bit is 1

Bit-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 1/30/2022 CDA 3100 12

Bit-wise OR • Apply OR bit by bit – The resulting bit is 1

Bit-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 also two versions of OR • As in andi, the immediate is treated as an unsigned 16 -bit number 1/30/2022 CDA 3100 13

NOT • Since NOT takes one operand results in one operand, it is not

NOT • Since NOT takes one operand 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 1/30/2022 CDA 3100 14

Instructions for Making Decisions • A distinctive feature of programs is that they can

Instructions for Making Decisions • A distinctive feature of programs is that they can make different decisions based on the input data 1/30/2022 CDA 3100 15

Instruction beq (branch if equal) • To support decision making, MIPS has two conditional

Instruction 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 L 1 is a label and we are comparing values in register 1 and register 2 1/30/2022 CDA 3100 16

Instruction bne • Similarly, bne (branch not equal) means go to the statement labeled

Instruction bne • Similarly, bne (branch not equal) means go to the statement labeled with L 1 if the value in register 1 does not equal to the value in regster 2 – Equivalent to 1/30/2022 CDA 3100 17

Instruction j (jump) • MIPS has also an unconditional branch, equivalent to goto in

Instruction j (jump) • MIPS has also an unconditional branch, equivalent to goto in C – Jump to the instruction labeled with L 1 1/30/2022 CDA 3100 18

Compiling if-then-else • Suppose variables f, g, h, i, and j are in registers

Compiling if-then-else • Suppose variables f, g, h, i, and j are in registers $s 0 through $s 4, how to implement the following in MIPS? 1/30/2022 CDA 3100 19

Compiling if-then-else • Suppose variables f, g, h, i, and j are in registers

Compiling if-then-else • Suppose variables f, g, h, i, and j are in registers $s 0 through $s 4, how to implement the following in MIPS? 1/30/2022 CDA 3100 20

Compiling if-then-else • Suppose variables f, g, h, i, and j are in registers

Compiling if-then-else • Suppose variables f, g, h, i, and j are in registers $s 0 through $s 4, how to implement the following in MIPS? 1/30/2022 CDA 3100 21

MIPS Assembly for if-then-else • Now it is straightforward to translate the C program

MIPS Assembly for if-then-else • Now it is straightforward to translate the C program into MIPS assembly 1/30/2022 CDA 3100 22