This repository has been archived by the owner on Feb 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMul_test.go
140 lines (133 loc) · 5.44 KB
/
Mul_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package asm_test
import (
"testing"
"github.com/akyoto/asm"
"github.com/akyoto/assert"
)
func TestMulRegisterNumber(t *testing.T) {
usagePatterns := []struct {
Register string
Number int64
Code []byte
}{
{"rax", 2, []byte{0x48, 0x6b, 0xc0, 0x02}},
{"eax", 2, []byte{0x6b, 0xc0, 0x02}},
{"ax", 2, []byte{0x66, 0x6b, 0xc0, 0x02}},
{"rcx", 2, []byte{0x48, 0x6b, 0xc9, 0x02}},
{"ecx", 2, []byte{0x6b, 0xc9, 0x02}},
{"cx", 2, []byte{0x66, 0x6b, 0xc9, 0x02}},
{"rdx", 2, []byte{0x48, 0x6b, 0xd2, 0x02}},
{"edx", 2, []byte{0x6b, 0xd2, 0x02}},
{"dx", 2, []byte{0x66, 0x6b, 0xd2, 0x02}},
{"rbx", 2, []byte{0x48, 0x6b, 0xdb, 0x02}},
{"ebx", 2, []byte{0x6b, 0xdb, 0x02}},
{"bx", 2, []byte{0x66, 0x6b, 0xdb, 0x02}},
{"rsi", 2, []byte{0x48, 0x6b, 0xf6, 0x02}},
{"esi", 2, []byte{0x6b, 0xf6, 0x02}},
{"si", 2, []byte{0x66, 0x6b, 0xf6, 0x02}},
{"rdi", 2, []byte{0x48, 0x6b, 0xff, 0x02}},
{"edi", 2, []byte{0x6b, 0xff, 0x02}},
{"di", 2, []byte{0x66, 0x6b, 0xff, 0x02}},
{"rsp", 2, []byte{0x48, 0x6b, 0xe4, 0x02}},
{"esp", 2, []byte{0x6b, 0xe4, 0x02}},
{"sp", 2, []byte{0x66, 0x6b, 0xe4, 0x02}},
{"rbp", 2, []byte{0x48, 0x6b, 0xed, 0x02}},
{"ebp", 2, []byte{0x6b, 0xed, 0x02}},
{"bp", 2, []byte{0x66, 0x6b, 0xed, 0x02}},
{"r8", 2, []byte{0x4d, 0x6b, 0xc0, 0x02}},
{"r8d", 2, []byte{0x45, 0x6b, 0xc0, 0x02}},
{"r8w", 2, []byte{0x66, 0x45, 0x6b, 0xc0, 0x02}},
{"r9", 2, []byte{0x4d, 0x6b, 0xc9, 0x02}},
{"r9d", 2, []byte{0x45, 0x6b, 0xc9, 0x02}},
{"r9w", 2, []byte{0x66, 0x45, 0x6b, 0xc9, 0x02}},
{"r10", 2, []byte{0x4d, 0x6b, 0xd2, 0x02}},
{"r10d", 2, []byte{0x45, 0x6b, 0xd2, 0x02}},
{"r10w", 2, []byte{0x66, 0x45, 0x6b, 0xd2, 0x02}},
{"r11", 2, []byte{0x4d, 0x6b, 0xdb, 0x02}},
{"r11d", 2, []byte{0x45, 0x6b, 0xdb, 0x02}},
{"r11w", 2, []byte{0x66, 0x45, 0x6b, 0xdb, 0x02}},
{"r12", 2, []byte{0x4d, 0x6b, 0xe4, 0x02}},
{"r12d", 2, []byte{0x45, 0x6b, 0xe4, 0x02}},
{"r12w", 2, []byte{0x66, 0x45, 0x6b, 0xe4, 0x02}},
{"r13", 2, []byte{0x4d, 0x6b, 0xed, 0x02}},
{"r13d", 2, []byte{0x45, 0x6b, 0xed, 0x02}},
{"r13w", 2, []byte{0x66, 0x45, 0x6b, 0xed, 0x02}},
{"r14", 2, []byte{0x4d, 0x6b, 0xf6, 0x02}},
{"r14d", 2, []byte{0x45, 0x6b, 0xf6, 0x02}},
{"r14w", 2, []byte{0x66, 0x45, 0x6b, 0xf6, 0x02}},
{"r15", 2, []byte{0x4d, 0x6b, 0xff, 0x02}},
{"r15d", 2, []byte{0x45, 0x6b, 0xff, 0x02}},
{"r15w", 2, []byte{0x66, 0x45, 0x6b, 0xff, 0x02}},
}
for _, pattern := range usagePatterns {
a := asm.New()
t.Logf("imul %s, %d", pattern.Register, pattern.Number)
a.MulRegisterNumber(pattern.Register, uint64(pattern.Number))
assert.DeepEqual(t, a.Code(), pattern.Code)
}
}
func TestMulRegisterRegister(t *testing.T) {
usagePatterns := []struct {
Destination string
Source string
Code []byte
}{
{"rax", "rax", []byte{0x48, 0x0f, 0xaf, 0xc0}},
{"eax", "eax", []byte{0x0f, 0xaf, 0xc0}},
{"ax", "ax", []byte{0x66, 0x0f, 0xaf, 0xc0}},
{"rcx", "rcx", []byte{0x48, 0x0f, 0xaf, 0xc9}},
{"ecx", "ecx", []byte{0x0f, 0xaf, 0xc9}},
{"cx", "cx", []byte{0x66, 0x0f, 0xaf, 0xc9}},
{"rdx", "rdx", []byte{0x48, 0x0f, 0xaf, 0xd2}},
{"edx", "edx", []byte{0x0f, 0xaf, 0xd2}},
{"dx", "dx", []byte{0x66, 0x0f, 0xaf, 0xd2}},
{"rbx", "rbx", []byte{0x48, 0x0f, 0xaf, 0xdb}},
{"ebx", "ebx", []byte{0x0f, 0xaf, 0xdb}},
{"bx", "bx", []byte{0x66, 0x0f, 0xaf, 0xdb}},
{"rsi", "rsi", []byte{0x48, 0x0f, 0xaf, 0xf6}},
{"esi", "esi", []byte{0x0f, 0xaf, 0xf6}},
{"si", "si", []byte{0x66, 0x0f, 0xaf, 0xf6}},
{"rdi", "rdi", []byte{0x48, 0x0f, 0xaf, 0xff}},
{"edi", "edi", []byte{0x0f, 0xaf, 0xff}},
{"di", "di", []byte{0x66, 0x0f, 0xaf, 0xff}},
{"rsp", "rsp", []byte{0x48, 0x0f, 0xaf, 0xe4}},
{"esp", "esp", []byte{0x0f, 0xaf, 0xe4}},
{"sp", "sp", []byte{0x66, 0x0f, 0xaf, 0xe4}},
{"rbp", "rbp", []byte{0x48, 0x0f, 0xaf, 0xed}},
{"ebp", "ebp", []byte{0x0f, 0xaf, 0xed}},
{"bp", "bp", []byte{0x66, 0x0f, 0xaf, 0xed}},
{"r8", "r8", []byte{0x4d, 0x0f, 0xaf, 0xc0}},
{"r8d", "r8d", []byte{0x45, 0x0f, 0xaf, 0xc0}},
{"r8w", "r8w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xc0}},
{"r9", "r9", []byte{0x4d, 0x0f, 0xaf, 0xc9}},
{"r9d", "r9d", []byte{0x45, 0x0f, 0xaf, 0xc9}},
{"r9w", "r9w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xc9}},
{"r10", "r10", []byte{0x4d, 0x0f, 0xaf, 0xd2}},
{"r10d", "r10d", []byte{0x45, 0x0f, 0xaf, 0xd2}},
{"r10w", "r10w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xd2}},
{"r11", "r11", []byte{0x4d, 0x0f, 0xaf, 0xdb}},
{"r11d", "r11d", []byte{0x45, 0x0f, 0xaf, 0xdb}},
{"r11w", "r11w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xdb}},
{"r12", "r12", []byte{0x4d, 0x0f, 0xaf, 0xe4}},
{"r12d", "r12d", []byte{0x45, 0x0f, 0xaf, 0xe4}},
{"r12w", "r12w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xe4}},
{"r13", "r13", []byte{0x4d, 0x0f, 0xaf, 0xed}},
{"r13d", "r13d", []byte{0x45, 0x0f, 0xaf, 0xed}},
{"r13w", "r13w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xed}},
{"r14", "r14", []byte{0x4d, 0x0f, 0xaf, 0xf6}},
{"r14d", "r14d", []byte{0x45, 0x0f, 0xaf, 0xf6}},
{"r14w", "r14w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xf6}},
{"r15", "r15", []byte{0x4d, 0x0f, 0xaf, 0xff}},
{"r15d", "r15d", []byte{0x45, 0x0f, 0xaf, 0xff}},
{"r15w", "r15w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xff}},
// Test the order
{"rax", "rcx", []byte{0x48, 0x0f, 0xaf, 0xc1}},
{"rcx", "rax", []byte{0x48, 0x0f, 0xaf, 0xc8}},
}
for _, pattern := range usagePatterns {
a := asm.New()
t.Logf("imul %s, %s", pattern.Destination, pattern.Source)
a.MulRegisterRegister(pattern.Destination, pattern.Source)
assert.DeepEqual(t, a.Code(), pattern.Code)
}
}