C trouble

Programming, for all ages and all languages.
Post Reply
thedude3253
Posts: 7
Joined: Wed Aug 25, 2021 12:16 pm

C trouble

Post by thedude3253 »

Hi all. This isn't exactly an OS question and more of a C question in general. I have some code that goes

Code: Select all

unsigned char getKey() {
    return 0;
}
while(1) {
    unsigned char key = getKey();
    if(key > 0) {
        printByte(key);
    }
}
This should always do nothing, correct? yet on qemu and bochs it will print out 20h occasionally for no apparent reason. I added a part to my getKey() that assigns, reassigns and then trashes a variable, and that seems to get rid of the problem on qemu, but not bochs. Is there something "under the hood" that I'm missing?
(If you want to peek for yourself, my signature has a link to my github. set the first line of the getKey() function in ioutils.c to be return 0 and it'll do it)
My toy OS BaseDOS 8)
Octocontrabass
Member
Member
Posts: 5531
Joined: Mon Mar 25, 2013 7:01 pm

Re: C trouble

Post by Octocontrabass »

thedude3253 wrote:Is there something "under the hood" that I'm missing?
Your ISRs don't save and restore the registers they modify.
thedude3253
Posts: 7
Joined: Wed Aug 25, 2021 12:16 pm

Re: C trouble

Post by thedude3253 »

Octocontrabass wrote:
thedude3253 wrote:Is there something "under the hood" that I'm missing?
Your ISRs don't save and restore the registers they modify.
Arg how could I be so blind?? #-o
Thank you so much, I see exactly where I'm going wrong and now I know what to fix. I somehow thought that the registers got pushed automatically during an interrupt call and popped during iretq haha
My toy OS BaseDOS 8)
iProgramInCpp
Member
Member
Posts: 81
Joined: Sun Apr 21, 2019 7:39 am

Re: C trouble

Post by iProgramInCpp »

thedude3253 wrote:
Octocontrabass wrote:
thedude3253 wrote:Is there something "under the hood" that I'm missing?
Your ISRs don't save and restore the registers they modify.
Arg how could I be so blind?? #-o
Thank you so much, I see exactly where I'm going wrong and now I know what to fix. I somehow thought that the registers got pushed automatically during an interrupt call and popped during iretq haha
Some do, yes. On 32-bit, EIP, CS and EFLAGS get pushed, unless you switch CPL (current privilege level), in which case SS and ESP also get pushed, and later taken by iretd. On 64-bit, RIP, CS, RFLAGS, SS and RSP get pushed and taken by iretq. The order is not the same, for more details check the Intel IA-32 or x86-64 Software Developer Manual
Hey! I'm developing two operating systems:

NanoShell --- A 32-bit operating system whose GUI takes inspiration from Windows 9x and early UNIX desktop managers.
Boron --- A portable SMP operating system taking inspiration from the design of the Windows NT kernel.
Post Reply