如何从键盘读取字符并将其保存到在DOS下集的文件? [英] How to read a character from the keyboard and save it to a file in DOS assembly?

查看:188
本文介绍了如何从键盘读取字符并将其保存到在DOS下集的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的三通遥控功能的文件并保存字符与40H功能(我读到它的这里

I'm creating the file with the 3Ch function and saving character with the 40h function (I read about it here)

我不明白如何把在读字DS:DX

下面是我的code:

org 100h
bits 16

section .data
    file DB 'file.txt',0

section .bss
    file_handle resw 1

; CREATE FILE
section .text
    mov DX, file 
    mov CX, 0000h
    mov AH, 3Ch
    int 21h

; START INPUT
INPUTSTART:
    mov AH,01h
    int 21h

    ; SAVE INPUT TO FILE
    mov SI, file_handle
    mov BX, [SI]
    mov CX, 0001h
    mov AH, 40h
    int 21h

   jmp INPUTSTART

    mov AH,4Ch
    int 21h

正如你所看到计划的目的是在循环工作,并编写尽可能多的字符内的用户类型。请获取文件句柄40H功能,并把数据帮助DS:DX

推荐答案

其实你不这样做的提出 DS字:DX ,而是你使用 DS:DX 引用到的字符或任何数据存储,在英特尔的内存地址语法这将是 [DS: DX] (但 DX 不能用于内存寻址)。

Actually you don't put the character in ds:dx, but rather you use ds:dx to refer to the memory address where the character or whatever data is stored, in Intel syntax it would be [ds:dx] (but dx cannot be used for memory addressing).

所以,你需要做的:存储从键盘DOS中断 21小时 / 读出的值啊= 01H ,中返回来一些内存的地址,然后是指内存地址与 DS:DX

So, what you need to do: store the value read from keyboard with DOS interrupt 21h / ah = 01h, returned in al to some memory address and then refer to that memory address with ds:dx.

在您的code你不从变量文件句柄可言,和负载存储0 file_handle 写之前,文件调用,因为这是你如何有它初始化。然后尝试从 DS读取文件句柄:0 (如 SI 等于0),并且不道理可言。所有你需要的文件句柄做的是保存它(在返回值文件创建/ trunction后),并始终将其加载到相关的寄存器在随后的 INT 21H 指的是同一个文件(写入文件,从文件,关闭文件,等等。读取)。

In your code you don't store the file handle at all, and load 0 from the variable file_handle before write to file call, as that's how you have it initialized. And then you try to read file handle from ds:0 (as si equals 0), and that doesn't make sense at all. All you need to do with the file handle is to store it (return value in ax after file creation/trunction) and always load it to relevant register in subsequent int 21h referring to that same file (write to file, read from file, close file, etc.).

所以,用它下面的修复应该工作(没有测试)。我还组织了函数调用的参数由Ralph Brown的中断列表中使用的,以使其更容易理解。

So, with the fixes below it should work (didn't test). I also organized the function call parameters to the order used by Ralph Brown's interrupt list to make it easier to understand.

.section data
file db 'file.txt',0
character_read db 0

...

% create file:
    mov ah,3Ch             % create or truncate a file
    mov dx,file            % ds:dx points to ASCIIZ filename
    xor cx,cx              % file attributes.
    int 21h

    mov [file_handle], ax  % file handle must be stored in memory or in a register!

INPUTSTART:
    mov ah,1               % read character from STDIN, with echo.
    int 21h
    mov [character_read], al  % store the ASCII code to memory.

    % unless you want to loop eternally, you can check the ASCII code and exit.

    cmp al,20h             % space.
    je  EXIT

    mov ah,40h             % write to file or device
    mov bx,[file_handle]   % file handle is just a number.
    mov cx,1               % number of bytes to write.
    mov dx,character_read
    int 21h

    jmp INPUTSTART

EXIT:
    % here you can add some clean-up code, if needed.

    mov ah,3Eh             % close file.
    mov bx,[file_handle]   % here you need the file handle again.
    int 21h

    mov ah,4Ch
    int 21h

这篇关于如何从键盘读取字符并将其保存到在DOS下集的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆