-
April 18th, 2012, 01:52 PM
#1
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.
Thanks.
-
October 2nd, 2012, 01:50 AM
#2
Re: How to read text file, line by line using NASM
section .bss
buffer: resb 2048 ; A 2 KB byte buffer used for read
section .data
buflen: dw 2048 ; Size of our buffer to be used for read
section .text
global _start
_start:
; open(char *path, int flags, mode_t mode);
; Get our command line arguments.
pop ebx ; argc
pop ebx ; argv[0] (executable name)
pop ebx ; argv[1] (desired file name)
mov eax, 0x05 ; syscall number for open
xor ecx, ecx ; O_RDONLY = 0
xor edx, edx ; Mode is ignored when O_CREAT isn't specified
int 0x80 ; Call the kernel
test eax, eax ; Check the output of open()
jns file_read ; If the sign flag is set (positive) we can begin reading the file
; = If the output is negative, then open failed. So we should exit
exit:
mov eax, 0x01 ; 0x01 = syscall for exit
xor ebx, ebx ; makes ebx technically set to zero
int 0x80
; = Begin reading the file
file_read:
; read(int fd, void *buf, size_t count);
mov ebx, eax ; Move our file descriptor into ebx
mov eax, 0x03 ; syscall for read = 3
mov ecx, buffer ; Our 2kb byte buffer
mov edx, buflen ; The size of our buffer
int 0x80
test eax, eax ; Check for errors / EOF
jz file_out ; If EOF, then write our buffer out.
js exit ; If read failed, we exit.
; Didn't read the whole file, so just output what we got and be done with it.
; ^ This is blah and needs to be updated when I find out how
file_out:
; write(int fd, void *buf, size_t count);
mov edx, eax ; read returns amount of bytes read
mov eax, 0x04 ; syscall write = 4
mov ebx, 0x01 ; STDOUT = 1
mov ecx, buffer ; Move our buffer into the arguments
int 0x80
jmp exit ; All done
-
July 3rd, 2013, 05:05 AM
#3
Re: How to read text file, line by line using NASM
Hello codeguru community,
This code is very great, thank you very much.
But, does the file`s content has to be stored in a buffer? Isn't it possible to read the file from a register, especially if we just need the first 16 bits?
How would one achieve that, and is this possible (i.e to read from file without any buffer. Just read the first bits)?
Thanks
nghct
-
July 3rd, 2013, 05:53 AM
#4
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!
nghct
-
July 3rd, 2013, 08:49 AM
#5
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.
-
July 3rd, 2013, 12:30 PM
#6
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?
-
July 3rd, 2013, 01:55 PM
#7
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 03: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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|