flat assembler
Message board for the users of flat assembler.
Index
> Windows > importing stdin Goto page 1, 2 Next |
Author |
|
m 03 Jun 2007, 00:27
Code: cinvoke fgets,buff,128,[stdin] _________________ Attitude! |
|||
03 Jun 2007, 00:27 |
|
m 03 Jun 2007, 00:57
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 ! |
|||
03 Jun 2007, 00:57 |
|
kohlrak 03 Jun 2007, 01:01
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. |
|||
03 Jun 2007, 01:01 |
|
m 03 Jun 2007, 01:10
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. |
|||
03 Jun 2007, 01:10 |
|
kohlrak 03 Jun 2007, 01:12
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.
|
|||
03 Jun 2007, 01:12 |
|
m 03 Jun 2007, 01:21
Thanks a lot again Mr. Kohlrak !
Looks like, when you think Assembly, then you forget Portability ! |
|||
03 Jun 2007, 01:21 |
|
kohlrak 03 Jun 2007, 01:26
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.
|
|||
03 Jun 2007, 01:26 |
|
Goplat 03 Jun 2007, 02:34
kohlrak wrote: Try this... 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. |
|||
03 Jun 2007, 02:34 |
|
kohlrak 03 Jun 2007, 02:42
Then how do you propose he gets the pointer?
|
|||
03 Jun 2007, 02:42 |
|
m 03 Jun 2007, 02:56
Yeah Goplat, please tell me how do I get the pointer !
|
|||
03 Jun 2007, 02:56 |
|
Goplat 03 Jun 2007, 05:07
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. |
|||
03 Jun 2007, 05:07 |
|
m 03 Jun 2007, 11:23
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! |
|||
03 Jun 2007, 11:23 |
|
LocoDelAssembly 03 Jun 2007, 14:43
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]". |
|||
03 Jun 2007, 14:43 |
|
vid 03 Jun 2007, 15:30
yeah, doing it "the right way" in assembly isn't very nice:
MSVC headers say: Code: #define stdin (&__iob_func()[0]) |
|||
03 Jun 2007, 15:30 |
|
kohlrak 03 Jun 2007, 16:57
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 |
|||
03 Jun 2007, 16:57 |
|
Goplat 03 Jun 2007, 17:23
m wrote: Hi Mr. Goplat, I believe that in PE console format fasm (or the macros) does not allow me to import a variable ! 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 kohlrak wrote: Just out of shear curiosity, would it be the same in crtdll as msvcrt? Quote: And is there another stdin, because i feel 32 bytes is a bit small for client data... Code: struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE; |
|||
03 Jun 2007, 17:23 |
|
LocoDelAssembly 03 Jun 2007, 17:31
Goplat wrote:
Yeah sorry, I forgot it doesn't have the "n" parameter. |
|||
03 Jun 2007, 17:31 |
|
kohlrak 03 Jun 2007, 17:50
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...
|
|||
03 Jun 2007, 17:50 |
|
vid 03 Jun 2007, 17:55
this is the problem with using libc in assembly. You need to code at least some small wrapper for libc to use it
|
|||
03 Jun 2007, 17:55 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.