Using 386 Interrupts in C++: How to Execute Assembly Code

  • C/C++
  • Thread starter yeestrada
  • Start date
  • Tags
    C++
In summary: If you're talking about Windows XP embedded systems, then definitely not without some help from someone more knowledgeable about that type of system.You're not wrong about Windows 3.1. Windows 95 and later support inline assembly, so you could write functions in assembly and call them from C or C++. However, you're probably not going to be able to do what you want to do in Windows XP without more help from someone more familiar with that environment.
  • #1
yeestrada
3
0
Hello every body, I have a doubt, I'm working right now with a micro 386, so I'm using it's interruptions, but I want to link c++ with assembly code and I don't know how can I use an interruption inside c++, I hear that is not possible to execute assembly code form a high level language, I,m not so sure of it. Can someone ask me if is possible to execute an interruption of assembly code form c++ and how can I do it?

thanks for your help

yordan
 
Technology news on Phys.org
  • #2


Last time I wrote my own interrupt handler was about 13 years ago, so I have no idea how these things are done today. But I am more than sure that you need to specify OS and compiler before you can get any help.
 
Last edited by a moderator:
  • #3


You can mix C++ and assembly code. If you're writing interrupt code, you need to avoid sharing variables between task code and interrupt code. I don't know what type of operating system you're micro 386 is using, but you'll need to use it's functions to communicate between interrupts and any task used by the micro operating system.

You'll also want to write some dummy C++ code that calls the C++ interrupt handler, then have the compiler output the assembly code from that dummy code, so you know the naming and calling conventions used by your compiler.

I've written and/or worked with a few small operating systems, and these only needed a small amount of assembly code in the low level interrupt handlers, with a call to a function (perhaps via a pointer to function) written in C. The cpu and OS specific context switch stuff is handled in assembly, while most of the I/O and logic is in the C function.

If you buy an OS, then the assembly code is already written, usually includes some basic example interrupt handlers written in C, and you just write the C part of the code. This could be done with C++ as well, but I personally haven't worked in such an environment.
 
Last edited:
  • #4
I'm now working with Windows XP sp2 and I'm programing with c++ builder 6. I was trying to do it with C# code, but as I understand, C# has its own assembly language. I'm a beginner on this topic, so I'll be very grateful if someone can provide an example (can be developed in c# or C++).

thanks everybody

yordan
 
  • #5
I'm going to assume that you don't mean actual interrupts, but just want to mix assembly code and C or C++ code. Although inline assembly is rare these days, there's no reason you can't write functions in assembly code than can be called and/or make calls to C or C++ functions. To deal with the naming and register usage conventions, I'd recommend creating a small C code function and using a compiler option to produce assembly code to get you started.

To access interrupts in Windows XP, you need to create and install a device driver. You'll need to download and install the WDK (windows driver kit). I haven't done this in years. I did a device driver for Windows NT, with what was called DDK (device driver kit), and used remote debugging, using two computers. The OS and CPU specific interrupt stuff are handled within Windows XP (some of this would be assembly code), and you supply pointers to C functions to handle interrupts for your driver.

If you're only using Windows XP to create code that runs in a different environment, then you can mix assembly, C, and C++ functions. You'll need some method to convert the .EXE image into a binary image for the target environment, which I assume the c++ builder 6 includes.
 
Last edited:
  • #6
Let me try to explain, I just want to use an interruption to show how is possible to connect asm code with a high level language, I'm in a course of assembly and this is a homework, I just want to use the graphic mode (int 10h) or simply use the int 21h to show an example. That's why I'm trying to insert asm code in c++ code.

thanks again

yordan
 
  • #7
I'm pretty sure that you won't be able to do what you want to do running Windows XP, especially the old DOS int 21h functions, and maybe the int 10h graphics functions. The last Microsoft OS that supported them was Windows 3.1 just prior to Windows 95.

If I'm wrong about any of this, hopefully someone will jump in and correct me.
 
  • #8
It is more complicated than that. I have just checked - my program written in Turbo Pascal and compiled on May 17th, 1995, switched my ATI graphic card into VGA 0x12 mode, modified palette and displayed whatever it should display - under XP.

If anyone want's (is brave enough) to try - I can send you both source and exe (6kb together). This is a simulation of sand falling down in liquid - so grains are getting separated depending on their size, as the color depends on the grain size, they create interesting landscapes. I have seen something like that made of sand in water, together with some air, placed between two sheets of glass. Whene you rotate it, air goes up, blocking sand, liquid goes down, then sand starts to fall between air bubbles, so it pours down in thin line. Bubbles move, so sand pours down in random places and for a random periods of time.

Code is just an attempt to recreate the effect, so it has nothing to do with physical accuracy.

Unfortunately, after graphic mode is changed, PrtScr no longer copies screen content to clipboard, so I can't show you the effect.
 
  • #9
yeestrada said:
I just want to use an interruption to show how is possible to connect asm code with a high level language
You mean issue a BIOS (Int 10->20h) or MSDOS (int 21h) call? Those should work in XP, although you'll probably need to be in full screen dos console mode for VGA calls to work.

Programs such as Glidos can also intercept these interrupts, probably via the debugger interface, but I'm not sure how it's done.

You could have the assembly code do something simpler, like writing your own strchr() function in assembly.
 
  • #10
Mark44 said:
...The last Microsoft OS that supported them (the old DOS int 21h functions, and maybe the int 10h graphics functions) was Windows 3.1 just prior to Windows 95.

If I'm wrong about any of this, hopefully someone will jump in and correct me.

Actually, I'm running Windows 98SE, and, with nasm, I've used both BIOS INT 10h and DOS INT 21h, with AH containing usually 09 -- this outputs a '$'-terminated string located at DS:DX.Also, OP: Delphi can connect to asm code, see here: http://delphi.about.com/library/bluc/text/uc052501a.htm
 
  • #11
Unit said:
Actually, I'm running Windows 98SE, and, with nasm, I've used both BIOS INT 10h and DOS INT 21h, with AH containing usually 09 -- this outputs a '$'-terminated string located at DS:DX.

I guess that's not when running native Windows program, but when using some kind of DOS box or whatever it was called.
 
  • #12
You can't directly access interrupts in windows >98.
You need to write a protected mode device driver in newer versions to access memory directly, even then you can't (IIRC) access bios interrupts.
 

1. What are 386 interrupts in C++?

386 interrupts are software interrupts used by the 386 microprocessor to switch between different programs or tasks. In C++, these interrupts can be handled by creating interrupt service routines (ISRs) which are functions that are executed in response to specific interrupt signals.

2. How are 386 interrupts handled in C++?

In C++, 386 interrupts are handled using interrupt service routines (ISRs). These ISRs are functions that are associated with specific interrupt signals and are executed when that interrupt occurs. ISRs are typically written in assembly language and can be used to perform specific tasks or handle hardware events.

3. What is the purpose of using 386 interrupts in C++?

The purpose of using 386 interrupts in C++ is to allow for efficient multitasking and handling of hardware events. By using interrupts, the processor can quickly switch between different tasks or programs without wasting processing power. Additionally, interrupts can be used to handle hardware events such as button presses or data transfers, making them an essential aspect of embedded systems programming.

4. Can 386 interrupts be used in modern C++ programs?

Yes, 386 interrupts can still be used in modern C++ programs. While they may not be as commonly used as they were in the past, they are still relevant in embedded systems and low-level programming. However, with the evolution of C++ and modern operating systems, the use of 386 interrupts may be less necessary for most applications.

5. Are there any limitations or drawbacks to using 386 interrupts in C++?

One limitation of using 386 interrupts in C++ is that they require knowledge of assembly language, which may not be familiar to all C++ programmers. Additionally, if not handled properly, interrupts can cause system crashes or other errors. It is important to have a thorough understanding of the interrupt handling process to avoid these potential issues.

Similar threads

  • Programming and Computer Science
Replies
16
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
4
Replies
122
Views
12K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
8
Views
834
  • Programming and Computer Science
Replies
3
Views
843
  • Programming and Computer Science
Replies
1
Views
504
Back
Top