4

I have been given some vendor supplied driver code written in C that runs on DOS (yes DOS) to access hardware. I am trying to work out what this code does, so far without much success. In particular I am having problems understanding the following code

void (interrupt *oldcan)(void);

void  interrupt far  can_isr(void)
{
    /* function body */
}

I am guessing the first line defines a function pointer but I have never seen the interrupt word which I am assuming is some kind of DOS API function. I have never used DOS before, and the function definition above looks to me that it has 3 return types - void, interrupt and far which clearly is not possible. The function actually has void return type, but what is the meaning of interrupt far ?? Any assistance will be gratefully received.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101

3 Answers3

8

The interrupt keyword is (was) used to create an interrupt handler -- i.e., this function was intended to be invoked by an interrupt. far means when it returns, it expects both CS and IP to be on the stack, and the interrupt keyword means it expects the flags register to have been pushed as well (all a given for anything that's invoked via interrupt, but it's also possible to simulate an interrupt by pushing the flags register, then doing a far jump).

The most common use was probably for code that was going to use a serial port -- the BIOS/DOS serial port handler would lose characters at higher speeds (anything above about 300 bps, originally) so you had to install a handler of your own. When a character showed up at the serial port, the serial port hardware would assert a line that interrupted the processor. Based on that, your interrupt handler routine would be invoked. Your code needed to read the data in from the serial port into memory (and do a few things like re-enabling the interrupt) and return.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 6
    +1 That is one of these questions that tells your age even if you hide it from your profile :) – Sergey Kalinichenko Sep 10 '12 at 14:26
  • Thanks for this answer, it gives me a starting point. Is this kind of code no longer used then? I've only been in the game full time for 5 years but I have never seen anything like this. – mathematician1975 Sep 10 '12 at 14:29
  • It's like watching a BONES episode. You're the Brennan of interrupts :-) – Simon Mourier Sep 10 '12 at 14:30
  • @mathematician1975: It was used under DOS, but Linux, Windows, etc., all normally provide adequate device drivers so it hasn't been needed since they came into use. On them, you can just open the serial port as a normal device and read/write it. The OS/device driver do the interrupt handling without your help. – Jerry Coffin Sep 10 '12 at 14:33
  • @SimonMourier: I hope I'm at least a *little* more socially acceptable (if not nearly as good looking). – Jerry Coffin Sep 10 '12 at 14:34
  • I was hoping it would all be DOS specific. This is actually code for CAN bus hardware. Thanks very much for your help I now have some idea of what to look into to work out what on earth is going on. – mathematician1975 Sep 10 '12 at 14:40
  • Sorry, I'm a noob who just came across this but can you please explain what CS and IP means in this scenario? – Songg Tùng Mar 17 '21 at 16:46
  • 1
    @SonggTùng: Sorry. CS is "code segment" and "IP" is "instruction pointer". The basic idea is that under DOS, an address came in two pieces, a segment and an offset into that segment. A `far` pointer contained both a segment and an offset (whereas a `near` pointer contained only an offset into some implicitly assumed segment). – Jerry Coffin Mar 17 '21 at 17:13
2

It's very likely a compiler-specific extension, that is used to "install" the function as an interrupt handler in some very platform-specific manner.

There is no interrupt keyword in the standard C language.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

check out link http://www.phanderson.com/printer/periodic_interrupt.html. The link discusses interrupts being used with code examples - mind you its very brief.

You can also see this post: What is the difference between far pointers and near pointers? for information on the far keyword.

Community
  • 1
  • 1
Robert H
  • 11,520
  • 18
  • 68
  • 110