用 x86 NASM 汇编语言打印 .txt 文件的内容

问题描述 投票:0回答:1

我正在学习汇编语言并尝试打印一个 .txt 文件。这是我的代码:

[org 0x0100] 
 jmp start 
filename: db 'test1.txt', 0 ;file that is to be loaded
buffer:   times 4096 db 0 ;allocating 4K of space to store file contents
handle:   dw 0      ;handle for the file

start:
    ; open the file
    mov ah, 0x3d     ; service 3d - open file
    mov al, 0       ; read-only mode
    mov dx, filename
    int 0x21
    mov [handle], ax  ; save the file handle

    ; read the file into buffer
    mov ah, 0x3f     ; service 3f - read from file
    mov bx, [handle]
    mov cx, 4096     ; read up to 4096 bytes
    mov dx, buffer
    int 0x21

    ; print the buffer to the console
    mov dx, buffer  ; making sure it actually prints buffer
    mov ah, 9       ; service 9 - print string
    int 0x21

    ; close the file
    mov ah, 0x3e     ; service 3e - close file
    mov bx, [handle]
    int 0x21

    ; exit the program
    mov ax, 0x4c00 ; terminate program 
    int 0x21

但是,它只打印空格,然后是 6-8 行垃圾。文件里面只有一个词。

我正在使用 DOS Box portable 来编译和运行我的 COM 格式汇编代码,我是这样做的。 我编译程序并为它制作一个 .com 可执行文件,另外 .lst 用于调试:

nasm myProgram.asm -o myProgram.com -l myProgram.lst
要运行程序,我只需输入
myProgram.com

经过研究和审查一个类似的问题我的代码中的问题似乎是在打印时没有添加偏移量。代码应该是

mov dx, offset buffer
。事实证明,我的编译器不支持这种语法并给出语法错误。我曾尝试手动调整偏移量
mov dx, [buffer + 2]
(我尝试更改偏移量大小)但它只会打印更多垃圾,只是采用不同的模式。

file assembly x86 nasm dos
1个回答
0
投票

我从来没有用过nasm作为编译器,所以这是我第一次。 ;) 但我认为这不是问题所在。问题出在功能啊,9。我创建了文本文件 Hello World! :) 此文本出现在屏幕上 + 在此之后的一些随机符号。在使用函数 ah 的文档中,9 文本应以 $ 结尾。所以我把它放在文件的末尾,现在程序运行正常。 :) 或者您可以计算文件中的字符数并在末尾添加 $ ,否则我认为将打印整个缓冲区。 :)

© www.soinside.com 2019 - 2024. All rights reserved.