Problem using LEAVE

This is a discussion on Problem using LEAVE within the Forth forums in Programming Languages category; I was working on a Project Euler problem and ran into a problem using LEAVE to exit a DO ... LOOP --------------------------- : ?prime ( n -- f ) \ check is number is prime dup 1 > \ only test when positive if 1 begin 1+ 2dup / 2dup > \ treat negative numbers, if 2drop dup 1 then \ zero, and one as non-prime over * 2 pick = until = else FALSE then ; : numprimes ( a b -- num ) \ find number of primes generated by n^2+an+b dup ?prime \ make sure b is ...

Go Back   Application Development Forum > Programming Languages > Forth

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-07-2008, 07:08 AM
cjmcc
Guest
 
Default Problem using LEAVE

I was working on a Project Euler problem and ran into a problem using
LEAVE to exit a DO ... LOOP

---------------------------
: ?prime ( n -- f ) \ check is number is prime
dup 1 > \ only test when positive
if 1 begin 1+ 2dup / 2dup > \ treat negative numbers,
if 2drop dup 1 then \ zero, and one as non-prime
over * 2 pick =
until =
else FALSE
then
;

: numprimes ( a b -- num ) \ find number of primes generated by
n^2+an+b
dup ?prime \ make sure b is prime (n=0)
if 1000 1 \ before testing n=1, 2, 3,
etc
do 2dup i rot over + * +
?prime not \ test if prime
if 2drop i leave then \ if not, then return count
loop \ else keep testing
else 2drop 0 \ NOTE - should terminate loop
early!
then
;
---------------------------
1 41 numprimes . \ should print 40
2 41 numprimes . \ should print 1

instead, I get a nasty EXCEPTION 0xC0000005 ACCESS VIOLATION
Error(9998).

Tracing the code (using debug in win32forth) shows everything working
as
expected until the program tries to exit the loop by executing LEAVE.

I've used LEAVE before, so any hints on what I've done wrong would be
appreciated.

Chris
Reply With Quote
  #2  
Old 08-07-2008, 01:04 PM
Brad Eckert
Guest
 
Default Re: Problem using LEAVE

On Aug 7, 4:08*am, cjmcc <cjmccorm...@gmail.com> wrote:
> instead, I get a nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION
> Error(9998).
>

It works okay on "Version: 6.10.05 Build: 2"
What version are you using?

-Brad
Reply With Quote
  #3  
Old 08-07-2008, 01:29 PM
Steve Graham
Guest
 
Default Re: Problem using LEAVE

On Aug 7, 5:08*am, cjmcc <cjmccorm...@gmail.com> wrote:
> I was working on a Project Euler problem and ran into a problem using
> LEAVE to exit a DO ... LOOP
>
> ---------------------------
> : ?prime *( n -- f ) *\ check is number is prime
> * dup 1 > * * * * * * * * * * * * * * * \only test when positive
> * if *1 begin * 1+ 2dup */ *2dup > * * *\ treat negative numbers,
> * * * * * if 2drop dup 1 then * * * * * \ zero, and one as non-prime
> * * * * * over * 2 pick =
> * * * * until *=
> * else *FALSE
> * then
> ;
>
> : numprimes ( a b -- num ) * \ find number of primes generated by
> n^2+an+b
> * dup ?prime * * * * * * * * * * * * * *\ make sure b is prime (n=0)
> * if * 1000 1 * * * * * * * * * * * * * \ before testing n=1, 2, 3,
> etc
> * * * *do *2dup *i rot over + * +
> * * * *?prime not * * * * * * * * * * * \ test if prime
> * * * *if *2drop i *leave *then * * * * \ if not, then return count
> * * * *loop * * * * * * * * * * * * * * \ else keep testing
> * else 2drop 0 * * * * * * * * * * * * *\ NOTE - should terminate loop
> early!
> * then
> ;
> ---------------------------
> 1 41 numprimes . * * \ should print 40
> 2 41 numprimes . * * \ should print 1
>
> instead, I get a nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION
> Error(9998).
>
> Tracing the code (using debug in win32forth) shows everything working
> as
> expected until the program tries to exit the loop by executing LEAVE.
>
> I've used LEAVE before, so any hints on what I've done wrong would be
> appreciated.
>
> Chris


I wonder if the line -- if 2drop i leave then -- leaves the stack
in a different state than it should have been left.


Steve
Reply With Quote
  #4  
Old 08-07-2008, 02:19 PM
George Hubert
Guest
 
Default Re: Problem using LEAVE

On Aug 7, 6:04*pm, Brad Eckert <nospaambr...@tinyboot.com> wrote:
> On Aug 7, 4:08*am, cjmcc <cjmccorm...@gmail.com> wrote:> instead, I geta nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION
> > Error(9998).

>
> It works okay on "Version: 6.10.05 Build: 2"
> What version are you using?
>
> -Brad


Are you sure? When I run it under the debugger I get the exception,
but not when run normally. Since it reports the word TRACE as
the faulty word then I think the debugger itself has a bug,
though other code with LEAVE seems to work.

George Hubert
Reply With Quote
  #5  
Old 08-07-2008, 03:40 PM
Brad Eckert
Guest
 
Default Re: Problem using LEAVE

>
> Are you sure? When I run it under the debugger I get the exception,
> but not when run normally. Since it reports the word TRACE as
> the faulty word then I think the debugger itself has a bug,
> though other code with LEAVE seems to work.


Yes, the debugger chokes on LEAVE. In fact, if I just put a LEAVE
immediately after the DO it crashes. I guess the debugger's LEAVE got
broken.

-Brad
Reply With Quote
  #6  
Old 08-08-2008, 09:15 AM
cjmcc
Guest
 
Default Re: Problem using LEAVE

On Aug 7, 7:08*am, cjmcc <cjmccorm...@gmail.com> wrote:
> I was working on a Project Euler problem and ran into a problem using
> LEAVE to exit a DO ... LOOP
> ...
> ---------------------------
> 1 41 numprimes . \ should print 40
> 2 41 numprimes . \ should print 1
> instead, I get a nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION
> Error(9998).
> ...


Well, I did have a stack mistake pointed out to me in my
initial ?prime routine which may have put the system in a
confused state. With the following code everything seems
to behave properly - including the debugger.

----------
\ code to support Project Euler problem 27

: ?prime ( n -- f ) \ check is number is prime
dup 1 > \ only test when positive
if 1 begin 1+ 2dup / 2dup > \ treat negative numbers,
if 2drop dup 1 then \ zero, and one as non-prime
over * 2 pick =
until =
else drop FALSE
then
;

: numprimes ( a b -- num ) \ find # of primes generated by n^2+an+b
1000 0
do 2dup i rot over + * +
?prime not \ test if prime
if 2drop i leave then \ if not, then return count
loop \ else keep testing
;
-------
Reply With Quote
  #7  
Old 08-10-2008, 04:22 AM
The Beez'
Guest
 
Default Re: Problem using LEAVE

On 8 aug, 15:15, cjmcc <cjmccorm...@gmail.com> wrote:
> Well, I did have a stack mistake pointed out to me in my
> initial ?prime routine which may have put the system in a
> confused state. With the following code everything seems
> to behave properly - including the debugger.

Works flawless on 4tH:

\ code to support Project Euler problem 27

include lib/pickroll.4th

: ?prime ( n -- f ) \ check is number is prime
dup 1 > \ only test when positive
if 1 begin 1+ 2dup / 2dup > \ treat negative numbers,
if 2drop dup 1 then \ zero, and one as non-prime
over * 2 pick =
until =
else drop FALSE
then
;

: numprimes ( a b -- num ) \ find # of primes made by
n^2+an+b
1000 0
do 2dup i rot over + * +
?prime not \ test if prime
if 2drop i leave then \ if not, then return count
loop \ else keep testing
;

1 41 numprimes . cr \ should print 40
2 41 numprimes . cr \ should print 1

------------------------------------
/home/user/habe/4th> 4th cxq euler27.4th
40
1
/home/user/habe/4th>


Hans Bezemer
Reply With Quote
  #8  
Old 08-10-2008, 04:37 PM
Albert van der Horst
Guest
 
Default Re: Problem using LEAVE

In article <78c9c558-8de1-472f-88bc-292482473dbc@26g2000hsk.googlegroups.com>,
The Beez' <hansoft@bigfoot.com> wrote:
>On 8 aug, 15:15, cjmcc <cjmccorm...@gmail.com> wrote:
>> Well, I did have a stack mistake pointed out to me in my
>> initial ?prime routine which may have put the system in a
>> confused state. With the following code everything seems
>> to behave properly - including the debugger.

>Works flawless on 4tH:
>
>\ code to support Project Euler problem 27


<Spoiler deleted>

>------------------------------------
>/home/user/habe/4th> 4th cxq euler27.4th
>40
>1
>/home/user/habe/4th>
>
>
>Hans Bezemer


Could you please mention spoiler in the subject line
if you publish solutions to euler problems?

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Reply With Quote
  #9  
Old 08-11-2008, 02:26 PM
George Hubert
Guest
 
Default Re: Problem using LEAVE

On Aug 7, 8:40*pm, Brad Eckert <nospaambr...@tinyboot.com> wrote:
> > Are you sure? When I run it under the debugger I get the exception,
> > but not when run normally. Since it reports the word TRACE as
> > the faulty word then I think the debugger itself has a bug,
> > though other code with LEAVE seems to work.

>
> Yes, the debugger chokes on LEAVE. In fact, if I just put a LEAVE
> immediately after the DO it crashes. I guess the debugger's LEAVE got
> broken.
>
> -Brad


Your right LEAVE was altered a while ago to use an address a cell
greater than previously (along with other branches) and the debugger
version wasn't updated (although other branches were). It's been
sorted now (in the V6.13 developement version in our CVS).

George Hubert
Reply With Quote
  #10  
Old 08-12-2008, 05:51 AM
The Beez'
Guest
 
Default Re: Problem using LEAVE

On 10 aug, 22:37, Albert van der Horst <alb...@spenarnc.xs4all.nl>
wrote:
> Could you please mention spoiler in the subject line
> if you publish solutions to euler problems?

That's alright, Albert, but why pick on me? I was just posting a
reacting to a previous post!

Hans Bezemer

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 05:49 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.