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

BlackVortex

I guess masm is actually doing all the compares instead of just depending on the Z flag, right ?

(too bored to assemble and disassemble,hehe)

jj2007

#16
Thanks, Michael, that you took the time to deliver a concrete basis for arguing. I appreciate.
Here is my version of your code (note that your last manual example was actually not a while but rather a repeat loop).

.NOLIST
include \masm32\include\masm32rt.inc
.686
include \masm32\macros\timers.asm
.LISTALL

.code
start:
;==============================================================================

invoke Sleep, 3000
    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
        .REPEAT
            dec ebx
        .UNTIL Zero? ; not: ebx == 0
    counter_end
    print str$(eax), " - rep loop",13,10

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

    xor esi, esi

    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
          jj = 1
if jj
.Repeat
test esi, esi
.Break .if !Zero?
dec ebx
.Until Zero?
else
        .WHILE ebx
            .BREAK .IF esi
            dec ebx
        .ENDW
endif
    counter_end
    print str$(eax)," - rep2 (ex while)", 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)," - rep2, manual", 13,10

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

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


Celeron M:
121 - rep loop
121 - rep manual
209 - rep2 (ex while)
209 - rep2, manual


Edit: P4:
177 - rep loop
177 - rep manual
293 - rep2 (ex while)
293 - rep2, manual


Edit: I took away this one because there was no hand-coded equivalent present
    counter_begin 1000, HIGH_PRIORITY_CLASS
        mov ebx, 100
        .WHILE ebx
            dec ebx
        .ENDW
    counter_end
    print str$(eax), " - while loop", 13,10

jj2007

Quote from: BlackVortex on March 15, 2010, 12:06:42 AM
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...)

You could use them by inserting the db xx, but:
Quote
Branch hint prefixes
GCC has support for this feature, but it has turned out to not gain
anything and was disabled by default, since branch reordering streamlines
code well enough to match the default predictor behaviour.
Same conclusion was done by other compiler teams too, ICC is not
generating the hints either.

sinsi

Try a .while/.repeat loop with multiple .break/.continue options and see what it does. 'dec ebx' isn't a great benchmark...

I'm sure I read somewhere that the branch prefix hints are ignored in the core2 and later, and were a real waste of time (agner maybe?).
Quote from: jj2007You could use them by inserting the db xx
Just use a CS: or DS: prefix, that's all they are.
Light travels faster than sound, that's why some people seem bright until you hear them.

hutch--

JJ,

These will do for satarters.

wordreplace
WordCount
szRemove
szRep
szMonoSpace
StrLen
qssorta
parse_line
ltok
wtok
InString
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Quote from: hutch-- on March 15, 2010, 07:49:21 AM
JJ,

These will do for satarters.

???

Quote from: sinsi on March 15, 2010, 07:42:49 AM
Try a .while/.repeat loop with multiple .break/.continue options and see what it does. 'dec ebx' isn't a great benchmark...
Example?

sinsi

>Example?
You are the clock meister, I am the one who doesn't use invoke, much less .while. Some .while/.break code when disassembled is like spaghetti...
Whatever happened to LAMPS? That was always good for a chuckle or two  :bg
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on March 15, 2010, 09:15:12 AM
Whatever happened to LAMPS? That was always good for a chuckle or two  :bg

LAMPS work only if the code is different in size, speed, or both. Since apparently nobody is able to find an example where a hand-coded loop is different from its matching high level construct, let alone more efficient, there is no point in applying LAMPs...

hutch--

JJ,

While we will happily agree about using .WHILE where it works, the list of library modules were all written with non-standard loop designs and while you could probably cobble together the same algorithms with .REPEAT and .WHILE structured loops, it would be ugly sloppy code that did it.

As usual, horses for courses.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php