Reputation: 43
I'm new to assembly. I'm trying to read from a text file, this is my code:
_DATI SEGMENT PARA PUBLIC 'DATA' USE16
Text_Buffer dw ?
filehandle dw ?
boolean db 'boolean.txt',0
_DATI ENDS
_CODE SEGMENT PARA PUBLIC 'CODE' USE16
ASSUME CS:_CODE, DS:_DATI, ES:_DATI
ReadFile proc
mov ah, 3dh ;open the file
mov al, 0 ;open for reading
lea dx, file_name
int 21h
mov [filehandle], ax
mov ah, 3fh
lea dx, Text_Buffer
mov cx, 1 ; Read 1 Byte
mov bx, [filehandle]
int 21h
mov bx, [filehandle]
mov ah, 3eh ;close file
int 21h
ret
ReadFile endp
START:
call ReadFile
_end:
mov ax, 4c00h
int 21h
_CODE ENDS
END START
Isn't the ASCII from the file supposed to be saved in the allocated buffer (Text_Buffer)?
All I get now is some random byte saved, and for some reason, the program ends after I enter input. Can someone explain what's the problem and how to solve it? Thanks in advance.
Upvotes: 1
Views: 13391
Reputation: 16586
I had to fix your code in question in these points:
boolean db 'boolean.txt',0
changed to (to have file_name
symbol defined):
file_name db 'boolean.txt',0
And the "main" was not setting up ds
:
...
START:
mov ax,SEG _DATI
mov ds,ax
call ReadFile
...
After this I compiled the code with TASM (tasm so_readf.asm
tlink /x so_readf.obj
, warning about missing stack reported and ignored, then td so_readf.exe
to debug it), created file "BOOLEAN.TXT" containing two bytes 0x78 0x0A (letter "x" with newline edited with linux text editor), and used turbo debugger to verify it works (under dosbox
emulator).
After the "read" service the ds:0000
memory view shows 78
"x" instead of 00
byte value, as expected.
I had problem to open the file because I created it after I already started the dosbox, so its existence was not known to the dosbox and it did report CF=1, ax=2
after "open file" service first time. After using "rescan" command of dosbox the next debugger run did work as expected. But this problem is related only to my testing environment (dosbox under linux), if you are using different emulator or live DOS system, you may not encounter this. Also if the "boolean.txt" is created as result of other code run inside the same emulator/dosbox, the file should be visible. You can do on DOS command line "type boolean.txt
" to see it's current content and verify it's existence.
But otherwise as expected, what you did post is correct, as long as all the prerequisites are, i.e. you have your ds
valid, and you are in correct working directory, and you didn't run out of file handles (do you close all the files you open before?), etc.. Also while debugging under debugger, the invalid cases with wrong filename (wrong ds
), and dosbox not aware of the file, did end correctly with CF=1 after "open file", so I could terminate the code in debugger early (as there's no error handling code, so it would do incorrect things afterwards).
Try again in debugger to identify what exactly doesn't work, and all return values from every int 21h
, for example the "read" ones returns number of bytes read in ax
, should be 1 in case of success.
Upvotes: 1