From 20621e073562ee5d423b636fae8b6aa8e38275fa Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 24 May 2023 22:29:46 -0400 Subject: readme and cleanup --- as/as | Bin 2088979 -> 0 bytes as/as.go | 20 +++++++++++++++++--- as/main.go | 3 +-- as/opcodes.go | 14 +++++++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) delete mode 100755 as/as (limited to 'as') diff --git a/as/as b/as/as deleted file mode 100755 index 3c80b92..0000000 Binary files a/as/as and /dev/null differ diff --git a/as/as.go b/as/as.go index bcc7cf8..9a2cc11 100644 --- a/as/as.go +++ b/as/as.go @@ -2,30 +2,44 @@ package main import ( "fmt" + "os" "strings" "github.com/holiman/uint256" - ) func Assemble(code string) []byte { bytecode := make([]byte, 0) t := strings.Fields(code) + push := byte(0) for i := range t { x, found := OpcodeStrings[t[i]] if found { - bytecode = append(bytecode, x) + push = (&x).IsPush() + fmt.Printf("token: %s, opcode: 0x%x, push = %d\n", t[i], int(x), int(push)) + bytecode = append(bytecode, byte(x)) } else { + if push == 0 { + fmt.Fprintf(os.Stderr, "unexpected token: %s\n", t[i]) + os.Exit(1) + } var n *uint256.Int = nil if len(t[i]) > 1 && t[i][1] == 'x' { n, _ = uint256.FromHex(t[i]) } else { n, _ = uint256.FromDecimal(t[i]) } + + if (t[i] == "PUSH") { + push = byte(n.ByteLen()) + } + + b := n.Bytes32() - bytecode = append(bytecode, b[:]...) + fmt.Printf("token: NUMBER, value: %s, push = %d\n", t[i], int(push), b[32 - push:]) + bytecode = append(bytecode, b[32-push:]...) } } return bytecode diff --git a/as/main.go b/as/main.go index 95aff46..6476dc2 100644 --- a/as/main.go +++ b/as/main.go @@ -6,7 +6,7 @@ import ( ) func usage(arg0 string) { - fmt.Fprintf(os.Stderr, "usage: %s [-o file] file.easm\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "usage: %s [-o file] file.easm\n", arg0) os.Exit(1) } @@ -20,7 +20,6 @@ func main() { var ain int = 0 for i := 0; i < len(os.Args); i++ { - fmt.Println(os.Args[i]) switch os.Args[i] { case "-o": i = i + 1 diff --git a/as/opcodes.go b/as/opcodes.go index 94c2e2b..be04d19 100644 --- a/as/opcodes.go +++ b/as/opcodes.go @@ -49,9 +49,10 @@ const ( PUSH30 = 0x7d PUSH31 = 0x7e PUSH32 = 0x7f + RETURN = 0xf3 ) -var OpcodeStrings = map[string]byte { +var OpcodeStrings = map[string]Opcode { "STOP": STOP, "ADD": ADD, "MUL": MUL, @@ -66,6 +67,7 @@ var OpcodeStrings = map[string]byte { "MLOAD": MLOAD, "MSTORE": MSTORE, "MSTORE8": MSTORE8, + "PUSH": PUSH1, "PUSH1": PUSH1, "PUSH2": PUSH2, "PUSH3": PUSH3, @@ -98,4 +100,14 @@ var OpcodeStrings = map[string]byte { "PUSH30": PUSH30, "PUSH31": PUSH31, "PUSH32": PUSH32, + "RETURN": RETURN, +} + +// returns 0 if not push, otherwise returns number of bytes +func (o *Opcode) IsPush() byte { + d := *o - PUSH1 + if d >= 0 && d < 31 { + return byte(d) + 1 + } + return 0 } -- cgit v1.2.3