Practical 3 questions
Name:
Section:
Name:
Section:
Read and perform the lab guide posted on the course website. Answer the questions in the lab guide as you get to them in the spaces below. Numbers in square brackets (e.g. []) indicate the point value of each question. The annotations in parentheses refer to the step of the lab guide.
Sum (p2)
-
[2] (Need) Where are
main
,loop
,done
,N
andSum
located (i.e. what address)? Hint: Assemble and use Settings > Show Labels Window in the Execute view. -
[2] (Performance) How many instruction does your modified program execute when
N
is equal to 5? Can this be improved? If so, how? -
[4] (Correctness) Will your modified program work if
N
is less than 0? You do not need to make any additional modifications.
Swap (p3)
-
[2] (Correctness) What is the value of max and maxindex at the end of the program?
-
[4] (Correctness) Comment out
slli x10, x7, 2
and rerun the program. What happens? Compare the address table between the program with and withoutslli
. Which is correct and why? -
[4] (Need/Correctness) If you repeatedly apply your modified program to the subarrays of
A
from 0 to \( N-i \) where \(i\) is the number of times you've applied your program, what is the final state ofA
? -
[4] (Iteration/Correctness) Like
p2.asm
this program doesn't work ifN
is equal to 0. It is brittle in other ways as well. For example, what happens if all of the elements are less than -1? What if they are all -2^{31}, the max negative value? Is there a more robust way to ensure the max value is identified?
Sort (p4)
-
[4] (Need) If
SwapMaxWithLast
needed to return a value how would that be accomplished? What if it needed to return more than 2 values? -
[4] (Iteration) What changes would you need to make if
SwapMaxWithLast
called another procedure in order to follow procedure calling conventions? What changes would you need to make top4-loop.asm
if the entirety ofp4-loop.asm
needed to be converted into a procedure (which in turn callsSwaMaxWithLast
itself)?
Fib (p5)
-
[6] (Need) Look at the registers being used and notice that
s
registers are being used to store bothn
andfib(n-1)
. Couldt
registers be used instead? Which would you choose and explain your choice. -
[4] (Correctness) When you have correctly implemented the calling conventions run
fib.asm
. Does it behave as expected? If you callfib(4)
, how many times isfib
recursively called? -
[6] (Iteration) In your
fib
code, you should have two internal calls tofib
. Set a breakpoint at the line AFTER the first internal call tofib
. Then, while calculatingfib(4)
, observe the state of the stack when the breakpoint is hit (e.g. when recursion using the firstjal fib
is complete). Then, draw the state of your stack on the next page. Draw the stack as a rectangle, indicate each stack frame, and what register and value is present in each word. -
[4] (Correctness) What happens if
fib
does not restore the return address before usingjalr x0, 0(ra)
?
Draw your stack here: