ali
ali

Reputation: 11045

Read command line in assembly

First time I play with ds, si and strings related instructions in assembly. I am trying to read the command line arguments char by char and this is how my code looks like now:

GetCommandLine:
    push                    ebp
    mov                     ebp,    esp
    push edi
    push esi
    call                    GetCommandLineW
    mov edi, eax
    mov esi, ebp
    Parse:
        lodsw
        cmp                 ax,     0dh ; until return is found
        jne                 Parse
    pop esi
    pop edi
    pop ebp
    ret

So, the GetCommandLineW function returns a correct pointer to the string. The problem is that the Parse section loops forever and I can't see the AX register being loaded with the correct next byte from the string. I think the EDI:ESI is not correctly loaded or something

Upvotes: 0

Views: 1802

Answers (2)

Devolus
Devolus

Reputation: 22074

Why do you think that 0x0d is used in the commandline? A normal C string is returned, so you should look for a 0 byte.

Upvotes: 1

Drew McGowen
Drew McGowen

Reputation: 11706

esi and edi are different pointers. ebp is used for saving the old stack pointer, and for saving/loading local variables. GetCommandLineW will return the pointer in eax, which you should then put into esi. Since you're only using lodsw (and not stos*), you don't need to touch edi.

Upvotes: 1

Related Questions