News:

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

Endless loop problem

Started by luks91, November 12, 2011, 10:50:26 AM

Previous topic - Next topic

luks91

Hello everyone. I've been working a while on some project which uses command line arguments. The thing is that a loop 'keyRetrieve' is endless even if the cx is set to proper value (iv printed the cx value before and it was proper). So instead of finishing after having read all the arguments, the 'argumentsRead' variable, which represents ammount of read symbols goes over 47 (maximum) so errorArgument is called. It's meant to read a key from command line just after the 'modyfier', so the whole thing goes like:
>NAME.EXE 0 3a:3a:3a:3a:3a:3a:3a:3a:3a:3a:3a:3a:3a:3a:3a:3a

argumentsInitialize proc            ;procedura przeksztalca dane wejsciowe wczytane z linii komend na modyfikator oraz klucz jednoczesnie eliminujac biale znaki w kluczu
   
    push ax
    push bx
   
    mov si,0                        ;wyzerowanie rejestru indeksowego si, ktory bedzie uzywany do
                                    ;kolejnego 'wyciagania' argumentow z linii komend
    modificatorRetrieve: 
                                         ;82h to miejsce po nazwie pliku + pierwszej spacji                 
        cmp byte ptr es:[81h+si],' '     ;sprawdz czy spacja
        je foundSpace               ;jesli tak ot wywolaj blok, ktory przesuwa sie o znak dalej   
       
        jmp getModifier           
                   
        foundSpace:                 ;znaleziono bialy znak
           inc si     
           jmp modificatorRetrieve
       
        getModifier:                ;pobierz modyfikator     
            mov al,es:[81h+si]
            mov [_optionModyfier],al
   
   
    inc si
   

   
    mov ax, seg _data       
    mov ds, ax
    mov dx, offset _data
    mov di,dx                      ;od tej pory oznaczenie ds:[di] to di-ty element tablicy _data
   
    mov bx,offset _argumentsRead      ; w celu inkrementacji ilosc 'wydobytych' znakow
   
    xor ax,ax
   
    mov ax,es:[80h]
    sub ax,si   
    sub ax,1
    mov cx,ax                      ;ilosc znakow minus juz przetworzone LICZNIK PETLI!   
   
    keyRetrieve:                   ;_ _ : _ _ - sposob zapisu bitow (jezeli 1 znak to poprzedzony 0)   
        cmp byte ptr es:[si+81h],' '
        je foundNextSpace
       
        jmp fillCell
       
        foundNextSpace:            ;znaleziono bialy znak
            inc si
            jmp nextElement
       
        fillCell:                  ;wypelnij kolejna komorke tablicy _data
            call testMe   
            xor ax,ax 
            mov al,es:[si+82h]
            mov ds:[di],al   
            inc di

           
            add byte ptr [_argumentsRead],1
           
            push ax       
        mov ah, 2 
        mov dl,[_argumentsRead]
        int 21h
        pop ax
         
            cmp byte ptr [_argumentsRead],47
            jna reloopIt
           
            call errorArgument
           
            reloopIt:
                inc si   
         nextElement:       
            loop keyRetrieve
             
    pop bx
    pop ax
ret   
argumentsInitialize endp

thanks for help,
Lucas

ps: forgive me polish comments, lol

dedndave

the length of the command-line tail is at PSP:80 hex
although, you can parse it without the length, as it's always terminated with a carriage return (0Dh)
the max length, including the terminator, will be 127 bytes, of course

the testMe and errorArgument routines are missing from the post

    mov ax,es:[80h]
    sub ax,si   
    sub ax,1
    mov cx,ax                      ;ilosc znakow minus juz przetworzone LICZNIK PETLI!


i would load CX first thing
then, subtract from the count as you parse out spaces
i.e. use a LOOP for that, as well

in fact, i would do things altogether differently - lol
        mov     ah,0
        mov     si,80h
        lodsb
        xchg    ax,cx        ;now, CX has the count, SI points to the first char


noCforMe posted a little state machine for parsing that may be of interest
http://www.masm32.com/board/index.php?topic=17682.msg148992#msg148992
it's 32-bit - but gives you the general idea
it depends on the parser requirements - it may or may not be advantageous to use a state machine

luks91

you're right. iv thought bout it quite deeper and found out a solution :)

Thank you very much!

//Lucas

dedndave

oh - and check to see if the tail length is zero before you start counting   :P