Skip to content

Commit

Permalink
Add resistor-color exercise
Browse files Browse the repository at this point in the history
Adapted from x86-64-assembly.
  • Loading branch information
keiravillekode committed Nov 9, 2024
1 parent b23d011 commit b7ce960
Show file tree
Hide file tree
Showing 12 changed files with 3,573 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "resistor-color",
"name": "Resistor Color",
"uuid": "89cdbbb0-1944-4f73-ac1f-7967ac55c100",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
Expand Down
39 changes: 39 additions & 0 deletions exercises/practice/resistor-color/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Instructions

If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know two things about them:

- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.

These colors are encoded as follows:

- black: 0
- brown: 1
- red: 2
- orange: 3
- yellow: 4
- green: 5
- blue: 6
- violet: 7
- grey: 8
- white: 9

The goal of this exercise is to create a way:

- to look up the numerical value associated with a particular color band
- to list the different band colors

Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array:
Better Be Right Or Your Great Big Values Go Wrong.

More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code].

[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"resistor_color.s"
],
"test": [
"resistor_color_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Convert a resistor band's color to its numeric representation.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
}
62 changes: 62 additions & 0 deletions exercises/practice/resistor-color/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
.data
black: .string "black"
brown: .string "brown"
red: .string "red"
orange: .string "orange"
yellow: .string "yellow"
green: .string "green"
blue: .string "blue"
violet: .string "violet"
grey: .string "grey"
white: .string "white"

color_array:
.dword black
.dword brown
.dword red
.dword orange
.dword yellow
.dword green
.dword blue
.dword violet
.dword grey
.dword white
.dword 0 /* Sentinel value to indicate end of array */

.text
.globl color_code
.globl colors

/* extern int color_code(const char *color); */
color_code:
adrp x1, color_array
add x1, x1, :lo12:color_array
mov x2, x1

.next:
mov x3, x0
ldr x4, [x2], #8 /* load pointer, post-increment */
cbz x4, .invalid

.compare:
ldrb w5, [x3], #1 /* load character, post-increment */
ldrb w6, [x4], #1 /* load character, post-increment */
cmp w5, w6
bne .next

cbnz w5, .compare

sub x2, x2, #8
sub x0, x2, x1
lsr x0, x0, #3
ret

.invalid:
mov x0, #-1
ret

/* extern const char **colors(void); */
colors:
adrp x0, color_array
add x0, x0, :lo12:color_array
ret
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

[49eb31c5-10a8-4180-9f7f-fea632ab87ef]
description = "Color codes -> Black"

[0a4df94b-92da-4579-a907-65040ce0b3fc]
description = "Color codes -> White"

[5f81608d-f36f-4190-8084-f45116b6f380]
description = "Color codes -> Orange"

[581d68fa-f968-4be2-9f9d-880f2fb73cf7]
description = "Colors"
36 changes: 36 additions & 0 deletions exercises/practice/resistor-color/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
9 changes: 9 additions & 0 deletions exercises/practice/resistor-color/resistor_color.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.text
.globl color_code
.globl colors

color_code:
ret

colors:
ret
49 changes: 49 additions & 0 deletions exercises/practice/resistor-color/resistor_color_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "vendor/unity.h"

#include <stddef.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

extern int color_code(const char *color);
extern const char **colors(void);

void setUp(void) {
}

void tearDown(void) {
}

void test_black(void) {
TEST_ASSERT_EQUAL_INT(0, color_code("black"));
}

void test_white(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(9, color_code("white"));
}

void test_orange(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(3, color_code("orange"));
}

void test_colors(void) {
TEST_IGNORE();
const char *expected[] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
const char **color_array = colors();
size_t size;

for (size = 0; color_array[size]; size++) {
}
TEST_ASSERT_EQUAL_UINT(ARRAY_SIZE(expected), size);
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, color_array, size);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_black);
RUN_TEST(test_white);
RUN_TEST(test_orange);
RUN_TEST(test_colors);
return UNITY_END();
}
Loading

0 comments on commit b7ce960

Please sign in to comment.