Skip to content

Commit

Permalink
Added instr_write_bytes() (#48)
Browse files Browse the repository at this point in the history
This pull has added the `instr_write_bytes()` function which is similar/
equivilent to the `db` directive in the usual NASM syntax, this new
function takes in a size and uses varidict arguments to define the x
amount of bytes as passed into the size argument. For more information,
see patch file and/or changelog
  • Loading branch information
cheng-alvin authored Jan 17, 2025
1 parent 2150648 commit cde3543
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions libjas/include/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ typedef struct {
*/
instr_encode_table_t instr_get_tab(instruction_t instr);

/**
* Function for generating an instruction struct with the given
* instruction type and operands. The function is used to create
* a Jas buffer and write it into a instruction, similar to the
* `db` and `dw` directives in NASM, but this uses the size and
* variadic arguments to write the bytes into the buffer.
*
* @param data_sz The size of the data to write
* @param ... The data to write into the buffer
*
* @return The instruction struct
*
* @example The **Jas** function call of:
* > instr_write_bytes(7, 0x48, 0x89, 0x80, 0xff, 0x00, 0x00, 0x00);
*
* Is equivalent to: (In NASM)
* > db 0x48, 0x89, 0x80, 0xff, 0x00, 0x00, 0x00
*/
instruction_t instr_write_bytes(size_t data_sz, ...);

/**
* Macros for defining the instruction operands in a more readable
* form for passing into the `instr_gen` function. The macros are
Expand Down
21 changes: 21 additions & 0 deletions libjas/instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,25 @@ instruction_t instr_gen(enum instructions instr, uint8_t operand_count, ...) {
.instr = instr,
.operands = operands,
};
}

instruction_t instr_write_bytes(size_t data_sz, ...) {
buffer_t data = BUF_NULL;
va_list args;
va_start(args, data_sz);

for (size_t i = 0; i < data_sz; i++) {
const uint8_t byte = va_arg(args, uint8_t);
buf_write_byte(&data, byte);
}

// clang-format off
return (instruction_t){
.instr = INSTR_DIR_WRT_BUF,
.operands = (operand_t[]){
op_construct_operand(OP_MISC, 0, &data, NULL),
OP_NONE, OP_NONE, OP_NONE,
},
};
// clang-format on
}

0 comments on commit cde3543

Please sign in to comment.