From ae4b3d029f50058caaa270368f8e979512ba7c3b Mon Sep 17 00:00:00 2001 From: Alvin Cheng <88267875+cheng-alvin@users.noreply.github.com> Date: Thu, 2 Nov 2023 20:11:42 +1100 Subject: [PATCH] Implemented and documented `write8` function --- include/write.h | 19 +++++++++++++++++++ src/codegen/write.c | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 include/write.h create mode 100644 src/codegen/write.c diff --git a/include/write.h b/include/write.h new file mode 100644 index 00000000..ee25df76 --- /dev/null +++ b/include/write.h @@ -0,0 +1,19 @@ +#include + +/** + * @author cheng-alvin + * @since v0.0.1 + * + * Writes 8 bits or a single byte to the specified `buffer` + * pointer. Useful for generating machine code in executables. + * + * @param buffer The pointer for the buffer to write to. + * @param value The value to write. + * + * @note Please note that the value should be 1 byte + * or 8 bits wide when passing into this function. + * + * @returns The pointer to the buffer. + */ + +uint8_t *write8(uint8_t *buffer, uint8_t value); diff --git a/src/codegen/write.c b/src/codegen/write.c new file mode 100644 index 00000000..d835a2f7 --- /dev/null +++ b/src/codegen/write.c @@ -0,0 +1,11 @@ +#include +#include + +uint8_t *write8(uint8_t *buffer, uint8_t value) { + const int len = *buffer / sizeof(uint8_t); + buffer = realloc(buffer, len + sizeof(uint8_t)); + + buffer[len] = value; + + return buffer; +} \ No newline at end of file