Why does using “int 21h” on Assembly x86 MASM caus

2019-01-20 20:35发布

问题:

I was trying to make my program accept input without the user having to press enter, so I tried the following:

mov ah,01h
int 21h

But it just crashes my program over an unhandled exception. This seems to be the way to do it according to much that I have read, so why isn't it working for me?

Now, I am fairly new to this language so I still do not exactly understand the process of how this piece of code works, so I would also appreciate what the logic is behind accepting input by pressing enter and accepting input without the user having to press enter.

MY OS is Windows, by the way.

回答1:

Your code looks like MS-DOS-era assembly. VS2010 doesn't support generating DOS executables, and modern versions of Windows don't support running them, either. Looks like you were going by some old book or site, one that was written in late 80'es-early 90's.

Decide what do you want to learn. If you want to learn modern assembly (and target Windows), get some recent guidance. The techniques are quite different, and int21h isn't among them :) If you're indeed after DOS-era assembly, set up a DOS virtual machine with DOSBox, and find some old free assembler. Visual Studio won't help you here.

Specifically why does your program crash. Int21h was how MS-DOS exposed its API. Windows doesn't support it for Windows executables - there are other ways of invoking the API. When you assemble with Visual Studio 2010, you end up with a Windows executable, not a DOS one, and there's no option to generate a DOS one. As for the Windows executables, they're not supposed to invoke interrupts at all - that's a crash condition.



回答2:

You need to obtain a tool set that can generate 16 MS-DOS programs. These should run on DOSBOX, or on a Virtual PC with MS-DOS installed on it. Microsoft included 16 bit tool sets up to Visual C / C++ 1.52, but Visual C / C++ 4.0 and 4.1 also contain the 1.52 16 bit tool set. The older version of the compilers would be named Microsoft C 8.xx or earlier version. I don't know if any the early versions of Visual Studio (2002 or 2003) include the 16 bit tool set.



回答3:

Use the linker version 5.60 to generate 16-bit DOS applications. You can get this from: http://download.microsoft.com/download/vc15/Update/1/WIN98/EN-US/Lnk563.exe

Dirk