从文件中间读取 ASM 8086

Read from the middle of file ASM 8086

我正在尝试从文件中间读取数据。

我用的是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

获取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
http://www.cs.cmu.edu/~ralf/files.html

--------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