代码之家  ›  专栏  ›  技术社区  ›  Pavel Shishmarev

简单的FASM“你好,世界!”DOS中断时崩溃

  •  2
  • Pavel Shishmarev  · 技术社区  · 8 年前

    在我的高中作业中,我必须编写一个程序,使用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

    1 回复  |  直到 7 年前
        1
  •  5
  •   Michael Petch    8 年前

    使用SASM IDE时 format ELF 在汇编代码中,FASM将文件组装到ELF对象( .o 然后(默认情况下)使用GCC和LD的MinGW版本将该ELF对象链接到Windows可执行文件(PE32)。这些可执行文件作为本机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 扩大很遗憾,您不能使用SASM IDE来调试或运行DOS程序。您可以从32位Windows XP命令行运行生成的程序。32位版本的Windows在 NTVDM (virtual DOS machine) .

    推荐文章