i have a question about the branchtables.
There are two ways to declare such a table:
- in the Data Sector (DS)
- in the Code Sector (CS)
Whats the different between this methods?
I’ve learned it this the following examples:
Case 1:
SECTION .data
i dd 2;
stab dd m1,m2,m3 ; branchtable for switch
SECTION .text
global start
start:
mov ebx , [ i ] ; switch ( i )
cmp ebx , 1 ;
jl end
cmp ebx , 3
jg end
shl ebx , 2 ; / ∗ stab 4 Bytes ∗ /
jmp [ stab+ebx −4];
m1: ;do something.....
....
Case 2:
SECTION .data
i dd 2;
SECTION .text
global start
start:
mov ebx , [ i ] ; switch ( i )
cmp ebx , 1 ;
jl end
cmp ebx , 3
jg end
shl ebx , 2 ; / ∗ stab 4 Bytes ∗ /
jmp [ cs : ebx+stab −4]; branchtable in codesegment
ALIGN 4 ;
stab dd m1,m2,m3
m1: ; do something
....
Our prof told us, that method 2 is more effectiv but why? Because to the branchtable it’s only a short jump and we doesn’t need to show in the DS?
greetz destiny
which method is more effective depends on the processor you are dealing with, however, I beg to differ with your prof, using
CSrequires a segment prefix override, making the code bigger, thus longer to process and less cache friendly. but on x86 windows (userland),CSandDSflatten out to the same linear address space, making it a moot optimization.Certain processors (Intel Atom) also have slower access to
CSwhen the segment base is non-zero, though under x64 this falls away as all segments apart fromFSandGSare ignored (their base is implicity 0), due to x64’s flat addressing model.It should also be noted that Intel advises the use of as few segment registers as possible (this ease the burden on the register renamer).