ASM for FUN - #7 SUB [Nested Loops - how to optimize them]

Started by frktons, May 03, 2010, 05:56:20 PM

Previous topic - Next topic

frktons

Quote from: dedndave on May 04, 2010, 04:30:43 PM
oh - gotcha
although - no way to return a value in EAX that way   :bg
i dunno how PB works, but C compilers use the EAX register as a return value from called procedures
in ASM, we can use EAX, ECX, and/or EDX

In the routine there is no CALLing, the ASM instructions are in
MAIN() alike all the others.  ::)
Mind is like a parachute. You know what to do in order to use it :-)

dedndave

attach your EXE file and i will see how it runs on mine

dedndave

QuoteIn the routine there is no CALLing, the ASM instructions are in MAIN() alike all the others.

certainly, you have some way to call subroutines - GOSUB/RETURN maybe ?

clive

Quote from: frktons
What about optimizing these loops? Any idea? 

Pre-compute a table with [n1, n2, n3, n4], and index through it in a *single* loop. Should save a couple of million cycles in your empty loop, but will take a lot of memory (more depending on your encoding scheme)

But again, you are not doing anything inside your loop, so you just have a cycle burning timing loop. Whatever you do inside the loop is going to scale at 4M:1, so what you plan to put inside the loop is the main thing to focus on.
It could be a random act of randomness. Those happen a lot as well.

frktons

Quote from: dedndave on May 04, 2010, 04:42:31 PM
certainly, you have some way to call subroutines - GOSUB/RETURN maybe ?

Nothing at all, just one instruction after the other  :P

Here you have the exe in a zipped form
Quote from: clive on May 04, 2010, 04:56:08 PM
Quote from: frktons
What about optimizing these loops? Any idea? 

Pre-compute a table with [n1, n2, n3, n4], and index through it in a *single* loop. Should save a couple of million cycles in your empty loop, but will take a lot of memory (more depending on your encoding scheme)

But again, you are not doing anything inside your loop, so you just have a cycle burning timing loop. Whatever you do inside the loop is going to scale at 4M:1, so what you plan to put inside the loop is the main thing to focus on.

Well, I'm going, in the future  ::) , to save the combinations in a file, so this loop
is just an exercise to see what kind of optimization is possible in a similar situation.
After that, I can read the whole file in an array and do what you suggest for
doing something with it, without the necessity to generate it again with nested loops.

ASM is an exercise for the time being, so let's learn something  :U
Mind is like a parachute. You know what to do in order to use it :-)

dedndave

here is what i get on the prescott
                   Total iterations ==> 100

                   Total combinations ==> 3,921,225

                   Elapsed time in MS ==>  813

                   Elapsed cycles ==> 2,438,347,215

8.13 mS is quite a bit higher than the 5.25 mS i calculated, but at least we have a benchmark

there has to be a way to execute routines from PB
if not, i would say it is totally handicapped

frktons

Quote from: dedndave on May 04, 2010, 01:02:03 AM
i get about 5.25 mS on a 3 GHz Prescott
3921225  combinations
15996963 clock cycles
15819197 clock cycles
15802771 clock cycles
15769103 clock cycles
15820043 clock cycles



If you add an ms counter to your routine I can see the difference in my pc
as well.

Quote from: dedndave on May 04, 2010, 05:57:12 PM
here is what i get on the prescott
                   Total iterations ==> 100

                   Total combinations ==> 3,921,225

                   Elapsed time in MS ==>  813

                   Elapsed cycles ==> 2,438,347,215

8.13 mS is quite a bit higher than the 5.25 mS i calculated, but at least we have a benchmark

there has to be a way to execute routines from PB
if not, i would say it is totally handicapped

Of course there are methods of CALLing whatever needed, I just
didn't need it.  :P

It is just a wasting cycles routine, as Clive stated, it doesn't do anything else
so why should I use a Proc ?  :lol
Mind is like a parachute. You know what to do in order to use it :-)

dedndave

to calculate the time in seconds from clock cycles, just divide the clock cycles by processor frequency in Hz
my prescott runs at 3 Ghz, so 15769103/3000000000 ~ 0.00525 or 5.25 mS
it takes a bit of code to measure cpu frequency, although i have done it
it is easier to divide with a calculator, though   :lol

hutch--

At a procedure level PB comforms to the Intel ABI in terms of register usage and return values in EAX for integer values. Its only curse is it has a bit higher stack overhead so that it conforms to the specifications of BASIC as a language.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php