Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. calling int 21h from a __asm block of code

calling int 21h from a __asm block of code

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
9 Posts 5 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CorvetteZ0606
    wrote on last edited by
    #1

    I'm trying to rewrite an assembly program in C++. Instead of reinventing the wheel, I am cutting and pasting a lot of asm into __asm blocks. However, whenever I try to call int 0x21, I get an unhandled exception. Why is this and how do I get around it?

    J A G 3 Replies Last reply
    0
    • C CorvetteZ0606

      I'm trying to rewrite an assembly program in C++. Instead of reinventing the wheel, I am cutting and pasting a lot of asm into __asm blocks. However, whenever I try to call int 0x21, I get an unhandled exception. Why is this and how do I get around it?

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      In this case you need to reinvent (most of) the wheel. The call int 0x21 (MS-DOS call) is probably allocating memory from the Upper Memory Area (UMA), which implies 16-bit code (old code). I am just surprised that that is the only problem you are asking about. If you need to rewrite this old code, then you need to understand it: 1) You need to know what programing language the assembly code was ment to be accessed from (assuming it is not entirely in assembly). 2) Which version of the compiler was used (if possible). 3) Which processor it was written for (probably intel386). 4) What each of the assembly commands mean (+ what each function call does). 5) etc... 6) Break out or find the old books, on the calling language and the assembly language used. The fact that you are trying this implies that you have a good idea of what you are attempting to do. If the assembly code (that calls int 0x21) represents a function call and you just copied it into and __asm block, in a C/C++ function, then post the funtion code. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

      C 1 Reply Last reply
      0
      • J John R Shaw

        In this case you need to reinvent (most of) the wheel. The call int 0x21 (MS-DOS call) is probably allocating memory from the Upper Memory Area (UMA), which implies 16-bit code (old code). I am just surprised that that is the only problem you are asking about. If you need to rewrite this old code, then you need to understand it: 1) You need to know what programing language the assembly code was ment to be accessed from (assuming it is not entirely in assembly). 2) Which version of the compiler was used (if possible). 3) Which processor it was written for (probably intel386). 4) What each of the assembly commands mean (+ what each function call does). 5) etc... 6) Break out or find the old books, on the calling language and the assembly language used. The fact that you are trying this implies that you have a good idea of what you are attempting to do. If the assembly code (that calls int 0x21) represents a function call and you just copied it into and __asm block, in a C/C++ function, then post the funtion code. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

        C Offline
        C Offline
        CorvetteZ0606
        wrote on last edited by
        #3

        The assembly program was a stand alone application; it was not called from anywhere. I wanted to add some functionality to it, but it's been so long since I've programmed in asm that I've forgotten most of it. The compiler that was originally used to compile it was a86. It was written for the 286 intel processor. (16bit only) I'm familiar with most of the code and what it's supposed to do since I had left some comments here and there, but this is the first time I've ever tried to put asm and c++ in the same program. Here is a block of code that's causing me problems: ;//;open file using handle (r/w) lea dx, name mov ax, 3D02h int 0021h jc end xchg ax, bx basically what it does is load the address of filename in memory into dx. name is a NULL terminated char array defined in the main() function. Then move 3D02 into ax. This will cause int21 to call function 3d which is the open filename command. The 02 byte in AL specifies to open the file in r/w mode. Then int21 is called to open the file. Afterwards the Carry flag is checked to see if the open was a success or failure. This should catch any errors with the open and prevent the program from crashing. (and yes, the label "end" is inside the same __asm block as the above code). Finally ax and bx are exchanged to put the file handle in bx. The next function called in asm code will require the handle be in bx, so might as well put it there now. I debuged the above code and it crashes when it gets to the call to int21. I'm not sure what to do from here. Also, I'm running Windows XP with a NTFS filesystem. When I run the asm code after it was compiled with a86, it runs fine. :omg: thanks!

        J 1 Reply Last reply
        0
        • C CorvetteZ0606

          I'm trying to rewrite an assembly program in C++. Instead of reinventing the wheel, I am cutting and pasting a lot of asm into __asm blocks. However, whenever I try to call int 0x21, I get an unhandled exception. Why is this and how do I get around it?

          A Offline
          A Offline
          Alexander M
          wrote on last edited by
          #4

          Interrupts are GENERALLY NOT ALLOWED in Windows! There is only one exception: Interrupt 2Eh, which is used for communication between user mode and kernel mode. Every other interrupt should cause an exception because it can only be called from Ring-0! Don't try it, just do it! ;-)

          1 Reply Last reply
          0
          • C CorvetteZ0606

            I'm trying to rewrite an assembly program in C++. Instead of reinventing the wheel, I am cutting and pasting a lot of asm into __asm blocks. However, whenever I try to call int 0x21, I get an unhandled exception. Why is this and how do I get around it?

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            Are you really sure you couldn't just recreate this assembly language program's functionality in a straight C++ application? Almost anything you try to do via int 0x21 is an MS-DOS function call, which may or may not be emulated in the Windows environment (depending upon how you're running your application). The unhandled exceptions you are seeing are probably related to access denials.


            Software Zen: delete this;

            C 1 Reply Last reply
            0
            • G Gary R Wheeler

              Are you really sure you couldn't just recreate this assembly language program's functionality in a straight C++ application? Almost anything you try to do via int 0x21 is an MS-DOS function call, which may or may not be emulated in the Windows environment (depending upon how you're running your application). The unhandled exceptions you are seeing are probably related to access denials.


              Software Zen: delete this;

              C Offline
              C Offline
              CorvetteZ0606
              wrote on last edited by
              #6

              I'm sure I can recreate the program in strait C++. It just would be easier to reuse my old code if it were possible. If you truely can't use any interupts except 2E in __asm blocks under windows, then I don't see where __asm blocks would be useful for anything. In all the asm programs I've written, the code has consisted of mostly shuffling registers and interupt calls. Without using interupts, how can assembly language be useful for anything?

              D G 2 Replies Last reply
              0
              • C CorvetteZ0606

                I'm sure I can recreate the program in strait C++. It just would be easier to reuse my old code if it were possible. If you truely can't use any interupts except 2E in __asm blocks under windows, then I don't see where __asm blocks would be useful for anything. In all the asm programs I've written, the code has consisted of mostly shuffling registers and interupt calls. Without using interupts, how can assembly language be useful for anything?

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                CorvetteZ0606 wrote: Without using interupts, how can assembly language be useful for anything? Unless you are using DOS-specific interrupts, or those that deal directly with hardware, you can do numerous things. But most assembly code has been "wrapped" by a much friendlier function. Since device drivers operate at ring 0 instead of ring 3, they are loaded with assembly code.


                "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                1 Reply Last reply
                0
                • C CorvetteZ0606

                  I'm sure I can recreate the program in strait C++. It just would be easier to reuse my old code if it were possible. If you truely can't use any interupts except 2E in __asm blocks under windows, then I don't see where __asm blocks would be useful for anything. In all the asm programs I've written, the code has consisted of mostly shuffling registers and interupt calls. Without using interupts, how can assembly language be useful for anything?

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #8

                  Assembly language still has its uses, even in modern Windows applications. The most common used are to either hand-optimize code that is performance-critical, or to use specific instructions out of the CPU instruction set. Consider this, however. In over ten years of Windows application development, I've never had code that was so performance-intensive that I felt the need to write assembly language to speed it up.


                  Software Zen: delete this;

                  1 Reply Last reply
                  0
                  • C CorvetteZ0606

                    The assembly program was a stand alone application; it was not called from anywhere. I wanted to add some functionality to it, but it's been so long since I've programmed in asm that I've forgotten most of it. The compiler that was originally used to compile it was a86. It was written for the 286 intel processor. (16bit only) I'm familiar with most of the code and what it's supposed to do since I had left some comments here and there, but this is the first time I've ever tried to put asm and c++ in the same program. Here is a block of code that's causing me problems: ;//;open file using handle (r/w) lea dx, name mov ax, 3D02h int 0021h jc end xchg ax, bx basically what it does is load the address of filename in memory into dx. name is a NULL terminated char array defined in the main() function. Then move 3D02 into ax. This will cause int21 to call function 3d which is the open filename command. The 02 byte in AL specifies to open the file in r/w mode. Then int21 is called to open the file. Afterwards the Carry flag is checked to see if the open was a success or failure. This should catch any errors with the open and prevent the program from crashing. (and yes, the label "end" is inside the same __asm block as the above code). Finally ax and bx are exchanged to put the file handle in bx. The next function called in asm code will require the handle be in bx, so might as well put it there now. I debuged the above code and it crashes when it gets to the call to int21. I'm not sure what to do from here. Also, I'm running Windows XP with a NTFS filesystem. When I run the asm code after it was compiled with a86, it runs fine. :omg: thanks!

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    I was thinking about a function call such as this (so I could call it): return_type OpenFile(char* name) { __asm { lea dx, name mov ax, 3D02h int 0021h jc end xchg ax, bx ... } ... } Anyway, it does not matter. I would go ahead and reinvent the wheel; actualy you are not reinventing the wheel, you are just bring it up to date. Things like accessing file I/0 should be handle at the current language level (things change). The only reason for keeping any of the code at assembly level is for speed. Any of the assembly code you wish to keep and wrap in a function call should be wrapped in C function calls, so you do not have to deal with the difficulties (name mangaling) of C++. Unless you are writting code at the driver level (ring 0), you normaly do not need to write assembly code. Modern day compilers are very good at otimising your code. Plus the fact that writting your code in C/C++ is much easier to understand (and modify). Understand this: Under Win2000 and above the system will get upset if you try to access memory (read/write) without the proper security clearance, this is intended to defeat crackers/hackers. __asm can be very useful, in time criticle applications. For good examples of its usage, see the source code for memset() and memcpy(). I know that I just told you to reinvent the wheel, but I realy beleive that is the best way to accomplish your goal (less headacks and, yes, less time in the long run). INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups