News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

.FOR built in JWasm

Started by habran, July 03, 2012, 11:38:29 PM

Previous topic - Next topic

Gunther

Good to know habran.  :t

Gunther
You have to know the facts before you can distort them.

habran

I have modified code to work exactly as C

Cod-Father

frktons

Quote from: habran on February 01, 2013, 06:35:52 AM
I have modified code to work exactly as C
Interesting stuff. JWasm is written in C, and now we can use JWasm as C in some
of its features. At least I hope the JWasm version is faster/smarter/smaller/...er than
C counterpart.   :t
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

habran

"Frankly" :biggrin: it's well optimized
I've wished someone gave me a feedback after testing it

thanks Frank
Cod-Father

jj2007

 :icon_confused:
Quote from: habran on February 01, 2013, 01:32:33 PM
I've wished someone gave me a feedback after testing it

JWasm32 told me that MSVCR110.dll was missing (on Win7-32). After fixing this, here are some timings:
AMD Athlon(tm) Dual Core Processor 4450B (SSE3)

22317   cycles for 100 * C style for loop
31539   cycles for 100 * Basic style for loop

14      bytes for C style for loop
30      bytes for Basic style for loop


So your code is really competitive :t
Here are the two loops compared:

        m2m eax, 40
        .for (ecx = 0¦ecx<=eax¦ecx+=1)
                inc edx        ; inc eax hangs
        .endfor

        m2m eax, 41
        For_ ecx=0 To eax-1
                inc eax        ; inc eax OK
        Next

The Basic loop is a bit more flexible, as it can handle eax+whatever, and references to the initial eax (or whatever) while looping. On the other hand, the C loop is shorter and faster. In real life, if speed was an issue, I would use a .Repeat ... .Until construct anyway, but I guess for C coders your add-on to JWasm is really handy :t

P.S.: I made a little test with Pelles C:

      int n=100;
      __asm int 3;
      __asm nop;
         for (jjc=0; jjc<n; jjc++) {
         n++;
      }
      __asm nop;

00401DD5  |.  90            nop
00401DD6  |.  31C0          xor eax, eax
00401DD8  |.  BA 64000000   mov edx, 64
00401DDD  |>  42            /inc edx
00401DDE  |.  40            |inc eax
00401DDF  |.  39D0          |cmp eax, edx
00401DE1  |.^ 7C FA         \jl short 00401DDD
00401DE3  |.  90            nop


Heavily optimised (bravo, Pelle :t), and it seems indeed that C keeps referring to the current loop counter, i.e. this loop stops only for edx = 80000000h 8)

habran

#50
thanks JJ for testing it
if you write it like this:
       
         .for (ecx = 0¦ecx<=40¦ecx++)
                inc eax       
        .endfor


you will get:

004C879  xor     ecx,ecx
004C87B  jmp    004C87Fh
004C87D  inc     ecx 
004C87F  cmp    ecx,28h
004C882  ja      004C888h
004C884  inc     eax 
004C886  jmp    004C87Dh


I have optimized it so that:
  ++ will be transformed to INC
  -- will be transformed to DEC
but:
+= is ADD
and:
-= is SUB
Cod-Father

frktons

Quote from: habran on February 01, 2013, 10:32:10 PM
thanks JJ for testing it
if you write it like this:
       
         .for (ecx = 0¦ecx<=40¦ecx++)
                inc eax       
        .endfor


you will get:

004C879  xor     ecx,ecx
004C87B  jmp    004C87Fh
004C87D  inc     ecx 
004C87F  cmp    ecx,28h
004C882  ja      004C888h
004C884  inc     eax 
004C886  jmp    004C87Dh




Compliments habran, a really good job.   :eusa_clap:
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

habran

Thanks Frank :biggrin:

One who can see quality of others, posses quality himself
    who wrote that? :shock:
      habran himself  :lol:
Cod-Father

habran

#53
I added Jwasm.zip with 32 bits version with /MT option

needs at least Windows version 6
(Japheth)

however, you have a source code and can build for your system if it is older
Cod-Father

japheth

Quote from: habran on February 04, 2013, 03:41:52 PM
I added Jwasm.zip with 32 bits version with /MT option

It should be noted that the binary is marked as "needs at least Windows version 6" ( which is Vista ). With XP, you get a message box saying that jwasm.exe "is not a valid Win32 application".

jj2007

 :redface:
Quote from: habran on February 01, 2013, 10:32:10 PM
thanks JJ for testing it
if you write it like this:
       
         .for (ecx = 0¦ecx<=40¦ecx++)
                inc eax       
        .endfor

Habran,

The immediate case ecx<=40 is trivial. More tricky is what happens when you pass the bounds in (volatile/non-volatile) registers:
        ; assume eax was set somewhere else, as a return value:
        .for (ecx = 0¦ecx<=eax¦ecx+=1)
                inc eax        ; hangs in C/your JWasm version, in contrast to BASIC/MasmBasic
        .endfor


The point is that ecx<=eax is not checked only at the beginning, but rather for every loop. Which implies that in practice you cannot use eax, ecx, edx for bounds if calls are inside the loops.

In MasmBasic it's possible, which is the reason why it is some cycles slower. Speed against convenience, as usual  :biggrin:

habran

thanks Japheth I was not aware of that
I don't have Xp nor Vista I have Windos 7 on all computers

jj2007 it is possible to write this:

LIMIT EQU 40
THE_BREAK EQU 20
.for (ecx = 0,eax=LIMIT,edx=THE_BREAK¦ecx<=eax¦ecx++)
                ;inc eax      it is illogical to increase a LIMIT especially with the same step
                                  ;of course it will hang
                .break .if (ecx > edx)
        .endfor

Cod-Father

jj2007

Quote from: habran on February 04, 2013, 10:04:30 PM
                ;inc eax      it is illogical to increase a LIMIT especially with the same step

It depends on where you come from. For C coders, it may look illogical, but assembler coders might want to use return values directly...

include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
        GetFiles \Masm32\m32lib\*to*        ; list conversion to something algos please

        ; # of files found is returned in eax, as usual
        For_ ebx=0 To eax-1        ; it is illogical to forbid the use of eax inside the loop
                Print Str$(ebx), Tb$, Files$(ebx), CrLf$
        Next

        Inkey
        Exit
end start

And no, I don't want you to change your logic :biggrin:

japheth

Quote from: jj2007 on February 04, 2013, 08:44:53 PM
The point is that ecx<=eax is not checked only at the beginning, but rather for every loop.

Yes, of course. Anything else would be VERY unintuitive ( at least for non-BASIC coders ).

Quote
In MasmBasic it's possible, which is the reason why it is some cycles slower. Speed against convenience, as usual

... another MB promotion thread ...  :icon_rolleyes:


jj2007

Quote from: japheth on February 05, 2013, 12:49:58 AM
Yes, of course. Anything else would be VERY unintuitive

japheth,

Take it easy. Assembler != C, and assuming some"logic" is a source of difficult to chase bugs. Remember the debate about Switch/Case and falling thru? VERY unintuitive what C does, at least for the rest of the programming world.