Added file scanner

This commit is contained in:
2023-10-30 16:17:41 +01:00
parent 5fe83543fd
commit 8b19989372
10 changed files with 178 additions and 71 deletions

View File

@ -2,6 +2,3 @@ package asm
// Address represents a memory address.
type Address = uint32
// Index references an instruction by its position.
// type Index = uint32

View File

@ -12,20 +12,17 @@ import (
// Assembler contains a list of instructions.
type Assembler struct {
Instructions []Instruction
// labels map[string]Index
Verbose bool
}
// New creates a new assembler.
func New() *Assembler {
return &Assembler{
Instructions: make([]Instruction, 0, 8),
// labels: map[string]Index{},
}
}
// Finalize generates the final machine code.
func (a *Assembler) Finalize() ([]byte, []byte) {
func (a *Assembler) Finalize(verbose bool) ([]byte, []byte) {
code := make([]byte, 0, len(a.Instructions)*8)
data := make(Data, 0, 16)
pointers := []Pointer{}
@ -46,8 +43,10 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
case SYSCALL:
code = x64.Syscall(code)
}
}
if a.Verbose {
if verbose {
for _, x := range a.Instructions {
log.Info.Println(x.String())
}
}
@ -63,11 +62,6 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
return code, data
}
// AddLabel creates a new label at the current position.
// func (a *Assembler) AddLabel(name string) {
// a.labels[name] = Index(len(a.instructions))
// }
// MoveRegisterData moves a data section address into the given register.
func (a *Assembler) MoveRegisterData(reg register.ID, data []byte) {
a.Instructions = append(a.Instructions, Instruction{

41
src/asm/Assembler_test.go Normal file
View File

@ -0,0 +1,41 @@
package asm_test
import (
"testing"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/linux"
"git.akyoto.dev/cli/q/src/register"
"git.akyoto.dev/go/assert"
)
func TestHello(t *testing.T) {
a := asm.New()
hello := []byte("Hello\n")
a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1)
a.MoveRegisterData(register.Syscall2, hello)
a.MoveRegisterNumber(register.Syscall3, uint64(len(hello)))
a.Syscall()
a.MoveRegisterNumber(register.Syscall0, linux.Exit)
a.MoveRegisterNumber(register.Syscall1, 0)
a.Syscall()
code, data := a.Finalize(false)
assert.DeepEqual(t, code, []byte{
0xb8, 0x01, 0x00, 0x00, 0x00,
0xbf, 0x01, 0x00, 0x00, 0x00,
0xbe, 0xa2, 0x00, 0x40, 0x00,
0xba, 0x06, 0x00, 0x00, 0x00,
0x0f, 0x05,
0xb8, 0x3c, 0x00, 0x00, 0x00,
0xbf, 0x00, 0x00, 0x00, 0x00,
0x0f, 0x05,
})
assert.DeepEqual(t, data, hello)
}