flat assembler
Message board for the users of flat assembler.

Index > Windows > What am I doing wrong here...

Author
Thread Post new topic Reply to topic
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 17 Jun 2005, 15:35
Code:
format PE CONSOLE 4.0
entry Main

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

Main:

          cinvoke printf, szAskInput
          cinvoke fgets, szBuffer, [sizeof_szBuffer], [stdin]<-----?
          cinvoke getch
          invoke ExitProcess,0


section '.data' data readable writeable

szAskInput      db "Please Enter a number and press enter: ",0
szBuffer        db 10 dup (0),0
sizeof_szBuffer dd $-szBuffer
stdin           dd 0

section '.idata' import data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         fgets, 'fgets',\
         getch, '_getch'
    


It's crashing at the fgets function and I think it's because of the 3rd parameter that I'm passing to it as stdin==0. Could someone give me a hand here?
Post 17 Jun 2005, 15:35
View user's profile Send private message Reply with quote
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius 17 Jun 2005, 16:18
I dunno but shouldn't this be on the Windows forum?

edit: I see that someone moved it. Folks, please remember to post in the correct board. Otherwise it can be annoying for the rest of us.

_________________
FASM Rules!
OS Dev is fun!
Pepsi tastes nasty!
Some ants toot!
It's over!
Post 17 Jun 2005, 16:18
View user's profile Send private message AIM Address Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 17 Jun 2005, 18:11
THEWizardGenius wrote:
I dunno but shouldn't this be on the Windows forum?

edit: I see that someone moved it. Folks, please remember to post in the correct board. Otherwise it can be annoying for the rest of us.


Excuse me but this doesn't involve windows but a c function hence the general area post. If it strains your eyes to view upon code that might run on windows then I can't help you. By the way, it's annoying to find people get annoyed by a post they think is incorrectly posted to the wrong board. Don't you have better things to do with your time?
Post 17 Jun 2005, 18:11
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 17 Jun 2005, 18:32
At first look I can't say what's wrong, but why don't you use scanf function? It is dedicated for getting input from user.
Post 17 Jun 2005, 18:32
View user's profile Send private message Visit poster's website Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 17 Jun 2005, 18:47
decard wrote:
At first look I can't say what's wrong, but why don't you use scanf function? It is dedicated for getting input from user.


I thought about it but it is error prone and doesn't do bound checking like this one. Would there be a better option than the one you mentioned?
Post 17 Jun 2005, 18:47
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 18 Jun 2005, 03:22
took a look at your code, and your using "fgets" which gets a string from a file, what you want i think is "gets", which gets a string from the 'stdin'
Post 18 Jun 2005, 03:22
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 20 Jun 2005, 12:12
madmatt wrote:
took a look at your code, and your using "fgets" which gets a string from a file, what you want i think is "gets", which gets a string from the 'stdin'


Yea but you can also get input from the stdin, stdout, ect.. I still have no idea why it won't work. I tried the code in a c compiler and it worked when in c format. I must be overlooking something. I might have to go with something else like getc() and do my own bounds checking.
Post 20 Jun 2005, 12:12
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 21 Jun 2005, 01:32
Again, change fgets (which is for file string input) to gets (which is for stdin (screen display). I added a printf command so you can what was just typed. See the modified code below, it works fine on my system.

Code:
format PE CONSOLE 4.0
entry Main

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

Main:

          cinvoke printf, szAskInput
          cinvoke gets, szBuffer, [sizeof_szBuffer], [stdin]
          cinvoke printf, szBuffer
          cinvoke getch
          invoke ExitProcess,0


section '.data' data readable writeable

szAskInput      db "Please Enter a number and press enter: ",0
szBuffer        db 10 dup (0),0
sizeof_szBuffer dd $-szBuffer
stdin           dd 0

section '.idata' import data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         gets, 'gets',\
         getch, '_getch'
    
Post 21 Jun 2005, 01:32
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 21 Jun 2005, 02:48
madmatt wrote:
Again, change fgets (which is for file string input) to gets (which is for stdin (screen display). I added a printf command so you can what was just typed. See the modified code below, it works fine on my system.

Code:
format PE CONSOLE 4.0
entry Main

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

Main:

          cinvoke printf, szAskInput
          cinvoke gets, szBuffer, [sizeof_szBuffer], [stdin]
          cinvoke printf, szBuffer
          cinvoke getch
          invoke ExitProcess,0


section '.data' data readable writeable

szAskInput      db "Please Enter a number and press enter: ",0
szBuffer        db 10 dup (0),0
sizeof_szBuffer dd $-szBuffer
stdin           dd 0

section '.idata' import data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         gets, 'gets',\
         getch, '_getch'
    


Thanks madmatt but according to my reference gets() only takes 1 parameter. This is what I have.

gets function

--------------------------------------------------------------------------------

gets is used to read a line of data from STDIN. By default STDIN is the keyboard. gets continues to read characters until NEWLINE or EOF is seen.


Library: stdio.h

Prototype: char *gets(char *s);

Syntax: char read_line[80];

gets( read_line);

Notes
gets does NOT check the size of the buffer and overflow on the stack can occour. Because of this, you should use fgets in preferance.

The NEWLINE character is NOT placed in the buffer. fgets will retain the NEWLINE.


I think I'm on the verge of figuring what I must do for it to work. I'm analyzing the stdio.h and seeing how FILE is defined. Once I do, I will post the code so maybe others won't have to go through what I went through. Thanks again for your help.
Post 21 Jun 2005, 02:48
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 21 Jun 2005, 03:45
Ok finally got it to work correctly! I found some C code that did this to give me an idea on how to do it!

Code:
#define stdin   (_iob[0])
#define stdout (_iob[1])
#define stderr  (_iob[2])
    


I looked at the msvcrt.dll with a hex editor and sure enough _iob is defined in there. So heres my working example of how to do it.

Code:
format PE CONSOLE 4.0
entry Main

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

Main:
          cinvoke printf, szMessage1
          cinvoke fgets, szBuffer, 25, [_iob+0]
          cinvoke printf, szMessage2
          cinvoke printf, szFormat, szBuffer
          cinvoke getch
          invoke ExitProcess


section '.data' data readable writeable
szBuffer        db 25 dup (0)
iSizeOf         dd 25
szFormat        db "%s",0
szMessage2      db 13,10,13,10,"Buffer= ",0
szMessage1      db "Please enter 24 characters. If you enter more, they will be ignored.",13,10,"Enter now: ",0

section '.idata' import data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         getch, '_getch',\
         fgets, 'fgets',\
         _iob, '_iob'
    



I tried changing the value of _iob to like 1 or 2 and it won't work, but with 0 ,which is the stdin, it works just like it should. The only question I have left is how is _iob defined as? I'm having trouble locating info on this item that breaks it down bit by bit. Any thoughts?
Post 21 Jun 2005, 03:45
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 21 Jun 2005, 20:53
Found out that it's good idea to clear the junk out of 'stdin' if you type more input than whats in the buffer. It causes any other fgets() to be skipped. To do that I just used fflsuh(_iob). Example below

Code:
format PE CONSOLE 4.0
entry Main

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

Main:

          cinvoke printf, szAskInput
          cinvoke fgets, szBuffer1, [sizeof_szBuffer], [_iob]
          cinvoke fflush, [_iob]
          cinvoke printf, szCRLFx2
          cinvoke printf, szAskInput
          cinvoke fgets, szBuffer2, [sizeof_szBuffer], [_iob]
          cinvoke fflush, [_iob]
          cinvoke strtol, szBuffer1, 0, 10
          push eax
          cinvoke strtol, szBuffer2, 0, 10
          pop edx
          add eax, edx
          mov [iResult], eax
          cinvoke printf, szResult, [iResult]
          cinvoke getch
          invoke ExitProcess,0


section '.data' data readable writeable

szAskInput      db "Please Enter a integer and press enter: ",0
szBuffer1       db 10 dup (0)
szBuffer2       db 10 dup (0)
iResult         dd ?
szResultBuffer  db 13 dup (0)
sizeof_szBuffer dd 11
szCRLFx2        db 13,10,13,10,0
szResult        db 13,10,13,10, "Result is: %u",0

section '.idata' import data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         fgets, 'fgets',\
         getch, '_getch',\
         _iob,'_iob',\
         strtol,'strtol',\
         fflush, 'fflush'
    
Post 21 Jun 2005, 20:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.