I'm writing a program to print everything in a text file to the screen.

Is there an interrupt or way, when opening a file, to get the file size or is there some sort of end of file character I can use to control the loop?

I've looked at The Art of Assembly and other source and I just seem to be missing it.


Posted on 2007-04-09 16:57:46 by Alklun
Posted on 2007-04-09 17:45:27 by Synfire
The GetFileSize function retrieves the size, in bytes, of the specified file.

DWORD GetFileSize(

    HANDLE hFile, // handle of file to get size of
    LPDWORD lpFileSizeHigh // address of high-order word for file size
Posted on 2007-04-09 18:50:45 by JimG
When dealing with DOS Interrupts as an API... keep a copy of RBIL handy ;)
Posted on 2007-04-09 19:49:52 by SpooK
read file

mov ax,3F00                             ;ah actually
mov bx,                   ;bl actually
mov cx,
int 21


If we put FFFF into cx, it will read the file and put the actual number of bytes read into ax if the file is smaller than FFFF bytes.

groovy. 8)
Posted on 2007-04-09 21:27:14 by eek
That's awsome everyone, Thanks so much.

I had heard something about clearing cx and having the file size in ax, but didnt get details.
Posted on 2007-04-09 21:39:26 by Alklun
CX and DX are the DWORD seek position for the DOS trap. If you set these values to zero (0x0000), you will get the size of the file. We have this in Windows too (SetFilePointer). Here is the code that I have written that retrieves the size of a given file. It has enough documentation and comments for you to understand it.

; ------------------------------
FileGetSize PROC
    Description : Gets the 32-bit size of an open file. See note(s).

    Calling Convention : Push from left to right.

    Parameter(s) :
      WORD Param1 = Target file's handle offset
      WORD Param2 = File size variable (16-bit offset)

    Stack Usage: 8 Bytes.

    Note : 1) Param2 is a 32-bit long variable which will receive the size of the file
              if the procedure do its job successfully.
          2) The Param2, although a 32-bit variable, should have its 16-bit offset passed onto
              the procedure.
          3) Param1 is the offset to the variable which holds the handle to an open file.
          4) The AX register will be set to 0000h (Zero) if the procedure is through successfully
              or one of the below values if an error occurred:
                AX = 0001h (Invalid function - Would not happen if you didn't modify
                            the procedure's code)
                AX = 0006h (Invalid handle)
          5) The value of the Param2 parameter will remain unchanged if the value of the AX register
              is not set to zero when the procedure ends.
          6) The file should have been opened before invoking this procedure.

    Example : Retrieve an open file's size and put the result in a
              32-bit DWORD variable called 'FileSize'.

      FileHandle            DW              ?
      FileSize              DD              ?
      PUSH    OFFSET FileHandle ; Push the file handle onto the stack
      PUSH    OFFSET FileSize  ; Push the receiver-variable onto the stack
      CALL    FileGetSize      ; Get the file size
      ADD    SP , 0004h        ; Remove both of the parameters
      TEST    AX , AX          ; See if AX == 0 (Success)
      JNE    @@Failed          ; Jump to ... if not
        ; Success
        ; FileSize = 0080FDA0h (For instance)
        ; Failure
  PUSH    BX                              ; Push the base index onto the stack
  PUSH    CX                              ; Push the count register onto the stack
  PUSH    DX                              ; Push the data register onto the stack
  PUSH    BP                              ; Push the base pointer onto the stack
  MOV    BP , SP                        ; Move the stack pointer to the base pointer
  MOV    AL , 02h                        ; Seek from the end of the file
  XOR    CX , CX                        ; High Order Word is equal to zero
  XOR    DX , DX                        ; Low Order Word is equal to zero
  MOV    BX , WORD PTR           ; BX is the pointer to the file handle
  MOV    BX , WORD PTR               ; BX is the file handle
  MOV    AH , 042h                      ; Request Seek operation
  DW      21CDh                          ; Issue the DOS interrupt
  JC      @@__FileGetSizeEP              ; Jump to ... if an error has occurred
  MOV    BX , WORD PTR           ; BX is the pointer to the new position variable
  MOV    WORD PTR , AX              ; Fill the Low Order Word of the variable
  MOV    WORD PTR , DX          ; Fill the High Order Word of the variable
  XOR    AX , AX                        ; AX = 0000h (Success)
  @@__FileGetSizeEP:                      ; End of the procedure routine
    POP    BP                            ; Restore the base pointer
    POP    DX                            ; Restore the data register
    POP    CX                            ; Restore the count register
    POP    BX                            ; Restore the base index
  RET                                    ; Return to the calling procedure
FileGetSize ENDP
; ------------------------------
Posted on 2007-04-10 00:13:31 by XCHG