If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to read text file, line by line using NASM

Hi all,

I'm new to this forum and new using Assembly language.

If possible, I would like to know how can I read from a text file, line by line, ignoring the "/n" and knowing when the file ends.

The main idea is to have a program that can find the right unciphered phrase against cipheredtext.txt file using a dictionary (dic.txt), I already have a macro that do that job! My problem is to know how to read the words and pass that "word" to the macro.

Re: How to read text file, line by line using NASM

Ok, it is possible; with replacing <i>buffer</i> with the <i>esp</i> register, and assigning the buffer size to <i>edx</i> by yourself. Worked for me. Thanks again, for this great example code. Helped a lot!

Re: How to read text file, line by line using NASM

Well, in this case you're still using a buffer, but instead of allocating it statically, you allocated it from the stack, so it's a variable local to your program's equivalent to a C/C++ program's main() function. ... at least I hope you did so, because otherwise you've just overwritten your stack.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: How to read text file, line by line using NASM

hmm, i think i'm having this problem now.
If I overwrite the stack (in the manner you meant), does this mean, the stack is overwritten and then read?

I've hacked some of the code above, in a c program (inline assembly), without any checks for errors. It works with bugs. I have to execute the program a number of times, and then the file is read, otherwise nothing is outputted. May this be the reason, i'm overwriting the stack?

Re: How to read text file, line by line using NASM

The symptoms you describe may very well indicate stack corruption, which results in the all-time popular undefined behavior. The stack essentially is a system resource and you can't simply use stack memory at will. You may only write to stack memory if you properly allocated it (this includes the PUSH instruction which does sort of an implicit allocation) or has been passed to you by the system or runtime environment for that purpose (this includes parameters your function got passed on the stack which, according to the function's signature, may or may not be allowed to be written to).

The original code from post #2 is a pure assembly language program and as such may even get away with the modifications you alluded to, in the unlikely (I'm not a *nix expert, so I can't really assess this probability) case that the system doesn't make use of the corrupted stack area while shutting down the program in response to the program's exit syscall. Even then, though, it's still extremely bad style - at least...

A C/C++ program relies on its runtime system and that, with practically absolute certainty, will attempt to make use of the corrupted stack area once main() returns. Your program may or may not read the file and produce the desired output, but then certainly crash, producing more or less "noise", because the enclosing function call's return address on the stack has been corrupted. (This is based on the assumption that the parts you copied from the original assembly code don't include the exit syscall. If it is icluded, however, technically my notes about the pure assembly language program above would apply. But that would be even much worse style...)

Last edited by Eri523; July 3rd, 2013 at 04:54 PM.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.