Page 1 of 2
Loops in Flowstone
Posted: Sun May 03, 2020 8:21 pm
by martinvicanek
Hi stoners,
I put together a little tutorial on loops (as a programming structure, not as repeating audio fragments) in Flowstone. It is quite elementary for now, I might elaborate more if there is interest. Your comments and suggestions are welcome!
Re: Loops in Flowstone
Posted: Sun May 03, 2020 10:12 pm
by tulamide
I can't say anything about DSP and ASM code loops, but I trust your expertise in that regard. I agree on green loops. I strongly disagree on Ruby loops, and hope, you trust my expertise in that regard.
Never use for-loops in Ruby. It's so contrary to the concept of Ruby that it is just bad. A Ruby for-loop actually is amethod of the object class and a very complicated way to do
Just try it:
Very strange right? Very ugly as well. And so un-Ruby'ish. So what happens here? Well, as a method, a for loop returns an object. r holds the returned object, and it is, as you might have guessed, the range passed to the for method.
Code: Select all
r #will show '0..9' in the inspector pane
x #will show '45'
That it returns a range, gives us a hint on what happens in a for method. For calls ::each, which is a method of the enumerator class. A for method is nothing else but a complicated and slow wrapper of the each method.
Code: Select all
## slow and non-Ruby
x = 0
for i in 0..9
x += i
end
##fast and Ruby
x = 0
(0..9).each do |i|
x += i
end
There is a looping method of the object class, however, that is constructed in C (the underlying code base of Ruby) with a for loop.
Code: Select all
x = 0
i = 0
while i < 9
i += 1
x += i
end
x #will show '45' in the inspector pane
While does not make use of a block. That makes it a very fast loop. Under certain circumstances faster than ::each. And you can use while much more specific, like this
Since your schematic is a tutorial, you should definitely refrain from using 'for' as a Ruby example!
Re: Loops in Flowstone
Posted: Mon May 04, 2020 6:31 am
by martinvicanek
Thanks Tula. I removed the Ruby section.
Re: Loops in Flowstone
Posted: Mon May 04, 2020 7:07 am
by Spogg
Many thanks Martin and tulamide!
Whenever I've come across the Green looper I'm always surprised at how fast it seems to run. No idea why...
Cheers
Spogg
Re: Loops in Flowstone
Posted: Mon May 04, 2020 11:41 am
by tektoog
Hey,
Thanks Martin! very enlightening with the comments you included

Re: Loops in Flowstone
Posted: Mon May 04, 2020 1:43 pm
by BobF
Thanks Martin,
Just what I have been looking for, very well presented . I am trying to learn to use the code box and the more examples I can study the better. This is great, I hope you keep these lessons comming! Anyone else interested in seeing more?
Thanks again, BobF.....
Re: Loops in Flowstone
Posted: Mon May 04, 2020 5:23 pm
by RJHollins
Absolutely !
Re: Loops in Flowstone
Posted: Sun May 10, 2020 10:17 am
by HughBanton
Hi Martin,
I note in the very last example you said "It has advantages to let the loop counter run backwrds". Interesting .. but I can't see why - can you elaborate?
Afraid I live permanently in FS4 world these days .. don't think any of my stuff works in 3.06 any more.

Wherein (in FS4), you've been able to do variable loop lengths, and indeed nested loops, inside a DSP box for quite a while, I've used both extensively.
Admittedly I invariably rely on hanging a text box onto the DSP box 'S' output to start me off in assem, it's the ideal way to start to learn how assembler works I reckon. Worked for me anyway - it's almost like a built-in tutorial.
Presumably it
would be possible to do nested loops in FS3 inside an Assem box?
H
Re: Loops in Flowstone
Posted: Sun May 10, 2020 4:37 pm
by martinvicanek
Thanks guys for your responses. I have added nested loops, refer to the top post.
Re: Loops in Flowstone
Posted: Tue May 12, 2020 8:49 pm
by HughBanton
Wowzer!
Some real-life examples would be helpful I expect. If I can get my head around all the pushes & pops, and with Martin's new info, I'll see if any of my earlier efforts can be smartened up enough to be presentable. A simple example I've used a number of times is an interpolator that generates, say, 61 values for a keyboard, derived instantly from the top and bottom note values. (Assembler runs at ludicrous-speed, not at green-speed

).
When I first moved things into FS4 last year I had several DLL modules in my scheme, almost all of them for generating obscure arrays of one kind or another. Of course they didn't work at first because they were all 32-bit, but I pretty soon discovered I could replace every one of them with 'dsp + signal-analyser pairs' without any of the dll hassle, very much easier. (Have never gone back to dll's).
And .. FS4 allows nested loops inside DSP! Mind you setting such things up directly in assembler like Martin does still looks pretty daunting to me, but I'll keep staring at it until something clicks.
So I basically always rely on the 'S' output from DSP modules to get a working basic assembler code and then proceed from there - MyCo has revised this feature considerably from the 3.0.x versions, his explains what's going on with all the
movaps and the
xorps much better than in the past.
However I note that in my current nested loops, as produced by FS4, ebx never makes an appearance; any internal loops are always achieved (I think!) by yet more pushes & pops of eax.
I can see that using ebx instead is a major simplification so there's my second challenge right there ....
Oooeck ...

H