From 7d14568e447ddadcf3d5cbffa259974311aa320c Mon Sep 17 00:00:00 2001 From: Adam Eury Date: Sun, 14 Jan 2024 22:51:23 -0500 Subject: [PATCH] Add DECX instruction --- examples/factorial.g | 2 +- gmachine.go | 4 ++++ gmachine_test.go | 14 ++++++++++++++ testdata/gc.txtar | 4 ++-- token/token.go | 1 + token/token_test.go | 1 + 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/examples/factorial.g b/examples/factorial.g index b4a0f2f..79c6dc6 100644 --- a/examples/factorial.g +++ b/examples/factorial.g @@ -1,6 +1,6 @@ ; Tasks: ; 1. Implement MULA - done -; 2. Implement SETX +; 2. Implement SETX - done ; 3. Implement DECX (and DECA, DECY) ; 4. Implement JXNZ ; 5. Implement CALL and RTRN diff --git a/gmachine.go b/gmachine.go index 376a667..8bea67a 100644 --- a/gmachine.go +++ b/gmachine.go @@ -25,6 +25,7 @@ const ( OpOUTA OpINCA OpDECA + OpDECX OpADDA OpMULA OpMOVA @@ -66,6 +67,7 @@ var opcodes = map[string]Word{ "OUTA": OpOUTA, "INCA": OpINCA, "DECA": OpDECA, + "DECX": OpDECX, "ADDA": OpADDA, "MULA": OpMULA, "MOVA": OpMOVA, @@ -130,6 +132,8 @@ func (g *Machine) Run() { g.A++ case OpDECA: g.A-- + case OpDECX: + g.X-- case OpADDA: switch g.Next() { case RegX: diff --git a/gmachine_test.go b/gmachine_test.go index c6a07a1..5dffca0 100644 --- a/gmachine_test.go +++ b/gmachine_test.go @@ -143,6 +143,20 @@ func TestDECA(t *testing.T) { } } +func TestDECX(t *testing.T) { + t.Parallel() + g := gmachine.New(nil) + g.X = 1 + var wantX gmachine.Word = 0 + err := assembleAndRunFromString(g, "DECX") + if err != nil { + t.Fatal("didn't expect an error", err) + } + if wantX != g.X { + t.Errorf("want X value %d, got %d", wantX, g.X) + } +} + func TestSETA(t *testing.T) { t.Parallel() g := gmachine.New(nil) diff --git a/testdata/gc.txtar b/testdata/gc.txtar index 273d5c9..f3ff2ea 100644 --- a/testdata/gc.txtar +++ b/testdata/gc.txtar @@ -9,6 +9,6 @@ SETA 42 OUTA -- want -- -0000000 0000 0000 0000 0900 0000 0000 0000 2a00 +0000000 0000 0000 0000 0a00 0000 0000 0000 2a00 0000010 0000 0000 0000 0300 -0000018 \ No newline at end of file +0000018 diff --git a/token/token.go b/token/token.go index c3bb7f5..8ad9979 100644 --- a/token/token.go +++ b/token/token.go @@ -24,6 +24,7 @@ var opcodes = map[string]TokenType{ "OUTA": OPCODE, "INCA": OPCODE, "DECA": OPCODE, + "DECX": OPCODE, "ADDA": OPCODE, "MULA": OPCODE, "MOVA": OPCODE, diff --git a/token/token_test.go b/token/token_test.go index b90c604..587c888 100644 --- a/token/token_test.go +++ b/token/token_test.go @@ -16,6 +16,7 @@ func TestLookupIdent(t *testing.T) { {"OUTA", token.OPCODE}, {"INCA", token.OPCODE}, {"DECA", token.OPCODE}, + {"DECX", token.OPCODE}, {"ADDA", token.OPCODE}, {"MULA", token.OPCODE}, {"MOVA", token.OPCODE},