News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Firse code (in 20+ years)

Started by drifter, February 25, 2013, 01:25:14 PM

Previous topic - Next topic

drifter

I spent an embarassing amount of time getting this to work, but I was learning along the way, so guess that's something.  All it does is diplay the command line and any arguments.  It doesn't seem to handle unmatched quotes very well, but I'm not sure if that's my fault or something wrong with getCL.  Anyway, I know it isn't much, but I'm looking forward to more challenging problems.  If there's anything I've done wrong or could do beter, I'd appreciate the advice.  (I do tend to get carried away with my naming conventions).


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                     Build this console app with
                  "MAKEIT.BAT" on the PROJECT menu.
        ----------------------------------------------------- *

    ThatsAGoodArgument      EQU 1
    YourArgumentDoesntExist EQU 2
    UnmatchedQuotes         EQU 3
    EmptyQuotes             EQU 4
    True                    EQU 1
    False                   EQU 0
    CRLF                    EQU 13,10
    TWOCRLF                 EQU 13,10,13,10
       
    Init     PROTO
    Fini     PROTO
    TheJudge PROTO   
    TheClerk PROTO :DWORD,:DWORD

    .data?
      argscore    dd ?         
      argpoint    dd ?
      argspace    db 128 dup (?)
      theverdict  dd ?
      YouGoToJail dd ?
     
    .data
      ArgNumber db "Arg number: ",0
      ArgValue  db "Arg value : ",0
      ArgSum    db "Arg count : ",0
      OddQuotes db "Non-matching quotation marks",0
      NilQuotes db "Empty quotation marks",0
      UnDefErr  db "Your problem is undefined...",0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
   
    invoke Init
    call   main
    invoke Fini
     
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    mov argscore,0
    mov argpoint,ptr$(argspace)
       
    .repeat
   
      invoke GetCL,argscore,argpoint      ; both GetCL and getcl_ex
      ; invoke getcl_ex,argscore,argpoint ; seem to work
     
      mov theverdict,eax                  ; save eax from being
      mov argpoint,trim$(argpoint)        ;  trashed
     
      invoke TheJudge
      mov eax,theverdict                  ; restore my eax

    .until eax == YourArgumentDoesntExist || YouGoToJail
   
    print OFFSET ArgSum
    print ustr$(argscore),13,10
   
    return 0
   
main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

Init proc   

    ; cls
    print " ",13,10

    ret

Init endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

TheJudge proc

   switch theverdict
   
     case     ThatsAGoodArgument               ; after getCL
       invoke TheClerk,argpoint,False          ;  eax = 1
       
     case     YourArgumentDoesntExist          ; after getCL
       mov    YouGoToJail,True                 ;  eax = 2
       
     case     UnmatchedQuotes                  ; after getCL
       invoke TheClerk,OFFSET OddQuotes,True   ;  eax = 3
       
     case     EmptyQuotes                      ; after getCL
       invoke TheClerk,OFFSET NilQuotes,True   ;  eax = 4
       
     default
       print  OFFSET UnDefErr,TWOCRLF          ; this should
       mov    YouGoToJail,True                 ;  never happen...
       
    endsw
   
  ret
   
TheJudge endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

TheClerk proc ArgStringPtr:DWORD,Verdict:DWORD

    print OFFSET ArgNumber       ; display argument number
    print ustr$(argscore),CRLF
   
    print OFFSET ArgValue        ; display argument
    print ArgStringPtr,TWOCRLF
   
    push eax
    mov eax,Verdict
    mov YouGoToJail,eax
    pop eax
    inc argscore                 ; increment argument count

  ret

TheClerk endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

Fini proc

    ; inkey

    ret

Fini endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

dedndave

the GetCommandLine function behaviour is a little strange
try this version
notice the difference when you click on the EXE in explorer and when you open the command window and type
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  GetCommandLine
        print   eax,13,10
        inkey
        exit

_main   ENDP

;###############################################################################################

        END     _main


dedndave

here is a little program i wrote a while back that shows info on a file
it has a nice little command line parser in it
http://masm32.com/board/index.php?topic=1142.msg11070#msg11070

drifter

Today at 01:46:43 PM dedndave wrote:
Quotenotice the difference when you click on the EXE in explorer and when you open the command window and type

I'll have to try GetCommandLine it seems to work better.  I remember Windows used to allow you to add command line arguments in the properties dialoge for the file - but I installed Windows 8 awhile back and it doesn't seem to allow it.  So when I click from windows, it will only display arg1.

Today at 01:46:43 PM deendave wrote:
Quotehere is a little program i wrote a while back that shows info on a file

Thanks!  I'll probably start playing with some of those routines next.  It would be nice to be able to check filenames and timestamps to verify things are what you're expecting them to be.

I've dowloaded the PE file specification from Microsoft, but need to get it printed out so I can study it.  Does that explain the file layout on the disk as well as when it's loaded into memory?

dedndave

if you want to see how the GetCL routine works, \masm32\m32lib\getcl.asm

if you want to add a command-line parm, create a shortcut to the EXE, then, modify the shortcut
you'll find this step necessary for testing, sometimes - things like start minimized, etc

to see the full results of what i am talking about, put the EXE in a folder where the path contains spaces
(like C:\Documents and Settings)
that causes the EXE name to be surrounded by double-quotes
if you open a command window, and change directory to the folder where the EXE is, that won't happen

the PE spec mostly pertains to the file format
you're kind of winging it when it comes to how things are loaded in memory
there is some reading on MSDN about that, but i don't remember where

hutch--

Here is a simple algo to test if you have matching quotes or not.



IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    are_quotes_matching PROTO :DWORD

    .data
      txt1 db "This is a test",0
      txt2 db "Bad test",34,0
      txt3 db 34,"OK test",34,0
      txt4 db 34,"Bad test",34,34,0


    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    invoke are_quotes_matching,OFFSET txt1
    print ustr$(eax),13,10

    invoke are_quotes_matching,OFFSET txt2
    print ustr$(eax),13,10

    invoke are_quotes_matching,OFFSET txt3
    print ustr$(eax),13,10

    invoke are_quotes_matching,OFFSET txt4
    print ustr$(eax),13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

  ; only uses EAX and EDX, no preservations needed.

are_quotes_matching proc ptxt:DWORD

  ; -------------------------------------
  ; return zero on non matching quotes
  ; return non zero on none or two quotes
  ; -------------------------------------

    mov eax, ptxt       ; load the string address into EAX
    xor edx, edx        ; zero EDX as counter
    sub eax, 1

  @@:
    add eax, 1
    cmp BYTE PTR [eax], 0       ; test for zero terminator
    je @F
    cmp BYTE PTR [eax], 34      ; text for quote character
    jne @B
    add edx, 1
    jmp @B

  @@:
    .if edx == 0
      mov eax, 1                ; no quotes therefore matching
      ret
    .elseif edx == 2
      mov eax, 1                ; 2 quotes therefore matching
      ret
    .else
      xor eax, eax              ; return zero on anything else
      ret
    .endif

are_quotes_matching endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

drifter

Today at 02:43:32 PM dedndave wrote:
Quoteto see the full results of what i am talking about, put the EXE in a folder where the path contains spaces
(like C:\Documents and Settings)
that causes the EXE name to be surrounded by double-quotes
if you open a command window, and change directory to the folder where the EXE is, that won't happen

OK I see.  I never noticed that before.  Is there a reason?  I could see why if the .exe filename had spaces, but it doesn't.

From now on I'll try not to depend on the pre-built macros so much and start coding my own.  I've been looking at the .asm file IdaPro generated - it's really interesting to see how the assmebler put it together.  I need to get more familiar with how the debugger works also, so I can step through some of these things.

dedndave

it's a quirk with the OS
the C compilers handle this stuff for you and offer variables that have the command line split up for you
i forget what they're named - arg1, arg2 - something like that

about a year ago, i was playing with this stuff
i tried running from a batch file, from explorer, from the console, from a shortcut,
from CreateProcess, with/without spaces, with/without double-quotes, etc
and - i don't remember what the results were   :biggrin:
i just know - you may get different things, so write your parser accordingly
also, you may not always have matched quotes
C:\> "\masm32\bin\test" "command line parm
in the parm, i didn't close it - it should still interpret it as
command line parm

drifter

Today at 03:02:51 PM hutch wrote:
QuoteHere is a simple algo to test if you have matching quotes or not.

Thanks!  I'll study that and add it to my collection.  I've also printed out the getcl.asm - I'll try to figure out why it works the way it does.  Any odd combination of quotes or spaces seems to crash it.