Page 1 of 1

FPU initialization

Posted: Fri Sep 11, 2020 5:20 pm
by abstractmath
Hello! I've been having trouble with floating point operations in my OS, and I've been having issues trouble shooting exactly what the problem is. One suggestion I got was to check if the FPU was initialized, and after a lot of googling I haven't been able to find much information on it. I have looked at the x86 floating point instruction set, but using the fninit instruction as suggested still yields the same problems. I'd really appreciate some advice when it comes to handling the FPU! Thanks!

Re: FPU initialization

Posted: Fri Sep 11, 2020 9:15 pm
by Octocontrabass
To initialize the x87 FPU, set CR0.MP and CR0.NE, clear CR0.EM and CR0.TS, and execute FNINIT. (I'm assuming you don't care about ancient or embedded hardware.)

What problems are you having, exactly?

Re: FPU initialization

Posted: Fri Sep 11, 2020 9:21 pm
by abstractmath
I'm getting a strange issue with floating point division. From what I can tell, multiplication, subtraction, and addition work just fine. Any floating point division results in a NaN.

Re: FPU initialization

Posted: Fri Sep 11, 2020 9:52 pm
by Octocontrabass
I notice from your github that you're assigning strings to variables before printing them. Is there some reason you can't directly pass string literals to your print function?

Re: FPU initialization

Posted: Fri Sep 11, 2020 10:02 pm
by abstractmath
Yeah, for some reason when I do that, it doesn't print anything. I didn't think it was entirely significant since I could just work out around it out of laziness.

Re: FPU initialization

Posted: Fri Sep 11, 2020 10:17 pm
by Octocontrabass
It's pretty significant. String constants and floating-point constants both go in the rodata section, and I'd guess yours is either not included in the final binary or is not loaded at the correct address.

You might need to start using a linker script for that.

Re: FPU initialization

Posted: Fri Sep 11, 2020 10:20 pm
by abstractmath
Oh wow I had no idea those two issues would be linked! As for linker scripts, I'm not entirely sure how I would go about making one, and how it would resolve this issue. Could you elaborate more?

Re: FPU initialization

Posted: Fri Sep 11, 2020 10:32 pm
by Octocontrabass
There's an example script here you can adapt for your own use. Pass its filename to ld using the "-T" option.

I suggest also examining the resulting binary in a hex editor to ensure your strings are actually being included - if they don't print but you see them in the binary, that means the section is present but the linker thinks it will be loaded at a different address. If you don't see them at all, there's a problem with the linker script. (I haven't tested the example linker script, so it may not be correct!)

Re: FPU initialization

Posted: Fri Sep 11, 2020 10:58 pm
by abstractmath
That fixed both issues, even with just copy/pasting that linker script into my code! Thanks a ton!