## Lab3 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)

• [1] (2.1) Where are main, loop, done, N and Sum located (i.e. what address)? Hint: Assemble and use Settings > Show Labels Window in the Execute view.

• [1] (2.4) How many instructions were actually executed? What is the final value of Sum?

• [2] (2.6) How many instructions does your modified program execute when N is equal to 5? Can this number be improved? If so, how? Hint: If your modified program executes 5 or more additional instructions, you can do better.

• [2] (2.7) Will your modified program work if N is less than 0? You do not need to make any additional modifications.

## Swap (p3)

• [1] (3.3) What is the value of max and maxindex at the end of the program?

• [2] (3.4) Comment out slli x10, x7, 2 and rerun the program. What happens? Why?

• [2] (3.7) 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 of A?

• [2] (3.8) Like p2.asm this program doesn't work if N 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? Is there a more robust way to set the maximum value, instead of using the magic constant of -1?

## Sort (p4)

• [2] (4.11) If SwapMaxWithLast needed to return a value how would that be accomplished?

• [2] (4.12) What changes would you need to make if SwapMaxWithLast needed more than 8 arguments?

• [2] (4.13) What changes would you need to make if SwapMaxWithLast called another procedure?

• [2] (4.14) The code in p4-loop.asm works almost like a procedure. Describe the changes you would make to convert the p4-loop.asm code into a sort procedure which is called and which in turn calls SwapMaxWithLast.

## Fib

• [3] (5.3) fib in fib.asm uses s registers to store both n and fib(n ? 1). Could t registers be used? Which would you choose?

• [2] (5.4) While calculating fib(4), how many times is fib called?

• [2] (5.5) In your fib code, you should have two internal calls to fib. Set a breakpoint at the line AFTER the first internal call to fib. Then, while calculating fib(4), observe the state of the stack when the breakpoint is hit (e.g. when recursion using the first jal 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.

• [2(5.5) What happens if fib does not restore the return address before using jalr x0, 0(ra)?

Draw your stack here: