Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 23rd, 2007, 7:23 PM   #1
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1 Grich is on a distinguished road
Questions about interrupts

I was reading a book the other day on assembly. I reached the chapter on interrupts and I got up to the DOS interrupt int 21h. I use it and now I have been playing around with it, but as I continued to read it said that this feature, although accessible, is out of date and you should use alternatives. My Questions are:
  1. Is int 21h still used?
  2. If there is an alternative, what is it?
I am using x86 and the FASM and MASM assemblers.
__________________
SYNTAX ERROR ...

Last edited by Grich; Oct 23rd, 2007 at 7:30 PM. Reason: Wrong Title of Book.
Grich is offline   Reply With Quote
Old Oct 23rd, 2007, 7:59 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
Re: Questions about interrupts

>Is int 21h still used?
Yes.

>If there is an alternative, what is it?
Any existing library that does what you want. Interrupts are generally considered obsolete because we're not using DOS anymore (though they're still used often on Linux). If you want to hook the OS in Windows, you would probably want to use the Win32 API. Both MASM and FASM make that trivial to import Win32 DLLs.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Oct 23rd, 2007, 8:06 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Questions about interrupts

Some systems still support the old MSDOS-based software-interrupts, such as int 21h. These interrupts are strictly implementation specific.

Hardware interrupts are an electrical/physical thing. Something requiring immediate attention sends an electrical signal. This signal may be applied directly to the microprocessor, or, in the case of several interrupts which might occur relatively simultaneously, the signals may be mediated by hardware in order to grant some sort of priority.

When the signal is seen by the the microprocessor, the microprocessor is immediately yanked away from its current point of execution and redirected to some previously specified point of execution. It is highly similar to a function call, but more machine state is saved, not just particular registers.

The code that services the interrupt needs to be short and sweet, because other things might occur during the service. All this has to do with real-time response. One cannot wait until a service routine happens to come up in the course of the OS servicing multiple processes in an interleaved manner.

Software interrupts are a slightly different beast. They are essentially no more than function calls. Instead of calling a function by name, one simply issues an int xxh instruction. In the system you're talking about, all the software interrupt function addresses were stored in memory beginning at address 0 (real, physical memory address). The int instruction multiplied the interrupt value by the length of an address (4 bytes in the olden days) and fetched the address of the function from the location indicated by that value.

One obvious value of this approach is that one could change the address of the function to be called merely by changing the value in the interrupt table. Think function pointers. Doing so was a Bad Thang, generally, unless one also called the replaced address when one was finished with one's replacement function. This is called "chaining" and it still exists today.

The alternative is to use whatever API/drivers the OS provides to accomplish the same task. Some of the ints were modified by the contents of certain registers. Consequently, the interrupt might work with the keyboard buffer, or it might terminate the program. Using the API provided by the operating system adds a layer of abstraction (and thus overhead, loss of speed), but increases the safety.

As an example, the C run-time library will insulate you from all this, if you want to fetch or display a character, but it will take longer and be less flexible.

The BIOS will also offer some basic capabilities, faster but less robust than a run-time library (usually). As a matter of fact, some of the software interrupts will lead you directly to the BIOS function.

Unless you have a specialized system, those software interrupts will not be present until AFTER the boot process fills them in. In some systems, they won't be available unless you drop to a lower mode of protection. In others, they won't be available at all.

EDIT: Dang it. It took me seven minutes to write this post. Narue beat me in there with a concise, short and sweet answer.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 23rd, 2007, 8:08 PM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: Questions about interrupts

Quote:
Originally Posted by Grich
Is int 21h still used?
If you're writing DOS programs in assembly, then yeah.
Quote:
Originally Posted by Grich
If there is an alternative, what is it?
The two main alternatives would be using the BIOS (which the INT 21H call might thunk down to, depending on the system and console drivers in place) and doing direct writes.

If you're interested in going via BIOS, be aware that it's generally a slow operation. Some of the BIOS character-mode output functions let you control color and stuff, which can be nice, but other than that, they offer nothing over the higher-level DOS calls. If you still want to go this route, grab yourself a copy of Ralf Brown's Interrupt List, which is an invaluable resource for any x86 DOS programmer (side note: Ralf's port list is also great if you want to do hardware fiddling via port I/O).

If you're interested in direct video writes, you need to know a few things. First is the current video mode. Second is the number of rows; the video mode alone might only determine that you are in, say, an 80-column mode, without making a distinction between 80x25 and 80x43/80x50. From the video mode, you can determine the address of the character-mode frame buffer, and from the dimensions, you can determine the size of the buffer. This ignores BIOS 'video pages', but you'll be set to page 0 immediately after a mode set; unless your console driver uses pages or fast scrolling via changing the frame buffer address, you'll be fine (and buggered otherwise). You'll also need to write your own translation routines for 'special' characters that have some particular action, such as BEL, BS, CR, and LF. You typically don't want to print a graphical character for these. As a final cavaet: direct video writes do not update the cursor position. You'll need to do this yourself, either via BIOS INT 10H, or port I/O. If you're interested, I can dig up some old code for this (but I'm not at home now, so it might take me a while).
Quote:
Originally Posted by DaWei
The int instruction multiplied the interrupt value by the length of an address (4 bytes in the olden days) and fetched the address of the function from the location indicated by that value.
Ahh, the good old interrupt vector table.

Quote:
Originally Posted by DaWei
Doing so was a Bad Thang, generally, unless one also called the replaced address when one was finished with one's replacement function. This is called "chaining" and it still exists today.
I remember when I was a wee lad, and being impressed by the stack abuse in the Borland library sources used to achieve chaining without multiple INT calls (the caller's return address was replaced, and the IRET chained to the next handler).
Quote:
Originally Posted by DaWei
EDIT: Dang it. It took me seven minutes to write this post. Narue beat me in there with a concise, short and sweet answer.
Yeah, and you both beat me. Think how I feel!
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Oct 23rd, 2007, 8:24 PM   #5
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
Re: Questions about interrupts

God, you guys are painfully slow. I took my time with that post too, to the point of searching for a suitable example in FASM and MASM for using the Win32 API and the standard C library, got half way through writing a couple, and then decided not to.

You two need to experience the old posting races of cprog. If you didn't get the first post with a perfect answer, you were the loser! Muahahahahaha!.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Oct 24th, 2007, 2:36 PM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: Questions about interrupts

Quote:
Originally Posted by Narue
God, you guys are painfully slow.
In my defense, I had zero sleep the night before. The girlfriend is having plumbing problems, and between dealing with the plumbing contractors, building strata, and calming down a very excitable female, it hasn't fit into the schedule. :/
Quote:
Originally Posted by Narue
You two need to experience the old posting races of cprog.
If you're referring to Usenet or IRC, I wouldn't know, but if you're referring to Fidonet, I remember the C echo. There were some good people there, but some weenies as well. Thanks to people there, I remained a 'Schildt virgin'- I was never tempted to open the covers of one of his books after being enlightened.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Oct 24th, 2007, 1:33 AM   #7
WaltP
Programmer
 
Join Date: Oct 2007
Posts: 39
Rep Power: 0 WaltP is on a distinguished road
Re: Questions about interrupts

Quote:
Originally Posted by lectricpharaoh View Post
I remember when I was a wee lad, and being impressed by the stack abuse in the Borland library sources used to achieve chaining without multiple INT calls (the caller's return address was replaced, and the IRET chained to the next handler).
Yeah, and you both beat me. Think how I feel!
Remember Borland Sidekick? What an interrupt nightmare that was!
WaltP is offline   Reply With Quote
Old Oct 23rd, 2007, 8:29 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Questions about interrupts

I'm going to laugh my butt off when you're old and all those previously useful brain-cells have sunk to the bottom, dead as a doornail. Muahahahahaha!.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 23rd, 2007, 8:38 PM   #9
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
Re: Questions about interrupts

I'm not too worried about that, oldtimer. You're what, 30 years older than me?
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Oct 23rd, 2007, 9:19 PM   #10
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1 Grich is on a distinguished road
Re: Questions about interrupts

Quote:
Originally Posted by Narue View Post
I'm not too worried about that, oldtimer. You're what, 30 years older than me?
It just means he's more experienced at life than all of us.
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Template Questions King C++ 3 Feb 3rd, 2007 7:30 PM
hardware questions programmingnoob Coder's Corner Lounge 28 Aug 8th, 2006 8:04 PM
ucf cs student has some questions raspberryh Coder's Corner Lounge 33 Sep 12th, 2005 2:49 PM
Emergency: Confusing C Programming assignment questions silvia C 3 Jul 13th, 2005 3:39 AM
n00b questions..A thread on dumb questions. Cipher Coder's Corner Lounge 12 Apr 12th, 2005 9:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC