Author Topic: Accessing texts from files  (Read 4160 times)

nobody

  • Guest
Accessing texts from files
« on: October 07, 2004, 08:23:10 AM »
Helllo! CAn somebody help me accessing the individual bytes using NASM! I called for the open and read system calls then have its individual bytes access... I did this to print a SELECTED TEXT from the file... Please reply...... I badly need it... Thanks!
(I'M USING LINUX!)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2358
  • Country: us
Re: Accessing texts from files
« Reply #1 on: October 07, 2004, 07:13:49 PM »
I'm not sure what you're trying to do... You've got some chunk of the file read into a buffer with sys_read... there are your bytes... Do you need addressing modes, or are you having trouble finding which bytes you want?

Are you trying to access text at some known offset into the file, or searching for some specified text?

Best,
Frank

nobody

  • Guest
Re: Accessing texts from files
« Reply #2 on: October 08, 2004, 05:33:10 AM »
Sorry, Frank! I'm very sorry....
I'm using linux! And I'm trying to simulate a simple grep instruction called FIND! It ask for the filename and the word to look for from the user and the program searches this word... The problem is that I don't exactly know where to put the code snippet that searches for the word... Our teacher only gives us a handouts sometime last week and we only studied it only for a short period of time which is not enough to be totally comprehended. He requires us to submit a project, simple grep, which is due next week. Given these constriants, we are                    arnt sure we cn cope w/ this. Heres the code we have made. This only diplays the particular portion of the file. This is just a test and if it proves to be successful we could proceed to of project proper. Could you give me a hint how to?

section .data
   prompt1:      db 'Filename: ',0
   prompt1len:      equ $-prompt1
   prompt2:      db 'Find: ',0
   prompt2len:      equ $-prompt2

section .bss
   file   resd 20      ;container for filename
   look   resb 20         ;container for the word to look for
   size   resb 1          ;container of the size the word to look for
   words  resb 1        ;container
   buf    resb 800         ;buffer
   fd     resd 1      ;file descriptor

section .text
   global _start

_start:
;displays "Filename: "
   mov eax,4      ;syscall # for write
   mov ebx,1      ;file descriptor # for stdout
   mov ecx,prompt1      ;the message
   mov edx,prompt1len   ;length of the message
   int 80h         

;capture input from keyboard
   mov eax,3      ;syscall # for read
   mov ebx,0      ;file descriptor # for stdin
   mov ecx,file      ;input buffer
   mov edx,20      ;size of buffer
   int 80h   

;omit the  newline character
   sub eax,1
   mov ebx,0
   mov [file + eax],ebx

;displays "Find: "
   mov eax,4
   mov ebx,1
   mov ecx,prompt2
   mov edx,prompt2len
   int 80h

;capture input from the keyboard
   mov eax,3
   mov ebx,0
   mov ecx,look
   mov edx,20
   int 80h
   mov [size],eax

;omit newline character
   sub eax,1
   mov ebx,0
   mov [look + eax],ebx

;open
   mov eax,5
   mov ebx,file ;This has no bracket bec. this refers to the address of the
   mov ecx,0    ;filename. When you put a brachet this will return the
   mov edx,0    ;value of the filename at that address.
   int 80h
   mov [fd],eax

;read
   mov eax,3
   mov ebx,[fd]
   mov ecx,buf
   mov edx,800
   int 80h

; THIS PART HERE DOWN IS THE PROB!
   mov   ecx,0
while:
   mov [size],ecx
   cmp ecx,5
   je  exit

mov ebx,[buf+ecx]
   mov [words+ecx],ebx
   add ecx,1
   jmp while

; print the word
exit:
   mov eax,4
   mov ebx,1
   mov ecx,words
   mov edx,size
   int 80h

;close the file
   mov eax,6
   mov ebx,[fd]
   int 80h   

;exit code
   mov eax,1
   mov ebx,0
   int 80h
We know our limits and for the mean time, we accept that we lack exact understand of the system calls. Hope you could help us.
good day

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2358
  • Country: us
Re: Accessing texts from files
« Reply #3 on: October 08, 2004, 01:30:40 PM »
Ah, okay! "grep"... just finding and printing a known offset would have been easier :)

There are a number of things about your code that could arguably be made "better", but the "problem" is quite simple! In your "print the word" routine, you want the "[contents]" of size, not its address/offset... so "mov edx, [size]".

Haven't tested this at all, but that's the only thing I see that looks like a problem. Good start! I'll try this, and maybe make some more comments on it, if I get a "round tuit"... just wanted to point out this simple problem quickly...

Best,
Frank