问题描述
我刚开始学习Win32上一些x86汇编,我已经使用附带的IDE,用于.ASM文件自定义生成规则中使用MASM与Visual Studio 2008。我一直在试图使用DOS中断打印到控制台,而是我收到消息:ASMTest.exe未处理的异常在0x00401004:0000005:访问冲突阅读位置为0xffffffff。八号线。我试图输出的单ASCII字符'A'(41H)这里是MASM code:
I've just begun learning some x86 assembly on win32, and I've used masm with visual studio 2008 using the custom build rule that comes with the ide for .asm files. I've been trying to use the DOS interrupt to print to the console, but instead I receive the message: "Unhandled exception at 0x00401004 in ASMTest.exe: 0xC0000005: Access violation reading location 0xffffffff." on the 8th line. I'm trying to output the single ascii character 'A' (41h) Here is the masm code:
.386
.MODEL flat, stdcall
.CODE
start:
mov dl, 41h
mov ah, 2
int 21h
ret
end start
当我使用则Debug.exe,并使用A命令输入所有。code指令,并运行它('G'),它工作正常。
When I use debug.exe, and use the 'a' command to input all .CODE instructions, and run it ('g'), it works fine.
任何人都可以见识一下如何使用DOS中断正确?谢谢!
Can anyone enlighten me on how to use the DOS interrupt correctly? Thanks!
编辑:当Win32编程,Managu是正确的,你应该使用Windows API调用像WriteConsoleA而不是使用DOS中断。 是一个有用的资源。如果有人正在寻找code要做到这一点(如我),在这里它是:
When programming on win32, Managu is right that you should use a windows api call like WriteConsoleA instead of using the DOS interrupt. This was a helpful resource. In case anyone is looking for the code to do this (like I was), here it is:
.386
.MODEL flat, stdcall
; Windows API prototypes
GetStdHandle proto :dword
WriteConsoleA proto :dword, :dword, :dword, :dword, :dword
ExitProcess proto :dword
STD_OUTPUT_HANDLE equ -11
.DATA
HelloWorldString db "hello, world", 10, 0
.CODE
strlen proc asciiData:dword
; EAX used as count, EBX as ascii char pointer, EDX (DL) as ascii char
mov eax, -1
mov ebx, asciiData
mov edx, 0
BeginLoop:
inc eax ; ++count (init is -1)
mov dl, [ebx] ; *dl = *asciiptr
inc ebx ; ++asciiptr
cmp dl, 0 ; if (*dl == '\0')
jne BeginLoop ; Goto the beginning of loop
ret
strlen endp
main proc
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov ecx, eax
invoke strlen, addr HelloWorldString
invoke WriteConsoleA, ecx, addr HelloWorldString, eax, 0, 0
ret
main endp
end
(设置的入口点主)
(Set the entry point to main)
推荐答案
当您使用Debug.exe将输入此code,你组装一个16位(8086架构,实模式),DOS程序。您指定的语义对于这样的程序是正确的。但是,当你组装你在这里与MASM了,然后链接它,你想创建一个32位程序(i386架构,保护模式)的Windows程序。我可能是错误的,但我不认为你甚至在法律上可以调用INT 21H在后一种情况。
When you use debug.exe to input this code, you're assembling a 16-bit (8086 architecture, "real mode") dos program. The semantics you specify are correct for such a program. However, when you assemble the program you've got here with MASM, and then link it, you're trying to create a 32-bit (i386 architecture, "protected mode") Windows program. I could be mistaken, but I don't think you can legally even invoke int 21h in the latter case.
这篇关于DOS中断在MASM x86汇编崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!