From 8d072d8dc0d22ffd716f53f40893644b8e748fc2 Mon Sep 17 00:00:00 2001 From: Moon Flower Date: Wed, 13 Aug 2025 19:33:41 +0330 Subject: arithmetics unit in emu --- pu2/pu2.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'pu2/pu2.c') diff --git a/pu2/pu2.c b/pu2/pu2.c index 66fefbf..a20d4ff 100644 --- a/pu2/pu2.c +++ b/pu2/pu2.c @@ -89,6 +89,37 @@ emu() { m[ 2 ] = getchar(); } + // 2: arithmetics unit + else if (int_code == 2) { + int op = m[ 2 ]; + int val1 = m[ 3 ]; + int val2 = m[ 4 ]; + + m[ 2 ] = 0; + + if (op == 0) { + // multiply + m[ 3 ] = val1 * val2; + } else if (op == 1) { + // divide + m[ 3 ] = val1 / val2; + } else if (op == 2) { + // modulo + m[ 3 ] = val1 % val2; + } else if (op == 3) { + // left shift + m[ 3 ] = val1 << val2; + } else if (op == 4) { + // right shift + m[ 3 ] = val1 >> val2; + } + + else { + // unknown op, set m[2] + m[ 2 ] = 1; + } + } + else { // unknown interrupt. what to do? } @@ -194,7 +225,7 @@ clean_slice_ref(slice *val) { free(val->ref); } str_len++; \ int *dest_start = Data -= str_len; \ int y; \ - for (y = 0; y < str_len-1; y++) { dest_start[ y ] = str_start[ y ]; } \ + for (y = 0; y < str_len - 1; y++) { dest_start[ y ] = str_start[ y ]; } \ Data[ -1 ] = 0; \ value = Data - m; \ } else { \ @@ -204,7 +235,7 @@ clean_slice_ref(slice *val) { free(val->ref); } value; \ }) -T; +char T; string ERR; string ERR_code; int *Code = &m[ 4000 ]; @@ -218,7 +249,7 @@ _ir2(string I, entry macros[ 1 << 9 ]) { if (T == '/' && I[ 1 ] == '/') { // comment I += 2; - while (*I != '\n') I++; + while (*I != '\n' && *I != '\0') I++; } // handle assignments! @@ -499,7 +530,7 @@ string op_names[] = { "hlt", "imm", "lod", "str", "swp", "add", "sub", "and", "n main() { size_t o_size; - ir2(read_file("testing.i2", &o_size)); + ir2(read_file("code.i2", &o_size)); m[ 7 ] = 4000; size_t res_size = Code - &m[ 4000 ]; -- cgit v1.2.3