MCPcopy Create free account
hub / github.com/WheretIB/nullc / encodeAddress

Function encodeAddress

NULLC/Translator_X86.cpp:24–87  ·  view source on GitHub ↗

encode [base], [base+displacement], [index*multiplier+displacement] and [index*multiplier+base+displacement]

Source from the content-addressed store, hash-verified

22
23// encode [base], [base+displacement], [index*multiplier+displacement] and [index*multiplier+base+displacement]
24unsigned int encodeAddress(unsigned char* stream, x86Reg index, int multiplier, x86Reg base, int displacement, char spareField)
25{
26 assert(index != rESP);
27 unsigned char* start = stream;
28
29 bool dispImm8 = (char)(displacement) == displacement && base != rNONE;
30
31 unsigned char mod = 0;
32 if(displacement)
33 {
34 if(dispImm8)
35 mod = 1 << 6;
36 else
37 mod = 2 << 6;
38 }
39
40 // special case: [ebp] should be encoded as [ebp+0]
41 if(displacement == 0 && base == rEBP)
42 mod = 1 << 6;
43 if(index == rNONE && base == rNONE)
44 mod = 0;
45
46 unsigned char spare = spareField << 3;
47
48 unsigned char RM = regCode[rEBP]; // by default, it's simply [displacement]
49 if(base != rNONE)
50 RM = regCode[base]; // this is [base + displacement]
51 if(index != rNONE)
52 RM = regCode[rESP]; // this changes mode to [index*multiplier + base + displacement]
53
54 unsigned char sibBase = regCode[base];
55
56 if(index != rNONE && base == rNONE)
57 {
58 mod = 0;
59 sibBase = regCode[rEBP];
60 }
61
62 *stream++ = mod | spare | RM;
63 unsigned char sibScale = 0;
64 if(multiplier == 2)
65 sibScale = 1 << 6;
66 else if(multiplier == 4)
67 sibScale = 2 << 6;
68 else if(multiplier == 8)
69 sibScale = 3 << 6;
70 assert(multiplier == 0 || multiplier == 1 || multiplier == 2 || multiplier == 4 || multiplier == 8);
71
72 unsigned char sibIndex = (index != rNONE ? regCode[index] << 3 : regCode[rESP] << 3);
73
74 if(index != rNONE || base == rESP)
75 *stream++ = sibScale | sibIndex | sibBase;
76
77 if(dispImm8)
78 {
79 *stream = (char)displacement;
80 if(mod)
81 stream++;

Callers 15

x86FLDFunction · 0.85
x86FILDFunction · 0.85
x86FSTFunction · 0.85
x86FSTPFunction · 0.85
x86FISTPFunction · 0.85
x86FADDFunction · 0.85
x86FSUBFunction · 0.85
x86FSUBRFunction · 0.85
x86FMULFunction · 0.85
x86FDIVFunction · 0.85
x86FDIVRFunction · 0.85
x86FCOMPFunction · 0.85

Calls 1

assertFunction · 0.85

Tested by

no test coverage detected