









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Unless otherwise noted, the following problems are from the Patterson & Hennessy textbook (4th ed.).
Chapter 2: Exercise 2.4. Part (b) only (i.e., 2.4.1b-2.4.6b):
Parts 2.4.1-3 deal with translating from C to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.
f = g - A[B[4]];
For the C statement above, what is the corresponding MIPS assembly code?
lw $s0, 16($s7) //f = B[4] sll $s0, $s0, 2 //f = f4 = B[4] add $s0, $s0, $s6 //f = &A[f] = &A[B[4]] lw $s0, 0($s0) //f = A[f] = A[B[4]] sub $s0, $s1, $s0 //f = g-f = g-A[B[4]]
For the C statement above, how many MIPS assembly instructions are needed?
5 instructions
For the C statement above, how many different registers are needed?
4 registers
Parts 2.4.4-6 deal with translating from MIPS to C. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.
lw $s0, 4($s6)
For the MIPS assembly instructions above, what is the corresponding C statement?
f = A[1]
For the MIPS assembly instructions above, rewrite the assembly code to minimize the number of MIPS instructions (if possible) needed to carry out the same function.
No minimization possible.
How many registers are needed to carry out the MIPS assembly as written above? If you could rewrite the code above, what is the minimal number of registers needed?
2 registers, cannot be minimized.
3 Problem 3
New instructions: Implement register indirect conditional branches (beqr and bner) as pseudo-instructions. Give a proposal for adding them to the ISA (i.e., describe how they could be encoded in the I-Type, R-Type, or J-Type format, and propose an opcode for them (use Figure B.10.2)). Give a (brief) argument for or against their inclusion.
One way to implement this for beqr $s0, $s1, $s2:
bne $s0, $s1, skip # if (s0==s1) skip branch add $zero, $zero, $zero # nop jr $s2 # branch to $s add $zero, $zero, $zero # nop skip:
bner is the same, but use beq instead for the first line.
We can encode this instruction as an R-Type instruction. As a R-Type instruction, the opcode would be 0, and we can use any funct value that has not yet been assigned.
Arguments for:
Arguments against:
4 Problem 4
Fibonacci sequence: Write MIPS assembly to compute the Nth Fibonacci number. Assume N is passed to your function in register $a0. Your output should be in register $v0 at the end of your function. Submit your code and a screenshot of Spim that shows the registers with correct output value for N=10, i.e., Fib(10) = 55 = 0x37. Note: You must implement Fibonacci recursively. The purpose of this assignment is to learn how to manipulate the stack correctly in MIPS.
One implementation of the fib function:
.text .globl __start
__start: or $a0, $zero, $s0 # Call fib(N) jal fib or $zero, $zero, $zero # nop in delay slot or $s0, $zero, $v0 # Save the returned value of fib(N)
addiu $v0, $zero, 10 # Prepare to exit (system call 10) syscall # Exit
fib:
addi $sp, $sp, - sw $ra, 0($sp)
or $v0, $zero, $zero beq $a0, $zero, end or $zero, $zero, $zero # nop in delay slot
ori $v0, $zero, 1 beq $a0, $v0, end or $zero, $zero, $zero # nop in delay slot
5 Problem 5
Performance Processors: The table below describes the performance of two processors, the rAlpha and the c86, and two compilers on a common ’benchmark’ program.
Compiler A Compiler B GHz Cost Instructions Average CPI Instructions Average CPI rAlpha 3.4 $100 7000 1.2 5000 1. c86 2.6 $100 1500 2.2 1000 4.
Which is the best compiler-machine combination?
Using the execution time of the program as the performance metric, where
seconds program
instructions program
clock cycles instruction
seconds clock cycle
Compiler A Compiler B
rAlpha 7000 ∗^1.^2
c86 1500 ∗^2.^2
Both hardware vendors release quad-core versions of the processor for 1.7x the cost of the single core version. The results on the two new systems:
Compiler A Compiler B GHz Cost Instructions Average CPI Instructions Average CPI rAlphaX 3.4 $170 2250/core 1.5 1750/core 1. c86x4 2.6 $170 625/core 3.2 500/core 5.
Assume the benchmarks used are perfectly parallelizable i.e. the cores process equal parts of the program independently and at the same time. Now which combination performs best?
Using the same performance metric as 5a.,
Compiler A Compiler B
rAlpha 2250 ∗^1.^5
c86 625 ∗^3.^2
Using the metric (Cycles/s)*Cores/(Dollars^2 ), which is the best machine? Is this metric useful?
rAlphaX:
cyclescore secondsdollars^2
c86x4:
cyclescore secondsdollars^2
Using this metric, rAlpha has the highest value. However, this metric is not very useful because 5b. shows that the c86x4 can run programs faster with the same number of cores and cost. It is not indicative of a good computer for its price.
The four processors have the following power ratings:
rAlpha 20 W c86 35 W rAlphaX 50 W c86x4 80 W
If you wanted to show that the first processor had the best power efficiency for performance, what metric would you use? For this part, use values only from Compiler A.
Either 1 latency∗power^2
would be better metrics. If power is not valued more, the metric does not show rAlpha as the best processor.
If the number of instructions executed in a certain program is divided equally among the classes of instructions except for class E, which occurs twice as often as each of the others: Which computer is faster? How much faster is it?
P2 performance P1 performance
P1 execution time P2 execution time
P2 is 1.031 faster than P1.
The table below shows instruction-type breakdown for different programs. Using this data, you will be exploring the performance tradeoffs with different changes made to a MIPS processor.
Instructions Program Compute Load Store Branch Total Program 4 1500 300 100 100 1750
Assuming that computes take 1 cycle, loads and store instructions take 10 cycles, and branches take 3 cycles, find the execution time of this program on a 3 GHz MIPS processor.
Assuming that computes take 1 cycle, loads and store instructions take 2 cycles, and branches take 3 cycles, find the execution time of this program on a 3 GHz MIPS processor.
Assuming that computes take 1 cycle, loads and store instructions take 2 cycles, and branches take 3 cycles, what is the speed-up of a program if the number of compute instructions can be reduced by one-half?
Speedup:
Old execution time New execution time
Execution time in a multiprocessor system can be split into computing time for the routines plus routing time spent sending data from one processor to another. Consider the execution time and routing time given in the following table.
2 20 78 9 65 4 11 4 12 44 4 34 2 13 8 1 23 3 19 3 17 16 4 13 1 10 2 22 32 2 5 1 5 1 23 64 1 3 0.5 1 1 26
For each doubling of the number of processors, determine the ratio of new to old computing time and the ratio of new to old routing time.
8 Problem 8
MIPS ISA: Exercises 2.12.1 - 2.12.3. Parts c, d. are not from the book!
In the following problems, you will investigate the impact of certain modifications to the MIPS ISA.
If the instruction set of the MIPS processor is modified, the instruction format must also be changed. For each of the suggested changes, show the size of the bit fields of an R-type format instruction. What is the total number of bits needed for each instruction?
a. 8 Registers
26 bits.
opcode (6) rs (3) rt (3) rd (3) shamt (5) funct(6)
b. 10 bit immediate constants
No change.
c. 128 Registers
38 bits.
opcode (6) rs (7) rt (7) rd (7) shamt (5) funct(6)
d. All arithmetic instructions can use base addressing mode with the last argument (Example: add $a0, $a2, 0[$t1])
48 bits.
opcode (6) rs (5) rt (5) rd (5) shamt (5) funct(6) immediate (16)
R-type instructions use the opcode 0 and the funct to denote the operation. Using an unused opcode (there are many) and the same funct combinations, all instructions with base addressing can be added. The length of the immediate was unspecified -- 16 is a good choice since it is a half-word, and the size used in I-type instructions.
However, no instruction that uses the shamt field can have base addressing as asked in the question, so it is possible to use the shamt field and allow only 5-bit offsets and retain the instruction length.
If the instruction set of the MIPS processor is modified, the instruction format must also be changed. For each of the suggested changes, show the size of the bit fields of an I-type format instruction. What is the total number of bits needed for each instruction?
d. All arithmetic instructions can use base addressing mode with the last argument (Example: add $a0, $a2, 0[$t1])
Two acceptable answers:
opcode (6) rs (5) rt (5) immediate (16) rt2(5)
Why could the suggested change decrease the size of a MIPS assembly program? Why could the suggested change increase the size of a MIPS assembly program?
a. 8 Registers
Smaller instruction encoding may mean smaller programs -- this may be difficult in practice since the reductions aren’t byte-aligned, so addressing subsequent instructions is difficult. It also breaks the MIPS tenet of fixed-length instructions. Programs may increase in size because fewer registers means more data will ’spill’ into memory, leading to more load/store instructions.
b. 10 bit immediate constants
Smaller instruction encoding may mean smaller programs -- this may be difficult in practice since the reductions aren’t byte-aligned, so addressing subsequent instructions is difficult. It also breaks the MIPS tenet of fixed-length instructions. Programs may increase in size because to use immediate numbers larger than 10-bits, you need additional shift/or instructions.
c. 128 Registers
Longer instruction-encoding means longer programs that do the same operations. The instruction formats may be even larger than indicated above since the increases aren’t byte-aligned and addressing subsequent instructions is difficult. It also breaks the MIPS tenet of fixed-length instructions. Programs may decrease in size because more registers means you can ’hold’ onto more data in the processor without going out to memory, leading to fewer load/store instructions.
d. All arithmetic instructions can use base addressing mode with the last argument (Example: add $a0, $a2, 0[$t1])
A larger instruction format means programs that cannot make use of the new addressing mode will be longer. This may be further exacerbated by extending I and J-type instructions to 48-bits as well, to keep instructions fixed-length. However, based-addressing allows many load operations to be combined into an arithmetic instruction -- this is a reduction from 2*32 bytes to 48 bytes for each such reduction. A program that heavily uses this might grow
smaller. If I and J-types are made 48-bytes as well, 32-bit immediate constants and longer jump offsets may also contribute to reducing the program length (though unlikely).
If the shamt field is used, the program will not grow larger with this addition, though it may grow smaller. However, adding base-addressing still adds complexity to the processor that may slow the program down. It is also less useful with just 5-bit offsets.