Assembly 从文件ASM 8086的中间读取

Assembly 从文件ASM 8086的中间读取,assembly,x86-16,Assembly,X86 16,我试图从文件的中间读取数据 我使用int 21h,ah=3fh,但它从一开始就读取文件 有没有办法不从文件开头读取数据 (ASM 8086,如果相关,我使用TASM)是的,打开文件后,可以使用int 21h函数42h-移动文件指针 mov ah,42h ;function mov al,0 ;to calculate offset from beginning of file mov bx,handle ;from op

我试图从文件的中间读取数据

我使用int 21h,ah=3fh,但它从一开始就读取文件

有没有办法不从文件开头读取数据


(ASM 8086,如果相关,我使用TASM)

是的,打开文件后,可以使用int 21h函数42h-移动文件指针

mov     ah,42h          ;function
mov     al,0            ;to calculate offset from beginning of file
mov     bx,handle       ;from opening the file
mov     cx,yyyy         ;most significant part of offset
mov     dx,xxxx         ;least significant part of offset
int     21h             ;system call
jc      error           ;check if errro
文件的下一次读取将从此位置开始

al的值

0 = offset from beginning of file
1 = offset from current position (cx:dx is signed)
2 = offset from end of file (ditto)

我建议您现在就找到我给您的提示文档。

接下来是一个完整的小程序,可以实现您想要实现的功能:打开文件,跳转到中间位置(字节500),读取一些内容,然后关闭文件。这是由EMU8086制成的:

.model small

.stack 100h

.data

filename    db 'tree_img.png',0
filehandler dw ?
buffer      db 10 dup (?)

.code
start:

;INITIALIZE DATA SEGMENT.
  mov  ax, @data
  mov  ds, ax                 

  call read_middle                     ;<==============

;WAIT FOR ANY KEY.    
  mov  ah, 7
  int  21h

;FINISH PROGRAM.
  mov  ax, 4c00h
  int  21h

;-----------------------------------------

read_middle proc

;OPEN FILE.
  mov  ah, 3dh          ;SERVICE TO OPEN FILE.
  mov  al, 0            ;OPEN AS READ ONLY.
  lea  dx, filename           
  int  21h  
  mov  filehandler, ax  ;NECESSARY FOR OPERATIONS ON FILE.

;JUMP TO POSITION INSIDE THE FILE.                            <==============
  mov  ah, 42h          ;SERVICE FOR SEEK.
  mov  al, 0            ;START FROM THE BEGINNING OF FILE.
  mov  bx, filehandler  ;FILE.
  mov  cx, 0            ;THE FILE POSITION MUST BE PLACED IN
  mov  dx, 500          ;CX:DX, EXAMPLE, TO JUMP TO POSITION
  int  21h              ;14000000 SET CX:DX = D5:9F80.

;READ ONE CHAR FROM CURRENT FILE POSITION.
  mov  ah, 3fh          ;SERVICE TO READ FROM FILE.
  mov  bx, filehandler
  mov  cx, 1            ;HOW MANY BYTES TO READ.
  lea  dx, buffer       ;WHERE TO STORE THE READ BYTES.  
  int  21h

;CLOSE FILE.
  mov  ah, 3eh          ;SERVICE TO CLOSE FILE.
  mov  bx, filehandler  
  int  21h

  ret
read_middle endp

;-----------------------------------------

end start
。型号小
.烟囱100小时
.数据
文件名db'tree_img.png',0
文件处理程序dw?
缓冲区DB10DUP(?)
.代码
开始:
;初始化数据段。
mov-ax,@data
mov-ds,ax

把read_称为middle 用于获取文件的长度,单位为DI:SI

xor     cx, cx               ;  set high                              
xor     dx, dx               ;  set low
mov     ax, 4202h            ; Get length of file from end
int   21h
jc  ERROR
mov     si, ax               ; return low
mov     di, dx               ; return high
xor     cx, cx               ;  set high
xor     dx, dx               ;  set low
mov     ax, 4200h            ; Set file pointer to the start position
int   21h
jc  ERROR
RBIL->inter61b.zip->INTERRUP.F


我该把什么传给艾尔?我想从那里读的职位是什么?@AmirZak我补充了一些信息。仔细阅读我的帖子,你想从中阅读的位置是
cx:dx
。我将这个
mov-dx,500'替换为
mov-dx,offset-filename',它成功了
--------D-2142-------------------------------
INT 21 - DOS 2+ - "LSEEK" - SET CURRENT FILE POSITION
AH = 42h
AL = origin of move
  00h start of file
  01h current file position
  02h end of file
BX = file handle
CX:DX = (signed) offset from origin of new file position
Return: CF clear if successful
DX:AX = new file position in bytes from start of file
CF set on error
AX = error code (01h,06h) (see #01680 at AH=59h/BX=0000h)
Notes:  for origins 01h and 02h, the pointer may be positioned before the
  start of the file; no error is returned in that case (except under
  Windows NT), but subsequent attempts at I/O will produce errors
  if the new position is beyond the current end of file, the file will
  be extended by the next write (see AH=40h); for FAT32 drives, the
  file must have been opened with AX=6C00h with the "extended size"
  flag in order to expand the file beyond 2GB
BUG: using this method to grow a file from zero bytes to a very large size
     can corrupt the FAT in some versions of DOS; the file should first be
     grown from zero to one byte and then to the desired large size
SeeAlso: AH=24h,INT 2F/AX=1228h