News:

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

managing exceptions ... register mess?

Started by indiocolifa, September 23, 2005, 10:12:27 PM

Previous topic - Next topic

indiocolifa

I've implemented for my 6502 monitor , a byte-input function, which keeps asking bytes until you enter the dot (.) character.
The problem is that this code apparently works perfect (raises exceptions properly) but after using it, other functions of my program starts to malfunction (seems a register messing problem).

This is the fundamental part of the code:

push    (eax);
    push    (ebx);
    push    (ecx);
    push    (edx);

    mov     (stat,edx);
    xor     (ecx,ecx);
// keep asking bytes until "enter" or reaching $FFFF
   
    mov     (startaddr, cx);
    mov     (memptr, ebx);
   
    REPEAT
           push (ecx);     // preserve in case of exception
           push (ebx);
           stdout.put (cx,":");
           TRY
                stdin.a_gets();
                mov         (eax, hexbyte);
                if (str.eq(hexbyte,".")) THEN
                    strfree (hexbyte);
                    jmp exitcmd;
                ENDIF;
               
                conv.strTob (hexbyte,0);        // value in AL                         
               
           EXCEPTION (ex.ConversionError);
                stdout.put ("Error - invalid value. Reenter or type '.' to finish.",nl);
                jmp lpop;
           EXCEPTION (ex.ValueOutOfRange);
                stdout.put ("Error - only hex byte values (00-FF) are valid.",nl);     
                jmp lpop;           
               
           ENDTRY;
           pop (ebx);
           pop (ecx);
                 
           
           // write value to 6502 memory
           mov (al, [ebx+ecx]);       
           
           inc (cx);
           jmp again;
           
    lpop:
            pop (ebx);
            pop (ecx);
    again:
            strfree(hexbyte);
   
    UNTIL (ecx > $FFFF);   
               
   
exitcmd:
    // keep monitor address
    mov (stat, edx);
    mov (cx, MON_ADDRPTR);
   
    pop (edx);
    pop (ecx);
    pop (ebx);
    pop (eax);



Some help plz?

thank you :U

indiocolifa

I've solved it, but anyway

Some recomendations on using exceptions on the above code?

thkz.

Sevag.K

You say you solved it, but I don't know if you updated the posted code.  The comments below are based on the code you have posted (they may no longer apply if you fixed the program).

You should not jump out of a TRY/ENDTRY structure.  It will leave garbage on the stack and in your program , the stack will be misalligned because you push ecx and ebx before the TRY/ENDTRY and then jump to exitcmd which pops only what you pushed at the beginning of the program leaving the wrong values in the registers.

Another point: after the exceptions, you jump to lpop, this is unnecessary as execution will drop down to the ENDTRY and you already pop the registers there.

The code could be written more efficiently using stdin.getb(), but I did some tests and it seems that stdin.peekc() is not working welll, and peekc  is essential to get it working properly.


Randall Hyde

Quote from: indiocolifa on September 23, 2005, 10:12:27 PM
I've implemented for my 6502 monitor , a byte-input function, which keeps asking bytes until you enter the dot (.) character.
The problem is that this code apparently works perfect (raises exceptions properly) but after using it, other functions of my program starts to malfunction (seems a register messing problem).


When an exception occurs, the only registers whose values you can count on are ESP, EBP, and EAX (which contains the exception code). All the other register values may be scrambled, based on their values when the exception occurred. If you need to restore registers to their values in the try..endtry block prior to the exception, you need to save the (important) register values in local variables and restore them from those variables in your exception section.
Cheers,
Randy Hyde