简体   繁体   中英

Assembly (MASM) running program with byte value parameters?

I am writing an assembly program that will require a String and an array of bytes as run time arguments. (The bytes are not fixed to character values and are all unsigned values between 0and 255)

Example execution command:

     Program.exe "A 32 byte array (fixed size)" "A Byte array of arbitrary length, (greater than 0)"

How do I get the arguments in assembly? also slightly irrelevant from the question, the array of bytes can be any length, will there be a problem if the length of the array is larger than a certain size?. the arguments will be loaded into memory at run time and changed as the program executes then it returns the array in its new state. (I am using assembly to speed up processing times)

For getting the arguments in a list like argv in C you need a shell32.lib which is not shipped with Irvine's book (as far as I can see in the free samples). You can get an appropriate shell32.lib here: http://www.masm32.com . When you have installed Masm32 you can play with my example:

INCLUDE Irvine32.inc

IncludeLib C:\masm32\lib\shell32.lib
GetCommandLineW PROTO STDCALL
CommandLineToArgvW PROTO STDCALL, :DWORD, :DWORD
WideCharToMultiByte PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD

.DATA
    szArglist   dd ?
    nArgs       dd ?
    arg         db 8192 dup (?) ; http://blogs.msdn.com/b/oldnewthing/archive/2003/12/10/56028.aspx

.CODE
main PROC

    invoke GetCommandLineW
    invoke CommandLineToArgvW, eax, OFFSET nArgs
    test eax, eax
    jz @@exit

    mov [szArglist], eax        ; Store the result of CommandLineToArgvW for LocalFree
    mov esi, [eax]              ; esi: *szArglist

    @@:
    invoke WideCharToMultiByte, 0,0,esi,-1,OFFSET arg,SIZEOF arg,0,0
    mov edx, OFFSET arg
    call   writeString          ; Write string to console (Irvine)
    call   CRLF                 ; New line on console (Irvine)

    shl eax, 1                  ; Result from WideCharToMultiByte multiplied by 2 due to Unicode
    add esi, eax                ; *szArglist += eax
    sub dword ptr [nArgs], 1    ; Further arguments in the list?
    jnz @B                      ; yes -> once more

    invoke LocalFree, dword ptr [szArglist] ; Free the memory occupied by CommandLineToArgvW

    @@exit:
    exit                        ; Irvine's exit

main ENDP

END main

I suggest to separate the elements of the array by spaces:

Program.exe "A 32 byte array (fixed size)" 1 2 3 4 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM