all repos — nand2tetris @ 04bd5fdba6789c1801f558e957b3c833397cb7bc

my nand2tetris progress

Rewrite: Add parse_c_type() structure
x1phosura x1phosura@x1phosura.zone
Mon, 06 Feb 2023 15:32:31 -0800
commit

04bd5fdba6789c1801f558e957b3c833397cb7bc

parent

c5ab5300b95bfef6db00c654c5ae76f06130c988

1 files changed, 55 insertions(+), 4 deletions(-)

jump to
M projects/06/assembler2/assembler2.cprojects/06/assembler2/assembler2.c

@@ -140,13 +140,64 @@

return add_symbol(label_str, (uint16_t)instruction_offset); } +bool parse_c_type_dest(const char *dest_line, uint16_t *dest) +{ + // also length check here since it's not being done in parse_c_type() + *dest = 0x05; + return true; +} + +bool parse_c_type_comp(const char *comp_line, uint16_t *comp) +{ + // also length check here since it's not being done in parse_c_type() + *comp = 0x32; + return true; +} + +bool parse_c_type_jump(const char *jump_line, uint16_t *jump) +{ + // also length check here since it's not being done in parse_c_type() + *jump = 3; + return true; +} + bool parse_c_type(const char *line, uint16_t *instruction) { - DBGLOG("C-type, offset %lu, line %lu | %s\n", instruction_offset, + size_t i; + char c; + const char *dest_start = NULL, *comp_start = NULL, *jump_start = NULL; + uint16_t dest = 0, comp = 0, jump = 0; // TODO: remove, just use instruction + + c = line[0]; + for (i = 0; !is_whitespace(c) && c != '\n' && c != '\0'; ++i) { + c = line[i]; + if (c == '=') { + dest_start = &line[0]; + comp_start = &line[i+1]; + } else if (c == ';') + jump_start = &line[i+1]; + } + + DBGLOG("C-type, offset %lu, line %lu | %s \n", instruction_offset, file_line_no, line); - uint16_t val = 0xb9c3; - *instruction = val; - return true; // STUB TODO remove me + + // only comp field is mandatory; dest/jump are optional, may not exist + if (comp_start == NULL) + comp_start = &line[0]; + + if (!parse_c_type_comp(comp_start, &comp)) + return false; + + if (dest_start != NULL) + if (!parse_c_type_dest(dest_start, &dest)) + return false; + + if (jump_start != NULL) + if (!parse_c_type_jump(jump_start, &jump)) + return false; + + *instruction = 0xe000 | (comp << 6) | (dest << 3) | jump; + return true; } bool parse_a_type(const char *line, uint16_t *instruction)