flat assembler
Message board for the users of flat assembler.
Index
> Windows > What am I doing wrong here... |
Author |
|
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! |
|||
17 Jun 2005, 16:18 |
|
shaolin007 17 Jun 2005, 18:11
THEWizardGenius wrote: I dunno but shouldn't this be on the Windows forum? 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? |
|||
17 Jun 2005, 18:11 |
|
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.
|
|||
17 Jun 2005, 18:32 |
|
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? |
|||
17 Jun 2005, 18:47 |
|
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'
|
|||
18 Jun 2005, 03:22 |
|
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. |
|||
20 Jun 2005, 12:12 |
|
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' |
|||
21 Jun 2005, 01:32 |
|
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. 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. |
|||
21 Jun 2005, 02:48 |
|
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? |
|||
21 Jun 2005, 03:45 |
|
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' |
|||
21 Jun 2005, 20:53 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.