MIPS instructions Shifts Shift instructions move all the

  • Slides: 18
Download presentation
MIPS instructions

MIPS instructions

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 • sll $t 2, $t 0, 2 • Shift right logical (srl) move all the bits to the right • srl $t 2, $t 0, 2 • Filling the emptied bits with 0’s 3/4/2021 CDA 3100 2

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 3/4/2021 CDA 3100 3

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 3/4/2021 CDA 3100 4

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 noting that the number is a signed number. • Overflow happens this time 3/4/2021 CDA 3100 5

Questions • Suppose $t 0 is storing 30, $t 1 is storing 20. After

Questions • Suppose $t 0 is storing 30, $t 1 is storing 20. After the following instructions, what will be the value in $t 2? sub $t 2, $t 0, $t 1 srl $t 2, 2 ori $t 2, 10 (a) 8 (b)10 (c)18 (d) None of the above.

Questions • Suppose word array A stores 0, 1, 2, 3, 4, 5, 6,

Questions • Suppose word array A stores 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, in this order. Assume the starting address of A is in $s 0. After the following instructions, what will be the value in $t 0? addi $s 0, 32 lw $t 0, 4($s 0) andi $t 0, 1 (a) 0 (b) 8 (c) 9 (d) None of the above.

Questions • Assume A is an integer array with 10 elements storing 0, 1,

Questions • Assume A is an integer array with 10 elements storing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Assume the starting address of A is in $s 0 and $t 0 is holding 3. After the running the following code, what will be the content of $t 0? sll $t 0, 3 add $t 0, $s 0, $t 0 lw $t 0, 0($t 0) srl $t 0, 1 (a) 3 (b) 1 (c) 0 (d) None of the above.

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 3/4/2021 CDA 3100 9

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

Instruction beq (branch if equal) • To support decision making, MIPS has conditional branch instruction: beq register 1, register 2, L 1 Equivalent to the following in C: if (register 1 == register 2) goto L 1; In other words, if the two registers have the same value, go to L 1; otherwise, nothing happens • L 1 is a label, which can be considered as the name of the address of an instruction. – An instruction is also stored in the memory, and of course has an address – The address is 32 bits, but is impossible to know or be remembered by the programmers, so we create labels to refer to the addresses – We can write lines like to introduce a label for an instruction: L 1: add $t 0, $t 1, $t 2 – Go to a label means that fetch that instruction at the address associated with a label from the memory and execute the instruction – An address can be associated with more than one label • Keep in mind that if the condition is not true, nothing happens, meaning that the processor will just execute the instruction appearing immediately after the beq in your program 3/4/2021 CDA 3100 10

Instruction bne • Similarly, bne (branch not equal) means go to L 1 if

Instruction bne • Similarly, bne (branch not equal) means go to L 1 if the value in register 1 does not equal to the value in regster 2 bne register 1, register 2, L 1 3/4/2021 CDA 3100 11

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: j L 1 Jump to the instruction labeled with L 1 3/4/2021 CDA 3100 12

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? 3/4/2021 CDA 3100 13

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? 3/4/2021 CDA 3100 14

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? 3/4/2021 CDA 3100 15

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 3/4/2021 CDA 3100 16

Questions • If $t 0 is holding 17, $t 1 is holding 8, what

Questions • If $t 0 is holding 17, $t 1 is holding 8, what will be the value stored in $t 2 after the following instructions? andi $t 0, 3 beq $t 0, $0, L 1 addi $t 0, 1 L 1: add $t 2, $t 0, $t 1 (a) 10. (b) 8. (c) 2. (d) None of the above.

In Class Exercise • If-Else

In Class Exercise • If-Else