Skip to content

Commit

Permalink
Add food-chain exercise (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jan 9, 2025
1 parent e285438 commit 2effbff
Show file tree
Hide file tree
Showing 11 changed files with 3,861 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "5d4da9ec-5b46-4609-b942-6be556884ef3",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "luhn",
"name": "Luhn",
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.asm"
],
"test": [
"food_chain_test.c"
],
"example": [
".meta/example.asm"
]
},
"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.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
default rel

section .rodata

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

fly: db "fly", 0
spider: db "spider", 0
bird: db "bird", 0
cat: db "cat", 0
dog: db "dog", 0
goat: db "goat", 0
cow: db "cow", 0
horse: db "horse", 0

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

section .data

animal_array:
dq 0
dq fly
dq spider
dq bird
dq cat
dq dog
dq goat
dq cow
dq horse

animal2_array:
dq 0
dq 0
dq spider2
dq bird2
dq cat2
dq dog2
dq goat2
dq cow2


section .text
global recite

append:
lodsb
stosb
test al, al
jnz append

dec rdi
ret

; extern void recite(char *buffer, int start_verse, int end_verse);
recite:
cld
lea r8, [animal_array]
lea r9, [animal2_array]
mov r10, rsi ; current verse
jmp .start_verse

.next_verse:
inc r10
lea rsi, [newline]
call append

.start_verse:
mov r11, r10 ; current animal
lea rsi, [i_know]
call append

mov rsi, [r8 + r11 * 8] ; lookup animal_array
call append

lea rsi, [stop]
call append

cmp r10, 8
je .shes_dead

cmp r10, 1
je .i_dont

mov rsi, [r9 + r11 * 8] ; lookup animal2_array
call append

.she:
lea rsi, [she]
call append

mov rsi, [r8 + r11 * 8] ; lookup animal_array
call append

dec r11 ; previous animal
lea rsi, [to_catch]
call append

mov rsi, [r8 + r11 * 8] ; lookup animal_array
call append

cmp r11, 2
jne .stop

lea rsi, [that]
call append

.stop:
lea rsi, [stop]
call append

cmp r11, 1
jne .she ; continue inner loop over animals

.i_dont:
lea rsi, [i_dont]
call append

cmp r10, rdx
jne .next_verse ; continue outer loop over verses

ret

.shes_dead:
lea rsi, [shes_dead]
call append

ret

%ifidn __OUTPUT_FORMAT__,elf64
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
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"
46 changes: 46 additions & 0 deletions exercises/practice/food-chain/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
AS = nasm

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =
ASFLAGS = -g -F dwarf -Werror

ifeq ($(shell uname -s),Darwin)
ifeq ($(shell sysctl -n hw.optional.arm64 2>/dev/null),1)
ALL_CFLAGS = -target x86_64-apple-darwin
endif
ALL_LDFLAGS = -Wl,-pie
ALL_ASFLAGS = -f macho64 --prefix _
else
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
ALL_ASFLAGS = -f elf64
endif

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

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

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

all: tests
@./$<

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

%.o: %.asm
@$(AS) $(ALL_ASFLAGS) -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
13 changes: 13 additions & 0 deletions exercises/practice/food-chain/food_chain.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default rel

section .data

section .text
global recite
recite:
; Provide your implementation here
ret

%ifidn __OUTPUT_FORMAT__,elf64
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
Loading

0 comments on commit 2effbff

Please sign in to comment.