2

只是想知道,关于我的帖子Alternatives to built-in Macros,是否可以通过使用int 21h windows API来避免使用StdOut宏?如:

.data
       msg dd 'This will be displayed'
;original macro usage:
invoke StdOut, addr msg
;what I want to know will work
push msg 
int 21h ; If this does what I think it does, it should print msg

是否存在这样的事情(如使用 int 21h 打印东西),或者确实存在类似的事情,但不完全是 int 21h。还是我完全错了。

有人可以为我澄清一下吗?

谢谢,

程序

4

3 回答 3

8

中断 21h 是MS-DOS函数的入口点。

例如,要在标准输出上打印某些内容,您必须:

mov ah, 09h  ; Required ms-dos function
mov dx, msg  ; Address of the text to print
int 21h      ; Call the MS-DOS API entry-point

字符串必须以“$”字符结尾。

但:

  • 您不能在 Windows 桌面应用程序中使用中断(它们仅适用于设备驱动程序)。
  • 如果您需要调用 MS-DOS 函数,您必须编写一个16 位应用程序。

然后......是的,你不能用它来打印消息,不存在这样的东西:你必须调用操作系统函数来打印你的消息,并且它们不能通过中断获得。

于 2012-05-14T12:49:08.033 回答
2

DOS 中断不能在 Windows 的保护模式下使用。

您可以使用WriteFileWin32 API 函数写入控制台,或改用 MASM 宏。

于 2012-05-14T12:51:20.257 回答
2

其他说您不能在 Windows 中使用中断的答案是完全错误的。如果你真的想要,你可以(不推荐)。至少在 32 位 x86 Windows 上存在int 2Eh基于 legacy 的系统调用接口。有关 x86 和 x86_64 Windows 上的系统调用机制的一些讨论,请参见此页面

这是一个非常简单的程序示例(使用 FASM 编译),该程序在 Windows 7 上使用int 0x2e(并在大多数其他版本上崩溃)立即退出:

format PE
NtTerminateProcess_Wind7=0x172
entry $
    ; First call terminates all threads except caller thread, see for details:
    ; http://www.rohitab.com/discuss/topic/41523-windows-process-termination/
    mov eax, NtTerminateProcess_Wind7
    mov edx, terminateParams
    int 0x2e
    ; Second call terminates current process
    mov eax, NtTerminateProcess_Wind7
    mov edx, terminateParams
    int 0x2e
    ud2    ; crash if we failed to terminate
terminateParams:
    dd 0, 0 ; processHandle, exitStatus

但请注意,这是使用 Windows 的一种不受支持的方式:系统调用号经常更改,通常不能依赖。在此页面上,您可以看到例如NtCreateFile在 Windows XP 上调用系统调用号0x25,而在 Windows Server 2003 上已经对应于NtCreateEvent,而在 Vista 上它是NtAlpcRevokeSecurityContext

进行系统调用的受支持(尽管文档不多)方式是通过Native API库的函数,ntdll.dll.

But even if you use the Native API, "printing things" is still very version-dependent. Namely, if you have a redirect to file, you must use NtWriteFile, but when writing to a true console window, you have to use LPC, where the target process depends on Windows version.

于 2018-04-17T09:47:53.710 回答