Improved assembler
This commit is contained in:
parent
ab48a86ccd
commit
a5ba316319
4
src/asm/Address.go
Normal file
4
src/asm/Address.go
Normal file
@ -0,0 +1,4 @@
|
||||
package asm
|
||||
|
||||
// Address represents a memory address.
|
||||
type Address = uint32
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user