News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

simple pointer question

Started by travism, November 07, 2008, 02:33:08 PM

Previous topic - Next topic

travism

So I have the code:


bmpOptions    db 17 dup  (?)
...
mov bmpOptions,dword ptr [ebx+4*1]


obviously it doesnt work. I always get stuck when I need to move a dword ptr .. into a variable... i _somehow_ need to get the dword ptr into bmpOptions... Yet im stuck and can't figure it out...thanks for your help everyone.

Neil

This will work :-

    mov eax,dword ptr [edi+4*1]
    mov bmpOptions,eax

But you have to re define your data as dd if you want to store eax directly, if you don't want to do that then you can do it this way :-

    lea edi, bmpOptions
    stosd

travism

Thanks neil!

Now, Its dying for some reason, Ther IS 3 arguments being sent so I don't see why this wouldnt work

      invoke crt___getmainargs,addr nArgc,addr bArgv,addr bEnv,NULL,NULL
      invoke Main,nArgc,bArgv
      exit
     
; --------------------------------------------------------
; main program procedure
; --------------------------------------------------------
Main  PROC mArgc:DWORD,mArgv:DWORD
      mov ebx,mArgv
     
      ; --------------------------------------------------
      ; store the options
          mov eax,dword ptr [ebx+3*1]
          mov bmpOptions,eax
          mov eax,dword ptr [ebx+3*2]
          mov bmpFile,eax
          mov eax,dword ptr [ebx+3*3]
          mov bmpAsciiFile,eax
         
          invoke Write2Console,addr bmpAsciiFile
Main endp


Theres 3 args... and I only need 1 2 and 3 not hte name of the program.. Ive tried adding mArgc to check for how many parameters but that doesnt work? am I passing it wrong? I also thought that __getmainargs was in msvcrt but only crt___getmainargs is :\

travism

tried using __getmainargs but it says its not in msvcrt?? anyone?

GregL

#4
travism,

Here is a test piece I wrote for using __getmainargs. Maybe it will help.


.686
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

INCLUDE windows.inc

INCLUDE kernel32.inc
INCLUDE msvcrt.inc
INCLUDE masm32.inc

INCLUDE macros.asm

INCLUDELIB kernel32.lib
INCLUDELIB msvcrt.lib
INCLUDELIB masm32.lib

main PROTO

.CONST

    szCrLf  BYTE 13,10,0

.DATA

    dwArgC           DWORD       0
    pArgV            PDWORD      0
    pEnv             PDWORD      0
    bWildCard        DWORD       0
    StartInfo        STARTUPINFO <>
    pszItem          PBYTE       0

.CODE

start:

    call main
    inkey SADD(13,10,"Press any key to exit ... ")
    INVOKE ExitProcess, 0

main PROC

    mov bWildCard, FALSE
    INVOKE crt___getmainargs, ADDR dwArgC, ADDR pArgV, ADDR pEnv, bWildCard, ADDR StartInfo
    ; pArgV is a pointer to an array of pointers.
    ;  Each pointer points to an individual command-line argument.
    ; dwArgC is the number of individual command-line arguments.
    ; pEnv is a pointer to an array of pointers.
    ;  Each pointer points to an individual environment variable.
    ;  A NULL pointer signals the end of the array of pointers.
    ; bWildCard is a boolean value controlling whether the command line
    ;   will be globbed (e.g. *.* expanded to be all files in the startup
    ;   directory).
    ; StartInfo is a pointer to a STARTUPINFO structure.

    ; Display command-line arguments
    INVOKE StdOut, SADD("Command-Line Arguments",13,10)
    xor ecx, ecx
  @@:
    push ecx
    mov eax, pArgV
    mov edx, [eax+ecx*4]
    mov pszItem, edx
    INVOKE StdOut, SADD(" ")
    INVOKE StdOut, pszItem
    INVOKE StdOut, ADDR szCrLf
    pop ecx
    inc ecx
    cmp ecx, dwArgC
    jb @B

    INVOKE StdOut, ADDR szCrLf

    ; Display environment variables
    INVOKE StdOut, SADD("Environment Variables",13,10)
    xor ecx, ecx
  @@:
    push ecx
    mov eax, pEnv
    mov edx, [eax+ecx*4]
    mov pszItem, edx
    .IF edx != 0
        INVOKE StdOut, SADD(" ")
        INVOKE StdOut, pszItem
        INVOKE StdOut, ADDR szCrLf
        pop ecx
        inc ecx
        jmp @B
    .ELSE
        pop ecx
    .ENDIF

    ret

main ENDP

END start


[edit] Removed unused variables.

travism

ok well i tried that and i still cant get it to print anything this currently prints "`<3" ... So I dont know I really dont want to give up and make it a gui :\

Main PROC
      ; --------------------------------------------------
      ; Need to get the cmdline arguments now
      mov bWCard,FALSE
      invoke crt___getmainargs,addr nArgc,addr bArgv,addr bEnv,bWCard,addr stInfo
     
; --------------------------------------------------------
; main program procedure
; --------------------------------------------------------
      mov eax,bArgv
     
      ; --------------------------------------------------
      ; store the option
          mov edi,[eax+1*4]
          mov bmpAsciiFile,edi
       
          invoke StdOut,addr bmpAsciiFile
          exit
Main endp

MichaelW

eschew obfuscation

travism

yeah his does for me too... but i still can't figure out why mine isn't working :(

MichaelW

bmpAsciiFile is a pointer to the first command line argument, so for the code to work it should be passed directly to StdOut, and there must be at least one command line argument. If you used more descriptive names it would help you to avoid the first problem. I have no way of knowing if these are the only problems, because you did not provide all of the relevant code.
eschew obfuscation

travism


.data
bmpAsciiFile   dd 100 dup (?)        ; this needs to recieve a file name

.....

.code
start:
      ; --------------------------------------------------
      ; Need to get the cmdline arguments now
      mov bWCard,FALSE
      invoke crt___getmainargs,addr nArgc,addr bArgv,addr bEnv,bWCard,addr stInfo
; --------------------------------------------------------
; main program procedure
; --------------------------------------------------------
      mov edx,bArgv                 
      ; --------------------------------------------------
      ; store the option
          mov edi,[edx+4*1]
          mov bmpAsciiFile,edi
       
          invoke StdOut,addr bmpAsciiFile
          exit
end start


this currently prints:
C:\>bmp2ascii.exe meh
→<3
C:\>

So thats pretty much all the code


Vortex


    mov     bWCard,FALSE
    invoke  crt___getmainargs,addr nArgc,addr bArgv,addr bEnv,bWCard,addr stInfo

    mov     edx,bArgv                 

    mov     edi,[edx]   ; [edx] points the first command-line parameter
                        ; [edx+4] points the second command-line parameter
                        ; [edx+8] the third etc.
       
    invoke  StdOut,edi
    invoke  ExitProcess,0

travism

thank you vortex!

working code finally!! :)

start:
     ; --------------------------------------------------
     ; Need to get the cmdline arguments now
     mov bWCard,FALSE
     invoke crt___getmainargs,addr nArgc,addr bArgv,addr bEnv,bWCard,addr stInfo
; --------------------------------------------------------
; main program procedure
; --------------------------------------------------------
     mov esi,bArgv
     ; --------------------------------------------------
     ; store the option
         add esi,4
         invoke wsprintf,addr bmpAsciiFile,addr blah, dword ptr [esi]
         invoke StdOut,addr bmpAsciiFile
         exit
end start

travism

So if i may ask so i know exactly why the code is working properly, why does it go in increments of 4?

Vortex

It's easy. All the command-line arguments are stored in an array of DWORD pointers.

mov esi,bArgv

Here, esi points the beginning of the array. Every member of this array is a DWORD value pointing a NULL terminated sting, a command-line argument.

[esi+4*0] -> first argument : your application's name
[esi+4*1] -> second argument
[esi+4*2] -> third argument
.
.
[esi+4*n]

BlackVortex

For your other question about moving pointers to pointers : I sometimes use the "m2m" macro of masm32 (you need to "include" the macros.asm file)

It makes my code more readable and it uses the stack, so it doesn't destroy the eax register.