Skip to content

Commit

Permalink
Add register X. Update instruction ADDA to ADAX. Add MVAX instruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
aleury committed Aug 16, 2023
1 parent 33f2ddc commit 3c67b0f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
22 changes: 12 additions & 10 deletions gmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const (
OpOUTA
OpINCA
OpDECA
OpADDA
OpADAX
OpMVAX
OpSETA
OpPSHA
OpPOPA
Expand All @@ -44,6 +45,7 @@ type Machine struct {
P Word
S Word
A Word
X Word
E Word
Out io.Writer
MemOffset Word
Expand All @@ -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,
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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], "'") {
Expand Down
53 changes: 43 additions & 10 deletions gmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}

0 comments on commit 3c67b0f

Please sign in to comment.