Author Topic: importing a string from a file  (Read 9328 times)

Offline richard.garrity

  • New Member
  • Posts: 1
importing a string from a file
« on: February 13, 2012, 06:19:39 PM »
I need to store a line of strings into a buffer, but I have no idea how to start.
« Last Edit: February 13, 2012, 07:37:52 PM by richard.garrity »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: loading a string with buffer?
« Reply #1 on: February 13, 2012, 07:04:53 PM »
My usual advice (although I don't always do it) is, "if you don't know where to start, write the comments first". In this case, we may need more information before we can even help you with that. "What OS?" will make a difference, probably. Then... I'm not sure what you mean by a "line of strings". Where are these strings coming from? Keyboard? Disk file? Someplace else (environment strings, e.g.)?

In any case, you'll need a buffer first. Then you'll want to make sure you don't overflow the buffer! Rather than "hard code" the buffer size, like:
Code: [Select]
section .bss
my_buffer resb 100h
it is probably wise to define the buffer size, so it can be changed easily:
Code: [Select]
%define BUFSIZ 100h
; or BUFSIZ equ 100h - either will work
section .bss
my_buffer resb BUFSIZ
then later, as you add strings to the buffer (I understand you want to put multiple strings into one buffer?), you can check for overflow...
Code: [Select]
; ??? depends on OS
cmp ???, my_buffer + BUFSIZ
jae overflow
; ...
overflow:
; issue a diagnostic... "Too much, you fool!"
; quit?
; or maybe just quit entering strings and continue...

Or maybe I don't understand what you're trying to do. Tell us more!

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: importing a string from a file
« Reply #2 on: February 13, 2012, 10:36:47 PM »
Well, what's happening to cause the segfault (I think) is that you're not finding either -1 (why would you expect that?) or a linefeed in the buffer, so you keep going until you hit invalid memory.

I'm not seeing either of your messages. This is printf's "dirty little secret". It doesn't print anything until the buffer is flushed! This is often not noticed, because it prints everything at the end... which your program doesn't reach because of the segfault! Easiest way to fix this is to add a linefeed at the end of each string (before the terminating zero).
Code: [Select]
;
; initialized data is put in the data segment here
;
   %define newline 10
   prompt1 db    "Enter a number: ", newline, 0
   testoutmsg   db    "test that step worked", newline, 0
That will flush the buffer (the i/o buffer, not yours), and printf will print something.

Dr. Carter's "read_char" reads just one character, and returns it in al. You put all of eax in your buffer. The upper parts of eax are "garbage" - zeros, in fact. You probably want:
Code: [Select]
mov [buffer], al
But since you've got the address of "buffer" in esi, and you increment esi...
Code: [Select]
mov [esi], al
would probably be better. Then, you can loop back and "read_char" another, until you find that linefeed you're looking for.

At present, your "ASCII_to_Hex" isn't used. I'm not sure where you intend to call it, or how you intend to use the result. If you've got 'a' in your buffer, and you put it in dl and call this function, it'll return 61h in dl. If you just print this, it'll produce 'a' again. If you expect to see "61", you'll need to print two characters. Maybe "print_int" will do what you want there? (although it'll print it in decimal, not hex) See how it goes...

As a minimum, I'd look for characters you're actually going to find in your buffer - 0, not -1, probably, or newline maybe (if you put it there). I think "print_string" expects a zero-terminated string, so you may want to add that - I don't think "read_char" will return it for you...

When posting code, if you put the word "code" in square brackets ('[]'), as if it were a memory reference in Nasm, and "/code" (in '[]'s) at the end, it'll put your code in "code tags", which makes it a little easier to read and a lot easier to "cut and paste". We like it if you use 'em!

See if you can get rid of the segfault first, and come back with more questions (if any :) ).

Best,
Frank



Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: importing a string from a file
« Reply #3 on: February 14, 2012, 10:02:44 PM »
Just a quick note about buffered i/o, if you are writing a string that doesn't have a newline and you want to force a flush, the c library comes with a function (fflush) to force the system to flush the buffer.

About Bryant Keller
bkeller@about.me