Author Topic: cant move return value of eax in to a buffer?  (Read 3746 times)

ltech19

  • Guest
cant move return value of eax in to a buffer?
« on: July 15, 2012, 12:55:06 AM »
Code: [Select]
.data

myName db 255 dup (0)

.code

start:

Invoke GetCommandLine
mov myName, eax

end start

why does this not work? i cannot mov eax in to MyName buffer..

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: cant move return value of eax in to a buffer?
« Reply #1 on: July 15, 2012, 01:09:25 AM »
Won't work, you need a DWORD sized memory operand for EAX. What you "should" be returning is a POINTER to the address of the string buffer.

Now in the case of "GetCommandLine" it actually returns a POINTER to a buffer that you do NOT allocate, the system does that. What you can do is copy it to a buffer of your own once you know how long the command line is and allocate a buffer of that size. In this context you use dynamic memory like GlobalAlloc or one of the other allocation strategies.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: cant move return value of eax in to a buffer?
« Reply #2 on: July 15, 2012, 01:22:18 AM »
For testing.

Code: [Select]
include \masm32\include\masm32rt.inc

.data?
myName dd ?
FirstArg dd ?

.code
start: invoke GetCommandLine
mov myName, eax
MsgBox 0, myName, "The raw commandline:", MB_OK
sub esp, 400 ; create a buffer
mov FirstArg, esp ; store address of buffer
invoke GetCL, 1, esp ; Masm32 library function
MsgBox 0, FirstArg, "Arg1:", MB_OK
add esp, 400
exit
end start