Implemented multiplication

This commit is contained in:
2024-06-24 22:43:01 +02:00
parent 625e8d5e2e
commit 34af20d7a0
11 changed files with 107 additions and 47 deletions

View File

@ -14,22 +14,23 @@ func TestAddRegisterNumber(t *testing.T) {
Number int
Code []byte
}{
{x64.RAX, 1, []byte{0x48, 0x83, 0xc0, 0x01}},
{x64.RCX, 1, []byte{0x48, 0x83, 0xc1, 0x01}},
{x64.RDX, 1, []byte{0x48, 0x83, 0xc2, 0x01}},
{x64.RBX, 1, []byte{0x48, 0x83, 0xc3, 0x01}},
{x64.RSP, 1, []byte{0x48, 0x83, 0xc4, 0x01}},
{x64.RBP, 1, []byte{0x48, 0x83, 0xc5, 0x01}},
{x64.RSI, 1, []byte{0x48, 0x83, 0xc6, 0x01}},
{x64.RDI, 1, []byte{0x48, 0x83, 0xc7, 0x01}},
{x64.R8, 1, []byte{0x49, 0x83, 0xc0, 0x01}},
{x64.R9, 1, []byte{0x49, 0x83, 0xc1, 0x01}},
{x64.R10, 1, []byte{0x49, 0x83, 0xc2, 0x01}},
{x64.R11, 1, []byte{0x49, 0x83, 0xc3, 0x01}},
{x64.R12, 1, []byte{0x49, 0x83, 0xc4, 0x01}},
{x64.R13, 1, []byte{0x49, 0x83, 0xc5, 0x01}},
{x64.R14, 1, []byte{0x49, 0x83, 0xc6, 0x01}},
{x64.R15, 1, []byte{0x49, 0x83, 0xc7, 0x01}},
{x64.RAX, 1, []byte{0x48, 0x83, 0xC0, 0x01}},
{x64.RCX, 1, []byte{0x48, 0x83, 0xC1, 0x01}},
{x64.RDX, 1, []byte{0x48, 0x83, 0xC2, 0x01}},
{x64.RBX, 1, []byte{0x48, 0x83, 0xC3, 0x01}},
{x64.RSP, 1, []byte{0x48, 0x83, 0xC4, 0x01}},
{x64.RBP, 1, []byte{0x48, 0x83, 0xC5, 0x01}},
{x64.RSI, 1, []byte{0x48, 0x83, 0xC6, 0x01}},
{x64.RDI, 1, []byte{0x48, 0x83, 0xC7, 0x01}},
{x64.R8, 1, []byte{0x49, 0x83, 0xC0, 0x01}},
{x64.R9, 1, []byte{0x49, 0x83, 0xC1, 0x01}},
{x64.R10, 1, []byte{0x49, 0x83, 0xC2, 0x01}},
{x64.R11, 1, []byte{0x49, 0x83, 0xC3, 0x01}},
{x64.R12, 1, []byte{0x49, 0x83, 0xC4, 0x01}},
{x64.R13, 1, []byte{0x49, 0x83, 0xC5, 0x01}},
{x64.R14, 1, []byte{0x49, 0x83, 0xC6, 0x01}},
{x64.R15, 1, []byte{0x49, 0x83, 0xC7, 0x01}},
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC1, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC2, 0xFF, 0xFF, 0xFF, 0x7F}},

View File

@ -0,0 +1,8 @@
package x64
import "git.akyoto.dev/cli/q/src/build/cpu"
// MulRegNum multiplies a register with a number.
func MulRegNum(code []byte, destination cpu.Register, number int) []byte {
return numRegReg(code, 0x6B, 0x69, byte(destination), byte(destination), number)
}

View File

@ -0,0 +1,57 @@
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 TestMulRegisterNumber(t *testing.T) {
usagePatterns := []struct {
Register cpu.Register
Number int
Code []byte
}{
{x64.RAX, 1, []byte{0x48, 0x6B, 0xC0, 0x01}},
{x64.RCX, 1, []byte{0x48, 0x6B, 0xC9, 0x01}},
{x64.RDX, 1, []byte{0x48, 0x6B, 0xD2, 0x01}},
{x64.RBX, 1, []byte{0x48, 0x6B, 0xDB, 0x01}},
{x64.RSP, 1, []byte{0x48, 0x6B, 0xE4, 0x01}},
{x64.RBP, 1, []byte{0x48, 0x6B, 0xED, 0x01}},
{x64.RSI, 1, []byte{0x48, 0x6B, 0xF6, 0x01}},
{x64.RDI, 1, []byte{0x48, 0x6B, 0xFF, 0x01}},
{x64.R8, 1, []byte{0x4D, 0x6B, 0xC0, 0x01}},
{x64.R9, 1, []byte{0x4D, 0x6B, 0xC9, 0x01}},
{x64.R10, 1, []byte{0x4D, 0x6B, 0xD2, 0x01}},
{x64.R11, 1, []byte{0x4D, 0x6B, 0xDB, 0x01}},
{x64.R12, 1, []byte{0x4D, 0x6B, 0xE4, 0x01}},
{x64.R13, 1, []byte{0x4D, 0x6B, 0xED, 0x01}},
{x64.R14, 1, []byte{0x4D, 0x6B, 0xF6, 0x01}},
{x64.R15, 1, []byte{0x4D, 0x6B, 0xFF, 0x01}},
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xC9, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xD2, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RBX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xDB, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RSP, 0x7FFFFFFF, []byte{0x48, 0x69, 0xE4, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RBP, 0x7FFFFFFF, []byte{0x48, 0x69, 0xED, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RSI, 0x7FFFFFFF, []byte{0x48, 0x69, 0xF6, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RDI, 0x7FFFFFFF, []byte{0x48, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R8, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R9, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xC9, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R10, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xD2, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R11, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xDB, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R12, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xE4, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R13, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xED, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R14, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xF6, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R15, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
}
for _, pattern := range usagePatterns {
t.Logf("mul %s, %x", pattern.Register, pattern.Number)
code := x64.MulRegNum(nil, pattern.Register, pattern.Number)
assert.DeepEqual(t, code, pattern.Code)
}
}

View File

@ -30,6 +30,7 @@ func TestSubRegisterNumber(t *testing.T) {
{x64.R13, 1, []byte{0x49, 0x83, 0xED, 0x01}},
{x64.R14, 1, []byte{0x49, 0x83, 0xEE, 0x01}},
{x64.R15, 1, []byte{0x49, 0x83, 0xEF, 0x01}},
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE8, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE9, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEA, 0xFF, 0xFF, 0xFF, 0x7F}},