S2.1.2 Write the Loop

EasyImplement20 minIcarus Verilog · serverReview the topic

Write a loop in RV32I instructions that adds every word of an array, and leaves the sum in x12.

Hardware context: A small model of an RV32I processor as test code: 32 registers, a PC and data memory, running a program given as rows. Reading a processor trace and writing short RV32I programs

An array of words lies in memory. Register x10 holds the address of its first word. Register x11 holds the number of words, which is 1 or more.

Write a program that adds all the words and leaves the sum in x12. The program starts at address 0, and must end at an ebreak.

You write the program as rows, one for each instruction, as in the lab of the lesson. The same loop must work for arrays of different lengths and values.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
The rows you are likely to need
The other kinds of the lab work too. A branch target is written in hex: 'h4 is the second row.
RowAssemblyMeaning
i(ADDI, a, b, c)addi xa, xb, cxa = xb + c. The number c may be negative.
i(ADD, a, b, c)add xa, xb, xcxa = xb + xc
i(LW, a, b, c)lw xa, c(xb)xa = the word at the address xb + c
i(BNE, a, b, c)bne xa, xb, cif xa is not equal to xb, the next instruction is the row at address c
i(EBREAK, 0, 0, 0)ebreakstop

How to read a result: stopped_at_ebreak is 1 when your program reached an ebreak within 4000 instructions. memory_unchanged is 1 when no word of memory was written. x12 is the sum, in decimal.

Examples

Example 1

Three Words
Input
three small words at 8000_0000
Output
stopped_at_ebreak = 1, memory_unchanged = 1, x12 = 12

The three words are added, and nothing in memory has changed.

Example 2

One Word
Input
one word at 8000_0040
Output
stopped_at_ebreak = 1, memory_unchanged = 1, x12 = 27

An array of one word: the sum is that word.

Example 3

Five Words
Input
five words at 8000_0100
Output
stopped_at_ebreak = 1, memory_unchanged = 1, x12 = 2477

Five turns of the loop. The words on both sides of the array are not added.

Constraints and Contract

  • When the program stops, x12 holds the sum of the x11 words that start at the address in x10. A sum that does not fit in 32 bits wraps round.
  • The program must not write memory. It may change any register.
  • At the start, every register except x0, x10 and x11 holds a leftover value. Do not expect a 0 anywhere but in x0.
  • The program has at most 64 rows.

Hints

Hint 1

Decide which three registers hold the sum, the address of the next word, and the words still to add.

Hint 2

One word is four bytes. By how much must the address grow in each turn?

Hint 3

What does x12 hold before your first instruction? Read the contract again.

What the grader checks: Arrays of 1 to 300 words: at the bottom, in the middle and in the last word of the memory. Words of 24 bits and small words, and 14 random arrays. For each array the bench checks the sum, that the program stopped at an ebreak, and that no word of memory changed. The words just below and just above the array hold values that a wrong loop would add.

32 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 2 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Three Words

Input
three small words at 8000_0000
Expected output
stopped_at_ebreak = 1, memory_unchanged = 1, x12 = 12

The three words are added, and nothing in memory has changed.

Run samples executes all 3 public checks. Check all tests runs 32 checks on the server.

Icarus Verilog · server