Page 1 of 1
C trouble
Posted: Thu Aug 26, 2021 3:27 pm
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)
Re: C trouble
Posted: Thu Aug 26, 2021 3:56 pm
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.
Re: C trouble
Posted: Thu Aug 26, 2021 4:04 pm
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??
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
Re: C trouble
Posted: Fri Apr 08, 2022 10:14 am
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??
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