From 3c67b0fa7e704a084ac83583725a264e8e01c3fd Mon Sep 17 00:00:00 2001 From: Adam Eury Date: Wed, 16 Aug 2023 00:53:11 -0400 Subject: [PATCH] Add register X. Update instruction ADDA to ADAX. Add MVAX instruction. --- gmachine.go | 22 +++++++++++--------- gmachine_test.go | 53 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/gmachine.go b/gmachine.go index b75329a..882cdf5 100644 --- a/gmachine.go +++ b/gmachine.go @@ -22,7 +22,8 @@ const ( OpOUTA OpINCA OpDECA - OpADDA + OpADAX + OpMVAX OpSETA OpPSHA OpPOPA @@ -44,6 +45,7 @@ type Machine struct { P Word S Word A Word + X Word E Word Out io.Writer MemOffset Word @@ -55,6 +57,7 @@ func New(out io.Writer) *Machine { P: Word(0), S: Word(0), A: Word(0), + X: Word(0), E: Word(0), Out: out, MemOffset: StackSize, @@ -82,9 +85,10 @@ func (g *Machine) Run() { g.A++ case OpDECA: g.A-- - case OpADDA: - g.A += g.Memory[g.MemOffset+g.P] - g.P++ + case OpADAX: + g.A += g.X + case OpMVAX: + g.X = g.A case OpSETA: g.A = g.Memory[g.MemOffset+g.P] g.P++ @@ -131,12 +135,10 @@ func Assemble(input string) ([]Word, error) { program = append(program, OpPSHA) case "POPA": program = append(program, OpPOPA) - case "ADDA": - num, err := strconv.ParseUint(parts[1], 10, 64) - if err != nil { - return nil, fmt.Errorf("%w: %s at line %d", ErrInvalidNumber, parts[1], lineNo+1) - } - program = append(program, OpADDA, Word(num)) + case "ADAX": + program = append(program, OpADAX) + case "MVAX": + program = append(program, OpMVAX) case "SETA": var operand Word if strings.HasPrefix(parts[1], "'") && strings.HasSuffix(parts[1], "'") { diff --git a/gmachine_test.go b/gmachine_test.go index c96f08a..40df867 100644 --- a/gmachine_test.go +++ b/gmachine_test.go @@ -28,6 +28,10 @@ func TestNew(t *testing.T) { if wantA != g.A { t.Errorf("want initial A value %d, got %d", wantA, g.A) } + var wantX gmachine.Word = 0 + if wantX != g.X { + t.Errorf("want initial X value %d, got %d", wantX, g.X) + } var wantMemValue gmachine.Word = 0 gotMemValue := g.Memory[gmachine.MemSize-1] if wantMemValue != gotMemValue { @@ -319,11 +323,29 @@ POPA } } -func TestADDA(t *testing.T) { +func TestMVAX(t *testing.T) { t.Parallel() g := gmachine.New(nil) - var wantA gmachine.Word = 4 - err := g.AssembleAndRun("SETA 2\nADDA 2") + err := g.AssembleAndRun("SETA 42\nMVAX\n") + if err != nil { + t.Fatal("didn't expect an error:", err) + } + var wantX gmachine.Word = 42 + if wantX != g.X { + t.Errorf("want X %d, got %d", wantX, g.X) + } +} + +func TestADAX(t *testing.T) { + t.Parallel() + g := gmachine.New(nil) + var wantA gmachine.Word = 10 + err := g.AssembleAndRun(` +SETA 6 +MVAX +SETA 4 +ADAX +`) if err != nil { t.Fatal("didn't expect an error:", err) } @@ -332,15 +354,26 @@ func TestADDA(t *testing.T) { } } -func TestADDAWithInvalidNumber(t *testing.T) { +func TestAddTwoNumbers(t *testing.T) { t.Parallel() g := gmachine.New(nil) - err := g.AssembleAndRun("ADDA a") - wantErr := gmachine.ErrInvalidNumber - if err == nil { - t.Fatal("expected an error to be returned for invalid argument to SETA") + err := g.AssembleAndRun(` +; x = 4, y = 6 +SETA 4 +PSHA +SETA 6 +PSHA +; add x y +POPA +MVAX +POPA +ADAX +`) + if err != nil { + t.Fatal("didn't expect an error:", err) } - if !errors.Is(err, wantErr) { - t.Errorf("wanted error %v, got %v", wantErr, err) + var wantA gmachine.Word = 10 + if wantA != g.A { + t.Errorf("want A %d, got %d", wantA, g.A) } }