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 bottle-song exercise #295

Merged
merged 1 commit into from
Jan 17, 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 @@ -571,6 +571,14 @@
"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/bottle-song/.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
14 changes: 14 additions & 0 deletions exercises/practice/bottle-song/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instructions append

## Output format

Output the lyrics as a null-terminated string.

## Registers

| Register | Usage | Type | Description |
| -------- | ------------ | ------- | ----------------------------- |
| `$a0` | input | integer | start bottles |
| `$a1` | input | integer | take down |
| `$a2` | input/output | address | null-terminated output string |
| `$t0-9` | temporary | any | for temporary storage |
57 changes: 57 additions & 0 deletions exercises/practice/bottle-song/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Instructions

Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.

Note that not all verses are identical.

```text
Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be nine green bottles hanging on the wall.

Nine green bottles hanging on the wall,
Nine green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be eight green bottles hanging on the wall.

Eight green bottles hanging on the wall,
Eight green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be seven green bottles hanging on the wall.

Seven green bottles hanging on the wall,
Seven green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be six green bottles hanging on the wall.

Six green bottles hanging on the wall,
Six green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be five green bottles hanging on the wall.

Five green bottles hanging on the wall,
Five green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be four green bottles hanging on the wall.

Four green bottles hanging on the wall,
Four green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be three green bottles hanging on the wall.

Three green bottles hanging on the wall,
Three green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be two green bottles hanging on the wall.

Two green bottles hanging on the wall,
Two green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be one green bottle hanging on the wall.

One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There'll be no green bottles hanging on the wall.
```
19 changes: 19 additions & 0 deletions exercises/practice/bottle-song/.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": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
}
133 changes: 133 additions & 0 deletions exercises/practice/bottle-song/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | ----------------------------- |
# | `$a0` | input | integer | start bottles |
# | `$a1` | input | integer | take down |
# | `$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 number_array |
# | `$t4` | temporary | integer | 4 |
# | `$t8` | temporary | address | number_array |
# | `$t9` | temporary | address | return address |

.globl recite

.data

green_bottles:
.asciiz " green bottles hanging on the wall"
green_bottle:
.asciiz " green bottle hanging on the wall"
and_if:
.asciiz "And if one green bottle should accidentally fall,\nThere'll be "
comma: .asciiz ",\n"
stop: .asciiz ".\n"

no: .asciiz "No"
one: .asciiz "One"
two: .asciiz "Two"
three: .asciiz "Three"
four: .asciiz "Four"
five: .asciiz "Five"
six: .asciiz "Six"
seven: .asciiz "Seven"
eight: .asciiz "Eight"
nine: .asciiz "Nine"
ten: .asciiz "Ten"

number_array:
.word no
.word one
.word two
.word three
.word four
.word five
.word six
.word seven
.word eight
.word nine
.word ten

.text

recite:
sll $a0, $a0, 2
sll $a1, $a1, 2
sub $a1, $a0, $a1 # end bottles
li $t4, 4
la $t8, number_array
move $t9, $ra # Save return address
j start

loop:
li $t0, '\n'
sb $t0, 0($a2) # write byte to destination
addi $a2, $a2, 1 # increment destination pointer

start:
la $t5, green_bottles
bne $a0, $t4, bottles_or_bottle1

la $t5, green_bottle

bottles_or_bottle1:
add $t3, $t8, $a0
lw $a3, 0($t3)
jal copy_string

move $a3, $t5
jal copy_string

la $a3, comma
jal copy_string

add $t3, $t8, $a0
lw $a3, 0($t3)
jal copy_string

move $a3, $t5
jal copy_string

la $a3, comma
jal copy_string

la $a3, and_if
jal copy_string

sub $a0, $a0, 4
la $t5, green_bottles
bne $a0, $t4, bottles_or_bottle2

la $t5, green_bottle

bottles_or_bottle2:
add $t3, $t8, $a0
lw $a3, 0($t3)
lb $t0, 0($a3) # load source byte
or $t0, $t0, 32 # force lower case
sb $t0, 0($a2) # write byte to destination
addi $a3, $a3, 1 # increment souce pointer
addi $a2, $a2, 1 # increment destination pointer
jal copy_string

move $a3, $t5
jal copy_string

la $a3, stop
jal copy_string

bne $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
31 changes: 31 additions & 0 deletions exercises/practice/bottle-song/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
description = "verse -> single verse -> first generic verse"

[0f0aded3-472a-4c64-b842-18d4f1f5f030]
description = "verse -> single verse -> last generic verse"

[f61f3c97-131f-459e-b40a-7428f3ed99d9]
description = "verse -> single verse -> verse with 2 bottles"

[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
description = "verse -> single verse -> verse with 1 bottle"

[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
description = "lyrics -> multiple verses -> first two verses"

[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
description = "lyrics -> multiple verses -> last three verses"

[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
description = "lyrics -> multiple verses -> all verses"
11 changes: 11 additions & 0 deletions exercises/practice/bottle-song/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