Improved assembler

This commit is contained in:
Eduard Urbach 2023-10-23 12:37:20 +02:00
parent d5f752bdd4
commit 8e193c69b6
Signed by: eduard
GPG key ID: 49226B848C78F6C8
22 changed files with 329 additions and 139 deletions

View file

@ -1,8 +1,38 @@
package asm
import "io"
import (
"fmt"
"io"
type Instruction interface {
Write(io.ByteWriter)
String() string
"git.akyoto.dev/cli/q/src/asm/x64"
"git.akyoto.dev/cli/q/src/register"
)
// Instruction represents a single instruction which can be converted to machine code.
type Instruction struct {
Mnemonic Mnemonic
Source register.ID
Destination register.ID
Number uint64
}
// Write writes the machine code of the instruction.
func (x *Instruction) Write(w io.ByteWriter) {
switch x.Mnemonic {
case MOV:
x64.MoveRegNum32(w, uint8(x.Destination), uint32(x.Number))
case SYSCALL:
x64.Syscall(w)
}
}
func (x *Instruction) String() string {
switch x.Mnemonic {
case MOV:
return fmt.Sprintf("%s %s, %x", x.Mnemonic, x.Destination, x.Number)
case SYSCALL:
return x.Mnemonic.String()
default:
return ""
}
}