简体   繁体   English

在汇编中写入txt文件

[英]Writting to txt file in assembly

I am currently working on a project and I need to write numbers to a file. 我目前正在从事一个项目,我需要在文件中写入数字。 Here is what I've tried so far: 到目前为止,这是我尝试过的:

    keyHolder dw ?  
    filename db 'drawlog.txt',0
    filehandle dw ?
    ErrorMsg db 'Error', 13, 10,'$'

proc OpenFile
    mov ah, 3Dh
    mov al, 2
    mov dx, offset filename
    int 21h
    jc openerror
    mov [filehandle], ax
    ret
    openerror:
        mov dx, offset ErrorMsg
        mov ah, 9h
        int 21h
        ret
endp OpenFile

proc closeFile
    mov ah,3Eh
    mov bx,[filehandle]
    int 21h
    ret
endp closeFile

proc writeKeyToFile
    mov ah, 40h
    mov bx,[filehandle]
    mov cx, 1
    mov dx, offset keyHolder
    int 21h
    mov cx,1
    mov ah, 40h
    mov dl, 13
    int 21h
    mov cx,1
    mov ah,40h
    mov dl, 10
    int 21h
    ret
endp writeKeyToFile

The code is sort of working, but there are two things I would like to ask. 该代码可以正常工作,但是我想问两件事。 First, after the write happens the file includes what should have been written and other weird symbols. 首先,写操作完成后,文件中应包含应写的内容以及其他怪异的符号。 Second, how can I go down a line when I want to (when writing to the file)? 其次,当我想写入文件时,如何下一行?

after the write happen the file include what should have been written and other weird symbols 发生写操作后,文件应包含应写的内容以及其他怪异的符号

You are specifying that 16 bytes should be written ( mov cx, 16 - I'm assuming this is DOS, though you don't mention). 您指定应写入16个字节( mov cx, 16我假设这是DOS,尽管您没有提到)。 The address that you specify is only a 2-byte variable. 您指定的地址仅为2字节变量。 The following 14 bytes presumably contain values that weren't meant to be written to the file. 接下来的14个字节可能包含并非要写入文件的值。

If keyHolder actually represents a string, don't declare it as a "word" ( dw ) - it is a sequence of bytes. 如果keyHolder实际上表示一个字符串,请不要将其声明为“单词”( dw )-它是字节序列。 (Can you even be sure that 2 bytes is enough to represent the number that you are trying to write?) (您甚至可以确定2个字节足以表示您要写入的数字吗?)

the second thing is how can I go down a line when I want to(when writing to the file) 第二件事是当我想(当写入文件时)如何下一行

Write a Carriage return + Line feed sequence (CRLF; byte value 13 followed by 10). 编写回车+换行序列(CRLF;字节值13,后跟10)。

keyHolder is not initialized in your code. 您的代码中未初始化keyHolder In order to pass to a new line in the file, declare this variable: 为了传递到文件中的新行,请声明此变量:

    newline db '\r\n'

Then write it to the file after you write keyHolder . 然后在编写keyHolder之后将其写入keyHolder So, the writeKeyToFile function becomes: 因此, writeKeyToFile函数变为:

proc writeKeyToFile
    mov ah, 40h
    mov bx,[filehandle]
    mov cx, 16
    mov dx, offset keyHolder
    int 21h
    mov ah, 40h
    mov bx,[filehandle]
    mov cx, 2
    mov dx, offset newline
    int 21h
ret

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM