Improved formatting of load and store commands

This commit is contained in:
2025-02-16 19:29:22 +01:00
parent c633f94d29
commit dc3ba6504f
4 changed files with 36 additions and 23 deletions

View File

@ -1,6 +1,12 @@
package asm
import "git.akyoto.dev/cli/q/src/cpu"
import (
"fmt"
"math"
"strings"
"git.akyoto.dev/cli/q/src/cpu"
)
type Memory struct {
Base cpu.Register
@ -8,3 +14,29 @@ type Memory struct {
OffsetRegister cpu.Register
Length byte
}
// Format returns a human readable version.
func (mem *Memory) Format(custom string) string {
tmp := strings.Builder{}
tmp.WriteString("[")
tmp.WriteString(fmt.Sprint(mem.Base))
if mem.OffsetRegister != math.MaxUint8 {
tmp.WriteString("+")
tmp.WriteString(fmt.Sprint(mem.OffsetRegister))
}
if mem.Offset != 0 {
if mem.Offset > 0 {
tmp.WriteString("+")
}
tmp.WriteString(fmt.Sprint(mem.Offset))
}
tmp.WriteString("], ")
tmp.WriteString(custom)
tmp.WriteString(", ")
tmp.WriteString(fmt.Sprint(mem.Length))
return tmp.String()
}