Skip to content

Commit

Permalink
Add food-chain exercise (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Dec 26, 2024
1 parent 7402a76 commit 16beaf7
Show file tree
Hide file tree
Showing 12 changed files with 3,866 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "bd684e71-75b9-4d31-8775-cb93eeb6dea0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "house",
"name": "House",
Expand Down
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": [
"food_chain.s"
],
"test": [
"food_chain_test.c"
],
"example": [
".meta/example.s"
]
},
"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"
}
147 changes: 147 additions & 0 deletions exercises/practice/food-chain/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
.data

i_know: .string "I know an old lady who swallowed a "
stop: .string ".\n"
shes: .string "She's dead, of course!\n"
i_dont: .string "I don't know why she swallowed the fly. Perhaps she'll die.\n"
she: .string "She swallowed the "
to: .string " to catch the "
that: .string " that wriggled and jiggled and tickled inside her"

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

spider2:
.string "It wriggled and jiggled and tickled inside her.\n"
bird2: .string "How absurd to swallow a bird!\n"
cat2: .string "Imagine that, to swallow a cat!\n"
dog2: .string "What a hog, to swallow a dog!\n"
goat2: .string "Just opened her throat and swallowed a goat!\n"
cow2: .string "I don't know how she swallowed a cow!\n"

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

animal2_array:
.dword 0
.dword 0
.dword spider2
.dword bird2
.dword cat2
.dword dog2
.dword goat2
.dword cow2

.macro LOAD register, label
adrp \register, \label
add \register, \register, :lo12:\label
.endm

.macro APPEND str

.copy_\str:
ldrb w15, [x14], #1 /* load byte, with post-increment */
strb w15, [x0], #1 /* store byte, post-increment */
cbnz w15, .copy_\str

sub x0, x0, #1
.endm

.text
.globl recite

/* extern void recite(char *buffer, int start_verse, int end_verse); */
recite:
LOAD x3, i_know
LOAD x4, stop
LOAD x5, shes
LOAD x6, i_dont
LOAD x7, she
LOAD x8, to
LOAD x9, that
LOAD x10, animal_array
LOAD x11, animal2_array

lsl x1, x1, #3
lsl x2, x2, #3

.verse:
mov x14, x3
APPEND i_know

ldr x14, [x10, x1]
APPEND animal

mov x14, x4
APPEND stop

cmp x1, #64
beq .shes

cmp x1, #8
beq .i_dont

ldr x14, [x11, x1]
APPEND second

mov x13, x1

.reason:
mov x14, x7
APPEND she

ldr x14, [x10, x13]
APPEND animal2

mov x14, x8
APPEND to

sub x13, x13, #8
ldr x14, [x10, x13]
APPEND animal3

cmp x13, #16
bne .stop

mov x14, x9
APPEND that

.stop:
mov x14, x4
APPEND stop2

cmp x13, #8
bne .reason

.i_dont:
mov x14, x6
APPEND i_dont

cmp x1, x2
beq .return

add x1, x1, #8
mov w15, #'\n'
strb w15, [x0], #1
b .verse

.shes:
mov x14, x5
APPEND shes

.return:
ret
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"
36 changes: 36 additions & 0 deletions exercises/practice/food-chain/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/food-chain/food_chain.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl recite

recite:
ret
Loading

0 comments on commit 16beaf7

Please sign in to comment.