Sign in to follow this  
chaosgame

Interrupts in inline asm

Recommended Posts

chaosgame    146
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 this post


Link to post
Share on other sites
Michalson    1657
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 this post


Link to post
Share on other sites
smr    2468
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 this post


Link to post
Share on other sites
Shannon Barber    1681
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 this post


Link to post
Share on other sites
chaosgame    146
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 this post


Link to post
Share on other sites
Rocket05    152
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 this post


Link to post
Share on other sites
ZQJ    496
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 this post


Link to post
Share on other sites
Prozak    898
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 this post


Link to post
Share on other sites
Anon Mike    1098
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 Prozak
If 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 this post


Link to post
Share on other sites
Prozak    898
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 this post


Link to post
Share on other sites
LessBread    1415
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 this post


Link to post
Share on other sites
Anon Mike    1098
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this