打开文件并读取表单以在程序集中缓冲

时间:2013-05-01 17:12:23

标签: file assembly x86 dos

嘿我是汇编的初学者,我不会打开文件并读取值“整数”  从它并保存缓冲区中的整数以在屏幕上打印这是我的代码它不起作用

include inout.asm
.model small,c
.486
.stack
.data
org 100h ; .com memory layout 
buf db ?
file db "c:\rtasm\bin\file.txt";the file name in bin
.code

mov dx, offset file ; address of file to dx 
mov al,0 ; open file (read-only) 
mov ah,3dh 
int 21h ; call the interupt 
mov bx,ax ; put handler to file in bx 


mov ah,40h
mov bx,ax
mov cx,2h                 ;; how many bytes you want to read
mov dx,offset buf  ;; where you want to store that data (see note on Offset above)
int 21h

call putchar,offset buf; print char on the screen


mov ah,3eh
mov bx,ax
int 21h 
.exit
END 

1 个答案:

答案 0 :(得分:2)

ds:dx中的Int 21h函数3Dh(“OPEN EXISTING FILE”)expects a zero-terminated string。您提供的字符串没有零终止符。文件名应声明为file db "c:\rtasm\bin\file.txt",0

如果失败,3Dh和40h功能都会返回错误代码。您应该检查这些并在发生错误时告知用户(在本例中为您自己),而不是假设操作总是会成功。

另一个问题是以下代码:

mov bx,ax ; put handler to file in bx 

mov ah,40h      
mov bx,ax    <-- gives you a nonsense file handle since ah now is 40h
mov cx,2h          ;; how many bytes you want to read
mov dx,offset buf  ;; where you want to store that data (see note on Offset above)
int 21h

第二个mov bx,ax是不必要的,因为bx已经包含文件句柄。事实上,它不仅是不必要的,而且也是不正确的,因为你用值40h覆盖了axah)的高位部分。 还有一个事实是你将两个字节读入一个只有一个字节空间的缓冲区。