How do I "Thunk" to 16 bit in my 32 Bit OS?
-
shacknetisp
- Member

- Posts: 29
- Joined: Wed Jan 25, 2012 2:37 pm
How do I "Thunk" to 16 bit in my 32 Bit OS?
How do I "Thunk" to 16 bit in my 32 Bit OS?
It is written in GNU Assembler and C, so NASM code isn't helpful.
I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Thank you.
It is written in GNU Assembler and C, so NASM code isn't helpful.
I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Thank you.
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
Hi,
Otherwise, see the wiki page about real mode.
Cheers,
Brendan
Why didn't you gather then information during boot when you were in real mode anyway (before you switched to 32-bit)?shacknetisp wrote:I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Otherwise, see the wiki page about real mode.
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
- brain
- Member

- Posts: 247
- Joined: Thu Nov 05, 2009 5:04 pm
- Location: UK
- Discord: https://discord.gg/brainbox
- GitHub: https://github.com/braindigitalis
- Contact:
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
Im guessing he's using grub like quite a few of us, in which case he never has to deal with real mode and the biosBrendan wrote:Hi,
Why didn't you gather then information during boot when you were in real mode anyway (before you switched to 32-bit)?shacknetisp wrote:I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Otherwise, see the wiki page about real mode.
Cheers,
Brendan
Most of the stuff you need info on is passed to you in the multiboot structure if you need that, for example vesa information or memory maps.
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
If you have just been loaded by Grub then you are probably executing at 0x100000 and the original interrupt table and real mode environment are still in place below 0x100000. Here's an example (approximately) from my loader.
It shows how thunking can be done.
It assumes a 1:1 page mapping and it's all a bit ugly with hard-coded addresses but never mind. You don't want to be switching into real mode once your OS is running so this sort of thing is only suitable during boot.
It shows how thunking can be done.
Code: Select all
int vbe_controller_info(vbe_info_block_t *p_vbe_info_blk) {
regpack16_t reg;
reg.ax = 0x4f00;
reg.di = 0xa000;
/* Copy the realmode code down to the first 64k */
memcpy((void *)0x7000, &start_real,
(uint32_t)&start_real_end - (uint32_t)&start_real);
/* Copy the regpack down to the real mode stack in the first 64k */
memcpy((void *)(0xe000 - sizeof(regpack16_t)), preg, sizeof(regpack16_t));
int10_exec();
/* Reload the protected mode descriptor tables */
cpu_init();
/* Bring back the copy of the regpack */
memcpy(®, (void *)(0xe000 - sizeof(regpack16_t)), sizeof(regpack16_t));
if (reg.ax != 0x4f00)
return -1;
/* Copy back the data */
memcpy(p_vbe_info_blk, (void *)0xa000, sizeof(vbe_info_blk));
return 0;
}If a trainstation is where trains stop, what is a workstation ?
-
shacknetisp
- Member

- Posts: 29
- Joined: Wed Jan 25, 2012 2:37 pm
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
I'm not using GRUB.
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
In that case, you are in 16bit mode, so Brendan's advice applies.
Every good solution is obvious once you've found it.
-
shacknetisp
- Member

- Posts: 29
- Joined: Wed Jan 25, 2012 2:37 pm
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
I'm not in 16 bit mode. 
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
If you haven't over-written the real mode environment and haven't made too many changes to the PC devices you should still be able to use the same scheme that I do from my Grub-launched code. Where exactly is your problem ?
If a trainstation is where trains stop, what is a workstation ?
-
shacknetisp
- Member

- Posts: 29
- Joined: Wed Jan 25, 2012 2:37 pm
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
Basically, I need to switch from 32 bit to 16 bit dynamically, and then switch back.
(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
Brendan's post provided a link that tells you how to switch modes. My post was an attempt to show you some thunking code that could work on top of that. Have you followed Brendans link? I'm not sure what you are up to.shacknetisp wrote:Basically, I need to switch from 32 bit to 16 bit dynamically, and then switch back.
(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
If a trainstation is where trains stop, what is a workstation ?
- brain
- Member

- Posts: 247
- Joined: Thu Nov 05, 2009 5:04 pm
- Location: UK
- Discord: https://discord.gg/brainbox
- GitHub: https://github.com/braindigitalis
- Contact:
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
What error does printf give, you didn't use the one from the standard library that isn't available in a freestanding environment right?
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
Actually... I am going to have to suggest against going back to real mode from protected mode for printf. While it certainly is possible, doing it while the protected mode operating environment has already been configured by the operating system can be very, very hard.
I suggest v86 mode or your own display driver over falling back to real mode. If you really want to use real mode, however, gerryg400 posted an accurate way of doing it regardless of what boot loader you are using provided you are in protected mode with identity mapping and your system configuration does not conflict with what the bios expects. I also suggest against using the name "printf" for a non-crt compliant function.
I suggest v86 mode or your own display driver over falling back to real mode. If you really want to use real mode, however, gerryg400 posted an accurate way of doing it regardless of what boot loader you are using provided you are in protected mode with identity mapping and your system configuration does not conflict with what the bios expects. I also suggest against using the name "printf" for a non-crt compliant function.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
If printf gets an error, which i presume it cannot access physical B80000, you better do it other way than resort to BIOS, like OUTs to serial port, etc.shacknetisp wrote:(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
-
shacknetisp
- Member

- Posts: 29
- Joined: Wed Jan 25, 2012 2:37 pm
Re: How do I "Thunk" to 16 bit in my 32 Bit OS?
Thanks,
I will use IO ports. Also I just switched to GRUB/QEMU
I will use IO ports. Also I just switched to GRUB/QEMU
