Loop for Items in .S (Novice question)
Author Message
Loop for Items in .S (Novice question)

I'm working figuring out a routine to sort the items in the stack.

Basically I'm trying to do it this way...

: SORT DO . SWITCH . UNTIL ;

: SWITCH DUP DUP < IF . SWAP ELSE 2DROP THEN ;

Now, SWITCH seems to work fine on two integers. Now I wanna make the SORT
routine iterate through all the items on the stack. Not sure what the
correct syntax is to get that value for the "UNTIL" flag.

Course, I could have it all hosed up here. Any pointers(no pun intended)
would be appreciated.

Caffeine Junky

Fri, 25 Mar 2005 06:18:26 GMT
Loop for Items in .S (Novice question)

Quote:

> I'm working figuring out a routine to sort the items in the stack.

> Basically I'm trying to do it this way...

> : SORT DO . SWITCH . UNTIL ;

> : SWITCH DUP DUP < IF . SWAP ELSE 2DROP THEN ;

> Now, SWITCH seems to work fine on two integers. Now I wanna make the SORT
> routine iterate through all the items on the stack. Not sure what the
> correct syntax is to get that value for the "UNTIL" flag.

> Course, I could have it all hosed up here. Any pointers(no pun intended)
> would be appreciated.

> Caffeine Junky

I can't see what you're trying to do. ( You don't even have stack
comments.) I don't see how ' DUP < ' differs from zero. When comparing
something to itself, less-than must always fail.

Jerry
--
Engineering is the art of making what you want from things you can get.

Fri, 25 Mar 2005 07:01:10 GMT
Loop for Items in .S (Novice question)

Quote:

> > I'm working figuring out a routine to sort the items in the stack.

> > Basically I'm trying to do it this way...

> > : SORT DO . SWITCH . UNTIL ;

> > : SWITCH DUP DUP < IF . SWAP ELSE 2DROP THEN ;

> > Now, SWITCH seems to work fine on two integers. Now I wanna make the SORT
> > routine iterate through all the items on the stack. Not sure what the
> > correct syntax is to get that value for the "UNTIL" flag.

> > Course, I could have it all hosed up here. Any pointers(no pun intended)
> > would be appreciated.

> > Caffeine Junky

> I can't see what you're trying to do. ( You don't even have stack
> comments.) I don't see how ' DUP < ' differs from zero. When comparing
> something to itself, less-than must always fail.

My guess is he means 2DUP < and can't have tested SWITCH much.

Caff: DUP DUP duplicates the top item twice, i.e., you now have three copies
of it, whereas 2DUP duplicates the top _pair_ of items.
Also, DO begins a structure that ends with LOOP or +LOOP, while
UNTIL ends a structure that begins with BEGIN.  You need to start with
a primer or text on Forth and master some vocabulary.

Also, I can't really see the utility of sorting the stack.  Normally stack
order is critical and something to be preserved.  If you have numbers to
be sorted, it makes better sense to have them in an array.

Cheers,
Elizabeth

Fri, 25 Mar 2005 07:40:29 GMT
Loop for Items in .S (Novice question)

Quote:

>> I can't see what you're trying to do. ( You don't even have stack
>> comments.) I don't see how ' DUP < ' differs from zero. When comparing
>> something to itself, less-than must always fail.

> My guess is he means 2DUP < and can't have tested SWITCH much.

> Caff: DUP DUP duplicates the top item twice, i.e., you now have three
> copies of it, whereas 2DUP duplicates the top _pair_ of items. Also, DO
> begins a structure that ends with LOOP or +LOOP, while UNTIL ends a
> structure that begins with BEGIN.  You need to start with a primer or
> text on Forth and master some vocabulary.

> Also, I can't really see the utility of sorting the stack.  Normally
> stack order is critical and something to be preserved.  If you have
> numbers to be sorted, it makes better sense to have them in an array.

> Cheers,
> Elizabeth

GForth today.

I assumed that using the stack would be quicker than sorting an array in
memory. I suppose I could create a seperate "sort" stack for just that
purpose, but if the speed gain would be negligable theres probably no
point in it. Could be good practice though.
My plan, per you're advice, is to garner some more vocabulary and then
create a routine that pulls data from an unsorted array and then pushes
the sorted results onto the stack. Should be fun.

Note: I usually use Ada95/C, so Forth is quite a switch for me. I'm
having fun playing with the "Interpreter" even though my instructions are
horrible at this point. Heh.

Thanks.

Caffiene Junky

Fri, 25 Mar 2005 08:01:34 GMT
Loop for Items in .S (Novice question)

Quote:

> ...
> > Also, I can't really see the utility of sorting the stack.  Normally
> > stack order is critical and something to be preserved.  If you have
> > numbers to be sorted, it makes better sense to have them in an array.
> ...

> I assumed that using the stack would be quicker than sorting an array in
> memory. I suppose I could create a seperate "sort" stack for just that
> purpose, but if the speed gain would be negligable theres probably no
> point in it. Could be good practice though.
> My plan, per you're advice, is to garner some more vocabulary and then
> create a routine that pulls data from an unsorted array and then pushes
> the sorted results onto the stack. Should be fun.

The nature of a stack is that you only have easy access to the top
few items.  It's ideal for passing parameters (e.g. the address and
length of an array to be sorted), but you don't want to try to manipulate
a lot of data there, either arrays or whole strings.  Either do your sort
in place or use a separate buffer.

Quote:
> Note: I usually use Ada95/C, so Forth is quite a switch for me. I'm
> having fun playing with the "Interpreter" even though my instructions are
> horrible at this point. Heh.

Yes, Forth is quite different from Ada!  It's like driving a sports car
with a stick shift compared with a Lincoln Continental with automatic
everything.  Very fast & powerful, but the driver is expected to
assume a lot of responsibility (and in return, has a lot of control).

Cheers,
Elizabeth

Fri, 25 Mar 2005 08:36:34 GMT
Loop for Items in .S (Novice question)

Quote:

> Also, I can't really see the utility of sorting the stack.  Normally stack
> order is critical and something to be preserved.  If you have numbers to
> be sorted, it makes better sense to have them in an array.

I've found one place where it seems worthwhile to sort items on the
parameter stack.  It's in WORDS, when it selects the next thread to display.

--

-Gary Chanson (MVP for Windows SDK)
-Software Consultant (Embedded systems and Real Time Controls)

-War is the last resort of the incompetent.

Fri, 25 Mar 2005 10:51:31 GMT
Loop for Items in .S (Novice question)

Quote:

>> I assumed that using the stack would be quicker than sorting an array
>> in memory. I suppose I could create a seperate "sort" stack for just
>> that purpose, but if the speed gain would be negligable theres probably
>> no point in it. Could be good practice though. My plan, per you're
>> advice, is to garner some more vocabulary and then create a routine
>> that pulls data from an unsorted array and then pushes the sorted
>> results onto the stack. Should be fun.

> The nature of a stack is that you only have easy access to the top few
> items.  It's ideal for passing parameters (e.g. the address and length
> of an array to be sorted), but you don't want to try to manipulate a lot
> of data there, either arrays or whole strings.  Either do your sort in
> place or use a separate buffer.

I picked up this assumption in a book by an author named Jeff Duntman
in an introduction book to assembly language. I dont remember the title
right off hand. But in the book, he draws bitmaps to the screen by
pushing a bunch of data on the stack, and then using "rep stosw" to fire
the data off to the video card via the BIOS. It at least seemed like a
fast technique. Of course this was on DOS with a 486 computer. I assumed
that I could push a bunch of pre-sorted data onto a stack/buffer and then
fire it off in a "rep stosw" type of way.
I picked up Donald Knuths Volume 1 of TAOCP, and he presents a bunch of
techniques that could be used similarly.

Perhaps I'm fishing in the wrong pond though.

Quote:
> Yes, Forth is quite different from Ada!  It's like driving a sports car
> with a stick shift compared with a Lincoln Continental with automatic
> everything.  Very fast & powerful, but the driver is expected to assume
> a lot of responsibility (and in return, has a lot of control).

> Cheers,
> Elizabeth

I really try not to make those types of comparisons. Yes, Forth is a lot
like a Sports car, but I see Ada as more of an M-1 Tank or a Freight
Train. Forth is designed to be small and fast. Ada is designed so that
with proper coding it wont hose up, ever. Forth is designed for small
projects by individuals or small teams(as I understand it), Ada was
designed for huge projects that can run into millions of lines of code.
It's like comparing apples and oranges.
They were designed with different goals in mind.

I might consider embedding a Forth compiler into one of my Ada projects
once I get the hang of it.

Aaaa, and think I got the hang of handling loops. I'll post up my
quicksort routine once I've got all the burs filed out.(Theres probably
half a zillion of them already, but hey, whats one more?)

Caffiene Junky.

Fri, 25 Mar 2005 12:42:50 GMT
Loop for Items in .S (Novice question)

Quote:

> [...] But in the book, he draws bitmaps to the screen by
> pushing a bunch of data on the stack, and then using "rep stosw" to fire
> the data off to the video card via the BIOS. It at least seemed like a
> fast technique. Of course this was on DOS with a 486 computer. I assumed
> that I could push a bunch of pre-sorted data onto a stack/buffer and then
> fire it off in a "rep stosw" type of way.

I will explain you what Forth stack is, and you will decide how to use it.

Ada and C also have a stack.

x := y*(5*z+4) + 2*(t+r+q);

Do you see the stack?
It's depth change is shown with parens:

( x := (y*((5*z)+4) + (2*((t+r)+q))) )

In RPN it will be (operations go to closing parens)

( x := ((y*((5*z)+4)) + (2*((t+r)+q))) )
x      y   5 z* 4+*    2   t r+ q+*+ :=

I am not sure that I have not missed something; in fact,
any long formulas are unreadable on a computer,
because all parens have the same size and shape, and division
has both arguments on the same line. RPN formulas in addition
use the unusual RPN, but RPN and infix formulas are equally [un]readable.

But back to the stack.
In C you cannot write

// push everything on the stack and perform stosw multiple times

in Forth you can do things like

n 0 DO I X [] LOOP  \ push everything on the stack
n 0 DO stosw LOOP   \ and perform stosw multiple times

but it is still a question whether you should do that.

PS
\ array indexing:

\ : []! ( value index baseaddr -- ) SWAP CELLS + ! ;
\ : []^ ( index baseaddr -- addr ) SWAP CELLS + ;
\ array indexing for char arrays:

\ : C[]! ( value index baseaddr -- ) SWAP CHARS + ! ;
\ : C[]^ ( index baseaddr -- addr ) SWAP CHARS + ;
\ etc.

Fri, 25 Mar 2005 15:38:30 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages