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 #136

Merged
merged 1 commit into from
Dec 26, 2024
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 @@ -242,6 +242,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "f8609511-8149-4df4-99ba-a162f7d6e84f",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "isbn-verifier",
"name": "ISBN Verifier",
Expand Down
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": [
"bottle_song.s"
],
"test": [
"bottle_song_test.c"
],
"example": [
".meta/example.s"
]
},
"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"
}
114 changes: 114 additions & 0 deletions exercises/practice/bottle-song/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
.data

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

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

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

.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_bottles, int take_down); */
recite:
LOAD x3, green_bottles
LOAD x4, green_bottle
LOAD x5, and
LOAD x6, comma
LOAD x7, stop
LOAD x8, number_array

lsl x1, x1, #3
lsl x2, x2, #3
sub x2, x1, x2 /* end bottles */

.verse:
ldr x14, [x8, x1]
APPEND number1

cmp x1, #8
csel x14, x4, x3, eq
APPEND green1

mov x14, x6
APPEND comma1

ldr x14, [x8, x1]
APPEND number2

cmp x1, #8
csel x14, x4, x3, eq
APPEND green2

mov x14, x6
APPEND comma2

mov x14, x5
APPEND and

sub x1, x1, #8
ldr x14, [x8, x1]
ldrb w15, [x14], #1 /* load byte, with post-increment */
orr w15, w15, #32 /* force lower case */
strb w15, [x0], #1 /* store byte, post-increment */
APPEND number3

cmp x1, #8
csel x14, x4, x3, eq
APPEND green3

mov x14, x7
APPEND stop1

cmp x1, x2
beq .return

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

.return:
ret
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"
36 changes: 36 additions & 0 deletions exercises/practice/bottle-song/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/bottle-song/bottle_song.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl recite

recite:
ret
Loading