Follow a return stack through calls three deep. Say what it holds, what each return gets from it, and what a stack that is too small loses.
Hardware context: Test code that replays the branches, calls and returns which the real core ap_rv32 executed, with a tournament of a 2-bit table and gshare, a branch target buffer and a return stack of several depths. Choosing and sizing the parts of a branch predictor under a bit budget
A program runs the same steps again and again. The addresses are in hex. Step 1: at 08, main calls the function a. Step 2: at 24, a calls b. Step 3: at 34, b calls c. Step 4: c returns. Step 5: b returns. Step 6: a returns. Step 7: at 10, main calls a once more. Steps 8 to 12 are steps 2 to 6 again.
This worksheet uses named constants. The first three entries show how to record your decisions.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
question | 8 | input of each check |
Full specification
Every call is a jal that keeps its return address, which is its own address plus 4. A call pushes that address on the return stack. A return pops one address.
A stack that is full loses its oldest address when another is pushed. A return that finds the stack empty uses the branch target buffer. That holds where this same return went the last time.
The branch target buffer must have seen a return once before the fetch knows that it is a return. So the first time that a return is met, it is fetched wrong.
One round has six returns. Enter every value in decimal.
| Entry | Your decision |
|---|---|
| PUSHED_BY_STEP_1 | Which address does step 1 push? Enter it in decimal. |
| HELD_AFTER_STEP_3 | How many addresses does a stack of depth 4 hold after step 3? |
| TOP_AFTER_STEP_3 | Which address is on top after step 3? Enter it in decimal. |
| HELD_AFTER_STEP_6 | How many addresses does it hold after step 6? |
| TARGETS_OF_THE_RETURN_OF_A | To how many different addresses does the return of a go, in one round? |
| RIGHT_WITHOUT_A_STACK | From the third round on: how many of the six returns of a round does the target buffer alone get right? |
| RIGHT_WITH_DEPTH_4 | From the third round on: how many does a stack of depth 4 get right? |
| LOST_WITH_DEPTH_2 | A stack of depth 2: which address does it lose at step 3? Enter it in decimal. |
| RIGHT_WITH_DEPTH_2 | From the third round on: how many of the six returns does a stack of depth 2 get right? |
| RIGHT_WITH_DEPTH_3 | And a stack of depth 3? |
| RIGHT_IN_ROUND_1_WITH_DEPTH_4 | In the very first round, how many of the six returns does a stack of depth 4 get right? |
| RIGHT_WITH_DEPTH_4_FOR_K | The twelve steps run K times. Give the number of returns that a stack of depth 4 gets right, as an expression in K. |
Examples
Example 1
PUSHED BY STEP 1- Input
Which address does step 1 push? Enter it in decimal.
- Output
matches = 1
08 plus 4 is 0c in hex, which is 12.
Example 2
HELD AFTER STEP 3- Input
How many addresses does a stack of depth 4 hold after step 3?
- Output
matches = 1
Three calls, and no return yet.
Example 3
TOP AFTER STEP 3- Input
Which address is on top after step 3? Enter it in decimal.
- Output
matches = 1
The call at 34 pushed 38 in hex, which is 56.
Constraints and Contract
- Keep the parameter and named constants. Use the numeric codes and units on this page.
- For RIGHT_WITH_DEPTH_4_FOR_K, enter a constant expression using K. Only this entry changes with the parameter. The supported integer range is 3 through 30, inclusive.
Hints
Hint 1
Write the stack after each of the twelve steps: a push adds on top, a pop removes the top.
Hint 2
For a small stack, cross out the oldest address when a push finds it full.
Hint 3
A return that finds no address takes the last target of that same return from the target buffer.
What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.
12 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 1 problem on this path