'How to return argc in assembly

I'm trying to return argc in assembly but the program returns 0. I'm using GNU 2.28 on Windows 10 !

There is my code:

.section .text

.globl main
main:
    pushl %ebp
    movl %esp, %ebp

    movl 8(%ebp), %eax

    movl %ebp, %esp
    popl %ebp
    ret

.globl _start
_start:
    pushl 0(%esp)
    call main
    addl 4, %esp
    
    movl %eax, %ebx
    movl $1, %eax
    int $0x80

I'm using the following commands to generate my exe:

as --32 test.s -o test.obj
ld -m i386pe test -o test.exe


Solution 1:[1]

MS Windows doesn't provide pointers to separated arguments pushed on machine stack, as GNU Linux does. When an executable program is launched from the terminal window with command e.g.

 as --32 test.s -o test.obj

you need to retrieve the command line using API function from "kernel32.dll" and parse it yourself.

Invokation of API function GetCommandLine returns pointer to a string which contains the entire command, starting with the executable name (as in this example), and ending with NUL character. When the program is launched from Explorer, the executable name may be completed to form the full path to executable, for instance "C:\Program Files\GNU utilities\as.exe".
Now you should parse the string character by character, test each one if it's argument-separator (white space), taking into account if you're inside double quotes, and count argument numbers.

You can find examples in my macrolibraries for Windows at GetArgCount or for Linux at GetArgCount.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitsoft