Implemented negation

This commit is contained in:
2024-07-28 15:42:51 +02:00
parent 6861ae9a90
commit bb74c0cf50
11 changed files with 136 additions and 21 deletions

View File

@ -0,0 +1,8 @@
package x64
import "git.akyoto.dev/cli/q/src/build/cpu"
// NegateRegister negates the value in the register.
func NegateRegister(code []byte, register cpu.Register) []byte {
return encode(code, AddressDirect, 0b011, register, 8, 0xF7)
}

View File

@ -0,0 +1,39 @@
package x64_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/go/assert"
)
func TestNegateRegister(t *testing.T) {
usagePatterns := []struct {
Register cpu.Register
Code []byte
}{
{x64.RAX, []byte{0x48, 0xF7, 0xD8}},
{x64.RCX, []byte{0x48, 0xF7, 0xD9}},
{x64.RDX, []byte{0x48, 0xF7, 0xDA}},
{x64.RBX, []byte{0x48, 0xF7, 0xDB}},
{x64.RSP, []byte{0x48, 0xF7, 0xDC}},
{x64.RBP, []byte{0x48, 0xF7, 0xDD}},
{x64.RSI, []byte{0x48, 0xF7, 0xDE}},
{x64.RDI, []byte{0x48, 0xF7, 0xDF}},
{x64.R8, []byte{0x49, 0xF7, 0xD8}},
{x64.R9, []byte{0x49, 0xF7, 0xD9}},
{x64.R10, []byte{0x49, 0xF7, 0xDA}},
{x64.R11, []byte{0x49, 0xF7, 0xDB}},
{x64.R12, []byte{0x49, 0xF7, 0xDC}},
{x64.R13, []byte{0x49, 0xF7, 0xDD}},
{x64.R14, []byte{0x49, 0xF7, 0xDE}},
{x64.R15, []byte{0x49, 0xF7, 0xDF}},
}
for _, pattern := range usagePatterns {
t.Logf("neg %s", pattern.Register)
code := x64.NegateRegister(nil, pattern.Register)
assert.DeepEqual(t, code, pattern.Code)
}
}