News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

reading a text file

Started by caprisun, May 06, 2007, 10:14:59 PM

Previous topic - Next topic

caprisun


.data
FileName DB "C:\text.txt
Buffer DB ? dup (?)
.code
mov ax,@data
mov ds,ax

mov dx,OFFSET FileName
mov al,2
mov ah,3Dh
int 21h

jc ErrorOpening

mov dx,offset Buffer
mov cx,100
mov ah,3Fh ; function 3Fh - read from file
int 21h


What i don't get is when these commands opens the text file and offsets it to the dx register. Where are all the data stored? and what type of data, such as a string ?

eek

Use this thing.
A picture is better than a thousand words.

http://www.btinternet.com/~btketman/tutpage.html

DS:DX points to the start so move dx to si (or di)

mov ah,3F ;<read
mov al,00 ;<bl has file handle
mov cx,[BYTES]
;<read 1 page
int 21
mov si,dx   ;  <--------------------------------move dx to si so you can manipulate it
mov cx,ax  ;bytes read


The data gets stored directly after your fixed variables

INTRO  ch10 Hello there
BYTES dw 00FF
OTHER db ?                      ; <-----so from around here you dynamic read starts, dx points to start

To keep going down the text file loop with ah3F int21, ax tells you the number of bytes read to memory.

japheth


There is also a bug: after successfully have opened a file (AH=3Dh), a handle is returned in AX, which must be moved to BX before doing the read (AH=3Fh).

MichaelW

Quote from: caprisun on May 06, 2007, 10:14:59 PM
What i don't get is when these commands opens the text file and offsets it to the dx register. Where are all the data stored? and what type of data, such as a string ?
Are you asking where the data for an open file is stored? If so, the answer is that the data is stored in the file, on the disk. When the system opens a file, it essentially reads the directory information for the file and sets up all the data it will need to access the file. MS-DOS stored most of this data in a System File Table (SFT) structure, for an example search this page for the string:

Format of DOS 4.0-6.0 system file tables

The handle that is returned when you open a file is essentially an index into the SFT. This handle must be passed to the functions that access the file data.

eschew obfuscation

japheth


> The handle that is returned when you open a file is essentially an index into the SFT.

That's not quite correct. The handle returned is an index into the JFT, which usually is located in the PSP, with an initial size of 20. And the entries in the JFT are (byte) indices into the SFT.


MichaelW

QuoteThe handle returned is an index into the JFT
Yes, I'm aware of this. I was trying to avoid too much detail, so I used a simplified explanation and qualified it with "essentially". Perhaps it would have been better to use "effectively" instead.
eschew obfuscation

caprisun

is there another debugger out there that would allow me to input my current code more easily than Ketman?

eek

I haven't used the debugger yet, I always use the data windows...

Alt-1

then press 'e' to edit and set up a window.

You can look directly into, and observe, any area of memory you like as the program step-executes.

You would probably need

FileRead            cs:si

once you move dx to si after loading a chunk of the file text to memory.
Use Shift and Alt with the up/down/left/right arrows to manipulate the window, and 0-9 to assign the viewable value you want, bytes, words, etc
The value 0 displays text in a readable format.

caprisun

ok manage to get the output i wanted by just incrementing num.
eg. [si+num]

since I assume that SI is a string I can't use this code to count the sum of array in SI with this?
Quote
L1: add ax, [si]
    add si, 1
    loop L1

my outcome is some large value.

I try using the more generic method


Size = ($ - Buffer)
mov ax, Size


This didn't work, got some illegeal instruction error.

Is there any other method or advise I can try to find the number of element in SI?

MichaelW

Size is a reserved word. You should have gotten:

error A2008: syntax error : size

Indicating that the problem is with the name "size".


.model small
.stack
.data
  buffer db 10 dup(1),0
  bufsize = $ - buffer
.code
.startup
    mov ax, bufsize         ; ax <- 11
    mov ax, size buffer     ; ax <- 10 (sizeof first initializer)
    mov ax, sizeof buffer   ; as <- 11

    xor si, si              ; zero index
  L1:
    mov al, [buffer+si]     ; get byte at buffer + index
    test al, al             ; test for null
    jz L2                   ; finished
    inc si                  ; increment index
    jmp L1                  ; continue
  L2:
    ; SI = 10
.exit
end

eschew obfuscation

caprisun

Lol, good thing I didn't use "Size" in my code. I didn't want to cut and paste my code and confused anyone, but it seems that I should cut and paste.

Anyhow, was there specific site you got this code from? this way I won't have to ask to many questions.

Since I don't my own pc infront of me right now, I can't try your code.

By the look of this, I think i would might come into a problem.

Nicole and I were also talking about Altar Boyz. Does any of the following dates work for you?
.data
  buffer db 10 dup(1),0
  bufsize = $ - buffer
.code


In my code, I start buffer as
Nicole and I were also talking about Altar Boyz. Does any of the following dates work for you?
.data
  buffer db ? dup(?)
.code


Reason for this, is because buffer is I store the data of the text file which has to be open with some int10 function.
Since the program first run without knowing the buffer size, will bufsize be able to count the amount of elements?

MichaelW

The count for the DUP operator must be specified. Using a ? will result in:

error A2009: syntax error in expression

If you need a buffer of some arbitrary size that is unknown at compile time, then depending on the maximum size required, you could allocate the buffer in the initialized (.DATA) or uninitialized (.DATA?) data segments, or allocate it from the OS at run time, after determining the actual required size.
eschew obfuscation

caprisun

Finally got home and tried to test your original code

First I used Emu8086
gave me first error at
illegal instruction: bufsize = $ - buffer

Second program i tried on FASM

got error on the first line:
.model small

Which complier/assemblier are you using? 

caprisun

duh.. obviously used masm32
Anyway, when i try complieing your code got 2 errors
error a2006:undefined symbol dgroup
error a2074:cannot access through segment regs....

eek

hehe  :bg

Thats why I ended up using Ketman.

Nothing flipping worked, even the simplest examples were crippleware.
Trying with java totally did my head in as well...

I must have about twenty different assemblers/compilers on my computer.