assembly - 简单 FASM "Hello world!"在 DOS 中断时崩溃

标签 assembly x86 dos x86-16 fasm

在我的高中作业中,我必须编写一个程序,使用 DOS 中断来输入和输出字符串,而不是 std printf/scanf 但是当我尝试运行这个程序时:

format ELF
use16
section '.data' writeable
    msg db 'Hello, world!', 0


section '.text' executable
public _main
_main:
   mov ebp, esp; for correct debugging
   mov   ah, 1
   int   21h 
   mov  ah,4Ch
   int   21h
   xor eax, eax
ret

它只是崩溃了。我附加了调试器,发现它在这一行崩溃:int 21h。我完全不知道为什么会发生这种情况。
我使用 FASM、SASM IDE 和 Windows XP SP3 x32

最佳答案

当使用 SASM IDE 并在汇编代码中使用 format ELF 时,FASM 会将文件汇编为 ELF 对象(.o 文件),然后使用(通过默认)MinGW 版本的 GCC 和 LD,将该 ELF 对象链接到 Windows 可执行文件 (PE32)。这些可执行文件作为 native Windows 程序运行,而不是 DOS。您不能在 Windows PE32 可执行文件中使用 DOS 中断,因为该环境中不存在 DOS 中断。最终结果是它在 int 21h 上崩溃。

如果您想创建一个可以在 32 位 Windows XP 中运行的 DOS 可执行文件,您可以这样做:

format MZ                   ; DOS executable format
stack 100h

entry code:main             ; Entry point is label main in code segment

segment text
msg db 'Hello, world!$'     ; DOS needs $ terminated string

segment code
main:
    mov   ax, text
    mov   ds, ax            ; set up the DS register and point it at
                            ; text segment containing our data
    mov   dx, msg
    mov   ah, 9
    int   21h               ; Write msg to standard output

    mov   ah, 4Ch
    int   21h               ; Exit DOS program

这将生成一个扩展名为 exe 的 DOS 程序。遗憾的是,您无法使用 SASM IDE 来调试或运行 DOS 程序。您可以从 32 位 Windows XP 命令行运行生成的程序。 32 位版本的 Windows 在 NTVDM (virtual DOS machine) 内运行 DOS 程序.

关于assembly - 简单 FASM "Hello world!"在 DOS 中断时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48991146/

相关文章:

assembly - 由 nasm 插入的冗余 DS 段覆盖前缀?

dos - 目录中的文件计数以及子目录中的文件计数

batch-file - DOS 7 中的 SET 命令等效项

c++ - 在DOS上作图用什么?

assembly - 编译器在这段汇编代码中做了什么?

c - 内存分配和设置

c - C 语言 hello world 程序的汇编代码

assembly - 为什么汇编中字符串变量末尾包含 0?

assembly - 为什么我不能在 MARS 中使用 li.s?

linux - x64 NASM 汇编程序在程序开始时显示段错误