This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Compare_test.go
207 lines (198 loc) · 8.83 KB
/
Compare_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package asm_test
import (
"testing"
"github.com/akyoto/asm"
"github.com/akyoto/assert"
)
func TestCompareRegisterNumber(t *testing.T) {
usagePatterns := []struct {
Register string
Number int64
Code []byte
}{
// 1-byte numbers
{"rax", 1, []byte{0x48, 0x83, 0xf8, 0x01}},
{"eax", 1, []byte{0x83, 0xf8, 0x01}},
{"ax", 1, []byte{0x66, 0x83, 0xf8, 0x01}},
{"rcx", 1, []byte{0x48, 0x83, 0xf9, 0x01}},
{"ecx", 1, []byte{0x83, 0xf9, 0x01}},
{"cx", 1, []byte{0x66, 0x83, 0xf9, 0x01}},
{"rdx", 1, []byte{0x48, 0x83, 0xfa, 0x01}},
{"edx", 1, []byte{0x83, 0xfa, 0x01}},
{"dx", 1, []byte{0x66, 0x83, 0xfa, 0x01}},
{"rbx", 1, []byte{0x48, 0x83, 0xfb, 0x01}},
{"ebx", 1, []byte{0x83, 0xfb, 0x01}},
{"bx", 1, []byte{0x66, 0x83, 0xfb, 0x01}},
{"rsi", 1, []byte{0x48, 0x83, 0xfe, 0x01}},
{"esi", 1, []byte{0x83, 0xfe, 0x01}},
{"si", 1, []byte{0x66, 0x83, 0xfe, 0x01}},
{"rdi", 1, []byte{0x48, 0x83, 0xff, 0x01}},
{"edi", 1, []byte{0x83, 0xff, 0x01}},
{"di", 1, []byte{0x66, 0x83, 0xff, 0x01}},
{"rsp", 1, []byte{0x48, 0x83, 0xfc, 0x01}},
{"esp", 1, []byte{0x83, 0xfc, 0x01}},
{"sp", 1, []byte{0x66, 0x83, 0xfc, 0x01}},
{"rbp", 1, []byte{0x48, 0x83, 0xfd, 0x01}},
{"ebp", 1, []byte{0x83, 0xfd, 0x01}},
{"bp", 1, []byte{0x66, 0x83, 0xfd, 0x01}},
{"r8", 1, []byte{0x49, 0x83, 0xf8, 0x01}},
{"r8d", 1, []byte{0x41, 0x83, 0xf8, 0x01}},
{"r8w", 1, []byte{0x66, 0x41, 0x83, 0xf8, 0x01}},
{"r9", 1, []byte{0x49, 0x83, 0xf9, 0x01}},
{"r9d", 1, []byte{0x41, 0x83, 0xf9, 0x01}},
{"r9w", 1, []byte{0x66, 0x41, 0x83, 0xf9, 0x01}},
{"r10", 1, []byte{0x49, 0x83, 0xfa, 0x01}},
{"r10d", 1, []byte{0x41, 0x83, 0xfa, 0x01}},
{"r10w", 1, []byte{0x66, 0x41, 0x83, 0xfa, 0x01}},
{"r11", 1, []byte{0x49, 0x83, 0xfb, 0x01}},
{"r11d", 1, []byte{0x41, 0x83, 0xfb, 0x01}},
{"r11w", 1, []byte{0x66, 0x41, 0x83, 0xfb, 0x01}},
{"r12", 1, []byte{0x49, 0x83, 0xfc, 0x01}},
{"r12d", 1, []byte{0x41, 0x83, 0xfc, 0x01}},
{"r12w", 1, []byte{0x66, 0x41, 0x83, 0xfc, 0x01}},
{"r13", 1, []byte{0x49, 0x83, 0xfd, 0x01}},
{"r13d", 1, []byte{0x41, 0x83, 0xfd, 0x01}},
{"r13w", 1, []byte{0x66, 0x41, 0x83, 0xfd, 0x01}},
{"r14", 1, []byte{0x49, 0x83, 0xfe, 0x01}},
{"r14d", 1, []byte{0x41, 0x83, 0xfe, 0x01}},
{"r14w", 1, []byte{0x66, 0x41, 0x83, 0xfe, 0x01}},
{"r15", 1, []byte{0x49, 0x83, 0xff, 0x01}},
{"r15d", 1, []byte{0x41, 0x83, 0xff, 0x01}},
{"r15w", 1, []byte{0x66, 0x41, 0x83, 0xff, 0x01}},
// 1-byte numbers in 1-byte registers
{"al", 1, []byte{0x3c, 0x01}},
{"cl", 1, []byte{0x80, 0xf9, 0x01}},
{"dl", 1, []byte{0x80, 0xfa, 0x01}},
{"bl", 1, []byte{0x80, 0xfb, 0x01}},
{"r8b", 1, []byte{0x41, 0x80, 0xf8, 0x01}},
{"r9b", 1, []byte{0x41, 0x80, 0xf9, 0x01}},
{"r10b", 1, []byte{0x41, 0x80, 0xfa, 0x01}},
{"r11b", 1, []byte{0x41, 0x80, 0xfb, 0x01}},
// 2-byte numbers
{"rax", 0x2710, []byte{0x48, 0x3d, 0x10, 0x27, 0x00, 0x00}},
{"eax", 0x2710, []byte{0x3d, 0x10, 0x27, 0x00, 0x00}},
{"ax", 0x2710, []byte{0x66, 0x3d, 0x10, 0x27}},
{"rcx", 0x2710, []byte{0x48, 0x81, 0xf9, 0x10, 0x27, 0x00, 0x00}},
{"ecx", 0x2710, []byte{0x81, 0xf9, 0x10, 0x27, 0x00, 0x00}},
{"cx", 0x2710, []byte{0x66, 0x81, 0xf9, 0x10, 0x27}},
{"rdx", 0x2710, []byte{0x48, 0x81, 0xfa, 0x10, 0x27, 0x00, 0x00}},
{"edx", 0x2710, []byte{0x81, 0xfa, 0x10, 0x27, 0x00, 0x00}},
{"dx", 0x2710, []byte{0x66, 0x81, 0xfa, 0x10, 0x27}},
{"rbx", 0x2710, []byte{0x48, 0x81, 0xfb, 0x10, 0x27, 0x00, 0x00}},
{"ebx", 0x2710, []byte{0x81, 0xfb, 0x10, 0x27, 0x00, 0x00}},
{"bx", 0x2710, []byte{0x66, 0x81, 0xfb, 0x10, 0x27}},
{"rsi", 0x2710, []byte{0x48, 0x81, 0xfe, 0x10, 0x27, 0x00, 0x00}},
{"esi", 0x2710, []byte{0x81, 0xfe, 0x10, 0x27, 0x00, 0x00}},
{"si", 0x2710, []byte{0x66, 0x81, 0xfe, 0x10, 0x27}},
{"rdi", 0x2710, []byte{0x48, 0x81, 0xff, 0x10, 0x27, 0x00, 0x00}},
{"edi", 0x2710, []byte{0x81, 0xff, 0x10, 0x27, 0x00, 0x00}},
{"di", 0x2710, []byte{0x66, 0x81, 0xff, 0x10, 0x27}},
{"rsp", 0x2710, []byte{0x48, 0x81, 0xfc, 0x10, 0x27, 0x00, 0x00}},
{"esp", 0x2710, []byte{0x81, 0xfc, 0x10, 0x27, 0x00, 0x00}},
{"sp", 0x2710, []byte{0x66, 0x81, 0xfc, 0x10, 0x27}},
{"rbp", 0x2710, []byte{0x48, 0x81, 0xfd, 0x10, 0x27, 0x00, 0x00}},
{"ebp", 0x2710, []byte{0x81, 0xfd, 0x10, 0x27, 0x00, 0x00}},
{"bp", 0x2710, []byte{0x66, 0x81, 0xfd, 0x10, 0x27}},
{"r8", 0x2710, []byte{0x49, 0x81, 0xf8, 0x10, 0x27, 0x00, 0x00}},
{"r8d", 0x2710, []byte{0x41, 0x81, 0xf8, 0x10, 0x27, 0x00, 0x00}},
{"r8w", 0x2710, []byte{0x66, 0x41, 0x81, 0xf8, 0x10, 0x27}},
{"r9", 0x2710, []byte{0x49, 0x81, 0xf9, 0x10, 0x27, 0x00, 0x00}},
{"r9d", 0x2710, []byte{0x41, 0x81, 0xf9, 0x10, 0x27, 0x00, 0x00}},
{"r9w", 0x2710, []byte{0x66, 0x41, 0x81, 0xf9, 0x10, 0x27}},
{"r10", 0x2710, []byte{0x49, 0x81, 0xfa, 0x10, 0x27, 0x00, 0x00}},
{"r10d", 0x2710, []byte{0x41, 0x81, 0xfa, 0x10, 0x27, 0x00, 0x00}},
{"r10w", 0x2710, []byte{0x66, 0x41, 0x81, 0xfa, 0x10, 0x27}},
{"r11", 0x2710, []byte{0x49, 0x81, 0xfb, 0x10, 0x27, 0x00, 0x00}},
{"r11d", 0x2710, []byte{0x41, 0x81, 0xfb, 0x10, 0x27, 0x00, 0x00}},
{"r11w", 0x2710, []byte{0x66, 0x41, 0x81, 0xfb, 0x10, 0x27}},
{"r12", 0x2710, []byte{0x49, 0x81, 0xfc, 0x10, 0x27, 0x00, 0x00}},
{"r12d", 0x2710, []byte{0x41, 0x81, 0xfc, 0x10, 0x27, 0x00, 0x00}},
{"r12w", 0x2710, []byte{0x66, 0x41, 0x81, 0xfc, 0x10, 0x27}},
{"r13", 0x2710, []byte{0x49, 0x81, 0xfd, 0x10, 0x27, 0x00, 0x00}},
{"r13d", 0x2710, []byte{0x41, 0x81, 0xfd, 0x10, 0x27, 0x00, 0x00}},
{"r13w", 0x2710, []byte{0x66, 0x41, 0x81, 0xfd, 0x10, 0x27}},
{"r14", 0x2710, []byte{0x49, 0x81, 0xfe, 0x10, 0x27, 0x00, 0x00}},
{"r14d", 0x2710, []byte{0x41, 0x81, 0xfe, 0x10, 0x27, 0x00, 0x00}},
{"r14w", 0x2710, []byte{0x66, 0x41, 0x81, 0xfe, 0x10, 0x27}},
{"r15", 0x2710, []byte{0x49, 0x81, 0xff, 0x10, 0x27, 0x00, 0x00}},
{"r15d", 0x2710, []byte{0x41, 0x81, 0xff, 0x10, 0x27, 0x00, 0x00}},
{"r15w", 0x2710, []byte{0x66, 0x41, 0x81, 0xff, 0x10, 0x27}},
}
for _, pattern := range usagePatterns {
a := asm.New()
t.Logf("cmp %s, %d", pattern.Register, pattern.Number)
a.CompareRegisterNumber(pattern.Register, uint64(pattern.Number))
assert.DeepEqual(t, a.Code(), pattern.Code)
}
}
func TestCompareRegisterRegister(t *testing.T) {
usagePatterns := []struct {
RegisterA string
RegisterB string
Code []byte
}{
{"rax", "rax", []byte{0x48, 0x39, 0xc0}},
{"eax", "eax", []byte{0x39, 0xc0}},
{"ax", "ax", []byte{0x66, 0x39, 0xc0}},
{"rcx", "rcx", []byte{0x48, 0x39, 0xc9}},
{"ecx", "ecx", []byte{0x39, 0xc9}},
{"cx", "cx", []byte{0x66, 0x39, 0xc9}},
{"rdx", "rdx", []byte{0x48, 0x39, 0xd2}},
{"edx", "edx", []byte{0x39, 0xd2}},
{"dx", "dx", []byte{0x66, 0x39, 0xd2}},
{"rbx", "rbx", []byte{0x48, 0x39, 0xdb}},
{"ebx", "ebx", []byte{0x39, 0xdb}},
{"bx", "bx", []byte{0x66, 0x39, 0xdb}},
{"rsi", "rsi", []byte{0x48, 0x39, 0xf6}},
{"esi", "esi", []byte{0x39, 0xf6}},
{"si", "si", []byte{0x66, 0x39, 0xf6}},
{"rdi", "rdi", []byte{0x48, 0x39, 0xff}},
{"edi", "edi", []byte{0x39, 0xff}},
{"di", "di", []byte{0x66, 0x39, 0xff}},
{"rsp", "rsp", []byte{0x48, 0x39, 0xe4}},
{"esp", "esp", []byte{0x39, 0xe4}},
{"sp", "sp", []byte{0x66, 0x39, 0xe4}},
{"rbp", "rbp", []byte{0x48, 0x39, 0xed}},
{"ebp", "ebp", []byte{0x39, 0xed}},
{"bp", "bp", []byte{0x66, 0x39, 0xed}},
{"r8", "r8", []byte{0x4d, 0x39, 0xc0}},
{"r8d", "r8d", []byte{0x45, 0x39, 0xc0}},
{"r8w", "r8w", []byte{0x66, 0x45, 0x39, 0xc0}},
{"r9", "r9", []byte{0x4d, 0x39, 0xc9}},
{"r9d", "r9d", []byte{0x45, 0x39, 0xc9}},
{"r9w", "r9w", []byte{0x66, 0x45, 0x39, 0xc9}},
{"r10", "r10", []byte{0x4d, 0x39, 0xd2}},
{"r10d", "r10d", []byte{0x45, 0x39, 0xd2}},
{"r10w", "r10w", []byte{0x66, 0x45, 0x39, 0xd2}},
{"r11", "r11", []byte{0x4d, 0x39, 0xdb}},
{"r11d", "r11d", []byte{0x45, 0x39, 0xdb}},
{"r11w", "r11w", []byte{0x66, 0x45, 0x39, 0xdb}},
{"r12", "r12", []byte{0x4d, 0x39, 0xe4}},
{"r12d", "r12d", []byte{0x45, 0x39, 0xe4}},
{"r12w", "r12w", []byte{0x66, 0x45, 0x39, 0xe4}},
{"r13", "r13", []byte{0x4d, 0x39, 0xed}},
{"r13d", "r13d", []byte{0x45, 0x39, 0xed}},
{"r13w", "r13w", []byte{0x66, 0x45, 0x39, 0xed}},
{"r14", "r14", []byte{0x4d, 0x39, 0xf6}},
{"r14d", "r14d", []byte{0x45, 0x39, 0xf6}},
{"r14w", "r14w", []byte{0x66, 0x45, 0x39, 0xf6}},
{"r15", "r15", []byte{0x4d, 0x39, 0xff}},
{"r15d", "r15d", []byte{0x45, 0x39, 0xff}},
{"r15w", "r15w", []byte{0x66, 0x45, 0x39, 0xff}},
// 1-byte registers
{"al", "al", []byte{0x38, 0xc0}},
{"cl", "cl", []byte{0x38, 0xc9}},
{"dl", "dl", []byte{0x38, 0xd2}},
{"bl", "bl", []byte{0x38, 0xdb}},
{"r8b", "r8b", []byte{0x45, 0x38, 0xc0}},
{"r9b", "r9b", []byte{0x45, 0x38, 0xc9}},
{"r10b", "r10b", []byte{0x45, 0x38, 0xd2}},
{"r11b", "r11b", []byte{0x45, 0x38, 0xdb}},
}
for _, pattern := range usagePatterns {
a := asm.New()
t.Logf("cmp %s, %s", pattern.RegisterA, pattern.RegisterB)
a.CompareRegisterRegister(pattern.RegisterA, pattern.RegisterB)
assert.DeepEqual(t, a.Code(), pattern.Code)
}
}