Skip to content

Commit

Permalink
Implemented and documented write8 function
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng-alvin committed Nov 2, 2023
1 parent 8d515bc commit ae4b3d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/write.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdint.h>

/**
* @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);
11 changes: 11 additions & 0 deletions src/codegen/write.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdint.h>
#include <stdlib.h>

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;
}

0 comments on commit ae4b3d0

Please sign in to comment.