Skip to content

Commit 1fbaec1

Browse files
Add bottle-song
1 parent 34453b7 commit 1fbaec1

File tree

12 files changed

+3781
-0
lines changed

12 files changed

+3781
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@
327327
"prerequisites": [],
328328
"difficulty": 5
329329
},
330+
{
331+
"slug": "bottle-song",
332+
"name": "Bottle Song",
333+
"uuid": "86a6f08f-efac-47d7-bc10-04178585aee1",
334+
"practices": [],
335+
"prerequisites": [],
336+
"difficulty": 5
337+
},
330338
{
331339
"slug": "largest-series-product",
332340
"name": "Largest Series Product",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Instructions
2+
3+
Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.
4+
5+
Note that not all verses are identical.
6+
7+
```text
8+
Ten green bottles hanging on the wall,
9+
Ten green bottles hanging on the wall,
10+
And if one green bottle should accidentally fall,
11+
There'll be nine green bottles hanging on the wall.
12+
13+
Nine green bottles hanging on the wall,
14+
Nine green bottles hanging on the wall,
15+
And if one green bottle should accidentally fall,
16+
There'll be eight green bottles hanging on the wall.
17+
18+
Eight green bottles hanging on the wall,
19+
Eight green bottles hanging on the wall,
20+
And if one green bottle should accidentally fall,
21+
There'll be seven green bottles hanging on the wall.
22+
23+
Seven green bottles hanging on the wall,
24+
Seven green bottles hanging on the wall,
25+
And if one green bottle should accidentally fall,
26+
There'll be six green bottles hanging on the wall.
27+
28+
Six green bottles hanging on the wall,
29+
Six green bottles hanging on the wall,
30+
And if one green bottle should accidentally fall,
31+
There'll be five green bottles hanging on the wall.
32+
33+
Five green bottles hanging on the wall,
34+
Five green bottles hanging on the wall,
35+
And if one green bottle should accidentally fall,
36+
There'll be four green bottles hanging on the wall.
37+
38+
Four green bottles hanging on the wall,
39+
Four green bottles hanging on the wall,
40+
And if one green bottle should accidentally fall,
41+
There'll be three green bottles hanging on the wall.
42+
43+
Three green bottles hanging on the wall,
44+
Three green bottles hanging on the wall,
45+
And if one green bottle should accidentally fall,
46+
There'll be two green bottles hanging on the wall.
47+
48+
Two green bottles hanging on the wall,
49+
Two green bottles hanging on the wall,
50+
And if one green bottle should accidentally fall,
51+
There'll be one green bottle hanging on the wall.
52+
53+
One green bottle hanging on the wall,
54+
One green bottle hanging on the wall,
55+
And if one green bottle should accidentally fall,
56+
There'll be no green bottles hanging on the wall.
57+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"bottle_song.s"
8+
],
9+
"test": [
10+
"bottle_song_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
14+
]
15+
},
16+
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
19+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
.section .rodata
2+
3+
green:
4+
.string " green bottle"
5+
hanging:
6+
.string " hanging on the wall"
7+
and:
8+
.string "And if one green bottle should accidentally fall,\nThere'll be "
9+
comma: .string ",\n"
10+
stop: .string ".\n\n"
11+
12+
numbers:
13+
.string "noonetwothreefourfivesixseveneightnineten"
14+
15+
numbers_table:
16+
.byte 0, 2, 5, 8, 13, 17, 21, 24, 29, 34, 38, 41
17+
18+
19+
.text
20+
.globl recite
21+
22+
23+
/* char *copy(char *dest, void *, void *, const char *source); */
24+
copy:
25+
lb t0, 0(a3)
26+
addi a3, a3, 1
27+
sb t0, 0(a0)
28+
addi a0, a0, 1
29+
bnez t0, copy
30+
31+
addi a0, a0, -1
32+
jalr zero, 0(t6) /* return */
33+
34+
35+
/* char *copy_n(char *dest, void *, void *, const char *source, size_t length); */
36+
copy_n:
37+
beqz a4, .copy_n_return
38+
39+
lb t0, 0(a3)
40+
addi a3, a3, 1
41+
sb t0, 0(a0)
42+
addi a0, a0, 1
43+
addi a4, a4, -1
44+
j copy_n
45+
46+
.copy_n_return:
47+
jalr zero, 0(t6) /* return */
48+
49+
50+
/* extern void recite(char *buffer, int start_bottles, int take_down); */
51+
recite:
52+
sub a2, a1, a2
53+
li a6, 1
54+
li a7, 's'
55+
56+
.stanza:
57+
move t4, a0
58+
59+
la a3, numbers
60+
la t0, numbers_table
61+
add t0, t0, a1
62+
lbu t1, 0(t0)
63+
add a3, a3, t1 /* start of number string */
64+
addi t0, t0, 1
65+
lbu t2, 0(t0)
66+
sub a4, t2, t1 /* length of number string */
67+
jal t6, copy_n /* subroutine call */
68+
69+
la a3, green
70+
jal t6, copy /* subroutine call */
71+
72+
beq a1, a6, .hanging1 /* 1? */
73+
74+
sb a7, 0(a0) /* 's' */
75+
addi a0, a0, 1
76+
77+
.hanging1:
78+
la a3, hanging
79+
jal t6, copy /* subroutine call */
80+
81+
la a3, comma
82+
jal t6, copy /* subroutine call */
83+
84+
lb t0, 0(t4)
85+
addi t0, t0, -32 /* convert first letter to upper case */
86+
sb t0, 0(t4)
87+
88+
move a3, t4 /* start of most recent line */
89+
sub a4, a0, t4 /* length of most recent line */
90+
jal t6, copy_n /* subroutine call */
91+
92+
la a3, and
93+
jal t6, copy /* subroutine call */
94+
95+
addi a1, a1, -1 /* decrement number of bottles */
96+
97+
la a3, numbers
98+
la t0, numbers_table
99+
add t0, t0, a1
100+
lbu t1, 0(t0)
101+
add a3, a3, t1 /* start of number string */
102+
addi t0, t0, 1
103+
lbu t2, 0(t0)
104+
sub a4, t2, t1 /* length of number string */
105+
jal t6, copy_n /* subroutine call */
106+
107+
la a3, green
108+
jal t6, copy /* subroutine call */
109+
110+
beq a1, a6, .hanging4 /* 1? */
111+
112+
sb a7, 0(a0) /* 's' */
113+
addi a0, a0, 1
114+
115+
.hanging4:
116+
la a3, hanging
117+
jal t6, copy /* subroutine call */
118+
119+
la a3, stop
120+
jal t6, copy /* subroutine call */
121+
122+
bne a1, a2, .stanza
123+
124+
sb zero, -1(a0)
125+
ret
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
13+
description = "verse -> single verse -> first generic verse"
14+
15+
[0f0aded3-472a-4c64-b842-18d4f1f5f030]
16+
description = "verse -> single verse -> last generic verse"
17+
18+
[f61f3c97-131f-459e-b40a-7428f3ed99d9]
19+
description = "verse -> single verse -> verse with 2 bottles"
20+
21+
[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
22+
description = "verse -> single verse -> verse with 1 bottle"
23+
24+
[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
25+
description = "lyrics -> multiple verses -> first two verses"
26+
27+
[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
28+
description = "lyrics -> multiple verses -> last three verses"
29+
30+
[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
31+
description = "lyrics -> multiple verses -> all verses"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = /usr/bin/riscv64-linux-gnu-as
2+
CC = /usr/bin/riscv64-linux-gnu-gcc
3+
4+
CFLAGS = -g -Wall -Wextra -pedantic -Werror
5+
LDFLAGS =
6+
7+
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
8+
9+
ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
10+
ALL_LDFLAGS += $(LDFLAGS)
11+
12+
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
13+
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
14+
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)
15+
16+
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<
17+
18+
all: tests
19+
qemu-riscv64 -L /usr/riscv64-linux-gnu ./$<
20+
21+
tests: $(ALL_OBJS)
22+
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)
23+
24+
%.o: %.s
25+
@$(AS) -o $@ $<
26+
27+
%.o: %.c
28+
@$(CC_CMD)
29+
30+
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
31+
@$(CC_CMD)
32+
33+
clean:
34+
@rm -f *.o vendor/*.o tests
35+
36+
.PHONY: all clean
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text
2+
.globl recite
3+
4+
recite:
5+
ret

0 commit comments

Comments
 (0)