all repos — nand2tetris @ b41b4800291586c227f6fcacf7a3b51fb0d7caf7

my nand2tetris progress

Rewrite: Parse C-type dest field
x1phosura x1phosura@x1phosura.zone
Wed, 08 Feb 2023 00:43:36 -0800
commit

b41b4800291586c227f6fcacf7a3b51fb0d7caf7

parent

04bd5fdba6789c1801f558e957b3c833397cb7bc

1 files changed, 24 insertions(+), 2 deletions(-)

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

@@ -142,8 +142,30 @@ }

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; + size_t i; + uint8_t a_dest = 0, d_dest = 0, m_dest = 0; + + for (i = 0; dest_line[i] != '='; ++i) { + switch (dest_line[i]) { + case 'A': ++a_dest; break; + case 'D': ++d_dest; break; + case 'M': ++m_dest; break; + default: return err("error: invalid destination register " + "'%c'\n", dest_line[i]); + } + + if (i >= 3) + return err("error: too many destinations\n"); + } + + if (a_dest > 1) + return err("err: multiple 'A's found in destination\n"); + if (d_dest > 1) + return err("err: multiple 'D's found in destination\n"); + if (m_dest > 1) + return err("err: multiple 'M's found in destination\n"); + + *dest = (uint16_t)((a_dest << 2) | (d_dest << 1) | (m_dest)); return true; }