【问题标题】:open file and read form it to buffer in assembly打开文件并从中读取以在程序集中缓冲
【发布时间】:2013-05-01 17:12:22
【问题描述】:

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

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 

【问题讨论】:

    标签: file assembly x86 dos


    【解决方案1】:

    Int 21h 函数 3Dh(“打开现有文件”)expects a zero-terminated string in ds:dx。您提供的字符串没有零终止符。文件名应声明为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 覆盖了ax (ah) 的高位部分。 还有一个事实是,您正在将两个字节读入一个只有一个字节空间的缓冲区。

    【讨论】:

    • 文件句柄是什么? mov bx, FHndl ;获取文件句柄值
    • 怎么样?您在问题中发布的代码中没有这样的行。
    • 不在我的代码中,但我在很多网站上都读到过......像这样 mov FHndl, ax ;保存文件句柄 LP: mov ah,3fh ;从文件 lea dx, Buffer 中读取数据;数据缓冲区地址 mov cx, 1 ;读取一个字节 mov bx, FHndl ;获取文件句柄值 int 21h handle is db (buffer)
    • 如果您需要保存以备后用,您可以将其存放在任何合适的地方。即使您更改了ax 寄存器的一部分,或者调用了修改ax 的中断函数,您似乎仍依赖ax 来维持其值。这显然行不通,所以我建议您阅读寄存器的工作原理并更仔细地研究您正在使用的中断。
    • 我接受一个命令行参数,当然它不是以零结尾的。如何让参数以零结尾?
    猜你喜欢
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2019-07-11
    • 2011-05-11
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多