flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > importing stdin

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
m



Joined: 28 Dec 2006
Posts: 304
Location: in
Code:
cinvoke fgets,buff,128,[stdin]    
i cant get it work,Why?

_________________
Attitude!
Post 03 Jun 2007, 00:27
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
MSDN wrote:
char *fgets(
char* string,
int n,
FILE *stream
);


The third parameter is a pointe,r in your code, it's the value referanced by the pointer. Give it a pointer, unless the pointer is stored in the variable stdin, which in that case, you'll have to show the rest of the program to see what is wrong.
Post 03 Jun 2007, 00:39
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
m



Joined: 28 Dec 2006
Posts: 304
Location: in
The code is written for PE console format. fgets is supposed to be imported with the help of import32.inc macros and the stdin is the famous stdin that we can use in C.

I wanted to do what looks like this in C:
Code:
fgets(buff,128,stdin);    

Help !
Post 03 Jun 2007, 00:57
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
Try this...

Code:
invoke  GetStdHandle, STD_INPUT_HANDLE
cinvoke fgets, buff, 128, eax    


GetStdHandle is in kernel32.dll i think... Not sure, but it *MIGHT* have A and W versions, so if you get an error that it's not found, just put an A there.
Post 03 Jun 2007, 01:01
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
m



Joined: 28 Dec 2006
Posts: 304
Location: in
Thanks a lot Mr Kohlrak !
Any thoughts on why we can't access the stdin directly ?
I am worried cause if I use GetStdHandle it will make my code unportable on Linux.
Post 03 Jun 2007, 01:10
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
It's because STDIN is program specific. If you want STDIN, you have to get it dynamically (from what i've seen)... In windows that is. I don't know how to do so in linux.
Post 03 Jun 2007, 01:12
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
m



Joined: 28 Dec 2006
Posts: 304
Location: in
Thanks a lot again Mr. Kohlrak !
Looks like, when you think Assembly, then you forget Portability !
Post 03 Jun 2007, 01:21
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
True, but anything that uses something outside the standard library, and it looses compatability, and many forget this. I'm looking at your window code, now.
Post 03 Jun 2007, 01:26
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
kohlrak wrote:
Try this...

Code:
invoke  GetStdHandle, STD_INPUT_HANDLE
cinvoke fgets, buff, 128, eax    


GetStdHandle is in kernel32.dll i think... Not sure, but it *MIGHT* have A and W versions, so if you get an error that it's not found, just put an A there.


That won't work; GetStdHandle returns a win32 HANDLE, but fgets expects a pointer to a FILE structure.

Usually C's stdin/stdout/stderr are not actual variables, but macros. If you look in stdio.h you will find something like #define stdin &_iob[0], #define stdout &_iob[1], and #define stderr &_iob[2], where _iob is an array of FILEs.
Post 03 Jun 2007, 02:34
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
Then how do you propose he gets the pointer?
Post 03 Jun 2007, 02:42
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
m



Joined: 28 Dec 2006
Posts: 304
Location: in
Yeah Goplat, please tell me how do I get the pointer !
Post 03 Jun 2007, 02:56
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
I assume you're using msvcrt? Just import _iob, then [_iob] is the address of stdin. To get stdout or stderr you add 32 or 64, since there are 32 bytes in a FILE structure.

There are also functions called __p__iob and __iob_func, which seem to always just return the same address you get from importing _iob. If you're using a different version of the C library which doesn't have plain _iob then try those.
Post 03 Jun 2007, 05:07
View user's profile Send private message Reply with quote
m



Joined: 28 Dec 2006
Posts: 304
Location: in
Hi Mr. Goplat, I believe that in PE console format fasm (or the macros) does not allow me to import a variable !
I'll try other option suggested by you. And yes I was using msvcrt.dll.

_________________
Attitude!
Post 03 Jun 2007, 11:23
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4634
Location: Argentina
Do you really need stdin because your code should read both files and stdin or you just need to read from stdin? If the latter is the case use gets instead http://board.flatassembler.net/topic.php?t=3895

If you need pointer to _iob[0] then according to what I see in IDApro you have "__iob_func" and "__p__iob" which returns it. Also you have "_iob" which is a variable. To import a variable just add it in the import table like if it were a function but never call it, just access it with "[_iob]".
Post 03 Jun 2007, 14:43
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
yeah, doing it "the right way" in assembly isn't very nice:

MSVC headers say:
Code:
#define stdin  (&__iob_func()[0])    
Post 03 Jun 2007, 15:30
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
Just out of shear curiosity, would it be the same in crtdll as msvcrt? And is there another stdin, because i feel 32 bytes is a bit small for client data...
APJ 9 wrote:
CGI Environment
---------------
A CGI program is not required to read data, but it is required to send it.
Client data is availiable on the StdIn. The length is in the CONTENT_LENGTH
environment variable. Also, 255 bytes of the data is in the QUERY_STRING
EnvVar. All out put must start with "Content-Type:" a space, the type, and two
newlines (CrLf). Common types include: "text/plain", "text/html", or
"image/gif". Example output:
Post 03 Jun 2007, 16:57
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
m wrote:
Hi Mr. Goplat, I believe that in PE console format fasm (or the macros) does not allow me to import a variable !
It worked when I tried it...

LocoDelAssembly wrote:
Do you really need stdin because your code should read both files and stdin or you just need to read from stdin? If the latter is the case use gets instead http://board.flatassembler.net/topic.php?t=3895
Only if you don't mind crashing when the user types a line that's too long.

kohlrak wrote:
Just out of shear curiosity, would it be the same in crtdll as msvcrt?
Yep, although crtdll only exports _iob itself; it doesn't have either of the functions.

Quote:
And is there another stdin, because i feel 32 bytes is a bit small for client data...
The FILE struct doesn't contain the buffer directly, just pointers to it. Here's the complete definition, if you're curious.
Code:
struct _iobuf {
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname;
        };
typedef struct _iobuf FILE;    
Post 03 Jun 2007, 17:23
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4634
Location: Argentina
Goplat wrote:

Only if you don't mind crashing when the user types a line that's too long.

Yeah sorry, I forgot it doesn't have the "n" parameter.
Post 03 Jun 2007, 17:31
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
Explains a bit. Now to figure out how to imporat all the other data for CGI apps, but that's for another topic, since that would be off topic. So i'm assuming _ptr would be to the first thing in the string, or would that be _base? ...hm, that is an interesting structure...
Post 03 Jun 2007, 17:50
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
this is the problem with using libc in assembly. You need to code at least some small wrapper for libc to use it
Post 03 Jun 2007, 17:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.