I have been working on a new kernel recently, and noticed some strange behaviour. I noticed that my exception handler was not displaying the correct error message on a division-by-zero error. After running the kernel through GDB, i found that the ISR being called was not zero, as I expected based on the wiki, but instead 6.
Is it possible that I have implemented something incorrectly, or is this just how things work and im misreading the wiki?
Relevant Information:
- Interrupts being tested with line `int i = 1/0`
- Compiler: GCC
- VM: Qemu
- Debugger: GDB
[SOLVED] Division exception triggering ISR 6?
-
- Posts: 7
- Joined: Sun Feb 02, 2025 5:59 pm
- GitHub: https://github.com/Toriiiiiiiiii
- Contact:
[SOLVED] Division exception triggering ISR 6?
Last edited by torii on Mon Feb 03, 2025 12:36 pm, edited 1 time in total.
-
- Member
- Posts: 5695
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Division exception triggering ISR 6?
In C, division by zero is undefined behavior. That means your compiler is free to generate whatever code it wants - including code that causes #UD (interrupt 6) when you were expecting #DE (interrupt 0).
If you want to see #DE, you can use inline assembly:
Code: Select all
asm( "div %ah" );
- JackScott
- Member
- Posts: 1036
- Joined: Thu Dec 21, 2006 3:03 am
- Location: Hobart, Australia
- Mastodon: https://aus.social/@jackscottau
- Matrix: @JackScottAU:matrix.org
- GitHub: https://github.com/JackScottAU
- Contact:
Re: Division exception triggering ISR 6?
I just tested this in my kernel. I also received interrupt #6. Note I had to add a line of code using i for something afterwards, otherwise my particular combination of GCC and CFLAGS would optimise it out.
-
- Posts: 7
- Joined: Sun Feb 02, 2025 5:59 pm
- GitHub: https://github.com/Toriiiiiiiiii
- Contact:
Re: Division exception triggering ISR 6?
Seems undefined behaviour was the problem - I hadn't even considered that as a possibility. Replacing the C code with inline assembly to trigger the interrupt resulted in #DE as expected.Octocontrabass wrote: ↑Sun Feb 02, 2025 6:15 pm If you want to see #DE, you can use inline assembly:
Code: Select all
asm( "div %ah" );
Thanks for the help!