-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
867c934
commit d4f29d7
Showing
11 changed files
with
3,641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Instructions | ||
|
||
For want of a horseshoe nail, a kingdom was lost, or so the saying goes. | ||
|
||
Given a list of inputs, generate the relevant proverb. | ||
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme: | ||
|
||
```text | ||
For want of a nail the shoe was lost. | ||
For want of a shoe the horse was lost. | ||
For want of a horse the rider was lost. | ||
For want of a rider the message was lost. | ||
For want of a message the battle was lost. | ||
For want of a battle the kingdom was lost. | ||
And all for the want of a nail. | ||
``` | ||
|
||
Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. | ||
No line of the output text should be a static, unchanging string; all should vary according to the input given. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"proverb.asm" | ||
], | ||
"test": [ | ||
"proverb_test.c" | ||
], | ||
"example": [ | ||
".meta/example.asm" | ||
] | ||
}, | ||
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
default rel | ||
|
||
section .rodata | ||
|
||
for_want: | ||
db "For want of a ", 0 | ||
the: db " the ", 0 | ||
was_lost: | ||
db " was lost.", 10, 0 | ||
and_all: | ||
db "And all for the want of a ", 0 | ||
stop: db ".", 10, 0 | ||
|
||
section .text | ||
global recite | ||
|
||
append: | ||
lodsb | ||
stosb | ||
test al, al | ||
jnz append | ||
|
||
dec rdi | ||
ret | ||
|
||
; extern void recite(char *buffer, const char **strings); | ||
recite: | ||
cld | ||
mov rdx, rsi | ||
mov r8, [rdx] ; first string | ||
add rdx, 8 | ||
test r8, r8 | ||
jz .empty | ||
|
||
mov r10, r8 ; current string | ||
jmp .next_line | ||
|
||
.for_want: | ||
lea rsi, [for_want] | ||
call append | ||
|
||
mov rsi, r10 | ||
call append | ||
|
||
lea rsi, [the] | ||
call append | ||
|
||
mov rsi, r11 | ||
call append | ||
|
||
lea rsi, [was_lost] | ||
call append | ||
|
||
mov r10, r11 ; current string | ||
|
||
.next_line: | ||
mov r11, [rdx] ; next string | ||
add rdx, 8 | ||
test r11, r11 | ||
jnz .for_want | ||
|
||
lea rsi, [and_all] | ||
call append | ||
|
||
mov rsi, r8 | ||
call append | ||
|
||
lea rsi, [stop] | ||
call append | ||
|
||
.empty: | ||
xor al, al | ||
stosb | ||
ret | ||
|
||
%ifidn __OUTPUT_FORMAT__,elf64 | ||
section .note.GNU-stack noalloc noexec nowrite progbits | ||
%endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 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. | ||
|
||
[e974b73e-7851-484f-8d6d-92e07fe742fc] | ||
description = "zero pieces" | ||
|
||
[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4] | ||
description = "one piece" | ||
|
||
[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e] | ||
description = "two pieces" | ||
|
||
[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83] | ||
description = "three pieces" | ||
|
||
[433fb91c-35a2-4d41-aeab-4de1e82b2126] | ||
description = "full proverb" | ||
|
||
[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7] | ||
description = "four pieces modernized" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
default rel | ||
|
||
section .text | ||
global recite | ||
recite: | ||
; Provide your implementation here | ||
ret | ||
|
||
%ifidn __OUTPUT_FORMAT__,elf64 | ||
section .note.GNU-stack noalloc noexec nowrite progbits | ||
%endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include "vendor/unity.h" | ||
|
||
#include <stddef.h> | ||
|
||
#define BUFFER_SIZE 400 | ||
|
||
extern void recite(char *buffer, const char **strings); | ||
|
||
void setUp(void) { | ||
} | ||
|
||
void tearDown(void) { | ||
} | ||
|
||
void test_zero_pieces(void) { | ||
const char *strings[] = { | ||
NULL, | ||
}; | ||
const char *expected = ""; | ||
char buffer[BUFFER_SIZE]; | ||
|
||
recite(buffer, strings); | ||
TEST_ASSERT_EQUAL_STRING(expected, buffer); | ||
} | ||
|
||
void test_one_piece(void) { | ||
TEST_IGNORE(); | ||
const char *strings[] = { | ||
"nail", | ||
NULL, | ||
}; | ||
const char *expected = | ||
"And all for the want of a nail.\n"; | ||
char buffer[BUFFER_SIZE]; | ||
|
||
recite(buffer, strings); | ||
TEST_ASSERT_EQUAL_STRING(expected, buffer); | ||
} | ||
|
||
void test_two_pieces(void) { | ||
TEST_IGNORE(); | ||
const char *strings[] = { | ||
"nail", | ||
"shoe", | ||
NULL, | ||
}; | ||
const char *expected = | ||
"For want of a nail the shoe was lost.\n" | ||
"And all for the want of a nail.\n"; | ||
char buffer[BUFFER_SIZE]; | ||
|
||
recite(buffer, strings); | ||
TEST_ASSERT_EQUAL_STRING(expected, buffer); | ||
} | ||
|
||
void test_three_pieces(void) { | ||
TEST_IGNORE(); | ||
const char *strings[] = { | ||
"nail", | ||
"shoe", | ||
"horse", | ||
NULL, | ||
}; | ||
const char *expected = | ||
"For want of a nail the shoe was lost.\n" | ||
"For want of a shoe the horse was lost.\n" | ||
"And all for the want of a nail.\n"; | ||
char buffer[BUFFER_SIZE]; | ||
|
||
recite(buffer, strings); | ||
TEST_ASSERT_EQUAL_STRING(expected, buffer); | ||
} | ||
|
||
void test_full_proverb(void) { | ||
TEST_IGNORE(); | ||
const char *strings[] = { | ||
"nail", | ||
"shoe", | ||
"horse", | ||
"rider", | ||
"message", | ||
"battle", | ||
"kingdom", | ||
NULL, | ||
}; | ||
const char *expected = | ||
"For want of a nail the shoe was lost.\n" | ||
"For want of a shoe the horse was lost.\n" | ||
"For want of a horse the rider was lost.\n" | ||
"For want of a rider the message was lost.\n" | ||
"For want of a message the battle was lost.\n" | ||
"For want of a battle the kingdom was lost.\n" | ||
"And all for the want of a nail.\n"; | ||
char buffer[BUFFER_SIZE]; | ||
|
||
recite(buffer, strings); | ||
TEST_ASSERT_EQUAL_STRING(expected, buffer); | ||
} | ||
|
||
void test_four_pieces_modernized(void) { | ||
TEST_IGNORE(); | ||
const char *strings[] = { | ||
"pin", | ||
"gun", | ||
"soldier", | ||
"battle", | ||
NULL, | ||
}; | ||
const char *expected = | ||
"For want of a pin the gun was lost.\n" | ||
"For want of a gun the soldier was lost.\n" | ||
"For want of a soldier the battle was lost.\n" | ||
"And all for the want of a pin.\n"; | ||
char buffer[BUFFER_SIZE]; | ||
|
||
recite(buffer, strings); | ||
TEST_ASSERT_EQUAL_STRING(expected, buffer); | ||
} | ||
|
||
int main(void) { | ||
UNITY_BEGIN(); | ||
RUN_TEST(test_zero_pieces); | ||
RUN_TEST(test_one_piece); | ||
RUN_TEST(test_two_pieces); | ||
RUN_TEST(test_three_pieces); | ||
RUN_TEST(test_full_proverb); | ||
RUN_TEST(test_four_pieces_modernized); | ||
return UNITY_END(); | ||
} |
Oops, something went wrong.