all repos — xyw @ 60e8c3488fd4af7de0ae9d8b97707687faa1b1c8

A minimal virtual machine and assembler for terminals.

Started actually assembling data.
h3rald h3rald@h3rald.com
Thu, 27 Nov 2025 15:00:40 +0100
commit

60e8c3488fd4af7de0ae9d8b97707687faa1b1c8

parent

4556523d0daf2ca52553186579e3c7691a73e880

2 files changed, 39 insertions(+), 8 deletions(-)

jump to
M xyw.cxyw.c

@@ -123,7 +123,6 @@ // TODO: Standardize errors

fprintf(stderr, "Assembly failed for file: %s\n", input_file); return -1; } - printf("Assembled %s to %s successfully.\n", input_file, output_file); } else {
M xywasm.cxywasm.c

@@ -62,11 +62,14 @@ static unsigned int token_length = 0;

static xyw_token_type token_type = TOKEN_TYPE_UNKNOWN; static xyw_short token_value = 0; +// Tracks whether we are inside a directive or not +static unsigned int directive = 0; + // Bytecode to be written -xyw_byte data[0xffff - 0x0100]; +xyw_byte data[0xffff]; // Current write pointer -xyw_short ptr = 0x0100; +xyw_short ptr = 0x0000; //// Utilities static xyw_token_terminator next_token(xyw_context *ctx);

@@ -212,6 +215,10 @@ // TODO: Remove

printf("Hex number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; + if (!directive) + { + push(value); + } return 0; }

@@ -241,6 +248,10 @@ // TODO: Remove

printf("Binary number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; + if (!directive) + { + push(value); + } return 0; }

@@ -276,6 +287,10 @@ // TODO: Remove

printf("Decimal number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; + if (!directive) + { + push(value); + } return 0; }

@@ -340,6 +355,10 @@ printf("Instruction:\t%s\t(%02X - %s)\n", token, opcode, bits);

write(opcode); token_type = TOKEN_TYPE_INSTRUCTION; token_value = opcode; + if (!directive) + { + push(opcode); + } return 0; } }

@@ -349,10 +368,8 @@

static int process_string(xyw_context *ctx) { (void)ctx; - // Token is a string enclosed in double quotes // TODO: Remove printf("String:\t\t%s\n", token); - // TODO: Store string data token_type = TOKEN_TYPE_STRING; return 0; }

@@ -377,7 +394,8 @@ {

return -1; } printf("Directive:\t.org %s\n", token); - // TODO: process org + // Move the pointer to the specified address + ptr = (xyw_short)token_value; return validate_single_directive_argument(ctx, r); }

@@ -417,7 +435,12 @@ return -1;

} process_string(ctx); printf("Directive:\t.string %s\n", token); - // TODO: process string + // Store string in memory (without quotes) + for (unsigned int i = 1; i < token_length - 1; i++) + { + data[ptr++] = (xyw_byte)token[i]; + } + data[ptr++] = 0; return validate_single_directive_argument(ctx, r); }

@@ -432,11 +455,17 @@ if (process_number(ctx) != 0)

{ return -1; } + if (token_value > 0xFF) + { + token_error("Byte value out of range (0-255)"); + return -1; + } if (r == TOKEN_END_EOL || r == TOKEN_END_EOF || r == TOKEN_END_COMMENT) { break; } - write((xyw_byte)token_value); + // Store byte in memory + data[ptr++] = (xyw_byte)token_value; } return 0; }

@@ -486,6 +515,7 @@ if (validate_symbol(ctx, token + 1, token_length - 1) != 0)

{ return -1; } + directive = 1; token_type = TOKEN_TYPE_DIRECTIVE; int r; if (strcmpn(token, ".label", 6) == 0)

@@ -517,6 +547,7 @@ {

token_error("Unknown directive"); r = -1; } + directive = 0; return r; }

@@ -682,5 +713,6 @@ return -1;

} (void)output; // TODO: write to output file + printf("Assembled %s successfully. Bytecode size: %d bytes\n", input, ptr); return 0; }