Jump to content

How do I change an existing byte in a file?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Bond

Bond

    Newbie

  • Members
  • Pip
  • 5 posts
I need to be able to change the value of an existing byte in a file. Primarily, this is because I need data stored towards the beginning of the file that I won't know until the end of creating the file.

How do I do this?

This is a file that has been created by the program (Int 21, 3Ch), so I'm guessing it's already open with write access. I would imagine all I need to do is change the pointer from the end of the file at that moment to the byte I need to patch and back again, but I have no idea where or how this pointer is stored. For what it's worth, I've got two files open: one file open for reading, and then this one which was created by the app - I don't know if this changes where the pointer would be stored.

Obviously, I'm a newbie. :) Oh, and this is for MASM compatible 16-bit code.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
File seek: int 21h, AH=42H

AL - Where your offset is relative to. 0 is beginning of the file, 1 is the current file position, 2 is from the end of the file.
BX - The file handle
CX:DX - The offset to move to as a signed two's complement 32-bit integer.

You can't modify the file pointer directly, only through this interrupt. To seek to the end of the file, set CX:DX to 0 and AL to 2. To move from the end of the file, CX:DX should be negative, though you're allowed to seek past the end of the file, in which case the file will automatically grow to accommodate the change.
sudo rm -rf /

#3
Bond

Bond

    Newbie

  • Members
  • Pip
  • 5 posts
Perfect, thank you so much for your help. :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users