Skip to content

Commit 5d4f2a6

Browse files
Add proverb exercise (#296)
1 parent aaab18b commit 5d4f2a6

File tree

9 files changed

+367
-8
lines changed

9 files changed

+367
-8
lines changed

config.json

+16-8
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@
386386
"prerequisites": [],
387387
"difficulty": 5
388388
},
389+
{
390+
"slug": "bottle-song",
391+
"name": "Bottle Song",
392+
"uuid": "b6d90e4b-9f17-459b-aff3-2716e49ccb9c",
393+
"practices": [],
394+
"prerequisites": [],
395+
"difficulty": 5
396+
},
389397
{
390398
"slug": "diamond",
391399
"name": "Diamond",
@@ -410,6 +418,14 @@
410418
"prerequisites": [],
411419
"difficulty": 5
412420
},
421+
{
422+
"slug": "proverb",
423+
"name": "Proverb",
424+
"uuid": "c4bb45c9-9c9d-4a56-933b-df7aa2d45945",
425+
"practices": [],
426+
"prerequisites": [],
427+
"difficulty": 5
428+
},
413429
{
414430
"slug": "raindrops",
415431
"name": "Raindrops",
@@ -579,14 +595,6 @@
579595
"practices": [],
580596
"prerequisites": [],
581597
"difficulty": 10
582-
},
583-
{
584-
"slug": "bottle-song",
585-
"name": "Bottle Song",
586-
"uuid": "b6d90e4b-9f17-459b-aff3-2716e49ccb9c",
587-
"practices": [],
588-
"prerequisites": [],
589-
"difficulty": 5
590598
}
591599
],
592600
"foregone": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Hints
2+
3+
## General
4+
5+
- The `$t0-9` registers can be used to temporarily store values
6+
- The instructions specify which registers are used as input and output
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Instructions append
2+
3+
## Input format
4+
5+
Each list of inputs is represented as a null-terminated string, with a newline character at the end of each input.
6+
7+
An example would be `"nail\nshoe\nhorse\nrider\nmessage\nbattle\nkingdom\n"`
8+
9+
## Registers
10+
11+
| Register | Usage | Type | Description |
12+
| -------- | ------------ | ------- | -------------------------------------------------------------- |
13+
| `$a0` | input | address | null-terminated input string with newline after each input |
14+
| `$a1` | input/output | address | null-terminated output string |
15+
| `$t0-9` | temporary | any | for temporary storage |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Instructions
2+
3+
For want of a horseshoe nail, a kingdom was lost, or so the saying goes.
4+
5+
Given a list of inputs, generate the relevant proverb.
6+
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:
7+
8+
```text
9+
For want of a nail the shoe was lost.
10+
For want of a shoe the horse was lost.
11+
For want of a horse the rider was lost.
12+
For want of a rider the message was lost.
13+
For want of a message the battle was lost.
14+
For want of a battle the kingdom was lost.
15+
And all for the want of a nail.
16+
```
17+
18+
Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content.
19+
No line of the output text should be a static, unchanging string; all should vary according to the input given.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"impl.mips"
8+
],
9+
"test": [
10+
"runner.mips"
11+
],
12+
"example": [
13+
".meta/example.mips"
14+
]
15+
},
16+
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# | Register | Usage | Type | Description |
2+
# | -------- | ------------ | ------- | ---------------------------------------------------------- |
3+
# | `$a0` | input | address | null-terminated input string with newline after each input |
4+
# | `$a1` | input/output | address | null-terminated output string |
5+
# | `$a2` | temporary | address | null-terminated or newline-terminated source string |
6+
# | `$t0` | temporary | byte | character for output |
7+
# | `$t6` | temporary | address | current string |
8+
# | `$t7` | temporary | address | first string |
9+
# | `$t8` | temporary | byte | '\n' newline |
10+
# | `$t9` | temporary | address | return address |
11+
12+
.globl recite
13+
14+
.data
15+
16+
for_want: .asciiz "For want of a "
17+
the: .asciiz " the "
18+
was_lost: .asciiz " was lost.\n"
19+
and_all: .asciiz "And all for the want of a "
20+
stop: .asciiz ".\n"
21+
22+
23+
.text
24+
25+
recite:
26+
move $t7, $a0 # first string
27+
li $t8, '\n'
28+
move $t9, $ra # Save return address
29+
j next
30+
31+
write_line:
32+
la $a2, for_want
33+
jal copy_string
34+
35+
move $a2, $t6
36+
jal copy_line
37+
38+
la $a2, the
39+
jal copy_string
40+
41+
move $a2, $a0
42+
jal copy_line
43+
44+
la $a2, was_lost
45+
jal copy_string
46+
47+
next:
48+
move $t6, $a0 # current string
49+
50+
scan:
51+
lb $t0, 0($a0) # read input byte
52+
addi $a0, $a0, 1 # increment input pointer
53+
bne $t0, $t8, scan # loop until newline
54+
55+
lb $t0, 0($a0) # first byte after newline
56+
bnez $t0, write_line
57+
58+
write_final_line:
59+
la $a2, and_all
60+
jal copy_string
61+
62+
move $a2, $t7 # first string
63+
jal copy_line
64+
65+
la $a2, stop
66+
jal copy_string
67+
68+
jr $t9
69+
70+
71+
copy_string:
72+
# copy string from source $a2 to destination $a1
73+
lb $t0, 0($a2) # load source byte
74+
sb $t0, 0($a1) # write byte to destination
75+
addi $a2, $a2, 1 # increment souce pointer
76+
addi $a1, $a1, 1 # increment destination pointer
77+
bnez $t0, copy_string # repeat until we have reached null terminator
78+
79+
subi $a1, $a1, 1 # decrement destination pointer,
80+
# ready to append other strings
81+
jr $ra
82+
83+
84+
copy_line:
85+
# copy line from source $a2 to destination $a1
86+
# assume $t8 is '\n'
87+
lb $t0, 0($a2) # load source byte
88+
sb $t0, 0($a1) # write byte to destination
89+
addi $a2, $a2, 1 # increment souce pointer
90+
addi $a1, $a1, 1 # increment destination pointer
91+
bne $t0, $t8, copy_line # repeat until we have reached '\n' line terminator
92+
93+
subi $a1, $a1, 1 # decrement destination pointer,
94+
# ready to append other strings
95+
jr $ra
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[e974b73e-7851-484f-8d6d-92e07fe742fc]
13+
description = "zero pieces"
14+
include = false
15+
16+
[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
17+
description = "one piece"
18+
19+
[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
20+
description = "two pieces"
21+
22+
[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
23+
description = "three pieces"
24+
25+
[433fb91c-35a2-4d41-aeab-4de1e82b2126]
26+
description = "full proverb"
27+
28+
[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
29+
description = "four pieces modernized"

exercises/practice/proverb/impl.mips

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# | Register | Usage | Type | Description |
2+
# | -------- | ------------ | ------- | ---------------------------------------------------------- |
3+
# | `$a0` | input | address | null-terminated input string with newline after each input |
4+
# | `$a1` | input/output | address | null-terminated output string |
5+
# | `$t0-9` | temporary | any | for temporary storage |
6+
7+
.globl recite
8+
9+
recite:
10+
jr $ra

0 commit comments

Comments
 (0)