Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

386 Interrupts and C++

  1. Apr 21, 2010 #1
    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
     
  2. jcsd
  3. Apr 21, 2010 #2

    Borek

    User Avatar

    Staff: Mentor

    Re: C++ to Assembly

    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: Apr 21, 2010
  4. Apr 21, 2010 #3

    rcgldr

    User Avatar
    Homework Helper

    Re: C++ to Assembly

    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: Apr 22, 2010
  5. Apr 22, 2010 #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
     
  6. Apr 22, 2010 #5

    rcgldr

    User Avatar
    Homework Helper

    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: Apr 22, 2010
  7. Apr 22, 2010 #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
     
  8. Apr 22, 2010 #7

    Mark44

    Staff: Mentor

    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.
     
  9. Apr 22, 2010 #8

    Borek

    User Avatar

    Staff: Mentor

    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.
     
  10. Apr 22, 2010 #9

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  11. May 19, 2010 #10
    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
     
  12. May 19, 2010 #11

    Borek

    User Avatar

    Staff: Mentor

    I guess that's not when running native Windows program, but when using some kind of DOS box or whatever it was called.
     
  13. May 19, 2010 #12

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: 386 Interrupts and C++
  1. To C or not to C (Replies: 31)

  2. C or C++? (Replies: 8)

Loading...