diff --git a/src/asm/Address.go b/src/asm/Address.go new file mode 100644 index 0000000..14b073b --- /dev/null +++ b/src/asm/Address.go @@ -0,0 +1,4 @@ +package asm + +// Address represents a memory address. +type Address = uint32 diff --git a/src/asm/Assembler.go b/src/asm/Assembler.go index 8f0e6d3..a751e33 100644 --- a/src/asm/Assembler.go +++ b/src/asm/Assembler.go @@ -6,37 +6,46 @@ import ( // Assembler contains a list of instructions. type Assembler struct { - Instructions []Instruction + instructions []Instruction + labels map[string]int } // New creates a new assembler. func New() *Assembler { return &Assembler{ - Instructions: make([]Instruction, 0, 8), + instructions: make([]Instruction, 0, 8), + labels: map[string]int{}, } } // Finalize generates the final machine code. -func (list *Assembler) Finalize() *Result { +func (a *Assembler) Finalize() *Result { final := Result{} - for _, instr := range list.Instructions { + for _, instr := range a.instructions { instr.Write(&final.Code) } return &final } -func (list *Assembler) MoveRegisterNumber(reg register.ID, number uint64) { - list.Instructions = append(list.Instructions, Instruction{ +// AddLabel creates a new label at the current position. +func (a *Assembler) AddLabel(name string) { + a.labels[name] = len(a.instructions) +} + +// MoveRegisterNumber moves a number into the given register. +func (a *Assembler) MoveRegisterNumber(reg register.ID, number uint64) { + a.instructions = append(a.instructions, Instruction{ Mnemonic: MOV, Destination: reg, Number: number, }) } -func (list *Assembler) Syscall() { - list.Instructions = append(list.Instructions, Instruction{ +// Syscall executes a kernel function. +func (a *Assembler) Syscall() { + a.instructions = append(a.instructions, Instruction{ Mnemonic: SYSCALL, }) } diff --git a/src/asm/Instruction.go b/src/asm/Instruction.go index 210f253..1883cee 100644 --- a/src/asm/Instruction.go +++ b/src/asm/Instruction.go @@ -26,6 +26,7 @@ func (x *Instruction) Write(w io.ByteWriter) { } } +// String returns the assembler representation of the instruction. func (x *Instruction) String() string { switch x.Mnemonic { case MOV: