# Interrupts in inline asm

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

At camp a week ago I was trying to write my own print() function in C++. I was using inline asm and trying to use interrupt 21h. I got errors but when i commented int 21h out, the errors stopped. The errors were runtime. The program would just hang. I was using ms visual c++ .net accademic. Can I use interrupts in inline assembly? [Edited by - chaosgame on August 4, 2005 8:54:34 AM]

##### Share on other sites
Interrupts should not (and cannot) be used in a 32bit Windows program. The interrupt you tried to use is a DOS software interrupt (basically a DOS API function). For obvious reasons this does not work on Windows, even if the Windows console looks like a DOS prompt to a beginner.

##### Share on other sites
Yes, you can. I've used it to break for the debugger. Is 21 a decimal value or a hexidecimal value? Make sure you know. 21 and 0x21 are different values.

EDIT:

Oh, and what Michalson said...

##### Share on other sites
You can use int 3 in user-mode, that might be the only one though.

To use the DOS stuff, you can try writing your code with debug (just type 'debug' at the command prompt). This is archaic assembly programming; you have to lay everything out yourself and even have to write your own code to save your code to disk (as a .com file, code starts at 0x100 IIRC).

##### Share on other sites
Is this just in inline asm? I wrote a hello world program entirely in assembly using int 21h and it assembled fine and ran under windows using the console.

##### Share on other sites
if you wrote it entirely in assembly, what assembler did you use? there's a good chance it compiled as a dos program that windows just emulated. when doing inline asm, your program is not being run through an emulator, therefore the dos interrupts aren't there.

##### Share on other sites
Can you compile it into a dos program? If so then would it work?

##### Share on other sites
yeah i guess, but why would you want to? there are 32bit ways using windows to do anything in windows like you could in dos

##### Share on other sites
I don't know how system calls work in Windows but in Linux they interrupts are used for that. The only other way I know of that it can be done it using the SYSENTER instruction (although I'm not exactly an expert on operating system programming). So if you try disassembling the Windows DLLs you might find a way of using interrupts in Win32 programs. It wouldn't be easy to decipher though.

##### Share on other sites
My Assembly is very rusty these days, even though I was once known for having written a multithreading library for Turbo Pascal in assembly.

I think that under Protected Mode (Win32) your can be fooled into thinking you've installed you own Interrupt Handling Routines, when in fact when the hardware triggers one, it still calls the OS, who in turn may or may not call other registered handlers (yours).

There is also no real way to debug this, you'll have to go with documentation and ask around to see if that is what Windows is actually doing, a Virtual IRQ Service.

Mind you that Interrupts are not OS-specific. They occur anytime, anywhere, whatever OS is installed. An interrupt is exactly that, an interruption in the normal CPU processing that gets interrupted so that it might take a look and handle this more important thing over here.

As an example, I did an override of the Clock IRQ, which triggered a few thousand times per second (this on a 486), and with a few register manipulations, I jumped back and forth between registered threads. That's called Preemptive Multitasking, because I stopped the execution of a thread without even telling it.

If you want to code in assembly, just drop back to DOS. I think it still is easy to get a copy of Turbo Pascal (you don't need to know Pascal, just need to know how to enter assembly opcodes into de code), or C (also supports imbedded assembly) from Borland.

The compilers are lightning fast, and you won't have to deal with Win32's idiosyncrasies....

##### Share on other sites
You can program in assembly under Win32 all you like. You just can't call DOS interfaces (e.g. int 21h). Complaining that your int 21h based code doesn't work in Win32 apps is pretty much exactly the same as complaining that your Macintosh app doesn't run under Windows - of course not, they're different OS's.

The issue is complicated by the fact that Windows will emulate DOS apps. So your int 21h code will work *if* you use DOS tools to build it. VC.Net is not a DOS tool.

Quote:
 Original post by ProzakIf you want to code in assembly, just drop back to DOS ... and you won't have to deal with Win32's idiosyncrasies....

Ye gods. Do you really want to go back to segmented architecture, 64k limits, and EMS/XMS memory?!? DOS has way more ideosyncasies than Win32 :).

##### Share on other sites
Quote:
 Original post by Anon MikeYe gods. Do you really want to go back to segmented architecture, 64k limits, and EMS/XMS memory?!? DOS has way more ideosyncasies than Win32 :).

Well, to get over the 64k limits all you need to do is go into protected mode, or install an EMX/XMS lib and work from that.

All he needs is an editor, and perhaps understanding the idiosyncrasies of DOS, he can see why some of the choices for Win32 where made the way they where made...

All I'm saying is that some people like to go back to the roots of it all... I know I'm one of them...

##### Share on other sites
How Do Windows NT System Calls REALLY Work?

Inside the Native API

Here's an example of the assembler code for invoking a system call on NT

NtCreateFile:    mov    eax, 0x0000001A    lea    edx, [esp+04]    int    0x2E     ret    0x2C

Eax holds the system call number, Edx hold a pointer to the function arguments.

The signature of this function is

NTSTATUS NtCreateFile(  PHANDLE FileHandle,  ACCESS_MASK DesiredAccess,  POBJECT_ATTRIBUTES ObjectAttributes,  PIO_STATUS_BLOCK IoStatusBlock,  PLARGE_INTEGER AllocationSize,  ULONG FileAttributes,  ULONG ShareAccess,  ULONG CreateDisposition,  ULONG CreateOptions,  PVOID EaBuffer,  ULONG EaLength);

There are more than 256 system calls available on Windows NT/2K/XP, most of them are wrapped in functions exported by ntdll.dll.

##### Share on other sites
The direct syscall interface for Windows is not guaranteed to never change. You'll save yourself a lot of headache by just calling the documented dll entrypoint.

##### Share on other sites

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.