Improved assembler

This commit is contained in:
2023-10-23 12:37:20 +02:00
parent a54c62f6e0
commit ab48a86ccd
22 changed files with 329 additions and 139 deletions

42
src/asm/Assembler.go Normal file
View File

@ -0,0 +1,42 @@
package asm
import (
"git.akyoto.dev/cli/q/src/register"
)
// Assembler contains a list of instructions.
type Assembler struct {
Instructions []Instruction
}
// New creates a new assembler.
func New() *Assembler {
return &Assembler{
Instructions: make([]Instruction, 0, 8),
}
}
// Finalize generates the final machine code.
func (list *Assembler) Finalize() *Result {
final := Result{}
for _, instr := range list.Instructions {
instr.Write(&final.Code)
}
return &final
}
func (list *Assembler) MoveRegisterNumber(reg register.ID, number uint64) {
list.Instructions = append(list.Instructions, Instruction{
Mnemonic: MOV,
Destination: reg,
Number: number,
})
}
func (list *Assembler) Syscall() {
list.Instructions = append(list.Instructions, Instruction{
Mnemonic: SYSCALL,
})
}