Skip to content

Commit 2fa83f2

Browse files
authored
Merge pull request #27 from robalb/develop
Fix Displacement renderig in Intel flavour diasssembly
2 parents 1681c7a + f41dc33 commit 2fa83f2

File tree

4 files changed

+159
-46
lines changed

4 files changed

+159
-46
lines changed

blink/blink/disarg.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ static const char *GetAddrReg(struct Dis *d, u64 rde, u8 x, u8 r) {
8888

8989
static char *DisRegister(char *p, const char *s) {
9090
p = HighStart(p, g_high.reg);
91-
if(!INTEL_SYNTAX)
92-
*p++ = '$';
91+
if (!INTEL_SYNTAX) *p++ = '$';
9392
p = stpcpy(p, s);
9493
p = HighEnd(p);
9594
return p;
@@ -140,8 +139,7 @@ static char *DisSym(struct Dis *d, char *p, i64 value, i64 addr) {
140139
}
141140

142141
static char *DisSymLiteral(struct Dis *d, u64 rde, char *p, u64 addr, u64 ip) {
143-
if(!INTEL_SYNTAX)
144-
*p++ = '$';
142+
if (!INTEL_SYNTAX) *p++ = '$';
145143
p = HighStart(p, g_high.literal);
146144
p = DisSym(d, p, addr, addr);
147145
p = HighEnd(p);
@@ -192,16 +190,7 @@ static char *DisDisp(struct Dis *d, u64 rde, char *p) {
192190
} else if (IsRealModrmAbsolute(rde)) {
193191
disp = Unrelative(rde, disp);
194192
}
195-
if(INTEL_SYNTAX){
196-
char* beforeDisSym = p;
197-
*p++ = ' ';
198-
p = DisSym(d, p, disp, disp);
199-
if(beforeDisSym[1] != '-'){
200-
*beforeDisSym = '+';
201-
}
202-
}else{
203-
p = DisSym(d, p, disp, disp);
204-
}
193+
p = DisSym(d, p, disp, disp);
205194
}
206195
return p;
207196
}
@@ -264,7 +253,7 @@ static char *DisBis(struct Dis *d, u64 rde, char *p) {
264253
}
265254
}
266255
if (base || index) {
267-
if(INTEL_SYNTAX){
256+
if (INTEL_SYNTAX) {
268257
*p++ = '[';
269258
if (base) {
270259
p = DisRegister(p, base);
@@ -274,15 +263,23 @@ static char *DisBis(struct Dis *d, u64 rde, char *p) {
274263
p = DisRegister(p, index);
275264
if (scale) {
276265
p = stpcpy(p, scale);
277-
if(scale[0]){
278-
*(p-2) = '*';
266+
if (scale[0]) {
267+
*(p - 2) = '*';
279268
}
280269
}
281270
}
271+
272+
// TODO(al): remove this hack
273+
char *beforeDisDisp = p;
282274
p = DisDisp(d, rde, p);
275+
if (p > beforeDisDisp && *beforeDisDisp != '-') {
276+
memmove(beforeDisDisp + 1, beforeDisDisp, p - beforeDisDisp);
277+
*beforeDisDisp = '+';
278+
p++;
279+
}
280+
283281
*p++ = ']';
284-
}
285-
else{
282+
} else {
286283
*p++ = '(';
287284
if (base) {
288285
p = DisRegister(p, base);
@@ -296,15 +293,18 @@ static char *DisBis(struct Dis *d, u64 rde, char *p) {
296293
}
297294
*p++ = ')';
298295
}
296+
} else {
297+
if (INTEL_SYNTAX) {
298+
p = DisDisp(d, rde, p);
299+
}
299300
}
300301
*p = '\0';
301302
return p;
302303
}
303304

304-
305305
static char *DisM(struct Dis *d, u64 rde, char *p) {
306306
p = DisSego(d, rde, p);
307-
if(!INTEL_SYNTAX){
307+
if (!INTEL_SYNTAX) {
308308
p = DisDisp(d, rde, p);
309309
}
310310
p = DisBis(d, rde, p);
@@ -427,11 +427,11 @@ static char *DisRdx(struct Dis *d, u64 rde, char *p) {
427427
}
428428

429429
static char *DisPort(struct Dis *d, u64 rde, char *p) {
430-
if(INTEL_SYNTAX){
430+
if (INTEL_SYNTAX) {
431431
*p++ = '[';
432432
p = DisRegister(p, kGreg[1][0][0][2]);
433433
*p++ = ']';
434-
}else{
434+
} else {
435435
*p++ = '(';
436436
p = DisRegister(p, kGreg[1][0][0][2]);
437437
*p++ = ')';
@@ -468,8 +468,7 @@ static char *DisRvds(struct Dis *d, u64 rde, char *p) {
468468
}
469469

470470
static char *DisKpvds(struct Dis *d, u64 rde, char *p, u64 x) {
471-
if(!INTEL_SYNTAX)
472-
*p++ = '$';
471+
if (!INTEL_SYNTAX) *p++ = '$';
473472
p = HighStart(p, g_high.literal);
474473
p = DisInt(p, x);
475474
p = HighEnd(p);
@@ -486,8 +485,7 @@ static char *DisPvds(struct Dis *d, u64 rde, char *p) {
486485
}
487486

488487
static char *DisOne(struct Dis *d, u64 rde, char *p) {
489-
if(!INTEL_SYNTAX)
490-
*p++ = '$';
488+
if (!INTEL_SYNTAX) *p++ = '$';
491489
p = HighStart(p, g_high.literal);
492490
p = stpcpy(p, "1");
493491
p = HighEnd(p);
@@ -521,11 +519,11 @@ static char *DisSw(struct Dis *d, u64 rde, char *p) {
521519
}
522520

523521
static char *DisSpecialAddr(struct Dis *d, u64 rde, char *p, int r) {
524-
if(INTEL_SYNTAX){
522+
if (INTEL_SYNTAX) {
525523
*p++ = '[';
526524
p = DisRegister(p, GetAddrReg(d, rde, 0, r));
527525
*p++ = ']';
528-
}else{
526+
} else {
529527
*p++ = '(';
530528
p = DisRegister(p, GetAddrReg(d, rde, 0, r));
531529
*p++ = ')';
@@ -584,11 +582,11 @@ static char *DisQq(struct Dis *d, u64 rde, char *p) {
584582
static char *DisEst(struct Dis *d, u64 rde, char *p) {
585583
p = DisRegister(p, "st");
586584
if (ModrmRm(rde) != 0) {
587-
if(INTEL_SYNTAX){
585+
if (INTEL_SYNTAX) {
588586
*p++ = '[';
589587
*p++ = '0' + ModrmRm(rde);
590588
*p++ = ']';
591-
}else{
589+
} else {
592590
*p++ = '(';
593591
*p++ = '0' + ModrmRm(rde);
594592
*p++ = ')';

blink/blink/disinst.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,28 +153,28 @@ static char *DisName(struct Dis *d, char *bp, const char *name,
153153
!StartsWith(name, "set"))) {
154154
if (Osz(rde)) {
155155
if (ambiguous || Mode(rde) != XED_MODE_REAL) {
156-
if(INTEL_SYNTAX){
156+
if (INTEL_SYNTAX) {
157157
p = stpcpy(p, " word");
158-
}else{
158+
} else {
159159
*p++ = 'w';
160160
}
161161
}
162162
} else if (Rexw(rde)) {
163-
if(INTEL_SYNTAX){
164-
p = stpcpy(p, " qword");
165-
}else{
166-
*p++ = 'q';
167-
}
163+
if (INTEL_SYNTAX) {
164+
p = stpcpy(p, " qword");
165+
} else {
166+
*p++ = 'q';
167+
}
168168
} else if (ambiguous && !notbyte && IsProbablyByteOp(d->xedd)) {
169-
if(INTEL_SYNTAX){
169+
if (INTEL_SYNTAX) {
170170
p = stpcpy(p, " byte");
171-
}else{
171+
} else {
172172
*p++ = 'b';
173173
}
174174
} else if (!notlong) {
175-
if(INTEL_SYNTAX){
175+
if (INTEL_SYNTAX) {
176176
p = stpcpy(p, " dword");
177-
}else{
177+
} else {
178178
*p++ = 'l';
179179
}
180180
}
@@ -190,7 +190,6 @@ static char *DisName(struct Dis *d, char *bp, const char *name,
190190
return p;
191191
}
192192

193-
194193
/**
195194
* Disassembles instruction based on string spec.
196195
* @see DisSpec()
@@ -218,16 +217,15 @@ char *DisInst(struct Dis *d, char *p, const char *spec) {
218217
p = HighStart(p, g_high.keyword);
219218
p = DisName(d, p, name, hasarg && !hasregister && hasmemory);
220219
p = HighEnd(p);
221-
if(INTEL_SYNTAX){
220+
if (INTEL_SYNTAX) {
222221
for (i = 0; i < n; ++i) {
223222
if (i && args[i][0]) {
224223
*p++ = ',';
225224
*p++ = ' ';
226225
}
227226
p = stpcpy(p, args[i]);
228227
}
229-
}
230-
else{
228+
} else {
231229
for (i = 0; i < n; ++i) {
232230
if (i && args[n - i][0]) {
233231
*p++ = ',';
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 4 ott
2+
3+
checking eflags values
4+
5+
6+
[ auxiliary sign overflow ]
7+
0x80001a92
8+
0b10000000000000000001101010010010
9+
| | | | |
10+
| 9 7 4 1
11+
| I S A
12+
11
13+
O
14+
15+
[ auxiliary ]
16+
0x10001212
17+
0b10000000000000001001000010010
18+
| | | |
19+
| 9 4 1
20+
| I A
21+
12
22+
23+
0 CF Carry Flag
24+
1 1 Reserved
25+
2 PF Parity Flag
26+
3 0 Reserved
27+
4 AF Auxiliary Carry Flag
28+
5 0 Reserved
29+
6 ZF Zero Flag
30+
7 SF Sign Flag
31+
8 TF Trap Flag
32+
9 IF Interrupt Enable Flag
33+
10 DF Direction Flag
34+
11 OF Overflow Flag
35+
12-13 IOPL I/O Privilege Level
36+
14 NT Nested Task
37+
15 0 Reserved
38+
16 RF Resume Flag
39+
17 VM Virtual-8086 Mode
40+
18 AC Alignment Check / Access Control
41+
19 VIF Virtual Interrupt Flag
42+
20 VIP Virtual Interrupt Pending
43+
21 ID ID Flag
44+
22-63 0 Reserved
45+
46+
47+
# 5 ott
48+
49+
fixing disass visualization bug
50+
51+
disinst.c
52+
```
53+
NOT relevant
54+
static char *DisSpecialAddr(struct Dis *d, u64 rde, char *p, int r) {
55+
if(INTEL_SYNTAX){
56+
*p++ = '[';
57+
p = DisRegister(p, GetAddrReg(d, rde, 0, r));
58+
*p++ = ']';
59+
}else{
60+
*p++ = '(';
61+
p = DisRegister(p, GetAddrReg(d, rde, 0, r));
62+
*p++ = ')';
63+
}
64+
*p = '\0';
65+
return p;
66+
}
67+
```
68+
69+
# 8 ott
70+
71+
disSymLiteral
72+
73+
74+
# 12 ott
75+
76+
77+
DisM (at&t operand source Memory)
78+
displacement + `(...)`
79+
| /
80+
| /
81+
DisDisp /
82+
DisBis (draws [...])
83+
\
84+
\
85+
DisDysp --------> DisSym
86+
(draws +/-)
87+
88+
89+
90+
https://sdasgup3.github.io/Intel_Vs_Att_format/
91+
Intel Syntax
92+
instr foo,segreg:[base+index*scale+disp]
93+
AT&T Syntax
94+
instr %segreg:disp(base,index,scale),foo
95+
96+
97+
There are a few different forms of indirect operands in x86:
98+
99+
[reg]
100+
[reg + displacement]
101+
[displacement]
102+
[reg * constant + reg]
103+
[reg * constant + reg + displacement]
104+
105+
The "displacement" is just a constant that gets added to the rest of the address. In cases where there is no component of the address other than the constant, it is still called a "displacement". This is mainly for consistency with the other addressing forms.
106+
107+
Another way to look at it is that all addresses are of the form
108+
109+
[reg * constant + reg + displacement]
110+
111+
With each of the components allowing a value of 0.
112+
113+
The [displacement] form is just the encoding where all components other than the displacement are zero.
114+
115+
As a compiler writer the last 2 forms are particularly interesting. They make it easy to encode things like pArray[index]->field + 1in a single instruction.
116+
117+

webapp/src/assets/blinkenlib.wasm

163 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)