From c67028520cfdd67592e70a0fb2c476553fbef44a Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 20 Nov 2021 10:19:08 -1000 Subject: [PATCH] Hex and binary literal support in hex --- asm/assemble.go | 4 ++-- test/asm_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/asm/assemble.go b/asm/assemble.go index 1d3d26e..0e9dca8 100644 --- a/asm/assemble.go +++ b/asm/assemble.go @@ -106,14 +106,14 @@ func assembleOperand(op string, t operandType) (*vm.Operand, error) { switch { case t == s || (t == us && (strings.HasPrefix(numStr, "+") || strings.HasPrefix(numStr, "-"))): - num, err := strconv.ParseInt(numStr, 10, 64) + num, err := strconv.ParseInt(numStr, 0, 64) if err != nil { return ret, errors.Wrapf(err, "While parsing operand value %s", numStr) } ret.Value = uint64(num) case t == u || t == us || t == r: - num, err := strconv.ParseUint(numStr, 10, 64) + num, err := strconv.ParseUint(numStr, 0, 64) if err != nil { return ret, errors.Wrapf(err, "While parsing operand value %s", numStr) } diff --git a/test/asm_test.go b/test/asm_test.go index e10e812..48ec345 100644 --- a/test/asm_test.go +++ b/test/asm_test.go @@ -33,3 +33,33 @@ functions: t.Fatal("Lack of error when passing signed value to ltu") } } + +func TestHex(t *testing.T) { + prog, err := asm.AssembleString(` +functions: +- - [mov, g0, 0xfeedc0de] +`) + if err != nil { + t.Fatal(err) + } + + val := prog.Functions[0].Instructions[0].Operands[1].Value + if val != 4276994270 { + t.Fatalf("Mismatch: expected=4276994270, actual=%d", val) + } +} + +func TestBinary(t *testing.T) { + prog, err := asm.AssembleString(` +functions: +- - [mov, g0, 0b100101] +`) + if err != nil { + t.Fatal(err) + } + + val := prog.Functions[0].Instructions[0].Operands[1].Value + if val != 37 { + t.Fatalf("Mismatch: expected=37, actual=%d", val) + } +}