FPU initialization
-
- Member
- Posts: 46
- Joined: Mon Sep 07, 2020 5:50 pm
FPU initialization
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!
-
- Member
- Posts: 5572
- Joined: Mon Mar 25, 2013 7:01 pm
Re: FPU initialization
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?
What problems are you having, exactly?
-
- Member
- Posts: 46
- Joined: Mon Sep 07, 2020 5:50 pm
Re: FPU initialization
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.
-
- Member
- Posts: 5572
- Joined: Mon Mar 25, 2013 7:01 pm
Re: FPU initialization
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?
-
- Member
- Posts: 46
- Joined: Mon Sep 07, 2020 5:50 pm
Re: FPU initialization
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.
-
- Member
- Posts: 5572
- Joined: Mon Mar 25, 2013 7:01 pm
Re: FPU initialization
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.
You might need to start using a linker script for that.
-
- Member
- Posts: 46
- Joined: Mon Sep 07, 2020 5:50 pm
Re: FPU initialization
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?
-
- Member
- Posts: 5572
- Joined: Mon Mar 25, 2013 7:01 pm
Re: FPU initialization
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!)
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!)
-
- Member
- Posts: 46
- Joined: Mon Sep 07, 2020 5:50 pm
Re: FPU initialization
That fixed both issues, even with just copy/pasting that linker script into my code! Thanks a ton!