Skip to content

Commit

Permalink
Add DECX instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
aleury committed Jan 15, 2024
1 parent b7dcbef commit 7d14568
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/factorial.g
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions gmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
OpOUTA
OpINCA
OpDECA
OpDECX
OpADDA
OpMULA
OpMOVA
Expand Down Expand Up @@ -66,6 +67,7 @@ var opcodes = map[string]Word{
"OUTA": OpOUTA,
"INCA": OpINCA,
"DECA": OpDECA,
"DECX": OpDECX,
"ADDA": OpADDA,
"MULA": OpMULA,
"MOVA": OpMOVA,
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions gmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions testdata/gc.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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
0000018
1 change: 1 addition & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var opcodes = map[string]TokenType{
"OUTA": OPCODE,
"INCA": OPCODE,
"DECA": OPCODE,
"DECX": OPCODE,
"ADDA": OPCODE,
"MULA": OPCODE,
"MOVA": OPCODE,
Expand Down
1 change: 1 addition & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 7d14568

Please sign in to comment.