After I read a byte from a file in assembly, the current file position is updated. How can I want to modify that byte and than write it in the file on the same position I read it from? I am working in tasm on x8086
READ_LOOP:
MOV AH,3FH
MOV BX, HANDLE
MOV CX, 1
LEA DX, BUFFER
INT 21H
INC SI
;if no byte was read we reached the end of file or an error occured
CMP AL, 1
JB EOF_END_ERR
;write the read content to output
;MOV AH, 02H
;MOV DL, BUFFER
;INT 21H
JMP ENCRYPTING
JMP FINAL
ENCRYPTING:
XOR AX,AX
MOV DL, BUFFER[0]
ADD DL, NUMBER[0]
MOV TEMP, DL
MOV AH, 42H
MOV BX, HANDLE
MOV CX,0FFFFH
MOV DX, 0FFFFH
MOV AL,1
INT 21H
XOR DX,DX
MOV DL, 65
MOV BX, HANDLE
MOV CX,1
MOV AH, 40H
INT 21H
JMP READ_LOOP
I tried with the code above, to simply add NUMBER[0] to the ASCII code of the read byte and than write it back in the file. But it doesn't write what it should in the file
You can use service AH=42h for the same. After you have read a byte from the file the file pointer will be updated. Now to replace the previous read byte in the file to something else you need to first move the file pointer one byte backwards (so that it points to the byte that you want to replace) and this can be done with the following code :
Code to move the file pointer one byte backwards from its current position:
mov al, 1 ; relative to current file position
mov ah, 42h ; service for seeking file pointer
mov bx, handle
mov cx, -1 ; upper half of lseek 32-bit offset (cx:dx)
mov dx, -1 ; moves file pointer one byte backwards (This is important)
int 21h
After the execution of above code you can now overwrite the byte with the new byte and this can be done with the following code:
Code to write from the current position of file pointer:
mov ah, 40h ; service for writing to a file
mov bx, handle
mov cx, 1 ; number of bytes to write
mov dx, offset char ; buffer that holds the new character to be written
int 21h
For more about file operation goto here.