assembly - 创建文本文件时 Dosbox 关闭

标签 assembly dos x86-16 dosbox

为什么我创建文件后我的dosbox会自动关闭?我的代码有问题吗?

.model small
.stack 100h
.data
msg1    db      10, 13, 10, 13, "Please select an item:",0Dh,0Ah,0Dh,0Ah,09h
        db      "1- Create File",0Dh,0Ah,09h
        db      "2- show data",0Dh,0Ah,09h      
        db      "3- Exit",0Dh,0Ah,09h
        db      "Enter item number: " 
        db      '$'   
msg2    db      10,13,"Success Create$"
file    db      "test.txt"
handle  dw      ?
buffer  dw      10 dup (?)

.code
main proc 
    mov   ax,@data
    mov   ds,ax

ShowMenu:       
    lea     dx, msg1  
    mov     ah, 09h 
    int     21h     

    mov     ah, 01h ;get choice
    int     21h        

    cmp     al, "1"
    je      CreateFile
    cmp     al, "2"
    je      ShowData
    cmp     al, "3"
    jmp     Quit
    jl      ShowMenu

Quit: 
   mov   ah,4ch
   int   21h   

CreateFile:
mov ah,3ch ;create file
mov cx,0
lea dx,file;set file name
int 21h

lea dx,msg2
int 21h
jmp ShowMenu

ShowData:
mov ah,3dh ;open file
mov al,0   ;open as read only
lea dx,file
int 21h
mov handle,ax

;read one char
mov ah,3fh   ;service to read file
mov bx,handle
mov cx,1        ;how many byte to read
mov dx,buffer   ;where to store
int 21h

;close file
mov ah,3eh
mov bx,handle
int 21h

jmp ShowMenu


main endp
 end main

创建一个文本文件

最佳答案

在显示成功消息( comment by David Wohlferd )之前不设置功能代码 09h 的问题通常只会关闭您的程序,但不会关闭 DOSBox 模拟器。
从创建文件中获得的句柄通常会离开 AH=0 并且随后会调用 DOS 终止函数。 Terminate program


file    db      "test.txt"
handle  dw      ?

程序中的另一个问题是您忘记以零终止文件规范:

 file    db      "test.txt", 0

垃圾文件规范可能会导致 DOSBox 关闭!

因为句柄变量是用 ? 定义的(未初始化的内存)您可能很不幸,后面的字节恰好不是零。

关于assembly - 创建文本文件时 Dosbox 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880686/

相关文章:

gcc - 与 Intel 语法相比,AT&T 语法中源操作数的顺序是什么?

batch-file - 有运行时间限制吗?

microcontroller - 在微 Controller 中将数据从内存移至内存

c - 如何在 16 位模式下使用 GDB?

c - GCC + LD + NDISASM = 大量汇编指令

assembly - LNK 2001 无法解析的外部符号 _mainCRTStartup MASM

c - 什么是适用于 OSX、Windows XP 或 DOS 的良好 C 编译器?

git - 重写 git 历史以使 crlf 保持一致

assembly - assembly 中的二维数组

c - 如何在 C 中使用 asm 添加两个 64 位数字时访问进位标志