Skip to content

Commit

Permalink
Add food-chain exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Jan 11, 2025
1 parent 708419f commit 26609ff
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 0 deletions.
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": "food-chain",
"name": "Food Chain",
"uuid": "28ad8056-fc54-4372-aad5-530d6d666a87",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "knapsack",
"name": "Knapsack",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/food-chain/.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/food-chain/.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 verse |
| `$a1` | input | integer | end verse |
| `$a2` | input/output | address | null-terminated output string |
| `$t0-9` | temporary | any | for temporary storage |
64 changes: 64 additions & 0 deletions exercises/practice/food-chain/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Instructions

Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.

While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.

This is a [cumulative song][cumulative-song] of unknown origin.

This is one of many common variants.

```text
I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.
I know an old lady who swallowed a horse.
She's dead, of course!
```

[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
19 changes: 19 additions & 0 deletions exercises/practice/food-chain/.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": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
}
150 changes: 150 additions & 0 deletions exercises/practice/food-chain/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# | 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 |
# | `$t1` | temporary | integer | 0..20 indicates spider..cow |
# | `$t3` | temporary | address | pointer into animal_array |
# | `$t5` | temporary | address | animal_array |
# | `$t6` | temporary | address | fly entry in animal_array |
# | `$t7` | temporary | address | spider entry in animal_array |
# | `$t8` | temporary | address | animal2_array |
# | `$t9` | temporary | address | return address |

.globl recite

.data

i_know: .asciiz "I know an old lady who swallowed a "
stop: .asciiz ".\n"
she_swallowed:
.asciiz "She swallowed the "
to_catch:
.asciiz " to catch the "
that_wriggled:
.asciiz " that wriggled and jiggled and tickled inside her"

fly: .asciiz "fly"
spider: .asciiz "spider"
bird: .asciiz "bird"
cat: .asciiz "cat"
dog: .asciiz "dog"
goat: .asciiz "goat"
cow: .asciiz "cow"
horse: .asciiz "horse"

fly2: .asciiz "I don't know why she swallowed the fly. Perhaps she'll die.\n"
spider2:
.asciiz "It wriggled and jiggled and tickled inside her.\n"
bird2: .asciiz "How absurd to swallow a bird!\n"
cat2: .asciiz "Imagine that, to swallow a cat!\n"
dog2: .asciiz "What a hog, to swallow a dog!\n"
goat2: .asciiz "Just opened her throat and swallowed a goat!\n"
cow2: .asciiz "I don't know how she swallowed a cow!\n"
horse2: .asciiz "She's dead, of course!\n"

animal_array:
.word 0
.word fly
.word spider
.word bird
.word cat
.word dog
.word goat
.word cow
.word horse

animal2_array:
.word 0
.word fly2
.word spider2
.word bird2
.word cat2
.word dog2
.word goat2
.word cow2
.word horse2


.text

recite:
sll $a0, $a0, 2
sll $a1, $a1, 2
la $t5, animal_array
add $t6, $t5, 4
add $t7, $t6, 4
la $t8, animal2_array
move $t9, $ra # Save return address
j start

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

start:
la $a3, i_know
jal copy_string

add $t3, $t5, $a0
lw $a3, 0($t3) # read from animal_array
jal copy_string

la $a3, stop
jal copy_string

add $a3, $t8, $a0
lw $a3, 0($a3) # read from animal2_array
jal copy_string

sub $t1, $a0, 8
bgtu $t1, 20, end_verse

inner:
la $a3, she_swallowed
jal copy_string

lw $a3, 0($t3) # read from animal_array
jal copy_string

la $a3, to_catch
jal copy_string

sub $t3, $t3, 4
lw $a3, 0($t3) # read from animal_array
jal copy_string

la $a3, that_wriggled
sne $t0, $t3, $t7
subi $t0, $t0, 1 # negative if spider
bltzal $t0, copy_string # conditional subroutine call

la $a3, stop
jal copy_string

bne $t3, $t6, inner

la $a3, fly2 # I don't know why ...
jal copy_string

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

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

[751dce68-9412-496e-b6e8-855998c56166]
description = "fly"

[6c56f861-0c5e-4907-9a9d-b2efae389379]
description = "spider"

[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
description = "bird"

[e866a758-e1ff-400e-9f35-f27f28cc288f]
description = "cat"

[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
description = "dog"

[4b3fd221-01ea-46e0-825b-5734634fbc59]
description = "goat"

[1b707da9-7001-4fac-941f-22ad9c7a65d4]
description = "cow"

[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
description = "horse"

[22b863d5-17e4-4d1e-93e4-617329a5c050]
description = "multiple verses"

[e626b32b-745c-4101-bcbd-3b13456893db]
description = "full song"
11 changes: 11 additions & 0 deletions exercises/practice/food-chain/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

0 comments on commit 26609ff

Please sign in to comment.