Author Topic: Fooling around with file handling  (Read 11199 times)

Offline sledge

  • Jr. Member
  • *
  • Posts: 19
Fooling around with file handling
« on: November 26, 2011, 07:14:15 AM »
This code's intention is to get the first byte from test.txt, increment it and replace it.

For example, let's say test.txt is "a", then when running this code, test.txt should have "b". If run again, then "c" and so on.

Code: [Select]
org 100h

mov ah, 3Dh
mov al, 2
mov dx, str_filename
int 21h

jc filebad

mov [handle], ax

mov ah, 3Fh
mov bx, [handle]
mov cx, 1
mov dx, data
int 21h

mov al, [data]
inc al
mov byte [data], al

mov ah, 40h
mov bx, [handle]
mov cx, 1
mov dx, data
int 21h

terminate:
mov ah, 4Ch
int 21h

filebad:
mov ah, 9
mov dx, str_filebad
int 21h
jmp terminate

str_filename db 'test.txt$'
str_filebad db 'Could not open file!$'
handle dw 0
data db 0

It's almost working as it should, but it inserts a new character to the file, what I want is for this first character to be replaced.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Fooling around with file handling
« Reply #1 on: November 26, 2011, 09:12:50 AM »
Once you've read the first byte, the "file pointer" is incremented to point to the second byte. Either close and reopen the file, or look into "lseek" to move the pointer back to the first byte.

Best,
Frank