HLA try...endtry with for loop.... 
Author Message
 HLA try...endtry with for loop....

Hello,
I was wondering if someone out here can help me out of a jam I am in.
The program I am writing is in HLA (High Level Assembly - by Randy
Hyde). Here is the basic problem that I am running into.

I have try....exception....endtry statement nested inside of a for
loop. Now from what I understand ( with very limited knowledge of this
language) is that this statement pushes and pops information from the
stack and is not a good idea to use it inside of a loop because of this
reason. ( I am not enritely sure about this ).

Here is what happens

I am asking  user to input 'x' number of integers through a
for....endfor loop. Inside this for loop there is
try...exception...endtry statement. Every time a wrong input is made
the try...exception...endtry statement catches it. There is only one
problem. After an error the value in the memory where the initial
integer is supposed to store contains some value that does not match
any thing entered after the error handeling.

I would be really glad and thankful if any one of you can point me in
the right direction.

Hope I provided enough info.

Thank you very much.

Yosh

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 03 Aug 2002 03:00:00 GMT  
 HLA try...endtry with for loop....

Whenever the program raises an exception, it
does not complete the execution of any code
between the point the exception occurs and
the exception handler.  This includes storing
any values into the destination memory locations.

The standard code for reading numeric data
from the user looks like this:

forever

    try

        stdout.put( "Enter an integer: " );
        stdin.get( intVar );
        unprotected break;

    exception( ex.ConversionError )

        stdout.put( "Input contained illegal characters" );

    exception( ex.ValueOutOfRange )

        stdout.put( "Value is out of range" );

    endtry;
    stdout.put( ", please reenter" nl );

endfor;

If the code gets to the "unprotected break;" statement, then
there was no error.  This statement bails out of the surrounding
forever..endfor.  OTOH, if an exception occurs, then the forever
loop forces the user to re-enter the data.

In your case, you'd need to slap your FOR loop around this whole
thing in order to read "n" integers from the user, e.g.,

for( mov( 0, ebx); ebx < n; inc( ebx ) do

    forever

        try

            stdout.put( "Enter an integer: " );
            stdin.geti32();  // Read integer into EAX
            unprotected break;

        exception( ex.ConversionError )

            stdout.put( "Input contained illegal characters" );

        exception( ex.ValueOutOfRange )

            stdout.put( "Value is out of range" );

        endtry;
        stdout.put( ", please reenter" nl );

    endfor;
    mov( eax, Array[ ebx*4] );

endfor;

Since reading an error-free integer is such a common operation,
I'd turn that into a procedure, e.g.,

procedure ReadI32( prompt:string ); nodisplay;
begin ReadI32;

    forever

        try

            stdout.put( prompt );
            stdin.geti32();  // Read integer into EAX
            unprotected break;

        exception( ex.ConversionError )

            stdout.put( "Input contained illegal characters" );

        exception( ex.ValueOutOfRange )

            stdout.put( "Value is out of range" );

        endtry;
        stdout.put( ", please reenter" nl );

    endfor;

end ReadI32;

Cheers,
Randy Hyde

Quote:
> Hello,
> I was wondering if someone out here can help me out of a jam I am in.
> The program I am writing is in HLA (High Level Assembly - by Randy
> Hyde). Here is the basic problem that I am running into.

> I have try....exception....endtry statement nested inside of a for
> loop. Now from what I understand ( with very limited knowledge of this
> language) is that this statement pushes and pops information from the
> stack and is not a good idea to use it inside of a loop because of this
> reason. ( I am not enritely sure about this ).

> Here is what happens

> I am asking  user to input 'x' number of integers through a
> for....endfor loop. Inside this for loop there is
> try...exception...endtry statement. Every time a wrong input is made
> the try...exception...endtry statement catches it. There is only one
> problem. After an error the value in the memory where the initial
> integer is supposed to store contains some value that does not match
> any thing entered after the error handeling.

> I would be really glad and thankful if any one of you can point me in
> the right direction.

> Hope I provided enough info.

> Thank you very much.

> Yosh

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sat, 03 Aug 2002 03:00:00 GMT  
 HLA try...endtry with for loop....

Quote:

>     This is just my personal opinion, so please don't take it as gospel.
> Exception handling should not be used in circumstances where the "error"
can
> be prevented by the program.  The HDD crashing can't be prevented by the
> program, whereas what the user types while under the control of the
program
> can.
>     A correct way to set up the sort of loop you're looking for is...
> i = 0
> while (i < the_number_of_values_to_get)
>   {
>   prompt the user to provide input (i + 1)
>   do
>     {
>     get user input
>     validate user input
>     }
>   while (the user input is invalid)
>   store the result in input array at position i
>   increment i
>   }

Actually, I added exception handling to the UCR Standard Library (v2.0)
and then to HLA because I was sick and tired of having to validate user
input all the time :-)

HLA's try..endtry statement pushes four dwords on the stack.  So there
are approximately 12 instructions of overhead associated with the
TRY..ENDTRY statement, assuming no exceptions occur.  This is often
far more efficient than trying to check everything yourself.

Of course, if an exception occurs, there is a tremendous amount of
overhead incurred, so one should never use the TRY..ENDTRY statement
as a normal control structure (e.g., to simulate a switch/case statement).

Since exceptions are usually rare, the overhead associated with
the handling of an exception is of little consequence.

BTW, HLA's exception handling facilities (mostly) use the Windows
Structured Exception Handling mechanism.  (I say mostly because
HLA and HLA Standard Library exceptions bypass Windows and
calls the HLA exception handler directly while hardware and Windows
related exceptions go through Windows to the HLA exception
handler).
Randy Hyde



Sat, 03 Aug 2002 03:00:00 GMT  
 HLA try...endtry with for loop....

Hi Yosh,

Quote:
> Hello,
> I was wondering if someone out here can help me out of a jam I am in.
> The program I am writing is in HLA (High Level Assembly - by Randy
> Hyde). Here is the basic problem that I am running into.

> I have try....exception....endtry statement nested inside of a for
> loop. Now from what I understand ( with very limited knowledge of this
> language) is that this statement pushes and pops information from the
> stack and is not a good idea to use it inside of a loop because of this
> reason. ( I am not enritely sure about this ).

    Without knowing how Mr. Hyde has implimented exception handling in HLA,
I can't give you a useful answer.  Though, having had a little experience
with exception handling mechanisms in other languages, I can see how
problems could arise.

Quote:
> Here is what happens

> I am asking  user to input 'x' number of integers through a
> for....endfor loop. Inside this for loop there is
> try...exception...endtry statement. Every time a wrong input is made
> the try...exception...endtry statement catches it. There is only one
> problem. After an error the value in the memory where the initial
> integer is supposed to store contains some value that does not match
> any thing entered after the error handeling.

    This is just my personal opinion, so please don't take it as gospel.
Exception handling should not be used in circumstances where the "error" can
be prevented by the program.  The HDD crashing can't be prevented by the
program, whereas what the user types while under the control of the program
can.
    A correct way to set up the sort of loop you're looking for is...
i = 0
while (i < the_number_of_values_to_get)
  {
  prompt the user to provide input (i + 1)
  do
    {
    get user input
    validate user input
    }
  while (the user input is invalid)
  store the result in input array at position i
  increment i
  }

    The "get user input" function should, at least, make sure the the user
can't type more characters than the input buffer can hold.  The "validate
user input" function should check whether the characters entered (if any)
correspond to a valid number.

Quote:
> I would be really glad and thankful if any one of you can point me in
> the right direction.

hope this helps,
-Brent
doomsday AT optusnet DOT com DOT au


Sun, 04 Aug 2002 03:00:00 GMT  
 HLA try...endtry with for loop....
The code I gave was incredibly stupid.
I'm try to explain that you're supposed to
save the registers around TRY..ENDTRY
and then I forgot to do this in my code post.
Here's the corrected example:

for( mov( 0, ebx); ebx < n; inc( ebx ) do

    push( ebx );
    forever

        try

            stdout.put( "Enter an integer: " );
            stdin.geti32();  // Read integer into EAX
            unprotected break;

        exception( ex.ConversionError )

            stdout.put( "Input contained illegal characters" );

        exception( ex.ValueOutOfRange )

            stdout.put( "Value is out of range" );

        endtry;
        stdout.put( ", please reenter" nl );

    endfor;
    pop( ebx );
    mov( eax, Array[ ebx*4] );

endfor;



Sun, 04 Aug 2002 03:00:00 GMT  
 HLA try...endtry with for loop....

[snip - some drivel which I wrote]

Quote:
> Actually, I added exception handling to the UCR Standard Library (v2.0)
> and then to HLA because I was sick and tired of having to validate user
> input all the time :-)

[snip]

<grin>
While working under DOS (I'm slowly becoming proficient in Windows assembly
now), I got sick of it too, so I sat down one day and wrote the mother of
all "user input" routines.  It had a parameter for the expected input type,
and it validated the input after each keypress, just to make sure!  'Twas
big, and (for assembly code) slow, but most of the time in any such routine
is spent waiting for the user, so it didn't really matter.

BTW, thanks for the explaination of HLA's exception handling.  As I'm not
working on a project ATM, I'll d/l it and have a look.

-Brent
doomsday AT optusnet DOT com DOT au



Mon, 05 Aug 2002 03:00:00 GMT  
 HLA try...endtry with for loop....


Quote:
> The code I gave was incredibly stupid.
> I'm try to explain that you're supposed to
> save the registers around TRY..ENDTRY
> and then I forgot to do this in my code post.
> Here's the corrected example:

> for( mov( 0, ebx); ebx < n; inc( ebx ) do

>     push( ebx );
>     forever

>         try

>             stdout.put( "Enter an integer: " );
>             stdin.geti32();  // Read integer into EAX
>             unprotected break;

>         exception( ex.ConversionError )

>             stdout.put( "Input contained illegal characters" );

>         exception( ex.ValueOutOfRange )

>             stdout.put( "Value is out of range" );

>         endtry;
>         stdout.put( ", please reenter" nl );

>     endfor;
>     pop( ebx );
>     mov( eax, Array[ ebx*4] );

> endfor;

Just wanted to thank you all for the inputs and explanations. The conde
works now with the suggestion from Randall Hyde.

Thanks

Yosh

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 05 Aug 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. HLA try...endtry with for loop....

2. try gfdg try kuiou try gsdfas try

3. Writing Compact HLA Programs (Taking Complete Control in HLA)

4. HLA v1.34->HLA v1.35

5. try: execpt:/ try: finally: behavior

6. break one loop and skip one iteration of outer loop

7. 2 files: a loop within a loop??

8. Problem with loop inside other loop

9. Impelement for loop and do while loop

10. using while loops within while loops?

11. Fast loop inside a slow loop?

12. Troubles using while loops and case loops

 

 
Powered by phpBB® Forum Software