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 twelve-days exercise #292

Merged
merged 1 commit into from
Jan 14, 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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "twelve-days",
"name": "Twelve Days",
"uuid": "3db2af89-e1ff-43b8-acb3-a0a1ce5f5dad",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "food-chain",
"name": "Food Chain",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/twelve-days/.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
Empty file.
36 changes: 36 additions & 0 deletions exercises/practice/twelve-days/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Your task in this exercise is to write code that returns the lyrics of the song: "The Twelve Days of Christmas."

"The Twelve Days of Christmas" is a common English Christmas carol.
Each subsequent verse of the song builds on the previous verse.

The lyrics your code returns should _exactly_ match the full song text shown below.

## Lyrics

```text
On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.

On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.

On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
```
19 changes: 19 additions & 0 deletions exercises/practice/twelve-days/.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": "Output the lyrics to 'The Twelve Days of Christmas'.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)"
}
112 changes: 112 additions & 0 deletions exercises/practice/twelve-days/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | ----------------------------- |
# | `$a0` | input | integer | start verse |
# | `$a1` | input | integer | end verse |
# | `$a2` | input/output | address | null-terminated output string |
# | `$a3` | temporary | address | null-terminated source string |
# | `$t0` | temporary | byte | character for output |
# | `$t3` | temporary | address | pointer into array |
# | `$t6` | temporary | address | ordinals array |
# | `$t7` | temporary | address | gifts |
# | `$t8` | temporary | address | array of offsets into gifts |
# | `$t9` | temporary | address | return address |

.globl recite

.data

on_the: .asciiz "On the "
day_of: .asciiz " day of Christmas my true love gave to me: "
gifts: .asciiz "twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n"

first: .asciiz "first"
second: .asciiz "second"
third: .asciiz "third"
fourth: .asciiz "fourth"
fifth: .asciiz "fifth"
sixth: .asciiz "sixth"
seventh:
.asciiz "seventh"
eighth: .asciiz "eighth"
ninth: .asciiz "ninth"
tenth: .asciiz "tenth"
eleventh:
.asciiz "eleventh"
twelfth:
.asciiz "twelfth"

ordinals:
.word 0
.word first
.word second
.word third
.word fourth
.word fifth
.word sixth
.word seventh
.word eighth
.word ninth
.word tenth
.word eleventh
.word twelfth

offsets:
.word 0
.word 235
.word 213
.word 194
.word 174
.word 157
.word 137
.word 113
.word 90
.word 69
.word 48
.word 26
.word 0


.text

recite:
sll $a0, $a0, 2
sll $a1, $a1, 2
la $t6, ordinals
la $t7, gifts
la $t8, offsets
move $t9, $ra # Save return address

loop:
la $a3, on_the
jal copy_string

add $t3, $t6, $a0
lw $a3, 0($t3) # read from ordinals
jal copy_string

la $a3, day_of
jal copy_string

add $t3, $t8, $a0
lw $a3, 0($t3) # read from offsets
add $a3, $t7, $a3
jal copy_string

end_verse:
add $a0, $a0, 4
ble $a0, $a1, loop

jr $t9


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

subi $a2, $a2, 1 # decrement destination pointer,
# ready to append other strings
jr $ra
55 changes: 55 additions & 0 deletions exercises/practice/twelve-days/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.

[c0b5a5e6-c89d-49b1-a6b2-9f523bff33f7]
description = "verse -> first day a partridge in a pear tree"

[1c64508a-df3d-420a-b8e1-fe408847854a]
description = "verse -> second day two turtle doves"

[a919e09c-75b2-4e64-bb23-de4a692060a8]
description = "verse -> third day three french hens"

[9bed8631-ec60-4894-a3bb-4f0ec9fbe68d]
description = "verse -> fourth day four calling birds"

[cf1024f0-73b6-4545-be57-e9cea565289a]
description = "verse -> fifth day five gold rings"

[50bd3393-868a-4f24-a618-68df3d02ff04]
description = "verse -> sixth day six geese-a-laying"

[8f29638c-9bf1-4680-94be-e8b84e4ade83]
description = "verse -> seventh day seven swans-a-swimming"

[7038d6e1-e377-47ad-8c37-10670a05bc05]
description = "verse -> eighth day eight maids-a-milking"

[37a800a6-7a56-4352-8d72-0f51eb37cfe8]
description = "verse -> ninth day nine ladies dancing"

[10b158aa-49ff-4b2d-afc3-13af9133510d]
description = "verse -> tenth day ten lords-a-leaping"

[08d7d453-f2ba-478d-8df0-d39ea6a4f457]
description = "verse -> eleventh day eleven pipers piping"

[0620fea7-1704-4e48-b557-c05bf43967f0]
description = "verse -> twelfth day twelve drummers drumming"

[da8b9013-b1e8-49df-b6ef-ddec0219e398]
description = "lyrics -> recites first three verses of the song"

[c095af0d-3137-4653-ad32-bfb899eda24c]
description = "lyrics -> recites three verses from the middle of the song"

[20921bc9-cc52-4627-80b3-198cbbfcf9b7]
description = "lyrics -> recites the whole song"
11 changes: 11 additions & 0 deletions exercises/practice/twelve-days/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | ----------------------------- |
# | `$a0` | input | integer | start verse |
# | `$a1` | input | integer | end verse |
# | `$a2` | input/output | address | null-terminated output string |
# | `$t0-9` | temporary | any | for temporary storage |

.globl recite

recite:
jr $ra
Loading