IDT and little-endian

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Lovmy
Posts: 17
Joined: Fri Oct 05, 2007 5:58 am

IDT and little-endian

Post by Lovmy »

Hello,

I have problem with IDT. For example, y have structure:

struct IDT
{
unsigned short offset_0_15;
unsigned short selecteurSegment;
unsigned char zero;
unsigned char drapeau;
unsigned short offset_16_31;
};

If, for example, i use this values (it's only for example, it's not good values)

listeIDT.offset_0_15 = 0xAABB;
listeIDT.selecteurSegment = 0xCCDD;
listeIDT.zero = 0;
listeIDT.drapeau = 0x11;
listeIDT.offset_16_31 = 0xEEFF;

I have into memory:

Adress Value
0x40430C 0xBB
0x40430D 0xAA
0x40430E 0xDD
0x40430F 0xCC
0x404310 0x00
0x404311 0x11
0x404312 0xFF
0x404313 0xEE

But if my structure is:

struct IDT
{
unsigned short offset_0_15;
unsigned short selecteurSegment;
unsigned short zero_and_drapeau;
unsigned short offset_16_31;
};

and i use this values:

listeIDT.offset_0_15 = 0xAABB;
listeIDT.selecteurSegment = 0xCCDD;
listeIDT.drapeau_and_zero = 0x0011;
listeIDT.offset_16_31 = 0xEEFF;

i have into memory:

Adress Value
0x40430C 0xBB
0x40430D 0xAA
0x40430E 0xDD
0x40430F 0xCC
0x404310 0x11
0x404311 0x00
0x404312 0xFF
0x404313 0xEE

adress 0x404310 and 0x404311 are not in same order. Because little-endian.

How to order parameter of struct to have good order un memory ?

Best Regards.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: IDT and little-endian

Post by JamesM »

Hi,

What you're saying is correct. As to your question - the answer is the first structure you posted!

However, remember that the compiler will automatically pad your structure so that its members are aligned, so you must use __attribute__((packed)) on the structure declaration.

Having two 8-bit integers (chars) is the way to do it.

Cheers,

James
Lovmy
Posts: 17
Joined: Fri Oct 05, 2007 5:58 am

Re: IDT and little-endian

Post by Lovmy »

Hello,

Thank for your response JamesM, but my compiler, BCC32, don't know "__attribute__((packed))".

Best Regards
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: IDT and little-endian

Post by Brynet-Inc »

There is no "standard" way for doing that, some compilers have __packed; others might have a #pragma.

__attribute__((packed)) is basically the equivalent of aligning the structure to a 1 byte boundary..

Code: Select all

#ifdef __BORLANDC__
# pragma pack(push)
# pragma pack(1)
#endif 

struct IDT
{
unsigned short offset_0_15;
unsigned short selecteurSegment;
unsigned char zero;
unsigned char drapeau;
unsigned short offset_16_31;
};

#ifdef __BORLANDC__
# pragma pack(pop)
#endif
Rather ugly though, isn't it? :)

References:
http://gcc.gnu.org/onlinedocs/gcc-4.3.0 ... Attributes
http://www.decompile.com/cpp/faq/W8059_ ... g_Size.htm
http://en.wikipedia.org/wiki/Packed
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Lovmy
Posts: 17
Joined: Fri Oct 05, 2007 5:58 am

Re: IDT and little-endian

Post by Lovmy »

Hello,

to resolv problem of __packet, i use array[6] (tableauIDT[6]) for struct with limite and base.

I have ISR at adress 1F59h:

00001F59 55 push ebp
00001F5A 8BEC mov ebp,esp
00001F5C 60 pusha
00001F5D E8BCFDFFFF call 0x1d1e
00001F62 6817344000 push dword 0x403417
00001F67 E851F8FFFF call 0x17bd
00001F6C 59 pop ecx
00001F6D E833000000 call 0x1fa5
00001F72 E8A1FDFFFF call 0x1d18
00001F77 61 popa
00001F78 5D pop ebp
00001F79 CF iret

In memory i have:

0040430A F8 -> 07F8 = FF * 8 = length of IDT (limit) = tableauIDT[0]
0040430B 07
0040430C 10 -> 00404310 start of 256 (base adress)
0040430D 43
0040430E 40
0040430F 00

00404310 59 -> first struct IDT offset_0_15
00404311 1F -> 00001F59 adress of ISR
00404312 08 -> code segment selector 0008h
00404313 00
00404314 00 -> ZERO
00404315 8E -> 10001110 P=1 DLP=00
00404316 00 -> offset_16_31
00404317 00

...
i have same ISR for all interrupt (0 to 255)

struct IDT is:

struct IDT
{
unsigned short offset_0_15;
unsigned short selecteurSegment;
unsigned char zero;
unsigned char drapeau;
unsigned short offset_16_31;
};


i load IDT in C with:

adresseINT = (unsigned long)&tableauIDT[0];
asm {
LIDT FWORD PTR adresseINT
}

adresseINT = 0040430A

But PC reboot, where is my error ?

Best Regards.
Post Reply