2

My colleagues and I are trying to develop a small OS using nasm and pure C without libraries.

So we want to know how to implement interrupts if it is possible at all. I tried this but it does not assemble with NASM. (Editor's note: that's a 16-bit MASM answer that sets a real-mode IVT entry)

In case it is possible, documentation of how to achieve it or a simple example would be great.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
itasahobby
  • 301
  • 4
  • 15
  • 2
    Of course it is possible. However, interrupts work different in 64 bit (i.e. long) mode than they do in real mode as described in the answer you linked. Refer to [this article](https://wiki.osdev.org/IDT) for how to build an *IDT*, the structure you need to defined interrupt handlers for protected and long mode. – fuz Mar 08 '20 at 21:16
  • In the reference link they use c structures, I thought you need to do it in asm. However IA-32 is 32 bits that does mean that I have to define on 32 bit IDT for protected and another 32 bits IDT for the long mode? Also ¿could you bring a little example of an IDT structure and how to use it? – itasahobby Mar 08 '20 at 21:47
  • The C structure is just for notation. You can use C or assembly as you like. You only need a separate 32 bit IDT if you plan to switch between long mode and protected mode which you don't have to; you can execute 32 bit programs in long mode just fine. Let me see if I can find an example. I sadly don't have the time to write on right now. – fuz Mar 08 '20 at 22:08
  • https://wiki.osdev.org/Interrupts_tutorial is relevant but only shows 32-bit. Use https://wiki.osdev.org/Interrupt_Descriptor_Table#Structure_AMD64 instead. (And of course pusha/popa aren't available in 64-bit mode.) – Peter Cordes Mar 08 '20 at 22:58
  • 1
    Is your OS running in 16, 32 or 64-bit mode at this stage? Interrupt handling is quite different between them, especially 16-bit. – Nate Eldredge Mar 08 '20 at 23:43
  • @NateEldredge We are using 64 bits – itasahobby Mar 08 '20 at 23:43
  • 3
    Okay, so the code you linked is useless to you then, even if you were to adapt it to nasm syntax, since it's for 16 bit mode. – Nate Eldredge Mar 08 '20 at 23:46
  • 3
    This would be an excellent time to start reading the relevant section of the CPU software developer's manual, e.g. Volume 3 Chapter 6 of [Intel's](https://software.intel.com/en-us/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4). It is a somewhat bigger topic than would be reasonable to fully explain in an SO answer. – Nate Eldredge Mar 08 '20 at 23:50

0 Answers0