OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Mar 19, 2024 4:00 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: C trouble
PostPosted: Thu Aug 26, 2021 3:27 pm 
Offline

Joined: Wed Aug 25, 2021 12:16 pm
Posts: 7
Hi all. This isn't exactly an OS question and more of a C question in general. I have some code that goes
Code:
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)


Top
 Profile  
 
 Post subject: Re: C trouble
PostPosted: Thu Aug 26, 2021 3:56 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
thedude3253 wrote:
Is there something "under the hood" that I'm missing?

Your ISRs don't save and restore the registers they modify.


Top
 Profile  
 
 Post subject: Re: C trouble
PostPosted: Thu Aug 26, 2021 4:04 pm 
Offline

Joined: Wed Aug 25, 2021 12:16 pm
Posts: 7
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)


Top
 Profile  
 
 Post subject: Re: C trouble
PostPosted: Fri Apr 08, 2022 10:14 am 
Offline
Member
Member

Joined: Sun Apr 21, 2019 7:39 am
Posts: 76
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group