Hack Forums
[Assembly] Intel x86 NASM Using fgets - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Assembly] Intel x86 NASM Using fgets (/showthread.php?tid=4860009)



Intel x86 NASM Using fgets - splutter - Jun 11, 2015

So I have a piece of code and I'm attempting to scan in a string with a call to fgets. It's not reading anything in and the return value is NULL. May someone tell me how I could check my error value or teach me how I should approach this?

My code goes something like this:

Code
...
push DWORD  0x0      ; STDIN is 0
push BYTE   0xA      ; Max size is 10
push DWORD  buf      ; load address of buffer for input
call _fgets
add  esp    0xC      ; reset stack pointer
cmp  eax,   0x0      ; check return value in eax
jz   err      ; jump to error address if NULL
...



* - Only3 - Jun 11, 2015

char fgets ( char str, int num, FILE * stream ); http://www.cplusplus.com/reference/cstdio/fgets
push stream
push num
push str
call fgets


* - Mr. Dr. Prof. Patrick - Jun 11, 2015

STDIN is not 0, STDIN is a maco and there is a function you can use to find the address of STDIN

Code
push eax
call ___iob_func      ; returns an array containing addresses of Files like stdio, stdout
push eax            ; STDIN is the first in the array that is returned giving it postion 0
push BYTE   0xA      ; Max size is 10
push DWORD  buf      ; load address of buffer for input
call _fgets



* - Masm4ever - Jun 12, 2015

Shorter (but you need a modern assembler):
invoke crt_fgets, addr buffer, sizeof buffer, rv(crt___p__iob)