umx_asm

UMX virtual machine assembly compiler
git clone git://bsandro.tech/umx_asm
Log | Files | Refs | README | LICENSE

commit dd6689c60b7d3e3e9e327303f5277c6d3a389aed
parent 199674cc3db52a5628cbe2ea07f005c2229d059a
Author: bsandro <email@bsandro.tech>
Date:   Tue,  5 Jul 2022 01:58:28 +0300

quick and dirty compilation - first 12 opcodes supported

Diffstat:
Masm/instruction.go | 9+++++++++
Mmain.go | 36++++++++++++++++++++++++++++++++----
Msample.ums | 2+-
3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/asm/instruction.go b/asm/instruction.go @@ -58,6 +58,15 @@ func NewInstruction(input []string) *Instruction { return &instr } +func (i *Instruction) Pack() uint32 { + var ret uint32 = uint32(i.Opcode) << 28 + var r1 uint32 = uint32(i.RegA) << 6 + var r2 uint32 = uint32(i.RegB) << 3 + var r3 uint32 = uint32(i.RegC) + ret = ret | r1 | r2 | r3 + return ret +} + func (i *Instruction) String() string { var buf bytes.Buffer buf.WriteString(i.Opcode.String()) diff --git a/main.go b/main.go @@ -2,31 +2,59 @@ package main import ( "bufio" + "encoding/binary" "fmt" + "log" "os" + "path/filepath" "regexp" "strings" "umx_asm/asm" ) +const allowedExt = ".ums" + func main() { re := regexp.MustCompile(`[a-zA-Z0-9]+`) - file, err := os.Open("sample.ums") + if len(os.Args) < 2 { + log.Fatal("no assembly file supplied") + } + filename := os.Args[1] + if filepath.Ext(filename) != allowedExt { + log.Fatal(fmt.Sprintf("only files with %s extension are supported", allowedExt)) + } + file, err := os.Open(filename) if err != nil { - fmt.Println(err) - return + log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) + var bytecode []byte for scanner.Scan() { str, _, _ := strings.Cut(scanner.Text(), ";") if len(str) > 0 { ins := asm.NewInstruction(re.FindAllString(str, -1)) fmt.Println(ins.String()) + fmt.Printf("%.32b\n", ins.Pack()) + buf := make([]byte, 4) + binary.BigEndian.PutUint32(buf, ins.Pack()) + bytecode = append(bytecode, buf...) } } if err := scanner.Err(); err != nil { - fmt.Println(err) + log.Fatal(err) + } + fmt.Printf("code of %d bytes\n", len(bytecode)) + for i, b := range bytecode { + fmt.Printf("%.8b", b) + if (i+1)%4 == 0 { + fmt.Printf("\n") + } + } + // dump into file + newfilename := strings.TrimSuffix(filename, allowedExt) + ".um" + if err := os.WriteFile(newfilename, bytecode, 0660); err != nil { + log.Fatal(err) } } diff --git a/sample.ums b/sample.ums @@ -3,5 +3,5 @@ cmov 1 2 3 arri 1 8 15 -orth 5 493028 +;orth 5 493028 halt