Page 1 of 1

Help I've Borked my GDT and IDT [SOLVED]

Posted: Mon Oct 29, 2007 2:39 am
by LordMage
Okay, I am designing my OS primarily using VC++ and NASM for the asm parts. I was following the tutorials at Brokenthorn.com and was doing very well. everything was working up to the point where the tutorials stopped. Then I added some video text support with the tutorials off of osdever so now I want to install the IDT and get my ISRs working then work on getting keyboard input. After failing on my own I started following brans tutorial. Apparently it is good but alot of people have trouble with it. I'm no exception I always faithfully stick to the content of tutorials until I see that they work and then I modify what I want. I can't do that with his though because I am using VC++ and he uses GCC. I modified everything as little as possible and except for one command I think everything is working.

the problem is that in my original tutorials I set up a GDT in the second stage bootloader, and in brans he wants to resetup the GDT in C/ASM. I agree with this and think it would give more functionality and flexibility later on when I want to add things. My problem is that He and several others say that you have to do a far jump to reset the CS register after you load a GDT. I for some reason can't get the far jump to work. I think I have the correct command syntax but I can't put in the information that I want. I have tried both inline asm and including an asm file in my project. I also tried includeing a compiled obj file in my project but am not sure if I did that right. I would much rather do it that way but I don't know how and the information I have been able to find online is lacking in the details. So, I think I need either the way to do a proper far jump with intent of reseting the cs register, either in inline asm or in an inlcuded MASN asm file, or I need a way to include my nasm compiled obj file in my VC++ project. any help would be greatly appreciated.

just incase someone thinks those aren't the problems I will give you the problems I recieve from bochs

Code: Select all


[CPU0] fetch_raw_descriptor: GDT: index (1927)324 > (17)

in the little bit above the numbers sometimes change depending on what I am trying out at the moment but unless I load the GDT and do a far jump I get that error when I try to load my ISRs and/or IRQs. I first started seeing it when I tried to program the PICs but was told that didn't have anything to do with the error. Now I am getting it immediately after the isrs load or before. not real sure it is really annoying though

Posted: Mon Oct 29, 2007 5:26 am
by JamesM
Inline asm should work. I believe VC++ uses intel syntax (please correct me if I'm wrong) so it should look like:

Code: Select all

asm volatile("jmp 0x08:mylabel");
asm volatile("mylabel:");
If I'm wrong and it uses AT&T syntax then this should work

Code: Select all

asm volatile("ljmp mylabel, $0x08");
asm volatile("mylabel:");
I may be incorrect on the ordering of the parameters to ljmp.

As for adding a .o file - How do you normally link to assembled object files? How have you written your GDT/IDT code without it? Inline assembly seems a little OTT for that amount of code ;)

I think there's a linker LDADD-type box in Project Options that should allow you to add a .o file to link in. Not certain though.

Posted: Mon Oct 29, 2007 5:41 am
by neon
You are correct in that MSVC++ uses Intel syntax.

MSVC++ allows you to add specific object files to be linked in, as long as it is of the correct object file format (By default NASM uses binary, which is not supported in MSVC++).

I gotta go very soon, but Ill see if I can help you if the problem is not resolved by tonight ;)

Posted: Mon Oct 29, 2007 5:50 am
by LordMage
Okay, I have tried the jmp 0x08:label style and got nothing but errors. how important is the volatile keyword?

second, I found how to inlcude the asm file I even got it to work. but I can't pass my GDTR variable to the asm. I was following brans example and tried the extern _gp but I got an unknown external error. I tried everythig else I could think of too, does anyone know how to use a VC++ variable externally in a nasm win32 obj file??

my commandline for compileing the NASM file is :

Code: Select all


nasm -f win32 gdt_flush.asm

that works fine and I can access the function but I just can't access the GDT pointer variable from asm

I would love to switch to inline, I hate compiling in NASM and then in VC but I haven't gotten it to work yet. I will play with the volatile if I need to

EDIT: I tried it just like you did, _asm volatile("jmp 0x08:flush2"); and I get inline assembler syntax error in opcode; found 'data type'

Posted: Mon Oct 29, 2007 5:59 am
by JamesM
http://objectmix.com/asm-x86-asm-370/68 ... r-jmp.html

That page suggests that VC++ uses a dialect of WASM, that is you want

Code: Select all

asm("jmp fword ptr 0x08,flush2");
incidentally that page was the first hit for This search

Good luck!

As for externing variables, have you made sure you defined them by using the [GLOBAL ...] directive in the NASM file?

:)

Posted: Mon Oct 29, 2007 6:08 am
by LordMage
Okay, I just tried what you suggested and unfourtunately I got nada. I then clicked on the page you cited and found it is one of the pages that I have been to several times. I don't know where they got their info but it doesn't work with my version of VC++ as for the variable. I need to grab a variable defined in VC++ and use it in NASM. I know that if I did it the other way around it would be with the global I want to use the C++ var in NASM not the NASM var in C++ although that did just give me an idea.

Posted: Mon Oct 29, 2007 6:11 am
by JamesM
In which case make sure you use the extern "C" declarator:

Code: Select all

extern "C" void *myptr;
It'll strip the _Z11belehehhhhehehh mangleage off the front so NASM can access the variable by name.

As for inline asm, could you please try each combination, NASM, WASM and post the *exact* error messages back here?

Posted: Mon Oct 29, 2007 6:25 am
by Brynet-Inc
A quick 'googling' shows VC++'s syntax for inline-assembly is quite different.. ;)

Code: Select all

int addint(int a, int b) {
	int result;
	__asm {
		mov eax, a
		add eax, b
		mov result, eax
	}
	return result;
}
Rather ugly.. 8)

Posted: Mon Oct 29, 2007 6:30 am
by JamesM
Brynet-inc: I think you are really belittleing (sp?) the OP: He is asking about a specific, underused function in VC++ inline assembly. I'm pretty certain he knows how to construct an inline statement. AFAIK you can use both styles in VC++.

Posted: Mon Oct 29, 2007 6:31 am
by Brynet-Inc
JamesM wrote:Brynet-inc: I think you are really belittleing (sp?) the OP: He is asking about a specific, underused function in VC++ inline assembly. I'm pretty certain he knows how to construct an inline statement. AFAIK you can use both styles in VC++.
Just pointing out the obviously ;) never know.. belittling is fun 8)

Posted: Mon Oct 29, 2007 6:31 am
by LordMage
Okay, good news I have my GDT working. I did basicall what James suggested and just made my GDTR an external variable from NASM to VC++ so I did

Code: Select all


extern "C" gdt_ptr _cdecl gp;

that made it work for my NASM code

Code: Select all


SECTION .bss
     gp
SECTION .text
_gdt_flush:
.
.
.

So as far as I know that is working, now I am debugging and isolating where the problem occurs in my IDT/ISR loading routines. thanks for the assistance and brain jogging.

Posted: Mon Oct 29, 2007 8:18 pm
by pcmattman
Brynet-Inc wrote:
JamesM wrote:Brynet-inc: I think you are really belittleing (sp?) the OP: He is asking about a specific, underused function in VC++ inline assembly. I'm pretty certain he knows how to construct an inline statement. AFAIK you can use both styles in VC++.
Just pointing out the obviously ;)
The obviously???
belittling is fun 8)
:twisted:

Posted: Mon Oct 29, 2007 8:51 pm
by Brynet-Inc
Owchy :( no fair, I'm illiterate in the morning..... *whistles*

Posted: Tue Oct 30, 2007 11:36 am
by LordMage
YAAAAAAAAAAAAAAAAAAAAAAAAAAYYYYYY!!!!!!


I Got my ISRs working!!!

it took alot of :

Code: Select all


extern "C" void cdecl function();

and

Code: Select all


global _variable
global _function

extern function

SECTION .bss
     _variable

but everything works now and I get ZERO error/warning from bochs just smooth sailing :D