I've got a text file: 1.txt in c:\ that contains the word This
I want the following code to open the file, get the handle ( which I assume gives the starting position for the bytes of the file), directly access the 4 bytes and just add 3 to them.
This is what I've got so far but, it isn't changing the text in the file.

You would have to create the text file yourself to test the code.

Could someone please give me hint?


.model small
HANDLE word ?
FILENAME db "C:\1.txt", 0


main proc far
mov ax, @data
mov ds, ax


mov dx, offset FILENAME ; put filename in dx
mov al, 2 ; access mode - read and write
mov ah, 3Dh ; open the file
int 21h


mov HANDLE, ax ; save value of handle
mov bx, offset HANDLE ; bx contains the file handle

mov cx, 5 ; bytes to be accessed, used in loop
mov si, 0 ; used to move pointer

;-----change bytes
mov al, byte ptr ;get data at mem location
add al, 3
mov byte ptr , al

inc si
add bx, si ;move pointer to next byte
dec cx
cmp cx, 0
jg L1

mov bx, HANDLE
mov ah, 3Eh ; close file
int 21h

mov  ah, 4ch
    int  21h

main endp

end main
Posted on 2007-04-03 22:50:43 by Alklun
When you are working with the DOS trap, the handle of the file returned by function 0x3D is not the pointer to the physical memory location of the content of the file. What you have to do after obtaining the handle of the file is to issue the DOS trap again with function 0x3F in order to read from the file.

In your code, when you do this:

mov bx, offset HANDLE	; bx contains the file handle

You are not moving the value of the HANDLE variable into the base index. You are moving the memory location of that variable into BX so be aware of that too.

I was really frustrated with these DOS calls and other stuff which is why I coded OASML 2.0. You can read about it in this thread. It will save you some time and frustration. Below is the procedure that I've written for OASML. I think this might help you:

; ------------------------------
FileRead PROC
    Description : Reads the total amount of AX bytes from an open file. See note(s).

    Calling Convention : Push from left to right.

    Parameter(s) :
      WORD Param1 = Source file handle offset
      WORD Param2 = Buffer to read onto
      WORD Param3 = Total bytes read

    Stack Usage: 8 Bytes.

    Note : 1) The AX register will be set to zero (0000h) if no errors have occurred and
              will contain one of the below values if an error has occurred:
              AX = 0005h (Access denied)
              AX = 0006h (Invalid Handle)
          2) The Param3 will contain the actual bytes read from the file if the AX register
              is set to zero by the FileRead procedure.
          3) The total number of bytes to read from the file should be specified by the
              AX register before invoking the FileRead procedure.
          4) The Param3's value when the procedure is through will be less than the value
              you pass onto the AX register (Total bytes to read) if you have reached the EOF.
          5) If the AX is not equal to zero (An error has occurred), the value of Param2 and
              Param3 will remain unchanged.

    Example : Read 10 bytes from the file with its handle in a variable called FileHandle

      FileHandle            DW              ?
      ReadBytes            DW              ?
      Buffer                DB              11 DUP(?)
      PUSH    OFFSET FileHandle ; The file handle offset
      PUSH    OFFSET Buffer    ; The buffer to read onto
      PUSH    OFFSET ReadBytes  ; Total bytes read (Will be filled by the procedure)
      MOV    AX , 000Ah        ; Read 10 bytes
      CALL    FileRead          ; Read from the file
      ADD    SP , 0006h        ; Remove three parameters
      TEST    AX , AX          ; See if AX == 0 (Success)
      JNE    @@Failed          ; Jump to ... if not
        ; Success
        ; 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    CX , AX                        ; Total bytes to read
  MOV    DX , WORD PTR           ; Buffer to read the byte(s) onto
  MOV    BX , WORD PTR           ; The file handle pointer
  MOV    BX , WORD PTR               ; The file handle
  MOV    AH , 03Fh                      ; Read from file function
  DW      21CDh                          ; Issue the DOS interrupt
  JC      @@__FileReadEP                  ; Jump to ... if an error has occurred
  MOV    BX , WORD PTR           ; BX = Pointer to Param3 (Read Bytes)
  MOV    WORD PTR , AX              ; Param3 = Bytes Read
  XOR    AX , AX                        ; AX = 0000h (Success)
  @@__FileReadEP:                        ; End of the procedure routine
    POP    BP                            ; Restore the base pointer
    POP    DX                            ; Restore the destination index
    POP    CX                            ; Restore the count register
    POP    BX                            ; Restore the base index
  RET                                    ; Return to the calling procedure
FileRead ENDP
; ------------------------------
Posted on 2007-04-04 01:39:29 by XCHG