我正在学习汇编语言并尝试打印一个 .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]
(我尝试更改偏移量大小)但它只会打印更多垃圾,只是采用不同的模式。