64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package x64
|
|
|
|
import "git.akyoto.dev/cli/q/src/cpu"
|
|
|
|
// memoryAccessDynamic encodes a memory access using the value of a register as an offset.
|
|
func memoryAccessDynamic(code []byte, opCode8 byte, opCode32 byte, destination cpu.Register, offset cpu.Register, numBytes byte, source cpu.Register) []byte {
|
|
var (
|
|
w = byte(0)
|
|
r = byte(0)
|
|
x = byte(0)
|
|
b = byte(0)
|
|
opCode = opCode32
|
|
mod = AddressMemory
|
|
)
|
|
|
|
if numBytes == 1 {
|
|
opCode = opCode8
|
|
}
|
|
|
|
if offset == RSP {
|
|
tmp := offset
|
|
offset = destination
|
|
destination = tmp
|
|
}
|
|
|
|
if numBytes == 8 {
|
|
w = 1
|
|
}
|
|
|
|
if source > 0b111 {
|
|
r = 1
|
|
source &= 0b111
|
|
}
|
|
|
|
if offset > 0b111 {
|
|
x = 1
|
|
offset &= 0b111
|
|
}
|
|
|
|
if destination > 0b111 {
|
|
b = 1
|
|
destination &= 0b111
|
|
}
|
|
|
|
if destination == RBP || destination == R13 {
|
|
mod = AddressMemoryOffset8
|
|
}
|
|
|
|
if numBytes == 2 {
|
|
code = append(code, 0x66)
|
|
}
|
|
|
|
code = append(code, REX(w, r, x, b))
|
|
code = append(code, opCode)
|
|
code = append(code, ModRM(mod, byte(source), 0b100))
|
|
code = append(code, SIB(Scale1, byte(offset), byte(destination)))
|
|
|
|
if mod == AddressMemoryOffset8 {
|
|
code = append(code, 0x00)
|
|
}
|
|
|
|
return code
|
|
}
|