Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proverb exercise #296

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "b6d90e4b-9f17-459b-aff3-2716e49ccb9c",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "diamond",
"name": "Diamond",
Expand All @@ -410,6 +418,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "proverb",
"name": "Proverb",
"uuid": "c4bb45c9-9c9d-4a56-933b-df7aa2d45945",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "raindrops",
"name": "Raindrops",
Expand Down Expand Up @@ -579,14 +595,6 @@
"practices": [],
"prerequisites": [],
"difficulty": 10
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "b6d90e4b-9f17-459b-aff3-2716e49ccb9c",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
],
"foregone": [
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/proverb/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hints

## General

- The `$t0-9` registers can be used to temporarily store values
- The instructions specify which registers are used as input and output
15 changes: 15 additions & 0 deletions exercises/practice/proverb/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Instructions append

## Input format

Each list of inputs is represented as a null-terminated string, with a newline character at the end of each input.

An example would be `"nail\nshoe\nhorse\nrider\nmessage\nbattle\nkingdom\n"`

## Registers

| Register | Usage | Type | Description |
| -------- | ------------ | ------- | -------------------------------------------------------------- |
| `$a0` | input | address | null-terminated input string with newline after each input |
| `$a1` | input/output | address | null-terminated output string |
| `$t0-9` | temporary | any | for temporary storage |
19 changes: 19 additions & 0 deletions exercises/practice/proverb/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb.
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:

```text
For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.
```

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content.
No line of the output text should be a static, unchanging string; all should vary according to the input given.
19 changes: 19 additions & 0 deletions exercises/practice/proverb/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"impl.mips"
],
"test": [
"runner.mips"
],
"example": [
".meta/example.mips"
]
},
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
}
95 changes: 95 additions & 0 deletions exercises/practice/proverb/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | ---------------------------------------------------------- |
# | `$a0` | input | address | null-terminated input string with newline after each input |
# | `$a1` | input/output | address | null-terminated output string |
# | `$a2` | temporary | address | null-terminated or newline-terminated source string |
# | `$t0` | temporary | byte | character for output |
# | `$t6` | temporary | address | current string |
# | `$t7` | temporary | address | first string |
# | `$t8` | temporary | byte | '\n' newline |
# | `$t9` | temporary | address | return address |

.globl recite

.data

for_want: .asciiz "For want of a "
the: .asciiz " the "
was_lost: .asciiz " was lost.\n"
and_all: .asciiz "And all for the want of a "
stop: .asciiz ".\n"


.text

recite:
move $t7, $a0 # first string
li $t8, '\n'
move $t9, $ra # Save return address
j next

write_line:
la $a2, for_want
jal copy_string

move $a2, $t6
jal copy_line

la $a2, the
jal copy_string

move $a2, $a0
jal copy_line

la $a2, was_lost
jal copy_string

next:
move $t6, $a0 # current string

scan:
lb $t0, 0($a0) # read input byte
addi $a0, $a0, 1 # increment input pointer
bne $t0, $t8, scan # loop until newline

lb $t0, 0($a0) # first byte after newline
bnez $t0, write_line

write_final_line:
la $a2, and_all
jal copy_string

move $a2, $t7 # first string
jal copy_line

la $a2, stop
jal copy_string

jr $t9


copy_string:
# copy string from source $a2 to destination $a1
lb $t0, 0($a2) # load source byte
sb $t0, 0($a1) # write byte to destination
addi $a2, $a2, 1 # increment souce pointer
addi $a1, $a1, 1 # increment destination pointer
bnez $t0, copy_string # repeat until we have reached null terminator

subi $a1, $a1, 1 # decrement destination pointer,
# ready to append other strings
jr $ra


copy_line:
# copy line from source $a2 to destination $a1
# assume $t8 is '\n'
lb $t0, 0($a2) # load source byte
sb $t0, 0($a1) # write byte to destination
addi $a2, $a2, 1 # increment souce pointer
addi $a1, $a1, 1 # increment destination pointer
bne $t0, $t8, copy_line # repeat until we have reached '\n' line terminator

subi $a1, $a1, 1 # decrement destination pointer,
# ready to append other strings
jr $ra
29 changes: 29 additions & 0 deletions exercises/practice/proverb/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[e974b73e-7851-484f-8d6d-92e07fe742fc]
description = "zero pieces"
include = false

[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
description = "one piece"

[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
description = "two pieces"

[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
description = "three pieces"

[433fb91c-35a2-4d41-aeab-4de1e82b2126]
description = "full proverb"

[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
description = "four pieces modernized"
10 changes: 10 additions & 0 deletions exercises/practice/proverb/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | ---------------------------------------------------------- |
# | `$a0` | input | address | null-terminated input string with newline after each input |
# | `$a1` | input/output | address | null-terminated output string |
# | `$t0-9` | temporary | any | for temporary storage |

.globl recite

recite:
jr $ra
Loading