Improved formatting of load and store commands
This commit is contained in:
parent
c633f94d29
commit
dc3ba6504f
@ -1,6 +1,12 @@
|
|||||||
package asm
|
package asm
|
||||||
|
|
||||||
import "git.akyoto.dev/cli/q/src/cpu"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.akyoto.dev/cli/q/src/cpu"
|
||||||
|
)
|
||||||
|
|
||||||
type Memory struct {
|
type Memory struct {
|
||||||
Base cpu.Register
|
Base cpu.Register
|
||||||
@ -8,3 +14,29 @@ type Memory struct {
|
|||||||
OffsetRegister cpu.Register
|
OffsetRegister cpu.Register
|
||||||
Length byte
|
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()
|
||||||
|
}
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package asm
|
package asm
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MemoryLabel operates with a memory address and a number.
|
// MemoryLabel operates with a memory address and a number.
|
||||||
type MemoryLabel struct {
|
type MemoryLabel struct {
|
||||||
Label string
|
Label string
|
||||||
@ -13,11 +8,7 @@ type MemoryLabel struct {
|
|||||||
|
|
||||||
// String returns a human readable version.
|
// String returns a human readable version.
|
||||||
func (data *MemoryLabel) String() string {
|
func (data *MemoryLabel) String() string {
|
||||||
if data.Address.OffsetRegister == math.MaxUint8 {
|
return data.Address.Format(data.Label)
|
||||||
return fmt.Sprintf("%dB [%s+%d], %s", data.Address.Length, data.Address.Base, data.Address.Offset, data.Label)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%dB [%s+%s+%d], %s", data.Address.Length, data.Address.Base, data.Address.OffsetRegister, data.Address.Offset, data.Label)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MemoryLabel adds an instruction with a memory address and a label.
|
// MemoryLabel adds an instruction with a memory address and a label.
|
||||||
|
@ -2,7 +2,6 @@ package asm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MemoryNumber operates with a memory address and a number.
|
// MemoryNumber operates with a memory address and a number.
|
||||||
@ -13,11 +12,7 @@ type MemoryNumber struct {
|
|||||||
|
|
||||||
// String returns a human readable version.
|
// String returns a human readable version.
|
||||||
func (data *MemoryNumber) String() string {
|
func (data *MemoryNumber) String() string {
|
||||||
if data.Address.OffsetRegister == math.MaxUint8 {
|
return data.Address.Format(fmt.Sprint(data.Number))
|
||||||
return fmt.Sprintf("%dB [%s+%d], %d", data.Address.Length, data.Address.Base, data.Address.Offset, data.Number)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%dB [%s+%s+%d], %d", data.Address.Length, data.Address.Base, data.Address.OffsetRegister, data.Address.Offset, data.Number)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MemoryNumber adds an instruction with a memory address and a number.
|
// MemoryNumber adds an instruction with a memory address and a number.
|
||||||
|
@ -2,7 +2,6 @@ package asm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/cpu"
|
"git.akyoto.dev/cli/q/src/cpu"
|
||||||
)
|
)
|
||||||
@ -15,11 +14,7 @@ type MemoryRegister struct {
|
|||||||
|
|
||||||
// String returns a human readable version.
|
// String returns a human readable version.
|
||||||
func (data *MemoryRegister) String() string {
|
func (data *MemoryRegister) String() string {
|
||||||
if data.Address.OffsetRegister == math.MaxUint8 {
|
return data.Address.Format(fmt.Sprint(data.Register))
|
||||||
return fmt.Sprintf("%dB [%s+%d], %s", data.Address.Length, data.Address.Base, data.Address.Offset, data.Register)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%dB [%s+%s+%d], %s", data.Address.Length, data.Address.Base, data.Address.OffsetRegister, data.Address.Offset, data.Register)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MemoryRegister adds an instruction with a memory address and a number.
|
// MemoryRegister adds an instruction with a memory address and a number.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user