News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Re: Nested loops in MASM32

Started by jj2007, March 13, 2010, 09:49:52 AM

Previous topic - Next topic

jj2007

Quote from: sinsi on March 13, 2010, 07:58:14 AM

How inefficient.
...
I thought a while loop tested before, so you test the initial value before any processing e.g.

mov ebx,whatever
@@:
test ebx,ebx
js @f
dec ebx
jmp @b
@@:

Good idea, but this version is a bit faster:
mov ebx, 1000
test ebx, ebx
js DontEnter
@@:
dec ebx ; **** insert optimised manual while loop here ****
jns @B
DontEnter:

Celeron M:
1025    cycles for .While ... .Endw
1025    cycles for manual while ... coding

Quote from: hutch-- on March 13, 2010, 08:01:09 AM
Just for the people who use the Campus, can we keep discussion on details for the Workshop or similar as we don't want to baffle a learner with technology.

I fully agree. Learners should not be confused by vague promises that there are ways to make a .While loop faster by some magic "hand coding".

BlackVortex

Why does every freaking thread turn into a cycle-saving witch hunt ?

Did anyone say it's speed-critical ? No. Next time, assume it isn't.

jj2007

Look inside this page for the first occurence of the word "speed".

BlackVortex

Yes, so donkey made a little reference/comment and you turned this into a benchmark !   :bg

hutch--

The difference is basically this, structured loops do the job in many places but there are many other designs where they don't work well. A structured loop has single entry and exit layouts and this is very clunky when you need multiple entry and exit loop designs that are often interdependent and then have similar subloops in them.

If your code design requires a structured loop, use it if it suits you but if you need more complicated logic, write what you need at the mnemonic level. Empty test pieces are close to useless as instruction scheduling and loop code are interdependent. It will be the architecture that makes the difference one way or another, not bean counting the instruction order.

As a matter of personal preference I code loops manually as I am used to doing it but there is nothing wrong with a structured loop if that is what you need to do the job.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

donkey

Hi Hutch and BlackVortex,

I am sorry, I did try not to get drawn into this mess. I don't really mind the optimization stuff but I wasn't going to be drawn into a completely useless test as loop optimization is extremely implementation specific. I just saw that in the original post he implied heavily nested loops and a complex algorithm so I mentioned that hand coding whether "magic" or not might help to speed things up but I also mentioned that the intrinsic HLC's were fairly efficient and if they fit the bill why not use them. I just don't normally get involved in the speed test garbage unless it really interests me and that is exceedingly rare as I see most optimization, especially a lot of the stuff on the board lately, as useless wastes of time.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

Quote from: BlackVortex on March 14, 2010, 02:25:59 AM
Yes, so donkey made a little reference/comment and you turned this into a benchmark !   :bg

A "little reference"? No, he promised a magic speed increase by "hand-coding", and I offered a benchmarking testbed where he could demonstrate his hand-coding skills. I like the magic of speedy and compact assembler code. What I don't like is false promises, especially in The Campus.

donkey

Quote from: jj2007 on March 14, 2010, 08:33:36 AM
Quote from: BlackVortex on March 14, 2010, 02:25:59 AM
Yes, so donkey made a little reference/comment and you turned this into a benchmark !   :bg

A "little reference"? No, he promised a magic speed increase by "hand-coding", and I offered a benchmarking testbed where he could demonstrate his hand-coding skills. I like the magic of speedy and compact assembler code. What I don't like is false promises, especially in The Campus.

Your benchmark is crap.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

hutch--

 :bg

Come on guys, everyone who has been around for some length of time knows this stuff, in non-critical loops .WHILE or .REPEAT loops work fine if thats how you like to code but there will always be loop designs that you can only code in mnemonics and the more complex they are the more this is the case. Structured loops have their limitations, especially for multiple entry/exit loops with nested dependencies and interactive loop design. Even adaptive optimising compilers have problems with complex loop design and this is among the reasons why they often speak against complex loop structures.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Hutch,
Just give me one example where a hand-coded loop is faster than its .While equivalent, and I'll never write about it again. Promised.

hutch--

JJ,

My only problem with .WHILE loops is when you need any other form of loop. I regularly write multiple entry and exit loops, interdependent loop, stepped nested loops and none of them code well using high level architecture.

This is why I agree with the advice that if a .WHILE loop does the job for you, use it but if you have other designs in mind, write them in mnemonics.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007


BlackVortex

Donkey is busy with more important stuff, his header project.   :thumbu

But what about the branch prediction hints, when using the conditionals you can't use them, isn't that a performance consideration in some cases ? (I've been reading the GoAsm manual, fisrt time I've heard of these prefixes...)

donkey

Hi BlackVortex,

Not so much that I don't have time, I'm just not interested, but it is fun popping in every once in a while to see how jj2007 is doing :)

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

MichaelW

In my tests, run on a P3, the .WHILE and .REPEAT .UNTIL loops have slightly more overhead, even without complex exit conditions.

;==============================================================================
.NOLIST
;==============================================================================
    include \masm32\include\masm32rt.inc
    .686
    include \masm32\macros\timers.asm
;==============================================================================
.LISTALL
;==============================================================================
    .data
    .code
;==============================================================================
start:
;==============================================================================

    invoke Sleep, 3000

    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
        .WHILE ebx
            dec ebx
        .ENDW
    counter_end
    print str$(eax),13,10

    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
        .REPEAT
            dec ebx
        .UNTIL ebx == 0
    counter_end
    print str$(eax),13,10

    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
      @@:
        dec ebx
        jnz @B
    counter_end
    print str$(eax),13,10

    xor esi, esi

    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
        .WHILE ebx
            .BREAK .IF esi
            dec ebx
        .ENDW
    counter_end
    print str$(eax),13,10

    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
      @@:
        test esi, esi
        jnz @F
        dec ebx
        jnz @B
      @@:
    counter_end
    print str$(eax),13,10

    inkey "Press any key to exit..."
    exit

;==============================================================================
end start


234
245
205
277
208

eschew obfuscation